zmalloc.h
對c的函數進行了封裝
void *zmalloc(size_t size); //調用zmalloc申請size個大小的空間
void *zcalloc(size_t size); // 調用系統函數calloc函數申請空間
void *zrealloc(void *ptr, size_t size); // 原內存重新調整空間爲size的大小
void zfree(void *ptr); //釋放內存 free
char *zstrdup(const char *s); // 字符串複製方法
size_t zmalloc_used_memory(void); //獲取當前已經佔用的內存大小
void zmalloc_enable_thread_safeness(void); // 是否設置線程安全模式
void zmalloc_set_oom_handler(void (*oom_handler)(size_t)); // 可自定義設置內存溢出的處理方法
float zmalloc_get_fragmentation_ratio(size_t rss); // 所給大小與已使用內存大小之比
size_t zmalloc_get_rss(void); // 獲取rss的大小
size_t zmalloc_get_private_dirty(void); //獲取私有的髒數據大小
void zlibc_free(void *ptr); // 原始系統free釋放方法
zmalloc.c
介紹幾個函數
#if defined(USE_TCMALLOC) //使用tcmalloc
#define malloc(size) tc_malloc(size)
#define calloc(count,size) tc_calloc(count,size)
#define realloc(ptr,size) tc_realloc(ptr,size)
#define free(ptr) tc_free(ptr)
#elif defined(USE_JEMALLOC) //使用jemalloc
#define malloc(size) je_malloc(size)
#define calloc(count,size) je_calloc(count,size)
#define realloc(ptr,size) je_realloc(ptr,size)
#define free(ptr) je_free(ptr)
#endif
#ifdef HAVE_ATOMIC
#define update_zmalloc_stat_add(__n) __sync_add_and_fetch(&used_memory, (__n))
#define update_zmalloc_stat_sub(__n) __sync_sub_and_fetch(&used_memory, (__n))
#else
#define update_zmalloc_stat_add(__n) do { \ //增加__n的大小到used_memory
pthread_mutex_lock(&used_memory_mutex); \
used_memory += (__n); \
pthread_mutex_unlock(&used_memory_mutex); \
} while(0)
#define update_zmalloc_stat_sub(__n) do { \ //減小__n的大小到used_memory
pthread_mutex_lock(&used_memory_mutex); \
used_memory -= (__n); \
pthread_mutex_unlock(&used_memory_mutex); \
} while(0)
#endif
變量解釋:
static size_t used_memory = 0; // redis服務使用的總的內存大小
static int zmalloc_thread_safe = 0; //線程安全 加鎖的方式增加或減小used_memory
pthread_mutex_t used_memory_mutex = PTHREAD_MUTEX_INITIALIZER;
#define update_zmalloc_stat_alloc(__n) do { \ //底層調用的上面談到的兩個宏
size_t _n = (__n); \
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \ // 字長對齊對申請n字節大小的內存進行內存大小的對齊 表示實際申請的大小 如果n=5,sizeof(long) = 8,那麼真正的n值大小等於n=5+8-5=8
if (zmalloc_thread_safe) { \
update_zmalloc_stat_add(_n); \
} else { \
used_memory += _n; \
} \
} while(0)
#define update_zmalloc_stat_free(__n) do { \ //同上
size_t _n = (__n); \
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
if (zmalloc_thread_safe) { \
update_zmalloc_stat_sub(_n); \
} else { \
used_memory -= _n; \
} \
} while(0)
示例函數
void *zrealloc(void *ptr, size_t size) {
#ifndef HAVE_MALLOC_SIZE
void *realptr;
#endif
size_t oldsize;
void *newptr;
if (ptr == NULL) return zmalloc(size); //直接分配即可
#ifdef HAVE_MALLOC_SIZE
oldsize = zmalloc_size(ptr);
newptr = realloc(ptr,size);
if (!newptr) zmalloc_oom_handler(size); //內存超終止
update_zmalloc_stat_free(oldsize);
update_zmalloc_stat_alloc(zmalloc_size(newptr));
return newptr;
#else
realptr = (char*)ptr-PREFIX_SIZE; //PREFIX_SIZE 等於size_t的大小 用於保存實際分配的內存大小
oldsize = *((size_t*)realptr);
newptr = realloc(realptr,size+PREFIX_SIZE);
if (!newptr) zmalloc_oom_handler(size);
*((size_t*)newptr) = size; //分配內存的前PREFIX_SIZE個字節用於記錄真實的大小 方便內存管理
update_zmalloc_stat_free(oldsize);
update_zmalloc_stat_alloc(size);
return (char*)newptr+PREFIX_SIZE;
#endif
}
#ifndef HAVE_MALLOC_SIZE
size_t zmalloc_size(void *ptr) {
void *realptr = (char*)ptr-PREFIX_SIZE;
size_t size = *((size_t*)realptr);
/* Assume at least that all the allocations are padded at sizeof(long) by
* the underlying allocator. */
if (size&(sizeof(long)-1)) size += sizeof(long)-(size&(sizeof(long)-1));
return size+PREFIX_SIZE;
}
#endif