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
nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c的更多相关文章
- Nginx源码完全注释(6)core/murmurhash
下面是摘自 Google Code 的 Murmurhash 开源项目主页上的 Murmurhash2,Nginx 就是采用的这个. uint32_t MurmurHash2 ( const void ...
- Nginx 源码完全注释(11)ngx_spinlock
Nginx 是多进程模式的,一个 master 与多个 workers,一般工作在多核 CPU 上,所以自旋锁就是必须用到的.Nginx 中的自旋锁的定义,位于 ngx_spinlock.c 中,如下 ...
- 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 ...
- Nginx源码完全注释(2)ngx_array.h / ngx_array.c
数组头文件 ngx_array.h #include <ngx_config.h> #include <ngx_core.h> struct ngx_array_s { voi ...
- Nginx源码完全注释(4)ngx_queue.h / ngx_queue.c
队列头文件ngx_queue.h #include <ngx_config.h> #include <ngx_core.h> #ifndef _NGX_QUEUE_H_INCL ...
- Nginx源码完全注释(3)ngx_list.h / ngx_list.c
列表头文件ngx_list.h #ifndef _NGX_LIST_H_INCLUDED_ #define _NGX_LIST_H_INCLUDED_ #include <ngx_config. ...
- Nginx 源码完全注释(10)ngx_radix_tree
ngx_radix_tree.h // 未被使用的节点 #define NGX_RADIX_NO_VALUE (uintptr_t) -1 typedef struct ngx_radix_node_ ...
- Nginx源码完全注释(9)nginx.c: ngx_get_options
本文分析 ngxin.c 中的 ngx_get_options 函数,其影响: nginx.c 中的: static ngx_uint_t ngx_show_help; static ngx_uint ...
- Nginx源码完全注释(8)ngx_errno.c
errno.h中的strerror(int errno)可以确定指定的errno的错误的提示信息.在 Nginx 中,将所有错误提示信息预先存储在一个数组里,而预先确定这个数组的大小,是在自动化脚本中 ...
随机推荐
- Web 漏洞分析与防御之 CSRF(二)
原文地址:Web 漏洞分析与防御之 CSRF(二) 博客地址:http://www.extlight.com 一.全称 跨站请求伪造(Cross-site Request Forgery) 二.原理 ...
- vue怎么自定义指令??
最近看看vue中自定义指令,感觉vue的指令和angular1的指令相差较大 <script> //指令钩子函数: /* bind 只调用一次,指令第一次绑定到元素的时调用 inserte ...
- volatile浅析
volatile的意思是不稳定的.易变的,但在多线程中却跟字面意思一毛钱关系没有.作为一个变量修饰符,它主要有两个作用:一个是告诉大家,该变量是一个在多个线程之间均可见的变量:另一个是告诉java虚拟 ...
- RK3288 添加WiFi&BT模块AP6212
CPU:RK3288 系统:Android 5.1 注:系统中自带的模块没有AP6212,相近的只有AP6210,设置为AP6210,直接添加固件也可以正常使用. 此文是手动添加AP6212的例程. ...
- python 多线程要点
要点整理 多线程 #coding=utf-8 import threading from time import ctime,sleep def music(func): for i in range ...
- 解决近期linux下yum更新出现HTTP Error 404 NOT FOUND错误的办法
本文转载自:http://tech.lezi.com/archives/47 最近两天使用yum的163源,出现404错误 [root@localhost yum.repos.d]# yum make ...
- (转)Inno Setup入门(五)——添加readme文件
本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17250771 这个实现起来很简单,就是在[files]段中的某个预先 ...
- nginx statistics in multi-workers
1. 从网上下载多进程统计补丁.https://github.com/arut/nginx-patches 2. Copy补丁文件per-worker-listener到nginx ...
- 以太坊客户端Geth命令用法
命令用法 geth [选项] 命令 [命令选项] [参数…] 命令: account 管理账户attach 启动交互式JavaScript环境(连接到节点)bug 上报bug Issuesconsol ...
- 数据结构与算法JavaScript描述——栈的使用
有一些问题特别适合用栈来解决.本节就介绍几个这样的例子. 1) 数制间的相互转换 可以利用栈将一个数字从一种数制转换成另一种数制.假设想将数字n 转换为以b 为基数的数字,实现转换的算法如下. 使 ...