#ifndef ZABBIX_MEMALLOC_H
#define ZABBIX_MEMALLOC_H
#include "zbxtypes.h"
#define MEM_MIN_ALLOC 24
#define MEM_MIN_BUCKET_SIZE MEM_MIN_ALLOC
#define MEM_MAX_BUCKET_SIZE 256
#define MEM_BUCKET_COUNT ((MEM_MAX_BUCKET_SIZE - MEM_MIN_BUCKET_SIZE) / 8 + 1)
typedef struct
{
void *base;
void **buckets;
void *lo_bound;
void *hi_bound;
zbx_uint64_t free_size;
zbx_uint64_t used_size;
zbx_uint64_t orig_size;
zbx_uint64_t total_size;
int shm_id;
char allow_oom;
const char *mem_descr;
const char *mem_param;
}
zbx_mem_info_t;
typedef struct
{
zbx_uint64_t free_size;
zbx_uint64_t used_size;
zbx_uint64_t min_chunk_size;
zbx_uint64_t max_chunk_size;
zbx_uint64_t overhead;
unsigned int chunks_num[MEM_BUCKET_COUNT];
unsigned int free_chunks;
unsigned int used_chunks;
}
zbx_mem_stats_t;
int zbx_mem_create(zbx_mem_info_t **info, zbx_uint64_t size, const char *descr, const char *param, int allow_oom,
char **error);
void zbx_mem_destroy(zbx_mem_info_t *info);
#define zbx_mem_malloc(info, old, size) __zbx_mem_malloc(__FILE__, __LINE__, info, old, size)
#define zbx_mem_realloc(info, old, size) __zbx_mem_realloc(__FILE__, __LINE__, info, old, size)
#define zbx_mem_free(info, ptr) \
\
do \
{ \
__zbx_mem_free(__FILE__, __LINE__, info, ptr); \
ptr = NULL; \
} \
while (0)
void *__zbx_mem_malloc(const char *file, int line, zbx_mem_info_t *info, const void *old, size_t size);
void *__zbx_mem_realloc(const char *file, int line, zbx_mem_info_t *info, void *old, size_t size);
void __zbx_mem_free(const char *file, int line, zbx_mem_info_t *info, void *ptr);
void zbx_mem_clear(zbx_mem_info_t *info);
void zbx_mem_get_stats(const zbx_mem_info_t *info, zbx_mem_stats_t *stats);
void zbx_mem_dump_stats(int level, zbx_mem_info_t *info);
size_t zbx_mem_required_size(int chunks_num, const char *descr, const char *param);
zbx_uint64_t zbx_mem_required_chunk_size(zbx_uint64_t size);