Compare commits

...
2 Commits
Author SHA1 Message Date
alufolie91 9936f9c740 have Z_IsMobjValid2 print an error in console
if mobj pointer is invalid
the callee should handle the behavior
2026-10-02 18:23:29 +02:00
alufolie91 bee5c8e5ed add code to check for mobj pointer validity
as we use the pool allocator we may check if a mobj points into the address range of the level pool specific to mobj allocations (g_level_large_pool)
this may help in debugging stuff without the need to run things like ASAN or valgrind
2026-10-02 18:23:26 +02:00
4 changed files with 48 additions and 0 deletions

No files matched your search

+24
View File
@@ -149,6 +149,30 @@ void PoolAllocator::release()
allocated_blocks_ = 0;
}
bool PoolAllocator::ptrcheck(const void* p) const
{
const uintptr_t paddr = reinterpret_cast<uintptr_t>(p);
ChunkFooter* next = nullptr;
for (ChunkFooter* chunk = first_chunk_; chunk != nullptr; chunk = next)
{
const uintptr_t blockstart = reinterpret_cast<uintptr_t>(chunk->start);
const uintptr_t blockend = blockstart + blocks_ * block_size_;
//CONS_Printf("mobj ptr %lu, block start %lu end %lu\n", paddr, blockstart, blockend);
if (paddr >= blockstart && paddr < blockend)
{
return true;
}
next = chunk->next;
}
return false;
}
static LinearMemory g_frame_memory {4 * 1024 * 1024};
void* Z_Frame_Alloc(size_t size)
+1
View File
@@ -65,6 +65,7 @@ public:
constexpr size_t allocated_bytes() const noexcept { return allocated_blocks_ * block_size_; };
void release();
bool ptrcheck(const void* p) const;
};
} // namespace srb2
+21
View File
@@ -804,6 +804,27 @@ void* Z_LevelPoolMalloc(size_t size)
return p;
}
boolean Z_IsMobjValid2(void *p, const char *file, INT32 line)
{
//(void)file;
//(void)line;
// mobj was removed
// no need to worry here
if (p == NULL)
return false;
// mobjs reside in the large pool
if (!g_level_large_pool.ptrcheck(p))
{
//I_Error("invalid mobj! at %s:%d", file, line);
CONS_Alert(CONS_ERROR, "invalid mobj! at %s:%d", file, line);
return false;
}
return true;
}
void* Z_LevelPoolCalloc(size_t size)
{
void* p = Z_LevelPoolMalloc(size);
+2
View File
@@ -173,6 +173,8 @@ char *Z_StrDup(const char *in);
size_t Z_LevelPoolUsage(void);
void *Z_LevelPoolMalloc(size_t size);
void *Z_LevelPoolCalloc(size_t size);
#define Z_IsMobjValid(p) Z_IsMobjValid2(p, __FILE__, __LINE__)
boolean Z_IsMobjValid2(void *p, const char *file, INT32 line);
void Z_LevelPoolFree(void *p, size_t size);
// for use with CLEANUP macro