diff --git a/src/core/memory.cpp b/src/core/memory.cpp index e0289a579..55faf9a19 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -149,6 +149,30 @@ void PoolAllocator::release() allocated_blocks_ = 0; } +bool PoolAllocator::ptrcheck(const void* p) const +{ + const uintptr_t paddr = reinterpret_cast(p); + + ChunkFooter* next = nullptr; + for (ChunkFooter* chunk = first_chunk_; chunk != nullptr; chunk = next) + { + const uintptr_t blockstart = reinterpret_cast(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) diff --git a/src/core/memory.h b/src/core/memory.h index 14b51a039..8687abbe4 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -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 diff --git a/src/z_zone.cpp b/src/z_zone.cpp index 8a8f751aa..a33c623f7 100644 --- a/src/z_zone.cpp +++ b/src/z_zone.cpp @@ -804,6 +804,26 @@ 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); + return false; + } + + return true; +} + void* Z_LevelPoolCalloc(size_t size) { void* p = Z_LevelPoolMalloc(size); diff --git a/src/z_zone.h b/src/z_zone.h index 0aade2dd2..04e3707ed 100644 --- a/src/z_zone.h +++ b/src/z_zone.h @@ -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