2021-01-09 13:35:00 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
|
|
|
#ifndef GAME_SERVER_ALLOC_H
|
|
|
|
#define GAME_SERVER_ALLOC_H
|
|
|
|
|
|
|
|
#include <new>
|
|
|
|
|
|
|
|
#include <base/system.h>
|
2021-11-02 23:51:03 +00:00
|
|
|
#if defined(__has_feature)
|
|
|
|
#if __has_feature(address_sanitizer)
|
|
|
|
#define ASAN
|
2021-11-02 23:21:46 +00:00
|
|
|
#include <sanitizer/asan_interface.h>
|
2021-11-02 23:51:03 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ASAN
|
|
|
|
#define ASAN_POISON_MEMORY_REGION(addr, size) \
|
|
|
|
((void)(addr), (void)(size))
|
|
|
|
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
|
|
|
|
((void)(addr), (void)(size))
|
|
|
|
#endif
|
2021-01-09 13:35:00 +00:00
|
|
|
|
|
|
|
#define MACRO_ALLOC_HEAP() \
|
|
|
|
public: \
|
|
|
|
void *operator new(size_t Size) \
|
|
|
|
{ \
|
|
|
|
void *p = malloc(Size); \
|
|
|
|
mem_zero(p, Size); \
|
|
|
|
return p; \
|
|
|
|
} \
|
|
|
|
void operator delete(void *pPtr) \
|
|
|
|
{ \
|
|
|
|
free(pPtr); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
private:
|
|
|
|
|
|
|
|
#define MACRO_ALLOC_POOL_ID() \
|
|
|
|
public: \
|
|
|
|
void *operator new(size_t Size, int id); \
|
|
|
|
void operator delete(void *p, int id); \
|
|
|
|
void operator delete(void *p); /* NOLINT(misc-new-delete-overloads) */ \
|
|
|
|
\
|
|
|
|
private:
|
|
|
|
|
2021-11-02 23:51:03 +00:00
|
|
|
#ifdef ASAN
|
|
|
|
#define POOL_POISON_CHECK(POOLTYPE, PoolSize) \
|
|
|
|
static bool s_Poisoned = false; \
|
|
|
|
if(!s_Poisoned) \
|
|
|
|
{ \
|
|
|
|
ASAN_POISON_MEMORY_REGION(ms_PoolData##POOLTYPE, sizeof(POOLTYPE) * PoolSize); \
|
|
|
|
s_Poisoned = true; \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define POOL_POISON_CHECK(POOLTYPE, PoolSize)
|
|
|
|
#endif
|
|
|
|
|
2021-01-09 13:35:00 +00:00
|
|
|
#define MACRO_ALLOC_POOL_ID_IMPL(POOLTYPE, PoolSize) \
|
|
|
|
static char ms_PoolData##POOLTYPE[PoolSize][sizeof(POOLTYPE)] = {{0}}; \
|
|
|
|
static int ms_PoolUsed##POOLTYPE[PoolSize] = {0}; \
|
|
|
|
void *POOLTYPE::operator new(size_t Size, int id) \
|
|
|
|
{ \
|
|
|
|
dbg_assert(sizeof(POOLTYPE) == Size, "size error"); \
|
|
|
|
dbg_assert(!ms_PoolUsed##POOLTYPE[id], "already used"); \
|
|
|
|
/*dbg_msg("pool", "++ %s %d", #POOLTYPE, id);*/ \
|
2021-11-02 23:51:03 +00:00
|
|
|
POOL_POISON_CHECK(POOLTYPE, PoolSize); \
|
2021-11-02 23:21:46 +00:00
|
|
|
ASAN_UNPOISON_MEMORY_REGION(ms_PoolData##POOLTYPE[id], sizeof(POOLTYPE)); \
|
2021-01-09 13:35:00 +00:00
|
|
|
ms_PoolUsed##POOLTYPE[id] = 1; \
|
|
|
|
mem_zero(ms_PoolData##POOLTYPE[id], Size); \
|
|
|
|
return ms_PoolData##POOLTYPE[id]; \
|
|
|
|
} \
|
|
|
|
void POOLTYPE::operator delete(void *p, int id) \
|
|
|
|
{ \
|
|
|
|
dbg_assert(ms_PoolUsed##POOLTYPE[id], "not used"); \
|
|
|
|
dbg_assert(id == (POOLTYPE *)p - (POOLTYPE *)ms_PoolData##POOLTYPE, "invalid id"); \
|
|
|
|
/*dbg_msg("pool", "-- %s %d", #POOLTYPE, id);*/ \
|
|
|
|
ms_PoolUsed##POOLTYPE[id] = 0; \
|
|
|
|
mem_zero(ms_PoolData##POOLTYPE[id], sizeof(POOLTYPE)); \
|
2021-11-02 23:21:46 +00:00
|
|
|
ASAN_POISON_MEMORY_REGION(ms_PoolData##POOLTYPE[id], sizeof(POOLTYPE)); \
|
2021-01-09 13:35:00 +00:00
|
|
|
} \
|
|
|
|
void POOLTYPE::operator delete(void *p) /* NOLINT(misc-new-delete-overloads) */ \
|
|
|
|
{ \
|
|
|
|
int id = (POOLTYPE *)p - (POOLTYPE *)ms_PoolData##POOLTYPE; \
|
|
|
|
dbg_assert(ms_PoolUsed##POOLTYPE[id], "not used"); \
|
|
|
|
/*dbg_msg("pool", "-- %s %d", #POOLTYPE, id);*/ \
|
|
|
|
ms_PoolUsed##POOLTYPE[id] = 0; \
|
|
|
|
mem_zero(ms_PoolData##POOLTYPE[id], sizeof(POOLTYPE)); \
|
2021-11-02 23:21:46 +00:00
|
|
|
ASAN_POISON_MEMORY_REGION(ms_PoolData##POOLTYPE[id], sizeof(POOLTYPE)); \
|
2021-01-09 13:35:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|