2023-08-13 09:58:00 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
2022-11-23 15:10:40 +08:00
|
|
|
#include <stdlib.h>
|
2023-06-08 21:04:56 +08:00
|
|
|
#include <assert.h>
|
2022-11-23 15:10:40 +08:00
|
|
|
|
2025-12-19 23:24:56 +08:00
|
|
|
#if FF_HAVE_MALLOC_USABLE_SIZE || FF_HAVE_MSVC_MSIZE
|
|
|
|
|
#include <malloc.h>
|
|
|
|
|
#elif FF_HAVE_MALLOC_SIZE
|
|
|
|
|
#include <malloc/malloc.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-01-18 14:07:29 +08:00
|
|
|
static inline void ffWrapFree(const void* pPtr)
|
2022-11-23 15:10:40 +08:00
|
|
|
{
|
2023-02-15 14:51:04 +08:00
|
|
|
assert(pPtr);
|
2022-11-23 15:10:40 +08:00
|
|
|
if(*(void**)pPtr)
|
|
|
|
|
free(*(void**)pPtr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define FF_AUTO_FREE __attribute__((__cleanup__(ffWrapFree)))
|
2025-12-19 23:24:56 +08:00
|
|
|
|
|
|
|
|
// ptr MUST be a malloc'ed pointer
|
|
|
|
|
static inline size_t ffMallocUsableSize(const void* ptr)
|
|
|
|
|
{
|
|
|
|
|
assert(ptr);
|
|
|
|
|
#if FF_HAVE_MALLOC_USABLE_SIZE
|
|
|
|
|
return malloc_usable_size((void*) ptr);
|
|
|
|
|
#elif FF_HAVE_MALLOC_SIZE
|
|
|
|
|
return malloc_size((void*) ptr);
|
|
|
|
|
#elif FF_HAVE_MSVC_MSIZE
|
|
|
|
|
return _msize((void*) ptr);
|
|
|
|
|
#else
|
|
|
|
|
return 0; // Not supported
|
|
|
|
|
#endif
|
|
|
|
|
}
|