diff --git a/src/doomdef.h b/src/doomdef.h index a16eae265..7e2589cf6 100644 --- a/src/doomdef.h +++ b/src/doomdef.h @@ -567,6 +567,12 @@ UINT32 quickncasehash (const char *p, size_t n) #endif #endif +// the GNU cleanup attribute: plugging memory leaks since 2003! +// on scope exit, the cleanup function is called with a pointer to the declared variable, +// essentially behaving like a C++ destructor +// NOTE: you WILL have nasal troubles if the variable is not initialized +#define CLEANUP(f) __attribute__((__cleanup__(f))) + // An assert-type mechanism. #ifdef PARANOIA #define I_Assert(e) ((e) ? (void)0 : I_Error("assert failed: %s, file %s, line %d", #e, __FILE__, __LINE__)) diff --git a/src/z_zone.h b/src/z_zone.h index 709fa1d96..25d5cfab4 100644 --- a/src/z_zone.h +++ b/src/z_zone.h @@ -171,6 +171,12 @@ void *Z_LevelPoolMalloc(size_t size); void *Z_LevelPoolCalloc(size_t size); void Z_LevelPoolFree(void *p, size_t size); +// for use with CLEANUP macro +FUNCINLINE static ATTRINLINE void Z_Pfree(void *p) +{ + Z_Free(*(void **)p); +} + #ifdef __cplusplus } // extern "C" #endif