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
|
2026-04-15 16:16:46 +08:00
|
|
|
#if __has_include(<malloc.h>)
|
|
|
|
|
#include <malloc.h>
|
|
|
|
|
#else
|
|
|
|
|
#include <malloc_np.h> // For DragonFly BSD
|
|
|
|
|
#endif
|
2025-12-19 23:24:56 +08:00
|
|
|
#elif FF_HAVE_MALLOC_SIZE
|
2026-04-15 16:16:46 +08:00
|
|
|
#include <malloc/malloc.h>
|
2025-12-19 23:24:56 +08:00
|
|
|
#endif
|
|
|
|
|
|
2026-07-10 21:13:03 +08:00
|
|
|
[[gnu::always_inline, gnu::nonnull(1)]]
|
2026-03-29 09:28:49 +08:00
|
|
|
static inline void ffWrapFree(const void* pPtr) {
|
|
|
|
|
if (*(void**) pPtr) {
|
|
|
|
|
free(*(void**) pPtr);
|
|
|
|
|
}
|
2022-11-23 15:10:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-10 21:13:03 +08:00
|
|
|
#define FF_AUTO_FREE [[gnu::cleanup(ffWrapFree)]]
|
2025-12-19 23:24:56 +08:00
|
|
|
|
|
|
|
|
// ptr MUST be a malloc'ed pointer
|
2026-09-17 21:00:36 +08:00
|
|
|
[[gnu::nonnull(1), nodiscard]]
|
2026-03-29 09:28:49 +08:00
|
|
|
static inline size_t ffMallocUsableSize(const void* 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
|
|
|
|
|
(void) ptr;
|
|
|
|
|
return 0; // Not supported
|
|
|
|
|
#endif
|
2025-12-19 23:24:56 +08:00
|
|
|
}
|