Nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c

Nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c


首先看 ngx_alloc.h 文件,主要声明或宏定义了 ngx_alloc,ngx_calloc,ngx_memalign,ngx_free。


/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) Nginx, Inc.
 */


#ifndef _NGX_ALLOC_H_INCLUDED_
#define _NGX_ALLOC_H_INCLUDED_


#include 
  
  

   
   #include 
   
   



    
    void *ngx_alloc(size_t size, ngx_log_t *log);

    
    void *ngx_calloc(size_t size, ngx_log_t *log);


    
    // 宏命名 free 为 ngx_free,Nginx 的习惯

    
    #define ngx_free          free



    
    /*
 * Linux has memalign() or posix_memalign()
 * Solaris has memalign()
 * FreeBSD 7.0 has posix_memalign(), besides, early version's malloc()
 * aligns allocations bigger than page size at the page boundary
 */


    
    #if (NGX_HAVE_POSIX_MEMALIGN || NGX_HAVE_MEMALIGN)


    
    void *ngx_memalign(size_t alignment, size_t size, ngx_log_t *log);


    
    #else


    
    #define ngx_memalign(alignment, size, log)  ngx_alloc(size, log)


    
    #endif


    
    // 声明三个可以被外部使用的变量

    
    extern ngx_uint_t  ngx_pagesize;

    
    extern ngx_uint_t  ngx_pagesize_shift;

    
    extern ngx_uint_t  ngx_cacheline_size;



    
    #endif /* _NGX_ALLOC_H_INCLUDED_ */

   
   
  
  

再来看 ngx_alloc.c,实现了内存分配函数 ngx_alloc,ngx_calloc,ngx_


/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) Nginx, Inc.
 */


#include 
  
  

   
   #include 
   
   
    
    


ngx_uint_t  ngx_pagesize;
ngx_uint_t  ngx_pagesize_shift;
ngx_uint_t  ngx_cacheline_size;


    
    /*
 * 封装malloc,增加分配失败判断及调试日志
 */

    
    void *
ngx_alloc(size_t size, ngx_log_t *log)
{
    
    
    void  *p;

    p = malloc(size);
    
    
    if (p == NULL) {
        ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
                      
    
    "malloc(%uz) failed", size);
    }

    
    
    /* 在编译时指定debug模式是否开启,如果不开启则此句仅是括号中的逗号表达式 */
    ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 
    
    0, 
    
    "malloc: %p:%uz", p, size);

    
    
    return p;
}


    
    /*
 * 封装ngx_alloc,如果分配成功,初始化为0
 */

    
    void *
ngx_calloc(size_t size, ngx_log_t *log)
{
    
    
    void  *p;

    p = ngx_alloc(size, log);

    
    
    /* 初始化为 0 */
    
    
    if (p) {
        ngx_memzero(p, size);
    }

    
    
    return p;
}



    
    #if (NGX_HAVE_POSIX_MEMALIGN)


    
    // 封装 posix_memalign,如果是 Solaris 则封装 memalign

    
    void *
ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
{
    
    
    void  *p;
    
    
    int    err;

    
    
    /*
     * 背景:
     *      1)POSIX 1003.1d
     *      2)POSIX 标明了通过malloc( ), calloc( ), 和 realloc( ) 返回的地址对于
     *      任何的C类型来说都是对齐的
     * 功能:由posix_memalign分配的内存空间,需要由free释放。
     * 参数:
     *      p           分配好的内存空间的首地址
     *      alignment   对齐边界,Linux中,32位系统是8字节,64位系统是16字节
     *      size        指定分配size字节大小的内存
     *
     * 要求:
     *      1)要求alignment是2的幂,并且是p指针大小的倍数
     *      2)要求size是alignment的倍数
     * 返回:
     *      0       成功
     *      EINVAL  参数不满足要求
     *      ENOMEM  内存分配失败
     * 注意:
     *      1)该函数不影响errno,只能通过返回值判断
     *
     */
    err = posix_memalign(&p, alignment, size);

    
    
    if (err) {
        ngx_log_error(NGX_LOG_EMERG, log, err,
                      
    
    "posix_memalign(%uz, %uz) failed", alignment, size);
        p = NULL;
    }

    ngx_log_debug3(NGX_LOG_DEBUG_ALLOC, log, 
    
    0,
                   
    
    "posix_memalign: %p:%uz @%uz", p, size, alignment);

    
    
    return p;
}


    
    #elif (NGX_HAVE_MEMALIGN)


    
    void *
ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
{
    
    
    void  *p;

    
    
    // 与 posix_memalign 的不同是其将分配好的内存块首地址做为返回值
    p = memalign(alignment, size);
    
    
    if (p == NULL) {
        ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
                      
    
    "memalign(%uz, %uz) failed", alignment, size);
    }

    ngx_log_debug3(NGX_LOG_DEBUG_ALLOC, log, 
    
    0,
                   
    
    "memalign: %p:%uz @%uz", p, size, alignment);

    
    
    return p;
}


    
    #endif

   
   
  
  

-

转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant

-

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钟超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值