ClickHouse/src/Common/clickhouse_malloc.cpp

56 lines
1.3 KiB
C++
Raw Normal View History

2021-08-05 11:45:38 +00:00
#include <Common/memory.h>
2021-08-05 20:36:46 +00:00
#include <cstdlib>
2021-08-05 11:45:38 +00:00
2021-08-05 20:51:39 +00:00
/** These functions can be substituted instead of regular ones when memory tracking is needed.
*/
2021-08-05 11:45:38 +00:00
extern "C" void * clickhouse_malloc(size_t size)
{
void * res = malloc(size);
if (res)
Memory::trackMemory(size);
return res;
}
2021-08-05 20:36:46 +00:00
extern "C" void * clickhouse_calloc(size_t number_of_members, size_t size)
{
void * res = calloc(number_of_members, size);
if (res)
Memory::trackMemory(number_of_members * size);
return res;
}
extern "C" void * clickhouse_realloc(void * ptr, size_t size)
{
2021-08-05 23:06:26 +00:00
if (ptr)
Memory::untrackMemory(ptr);
2021-08-05 20:36:46 +00:00
void * res = realloc(ptr, size);
if (res)
Memory::trackMemory(size);
return res;
}
extern "C" void * clickhouse_reallocarray(void * ptr, size_t number_of_members, size_t size)
{
2021-08-05 23:10:55 +00:00
size_t real_size = 0;
if (__builtin_mul_overflow(number_of_members, size, &real_size))
return nullptr;
return clickhouse_realloc(ptr, real_size);
2021-08-05 20:36:46 +00:00
}
2021-08-05 11:45:38 +00:00
extern "C" void clickhouse_free(void * ptr)
{
Memory::untrackMemory(ptr);
free(ptr);
}
extern "C" int clickhouse_posix_memalign(void ** memptr, size_t alignment, size_t size)
{
int res = posix_memalign(memptr, alignment, size);
if (res == 0)
Memory::trackMemory(size);
return res;
}