Nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c
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 #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 则封装 memalignvoid *
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
-
Nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c相关推荐
- Nginx源码完全注释(4)ngx_queue.h / ngx_queue.c
Nginx源码完全注释(4)ngx_queue.h / ngx_queue.c 作者:柳大·Poechant(钟超) 邮箱:zhongchao.ustc#gmail.com(# -> @) 博客 ...
- Nginx源码分析--数据对齐posix_memalign和memalign函数
posix_memalign函数() /* * 背景: * 1)POSIX 1003.1d * 2)POSIX 标明了通过malloc( ), calloc( ), 和 re ...
- 高并发 WEB 服务器 nginx 源码通读中文分析注释,带详细函数注释及函数调用注释,附 github 地址,后期持续维护更新...
github地址:https://github.com/y123456yz/reading-code-of-nginx-1.9.2 对源码通读注释,函数进行了详细注释.同时对相关功能进行了优化,日志功 ...
- nginx源码分析—内存池结构ngx_pool_t及内存管理
本博客( http://blog.csdn.net/livelylittlefish)贴出作者(阿波)相关研究.学习内容所做的笔记,欢迎广大朋友指正! Content 0.序 1.内存池结构 1.1 ...
- nginx源码编译、负载均衡及模块的扩展
1.nginx源码编译 实验环境: iptables和selinux关闭 redhat6.5 nginx:test1: 172.25.1.11 [root@test1 ~]# ls nginx-1.1 ...
- LNMP架构环境搭建之PHP、Nginx源码编译安装及其简单配置应用
LNMP架构中的Mysql见上一篇博文"LNMP架构环境搭建之mysql源码编译安装" 一.PHP简介 PHP(外文名:PHP: Hypertext Preprocessor,中文 ...
- nginx源码分析之内存池实现原理
建议看本文档时结合nginx源码: 1.1 什么是内存池?为什么要引入内存池? 内存池实质上是接替OS进行内存管理,应用程序申请内存时不再与OS打交道,而是从内存池中申请内存或者释放内存到内存池, ...
- NGINX源码之:ngx_hash
老规矩,先来看下ngx_hash大概的结构图: 注意: name的剩余字节是不在ngx_hash_elt_t的结构体占用内存中的,这里是个奇技淫巧,一定程度上保证key的不定长和内存浪费 在进入源码分 ...
- Nginx源码分析之ngx_hash_t
源码位置: nginx/src/core/ngx_hash.h nginx/src/core/ngx_hash.c (一)数组与hash表 从查询的角度来看,数组根据索引值的查询速度很快快. 原因在于 ...
最新文章
- python秒表游戏代码_用20行Python代码实现2048小游戏,你会吗?
- python中的控制流
- H5页面与ios交互返回上一级
- python给两个列表排序方法_Python连接两个字符串列表并对其元素排序的最佳方法...
- 应用PIC16F1938中IIC读取LM75A温度
- 怎么用editplus写asm文件_本周分享 | 用Typora写markdown文件
- HDU-3072-IntelligenceSystem(tarjan,贪心)
- Proguard 混淆之乱谈 3 混淆模板
- 安川机器人外部急停信号点不开_安川机器人外部IO启动
- python中文分词的主要方法
- 京城最有情调的14家餐厅
- 算法——霍夫曼编码压缩算法
- Python教程:在Python中遍历列表详解
- MpAndroidChart Y轴显示整数
- 2023年兔年好看的头像
- 腾讯物联网云平台 密钥 MQTT参数生成及密码加密算法实现
- 团队的英文翻译缩写_团队英语怎么写
- OpenCV之图像去噪声
- List Sorting
- 关于POS系统可靠性开发的一些考虑:
热门文章
- Unity3d之坦克大战(二)
- 晶体(crystal)和晶振(oscillator)的区别
- 洛谷 p2387 [noi2014] 膜fa♂森林 lct维护最小生成树
- 1.CSS3 教程-> 多列布局 > image模块 > cssTransition 过渡 > CSS Animations 动画 > Transform二维
- windows资源管理器关闭了怎么打开
- 【xla】七、【构图阶段】其他pass
- 初级中学计算机知识,计算机基础知识(初级中学级教学方案课程教案).doc
- iTutorGroup学员获2018全国青少年创意编程大赛一等奖
- winscp登录主机拒绝_winscp连接被拒绝怎么解决
- 埃隆 · 马斯克、山姆 · 奥特曼和 OpenAI 的秘史