设为首页 收藏本站
查看: 670|回复: 0

[经验分享] Nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c

[复制链接]

尚未签到

发表于 2016-12-28 11:06:47 | 显示全部楼层 |阅读模式
Nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c


  • 作者:柳大·Poechant
  • 时间:2012年7月1日
  • 博客:Blog.CSDN.net/Poechant


首先看 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 <ngx_config h=""><span class="preprocessor" style="color: rgb(136, 0, 0); ">#include </span><ngx_core h=""><span class="keyword" style="font-weight: bold; ">void</span> *ngx_alloc(size_t size, ngx_log_t *log);
<span class="keyword" style="font-weight: bold; ">void</span> *ngx_calloc(size_t size, ngx_log_t *log);
<span class="comment" style="color: rgb(136, 136, 136); ">// 宏命名 free 为 ngx_free,Nginx 的习惯</span>
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#define ngx_free          free</span>

<span class="comment" style="color: rgb(136, 136, 136); ">/*
* 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
*/</span>
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#if (NGX_HAVE_POSIX_MEMALIGN || NGX_HAVE_MEMALIGN)</span>
<span class="keyword" style="font-weight: bold; ">void</span> *ngx_memalign(size_t alignment, size_t size, ngx_log_t *log);
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#else</span>
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#define ngx_memalign(alignment, size, log)  ngx_alloc(size, log)</span>
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#endif</span>
<span class="comment" style="color: rgb(136, 136, 136); ">// 声明三个可以被外部使用的变量</span>
<span class="keyword" style="font-weight: bold; ">extern</span> ngx_uint_t  ngx_pagesize;
<span class="keyword" style="font-weight: bold; ">extern</span> ngx_uint_t  ngx_pagesize_shift;
<span class="keyword" style="font-weight: bold; ">extern</span> ngx_uint_t  ngx_cacheline_size;

<span class="preprocessor" style="color: rgb(136, 0, 0); ">#endif /* _NGX_ALLOC_H_INCLUDED_ */</span>
</ngx_core></ngx_config>
再来看 ngx_alloc.c,实现了内存分配函数 ngx_alloc,ngx_calloc,ngx_


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


#include <ngx_config h=""><span class="preprocessor" style="color: rgb(136, 0, 0); ">#include </span><ngx_core h="">

ngx_uint_t  ngx_pagesize;
ngx_uint_t  ngx_pagesize_shift;
ngx_uint_t  ngx_cacheline_size;
<span class="comment" style="color: rgb(136, 136, 136); ">/*
* 封装malloc,增加分配失败判断及调试日志
*/</span>
<span class="keyword" style="font-weight: bold; ">void</span> *
ngx_alloc(size_t size, ngx_log_t *log)
{
<span class="keyword" style="font-weight: bold; ">void</span>  *p;
p = malloc(size);
<span class="keyword" style="font-weight: bold; ">if</span> (p == NULL) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
<span class="string" style="color: rgb(136, 0, 0); ">"malloc(%uz) failed"</span>, size);
}
<span class="comment" style="color: rgb(136, 136, 136); ">/* 在编译时指定debug模式是否开启,如果不开启则此句仅是括号中的逗号表达式 */</span>
ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, <span class="number" style="color: rgb(0, 136, 0); ">0</span>, <span class="string" style="color: rgb(136, 0, 0); ">"malloc: %p:%uz"</span>, p, size);
<span class="keyword" style="font-weight: bold; ">return</span> p;
}
<span class="comment" style="color: rgb(136, 136, 136); ">/*
* 封装ngx_alloc,如果分配成功,初始化为0
*/</span>
<span class="keyword" style="font-weight: bold; ">void</span> *
ngx_calloc(size_t size, ngx_log_t *log)
{
<span class="keyword" style="font-weight: bold; ">void</span>  *p;
p = ngx_alloc(size, log);
<span class="comment" style="color: rgb(136, 136, 136); ">/* 初始化为 0 */</span>
<span class="keyword" style="font-weight: bold; ">if</span> (p) {
ngx_memzero(p, size);
}
<span class="keyword" style="font-weight: bold; ">return</span> p;
}

<span class="preprocessor" style="color: rgb(136, 0, 0); ">#if (NGX_HAVE_POSIX_MEMALIGN)</span>
<span class="comment" style="color: rgb(136, 136, 136); ">// 封装 posix_memalign,如果是 Solaris 则封装 memalign</span>
<span class="keyword" style="font-weight: bold; ">void</span> *
ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
{
<span class="keyword" style="font-weight: bold; ">void</span>  *p;
<span class="keyword" style="font-weight: bold; ">int</span>    err;
<span class="comment" style="color: rgb(136, 136, 136); ">/*
* 背景:
*      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,只能通过返回值判断
*
*/</span>
err = posix_memalign(&amp;p, alignment, size);
<span class="keyword" style="font-weight: bold; ">if</span> (err) {
ngx_log_error(NGX_LOG_EMERG, log, err,
<span class="string" style="color: rgb(136, 0, 0); ">"posix_memalign(%uz, %uz) failed"</span>, alignment, size);
p = NULL;
}
ngx_log_debug3(NGX_LOG_DEBUG_ALLOC, log, <span class="number" style="color: rgb(0, 136, 0); ">0</span>,
<span class="string" style="color: rgb(136, 0, 0); ">"posix_memalign: %p:%uz @%uz"</span>, p, size, alignment);
<span class="keyword" style="font-weight: bold; ">return</span> p;
}
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#elif (NGX_HAVE_MEMALIGN)</span>
<span class="keyword" style="font-weight: bold; ">void</span> *
ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
{
<span class="keyword" style="font-weight: bold; ">void</span>  *p;
<span class="comment" style="color: rgb(136, 136, 136); ">// 与 posix_memalign 的不同是其将分配好的内存块首地址做为返回值</span>
p = memalign(alignment, size);
<span class="keyword" style="font-weight: bold; ">if</span> (p == NULL) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
<span class="string" style="color: rgb(136, 0, 0); ">"memalign(%uz, %uz) failed"</span>, alignment, size);
}
ngx_log_debug3(NGX_LOG_DEBUG_ALLOC, log, <span class="number" style="color: rgb(0, 136, 0); ">0</span>,
<span class="string" style="color: rgb(136, 0, 0); ">"memalign: %p:%uz @%uz"</span>, p, size, alignment);
<span class="keyword" style="font-weight: bold; ">return</span> p;
}
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#endif</span>
</ngx_core></ngx_config>
-
转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant
-

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-320661-1-1.html 上篇帖子: 解剖Nginx·自动脚本篇(3)源码相关变量脚本 auto/sources 下篇帖子: centos6.3环境下nginx,php,memcache,memadmin的安装部署5
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表