10#define __has_feature(x) 0
12#if __has_feature(address_sanitizer)
13#include <sanitizer/asan_interface.h>
15#define ASAN_POISON_MEMORY_REGION(addr, size) \
16 ((void)(addr), (void)(size))
17#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
18 ((void)(addr), (void)(size))
21#define MACRO_ALLOC_HEAP() \
23 void *operator new(size_t Size) \
25 void *pObj = malloc(Size); \
26 mem_zero(pObj, Size); \
29 void operator delete(void *pPtr) \
36#define MACRO_ALLOC_POOL_ID() \
38 void *operator new(size_t Size, int Id); \
39 void operator delete(void *pObj, int Id); \
40 void operator delete(void *pObj); \
44#if __has_feature(address_sanitizer)
45#define MACRO_ALLOC_GET_SIZE(POOLTYPE) ((sizeof(POOLTYPE) + 7) & ~7)
47#define MACRO_ALLOC_GET_SIZE(POOLTYPE) (sizeof(POOLTYPE))
50#define MACRO_ALLOC_POOL_ID_IMPL(POOLTYPE, PoolSize) \
51 static char gs_PoolData##POOLTYPE[PoolSize][MACRO_ALLOC_GET_SIZE(POOLTYPE)] = {{0}}; \
52 static int gs_PoolUsed##POOLTYPE[PoolSize] = {0}; \
53 MAYBE_UNUSED static int gs_PoolDummy##POOLTYPE = (ASAN_POISON_MEMORY_REGION(gs_PoolData##POOLTYPE, sizeof(gs_PoolData##POOLTYPE)), 0); \
54 void *POOLTYPE::operator new(size_t Size, int Id) \
56 dbg_assert(sizeof(POOLTYPE) >= Size, "size error"); \
57 dbg_assert(!gs_PoolUsed##POOLTYPE[Id], "already used"); \
58 ASAN_UNPOISON_MEMORY_REGION(gs_PoolData##POOLTYPE[Id], sizeof(gs_PoolData##POOLTYPE[Id])); \
59 gs_PoolUsed##POOLTYPE[Id] = 1; \
60 mem_zero(gs_PoolData##POOLTYPE[Id], sizeof(gs_PoolData##POOLTYPE[Id])); \
61 return gs_PoolData##POOLTYPE[Id]; \
63 void POOLTYPE::operator delete(void *pObj, int Id) \
65 dbg_assert(gs_PoolUsed##POOLTYPE[Id], "not used"); \
66 dbg_assert(Id == (POOLTYPE *)pObj - (POOLTYPE *)gs_PoolData##POOLTYPE, "invalid id"); \
67 gs_PoolUsed##POOLTYPE[Id] = 0; \
68 mem_zero(gs_PoolData##POOLTYPE[Id], sizeof(gs_PoolData##POOLTYPE[Id])); \
69 ASAN_POISON_MEMORY_REGION(gs_PoolData##POOLTYPE[Id], sizeof(gs_PoolData##POOLTYPE[Id])); \
71 void POOLTYPE::operator delete(void *pObj)
\
73 int Id = (POOLTYPE *)pObj - (POOLTYPE *)gs_PoolData##POOLTYPE; \
74 dbg_assert(gs_PoolUsed##POOLTYPE[Id], "not used"); \
75 gs_PoolUsed##POOLTYPE[Id] = 0; \
76 mem_zero(gs_PoolData##POOLTYPE[Id], sizeof(gs_PoolData##POOLTYPE[Id])); \
77 ASAN_POISON_MEMORY_REGION(gs_PoolData##POOLTYPE[Id], sizeof(gs_PoolData##POOLTYPE[Id])); \