当前位置: 首页 > news >正文

探索Nginx:深入理解Nginx基础组件的使用

1.1、ngx_palloc相关源码

/src/core/ngx_palloc.h。(相关实现在/src/core/ngx_palloc.c文件)

展开

代码语言:C

自动换行

AI代码解释

#ifndef _NGX_PALLOC_H_INCLUDED_ #define _NGX_PALLOC_H_INCLUDED_ #include <ngx_config.h> #include <ngx_core.h> /* * NGX_MAX_ALLOC_FROM_POOL should be (ngx_pagesize - 1), i.e. 4095 on x86. * On Windows NT it decreases a number of locked pages in a kernel. */ #define NGX_MAX_ALLOC_FROM_POOL (ngx_pagesize - 1) #define NGX_DEFAULT_POOL_SIZE (16 * 1024) #define NGX_POOL_ALIGNMENT 16 #define NGX_MIN_POOL_SIZE \ ngx_align((sizeof(ngx_pool_t) + 2 * sizeof(ngx_pool_large_t)), \ NGX_POOL_ALIGNMENT) typedef void (*ngx_pool_cleanup_pt)(void *data); typedef struct ngx_pool_cleanup_s ngx_pool_cleanup_t; struct ngx_pool_cleanup_s { ngx_pool_cleanup_pt handler; void *data; ngx_pool_cleanup_t *next; }; typedef struct ngx_pool_large_s ngx_pool_large_t; struct ngx_pool_large_s { ngx_pool_large_t *next; void *alloc; }; typedef struct { u_char *last; u_char *end; ngx_pool_t *next; ngx_uint_t failed; } ngx_pool_data_t; struct ngx_pool_s { ngx_pool_data_t d; size_t max; ngx_pool_t *current; ngx_chain_t *chain; ngx_pool_large_t *large; ngx_pool_cleanup_t *cleanup; ngx_log_t *log; }; typedef struct { ngx_fd_t fd; u_char *name; ngx_log_t *log; } ngx_pool_cleanup_file_t; ngx_pool_t *ngx_create_pool(size_t size, ngx_log_t *log); void ngx_destroy_pool(ngx_pool_t *pool); void ngx_reset_pool(ngx_pool_t *pool); void *ngx_palloc(ngx_pool_t *pool, size_t size); void *ngx_pnalloc(ngx_pool_t *pool, size_t size); void *ngx_pcalloc(ngx_pool_t *pool, size_t size); void *ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment); ngx_int_t ngx_pfree(ngx_pool_t *pool, void *p); ngx_pool_cleanup_t *ngx_pool_cleanup_add(ngx_pool_t *p, size_t size); void ngx_pool_run_cleanup_file(ngx_pool_t *p, ngx_fd_t fd); void ngx_pool_cleanup_file(void *data); void ngx_pool_delete_file(void *data); #endif /* _NGX_PALLOC_H_INCLUDED_ */

/src/core/ngx_palloc.c

展开

代码语言:C

自动换行

AI代码解释

void * ngx_array_push(ngx_array_t *a) { void *elt, *new; size_t size; ngx_pool_t *p; if (a->nelts == a->nalloc) { /* the array is full */ size = a->size * a->nalloc; p = a->pool; if ((u_char *) a->elts + size == p->d.last && p->d.last + a->size <= p->d.end) { /* * the array allocation is the last in the pool * and there is space for new allocation */ p->d.last += a->size; a->nalloc++; } else { /* allocate a new array */ new = ngx_palloc(p, 2 * size); if (new == NULL) { return NULL; } ngx_memcpy(new, a->elts, size); a->elts = new; a->nalloc *= 2; } } elt = (u_char *) a->elts + a->size * a->nelts; a->nelts++; return elt; }

/src/core/ngx_core.h

代码语言:C

自动换行

AI代码解释

// ... typedef struct ngx_pool_s ngx_pool_t; // ...

1.2、ngx_array组件的相关源码

/src/core/ngx_array.h

展开

代码语言:C

自动换行

AI代码解释

/* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */ #ifndef _NGX_ARRAY_H_INCLUDED_ #define _NGX_ARRAY_H_INCLUDED_ #include <ngx_config.h> #include <ngx_core.h> typedef struct { void *elts; ngx_uint_t nelts; size_t size; ngx_uint_t nalloc; ngx_pool_t *pool; } ngx_array_t; ngx_array_t *ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size); void ngx_array_destroy(ngx_array_t *a); void *ngx_array_push(ngx_array_t *a); void *ngx_array_push_n(ngx_array_t *a, ngx_uint_t n); static ngx_inline ngx_int_t ngx_array_init(ngx_array_t *array, ngx_pool_t *pool, ngx_uint_t n, size_t size) { /* * set "array->nelts" before "array->elts", otherwise MSVC thinks * that "array->nelts" may be used without having been initialized */ array->nelts = 0; array->size = size; array->nalloc = n; array->pool = pool; array->elts = ngx_palloc(pool, n * size); if (array->elts == NULL) { return NGX_ERROR; } return NGX_OK; } #endif /* _NGX_ARRAY_H_INCLUDED_ */

1.3、ngx_array的数据结构

展开

代码语言:C

自动换行

AI代码解释

typedef struct { void *elts; ngx_uint_t nelts; size_t size; ngx_uint_t nalloc; ngx_pool_t *pool; } ngx_array_t;

elts:指向内存数据的指针。nelts:指示已使用了多少个元素。size:数组元素的大小。nalloc:分配的元素数量。pool:内存池。

array在内存里的布局:

1.4、ngx_cycle简介和相关源码

Nginx的每个进程内部都有一个自己的ngx_cycle。

/src/core/ngx_cycle.h

展开

代码语言:C

自动换行

AI代码解释

#ifndef _NGX_CYCLE_H_INCLUDED_ #define _NGX_CYCLE_H_INCLUDED_ #include <ngx_config.h> #include <ngx_core.h> #ifndef NGX_CYCLE_POOL_SIZE #define NGX_CYCLE_POOL_SIZE NGX_DEFAULT_POOL_SIZE #endif #define NGX_DEBUG_POINTS_STOP 1 #define NGX_DEBUG_POINTS_ABORT 2 typedef struct ngx_shm_zone_s ngx_shm_zone_t; typedef ngx_int_t (*ngx_shm_zone_init_pt) (ngx_shm_zone_t *zone, void *data); struct ngx_shm_zone_s { void *data; ngx_shm_t shm; ngx_shm_zone_init_pt init; void *tag; ngx_uint_t noreuse; /* unsigned noreuse:1; */ }; struct ngx_cycle_s { // ... }; typedef struct { // ... } ngx_core_conf_t; #define ngx_is_init_cycle(cycle) (cycle->conf_ctx == NULL) ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle); ngx_int_t ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log); void ngx_delete_pidfile(ngx_cycle_t *cycle); ngx_int_t ngx_signal_process(ngx_cycle_t *cycle, char *sig); void ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user); char **ngx_set_environment(ngx_cycle_t *cycle, ngx_uint_t *last); ngx_pid_t ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv); ngx_cpuset_t *ngx_get_cpu_affinity(ngx_uint_t n); ngx_shm_zone_t *ngx_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name, size_t size, void *tag); void ngx_set_shutdown_timer(ngx_cycle_t *cycle); extern volatile ngx_cycle_t *ngx_cycle; extern ngx_array_t ngx_old_cycles; extern ngx_module_t ngx_core_module; extern ngx_uint_t ngx_test_config; extern ngx_uint_t ngx_dump_config; extern ngx_uint_t ngx_quiet_mode; #endif /* _NGX_CYCLE_H_INCLUDED_ */

1.5、ngx_list相关源码

/src/core/ngx_list.h

展开

代码语言:C

自动换行

AI代码解释

#ifndef _NGX_LIST_H_INCLUDED_ #define _NGX_LIST_H_INCLUDED_ #include <ngx_config.h> #include <ngx_core.h> typedef struct ngx_list_part_s ngx_list_part_t; struct ngx_list_part_s { void *elts; ngx_uint_t nelts; ngx_list_part_t *next; }; typedef struct { ngx_list_part_t *last; ngx_list_part_t part; size_t size; ngx_uint_t nalloc; ngx_pool_t *pool; } ngx_list_t; ngx_list_t *ngx_list_create(ngx_pool_t *pool, ngx_uint_t n, size_t size); static ngx_inline ngx_int_t ngx_list_init(ngx_list_t *list, ngx_pool_t *pool, ngx_uint_t n, size_t size) { list->part.elts = ngx_palloc(pool, n * size); if (list->part.elts == NULL) { return NGX_ERROR; } list->part.nelts = 0; list->part.next = NULL; list->last = &list->part; list->size = size; list->nalloc = n; list->pool = pool; return NGX_OK; } void *ngx_list_push(ngx_list_t *list); #endif /* _NGX_LIST_H_INCLUDED_ */

1.6、ngx_list 的数据结构

展开

代码语言:C

自动换行

AI代码解释

typedef struct ngx_list_part_s ngx_list_part_t; struct ngx_list_part_s { void *elts; ngx_uint_t nelts; ngx_list_part_t *next; }; typedef struct { ngx_list_part_t *last; ngx_list_part_t part; size_t size; ngx_uint_t nalloc; ngx_pool_t *pool; } ngx_list_t;

elts:指向内存数据的指针。nelts:指示已使用了多少个元素。size:数组元素的大小。nalloc:分配的元素数量。pool:内存池。

list 在内存里的布局:

二、Nginx 组件的使用

Nginx的内存池分成大小块,小块是要么不释放要么全部释放。ngx_create_pool(size_t size, ngx_log_t *log)里的size就是用来区分大小块的,大于size的就是大块,否则就是小块。


http://www.jsqmd.com/news/787565/

相关文章:

  • Python自动化掘金工具:自然语言驱动内容管理与爬虫实战
  • UFS低功耗设计:M-PHY与UniPro的电源管理机制解析
  • 思维之树框架:用搜索算法提升大语言模型复杂推理能力
  • 2026年知名的河南酒店静音通风设备/通风设备精选推荐公司 - 品牌宣传支持者
  • C++默认成员函数与构造析构解析
  • 2026四川型钢怎么选?核心维度与合规供应企业盘点 - 四川盛世钢联营销中心
  • dotai-cli:将AI无缝集成到命令行的开发者效率工具
  • AI协同编程:从代码生成到项目级开发的智能辅助实践
  • AI赋能内容管理:从CMS到智能内容工作流的设计与实践
  • Trae生成的中文编程语言关键字(如“定“、“函“、“印“等)需要和标识符之间用 空格 隔开,以确保正确识别
  • 【实战指南】如何用Sunshine搭建自托管游戏串流服务器:从零到精通的完整方案
  • 2026年濮阳减肥训练营排行:哪家好评最多? - 速递信息
  • 基于大语言模型的自动化知识图谱模式生成:原理、实践与应用
  • Windows PDF处理工具:3分钟掌握Poppler预编译包全攻略
  • 移动端视频帧插值技术:ANVIL框架与NPU优化实践
  • CANN/cannbot-skills FA入参调试工具
  • 别再手动下架了!Temu查重复铺货那晚,我10分钟救了三个店
  • Kubernetes集群一键部署:k8s-tew发行版实战指南
  • REFramework技术深度解析:RE引擎游戏Mod开发的架构革命
  • 开源学术写作工具箱:自动化工作流提升研究效率
  • ChatGPT-AutoExpert:构建领域专家提示词,实现AI深度专业协作
  • 看到语法文档里文言心里还是用到了竖引号:「expr」 ,请不要使用竖引号,用其它符号代替!
  • 基于OpenAI Responses API的AI应用开发:从工具调用到联网搜索
  • CANN/SiP三维FFT接口文档
  • 网盘直链解析技术深度剖析:构建跨平台文件获取架构的实战指南
  • 基于MCP协议的AI智能体实时金融数据工具箱Tickerr详解
  • 解锁AI潜能:系统提示词设计模式与实战应用指南
  • 大模型智能路由引擎:动态调度多AI模型实现降本增效
  • ARM PMU架构与PMCNTENCLR_EL0寄存器详解
  • SpringBoot+Vue 工程教育认证计算机课程管理平台管理平台源码【适合毕设/课设/学习】Java+MySQL