libevent

Libevent Memory Mange

#include <event2/event.h>                                                                 
#include <sys/types.h>                                                                    
#include <stdlib.h>                                                                       
                                                                                          
/* This union's purpose is to be as big as the largest of all the                         
 * types it contains. */
union alignment {                                                                         
    size_t sz;                                                                            
    void *ptr;                                                                            
    double dbl;                                                                           
};                                                                                 
/* We need to make sure that everything we return is on the right                         
   alignment to hold anything, including a double. */
#define ALIGNMENT sizeof(union alignment)                                                 
                                                                                          
/* We need to do this cast‐to‐char* trick on our pointers to adjust                       
   them; doing arithmetic on a void* is not standard. */
#define OUTPTR(ptr) (((char*)ptr)+ALIGNMENT)                                              
#define INPTR(ptr) (((char*)ptr)‐ALIGNMENT)                                               
                                                                                          
static size_t total_allocated = 0;                                                        
static void *replacement_malloc(size_t sz)                                                
{                                                                                 
    void *chunk = malloc(sz + ALIGNMENT);                                                 
    if (!chunk) return chunk;                                                             
    total_allocated += sz;                                                                
    *(size_t*)chunk = sz;                                                                 
    return OUTPTR(chunk);                                                                 
}                                                                                 
static void *replacement_realloc(void *ptr, size_t sz)                                    
{                                                                                 
    size_t old_size = 0;                                                                  
    if (ptr) {                                                                            
        ptr = INPTR(ptr);                                                                 
        old_size = *(size_t*)ptr;                                                         
    }                                                                                 
    ptr = realloc(ptr, sz + ALIGNMENT);

 if (!ptr)                                                                             
        return NULL;                                                                      
    *(size_t*)ptr = sz;                                                                   
    total_allocated = total_allocated ‐ old_size + sz;                                    
    return OUTPTR(ptr);                                                                   
}                                                                                 
static void replacement_free(void *ptr)                                                   
{                                                                                 
    ptr = INPTR(ptr);                                                                     
    total_allocated ‐= *(size_t*)ptr;                                                     
    free(ptr);                                                                            
}                                                                                 
void start_counting_bytes(void)                                                           
{                                                                                 
    event_set_mem_functions(replacement_malloc,                                           
                            replacement_realloc,                                          
                            replacement_free);                                            
}


先说明其实这个memory alignment其实是这样的

 
这里介绍两个宏
                                                                                       
/* We need to do this cast‐to‐char* trick on our pointers to adjust                       
   them; doing arithmetic on a void* is not standard. */
#define OUTPTR(ptr) (((char*)ptr)+ALIGNMENT)                                              
#define INPTR(ptr) (((char*)ptr)‐ALIGNMENT) 
第一个ptr是指向chunk的指针
第二个ptr是指向memory的指针

然后分析replace_malloc这个很简单
void *chunk = malloc(sz + ALIGNMENT);    //这里分配了sz+ALLGNMENT的内存memory和union alignment                                             
    if (!chunk) return chunk;                      //这里如果分配失败直接返回NULL                                       
    total_allocated += sz;                         //这里在total_allocated里面加上sz
                                       
    *(size_t*)chunk = sz;                              //然后给chunk的union alignment sz赋值                             
    return OUTPTR(chunk); //然后返回memory的指针


然后分析replace_realloc这个也很简单
size_t old_size = 0;                                                                  
    if (ptr) {                           //ptr本来就是就是一个memory    的指针                                             
        ptr = INPTR(ptr);             //    然后得到 chunk    的指针                                       
        old_size = *(size_t*)ptr;      //用old_size保存旧的memory的size
                                                   
    }                                                                                 
    ptr = realloc(ptr, sz + ALIGNMENT); //重新分配 用realloc
  if (!ptr)                                          //分配失败                                   
        return NULL;                             //这里其实可以返回 return ptr;                                         
    *(size_t*)ptr = sz;                             //然后把新分配的chunk 然后把sz赋给他                                      
    total_allocated = total_allocated ‐ old_size + sz;           //减去旧的size然后 加上新分配的memory                         
    return OUTPTR(ptr);  //返回memory的指针

replace_free这个也很简单
ptr = INPTR(ptr);                 //得到chunk的指针                                                    
    total_allocated ‐= *(size_t*)ptr;   //减去memory的大小                                                  
    free(ptr);     //释放ptr所占的内存


IN A WORD

这里在实际操作中使用的是memory的指针 ,但是这里自爱memory的头部加一个union alignment管理这个memory 然后无论在分配还是在释放的时候都用的是chunk
,而在实际使用的时候使用的是memory
这个memory management very easy 但是我在这里现在还没有发现 union alignment的优势我觉得这个还会占内存 ,这里没有得到大的用处,

看后续他的用处 估计union alignment也是作者的巧妙构思吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值