首先看 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

nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c的更多相关文章

  1. Nginx源码完全注释(6)core/murmurhash

    下面是摘自 Google Code 的 Murmurhash 开源项目主页上的 Murmurhash2,Nginx 就是采用的这个. uint32_t MurmurHash2 ( const void ...

  2. Nginx 源码完全注释(11)ngx_spinlock

    Nginx 是多进程模式的,一个 master 与多个 workers,一般工作在多核 CPU 上,所以自旋锁就是必须用到的.Nginx 中的自旋锁的定义,位于 ngx_spinlock.c 中,如下 ...

  3. Nginx源码完全注释(7)ngx_palloc.h/ngx_palloc.c

    ngx_palloc.h /* * NGX_MAX_ALLOC_FROM_POOL should be (ngx_pagesize - 1), i.e. 4095 on x86. * On Windo ...

  4. Nginx源码完全注释(2)ngx_array.h / ngx_array.c

    数组头文件 ngx_array.h #include <ngx_config.h> #include <ngx_core.h> struct ngx_array_s { voi ...

  5. Nginx源码完全注释(4)ngx_queue.h / ngx_queue.c

    队列头文件ngx_queue.h #include <ngx_config.h> #include <ngx_core.h> #ifndef _NGX_QUEUE_H_INCL ...

  6. Nginx源码完全注释(3)ngx_list.h / ngx_list.c

    列表头文件ngx_list.h #ifndef _NGX_LIST_H_INCLUDED_ #define _NGX_LIST_H_INCLUDED_ #include <ngx_config. ...

  7. Nginx 源码完全注释(10)ngx_radix_tree

    ngx_radix_tree.h // 未被使用的节点 #define NGX_RADIX_NO_VALUE (uintptr_t) -1 typedef struct ngx_radix_node_ ...

  8. Nginx源码完全注释(9)nginx.c: ngx_get_options

    本文分析 ngxin.c 中的 ngx_get_options 函数,其影响: nginx.c 中的: static ngx_uint_t ngx_show_help; static ngx_uint ...

  9. Nginx源码完全注释(8)ngx_errno.c

    errno.h中的strerror(int errno)可以确定指定的errno的错误的提示信息.在 Nginx 中,将所有错误提示信息预先存储在一个数组里,而预先确定这个数组的大小,是在自动化脚本中 ...

随机推荐

  1. 记录一些WPF常用样式方便以后复用(二)(Button、CheckBox、输入账号密码框)(转)

    Button (一) <Style x:Key="ButtonSaveStyle" TargetType="{x:Type Button}"> &l ...

  2. [深度学习]实现一个博弈型的AI,从五子棋开始

    好久没有写过博客了,多久,大概8年???最近重新把写作这事儿捡起来……最近在折腾AI,写个AI相关的给团队的小伙伴们看吧. 搞了这么多年的机器学习,从分类到聚类,从朴素贝叶斯到SVM,从神经网络到深度 ...

  3. macOS -- 为什么XAMPP启动后输localhost跳转到http://localhost/dashboard?

    在XAMPP环境下,当我们在地址栏输入'localhost'的时候,进入的不是htdocs根目录下,而是直接跳转到了http://localhost/dashboard?下. 这是因为在xamppfi ...

  4. 自定义显示提示一段时间自动消失ShowMsgText控件

    public partial class ShowMsgText : Label { public string TextMsg { get { return Text; } set { timer1 ...

  5. VMware下安装的Mac OS X如何修改显示分辨率 (转)

    我在Win7下利用VMware安装了苹果的OS x 10.8系统,安装成功启动后,发现分辨率为1024*768,而宿机的分辨率是1440*900,我想让虚拟机全屏显示,也就是想在雪豹下屏幕的分辨率也能 ...

  6. 解决Intel SSD 330i 240G在Windows 8启动慢的问题

    笔者半年前更新了一下老迈的Thinkpad T410i笔记本电脑,在美国亚马逊海购了Intel SSD 330i 240G.(PS:购入价USD 129.99 ,现在最新的335i 240G差不多要U ...

  7. 部署docker

    部署和开发环境不一样,我们不需要频繁地进入到容器内部,所以一般我们会将代码和环境打包到一块,部署到服务器上 Clone 代码 将项目代码克隆到本地 git clone git@git.coding.n ...

  8. CentOS6.4安装辅助NIS的流程

    服务器端软件包安装 yum -y install yp-tools ypbind ypserv rpcbind 设置NIS的域名 echo 'NISDOMAIN=liebaonis.local' &g ...

  9. Java中的GetOpt操作

    在shell工具中,有专门的getopt函数,使用方法如下所示: while getopts "d:t:vh" opt; do case "${opt}" in ...

  10. 【BZOJ】2959: 长跑(lct+缩点)(暂时弃坑)

    题目 传送门:QWQ 分析 看起来就是一个支持link的东西. 但有环,考虑缩点...... 但疯狂Tle.大概是常数卡不过去. 行走的大常数noble_ 代码 #include <bits/s ...