手写Nginx内存池移植项目 注意点
1.所有需要传入内存池入口指针(pool)的地方都不用传入了,因为私有变量pool_存储的就是内存池入口指针
2.在C++中,void*
指针是一个通用指针类型,它可以指向任何类型的数据。然而,由于C++是一种类型安全的语言,直接将 void*
转换为其他类型的指针(或反之)而不进行显式转换 (即“强转”)通常是不被允许的,或者至少是不被推荐的。
0.预编译文件 1 2 3 4 5 6 #pragma once #ifndef PCH_H #define PCH_H #endif
1.ngx_mem_pool.h 变量和接口函数定义 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 #pragma once #include <stdlib.h> #include <memory.h> using u_char = unsigned char ;using ngx_uint_t = unsigned int ;struct ngx_pool_s ;typedef void (*ngx_pool_cleanup_pt) (void * data) ; struct ngx_pool_cleanup_s { ngx_pool_cleanup_pt handler; void * data; ngx_pool_cleanup_s* next; }; struct ngx_pool_large_s { ngx_pool_large_s* next; void * alloc; }; struct ngx_pool_data_t { u_char* last; u_char* end; ngx_pool_s* next; ngx_uint_t failed; } ; struct ngx_pool_s { ngx_pool_data_t d; size_t max; ngx_pool_s* current; ngx_pool_large_s* large; ngx_pool_cleanup_s* cleanup; }; #define ngx_align(d, a) (((d) + (a - 1)) & ~(a - 1)) #define NGX_ALIGNMENT sizeof(unsigned long) #define ngx_align_ptr(p, a) \ (u_char *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1)) #define ngx_memzero(buf, n) (void) memset(buf, 0, n) const int ngx_pagesize = 4096 ;const int NGX_MAX_ALLOC_FROM_POOL = ngx_pagesize - 1 ;const int NGX_DEFAULT_POOL_SIZE = 16 * 1024 ;const int NGX_POOL_ALIGNMENT = 16 ;const int NGX_MIN_POOL_SIZE = ngx_align ((sizeof (ngx_pool_s) + 2 * sizeof (ngx_pool_large_s)), NGX_POOL_ALIGNMENT); class ngx_mem_pool { public : void * ngx_create_pool (size_t size) ; void * ngx_palloc (size_t size) ; void * ngx_pnalloc (size_t size) ; void * ngx_pcalloc (size_t size) ; void ngx_pfree (void * p) ; void ngx_reset_pool () ; void ngx_destroy_pool () ; ngx_pool_cleanup_s* ngx_pool_cleanup_add (size_t size) ; private : ngx_pool_s* pool; void * ngx_palloc_small (size_t size, ngx_uint_t align) ; void * ngx_palloc_block (size_t size) ; void * ngx_palloc_large (size_t size) ; };
2.ngx_mem_pool.cpp 接口具体实现 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 #include "pch.h" #include "ngx_mem_pool.h" void * ngx_mem_pool::ngx_create_pool (size_t size) { ngx_pool_s* p; p = (ngx_pool_s*)malloc (size); if (p == nullptr ) { return nullptr ; } p->d.last = (u_char*)p + sizeof (ngx_pool_s); p->d.end = (u_char*)p + size; p->d.next = nullptr ; p->d.failed = 0 ; size = size - sizeof (ngx_pool_s); p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; p->current = p; p->large = nullptr ; p->cleanup = nullptr ; pool = p; return p; } void * ngx_mem_pool::ngx_palloc (size_t size) { if (size <= pool->max) { return ngx_palloc_small (size, 1 ); } return ngx_palloc_large (size); } void * ngx_mem_pool::ngx_pnalloc (size_t size) { if (size <= pool->max) { return ngx_palloc_small (size, 0 ); } return ngx_palloc_large (size); } void * ngx_mem_pool::ngx_pcalloc (size_t size) { void * p; p = ngx_palloc (size); if (p) { ngx_memzero (p, size); } return p; } void * ngx_mem_pool::ngx_palloc_small (size_t size, ngx_uint_t align) { u_char* m; ngx_pool_s* p; p = pool->current; do { m = p->d.last; if (align) { m = ngx_align_ptr (m, NGX_ALIGNMENT); } if ((size_t )(p->d.end - m) >= size) { p->d.last = m + size; return m; } p = p->d.next; } while (p); return ngx_palloc_block (size); } void * ngx_mem_pool::ngx_palloc_block (size_t size) { u_char* m; size_t psize; ngx_pool_s* p, * newpool; psize = (size_t )(pool->d.end - (u_char*)pool); m =(u_char*)malloc (psize); if (m == nullptr ) { return nullptr ; } newpool = (ngx_pool_s*)m; newpool->d.end = m + psize; newpool->d.next = nullptr ; newpool->d.failed = 0 ; m += sizeof (ngx_pool_data_t ); m = ngx_align_ptr (m, NGX_ALIGNMENT); newpool->d.last = m + size; for (p = pool->current; p->d.next; p = p->d.next) { if (p->d.failed++ > 4 ) { pool->current = p->d.next; } } p->d.next = newpool; return m; } void * ngx_mem_pool::ngx_palloc_large (size_t size) { void * p; ngx_uint_t n; ngx_pool_large_s* large; p = malloc (size); if (p == nullptr ) { return nullptr ; } n = 0 ; for (large = pool->large; large; large = large->next) { if (large->alloc == nullptr ) { large->alloc = p; return p; } if (n++ > 3 ) { break ; } } large = (ngx_pool_large_s*)ngx_palloc_small (sizeof (ngx_pool_large_s), 1 ); if (large == nullptr ) { free (p); return nullptr ; } large->alloc = p; large->next = pool->large; pool->large = large; return p; } void ngx_mem_pool::ngx_pfree (void * p) { ngx_pool_large_s* l; for (l = pool->large; l; l = l->next) { if (p == l->alloc) { free (l->alloc); l->alloc = NULL ; return ; } } } void ngx_mem_pool::ngx_reset_pool () { ngx_pool_s* p; ngx_pool_large_s* l; for (l = pool->large; l; l = l->next) { if (l->alloc) { free (l->alloc); } } p = pool; p->d.last = (u_char*)p + sizeof (ngx_pool_s); p->d.failed = 0 ; for (p = pool; p; p = p->d.next) { p->d.last = (u_char*)p + sizeof (ngx_pool_data_t ); p->d.failed = 0 ; } pool->current = pool; pool->large = nullptr ; } void ngx_mem_pool::ngx_destroy_pool () { ngx_pool_s* p, * n; ngx_pool_large_s* l; ngx_pool_cleanup_s* c; for (c = pool->cleanup; c; c = c->next) { if (c->handler) { c->handler (c->data); } } for (l = pool->large; l; l = l->next) { if (l->alloc) { free (l->alloc); } } for (p = pool, n = pool->d.next; ; p = n, n = n->d.next) { free (p); if (n == nullptr ) { break ; } } } ngx_pool_cleanup_s* ngx_mem_pool::ngx_pool_cleanup_add (size_t size) { ngx_pool_cleanup_s* c; c = (ngx_pool_cleanup_s*)ngx_palloc (sizeof (ngx_pool_cleanup_s)); if (c == nullptr ) { return nullptr ; } if (size) { c->data = ngx_palloc (size); if (c->data == nullptr ) { return nullptr ; } } else { c->data = nullptr ; } c->handler = nullptr ; c->next = pool->cleanup; pool->cleanup = c; return c; }
3.textNginxPool.cpp 测试文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #include "pch.h" #include "ngx_mem_pool.h" #include <stdio.h> #include <string.h> typedef struct Data stData;struct Data { char * ptr; FILE* pfile; }; void func1 (void * p1) { char * p = (char *)p1; printf ("free ptr mem!" ); free (p); } void func2 (FILE* pf1) { FILE *pf=(FILE*) pf1; printf ("close file!" ); fclose (pf); } int main () { ngx_mem_pool mempool; if (mempool.ngx_create_pool (512 ) == nullptr ) { printf ("ngx_create_pool fail..." ); return -1 ; } void * p1 = mempool.ngx_palloc (128 ); if (p1 == nullptr ) { printf ("ngx_palloc 128 bytes fail..." ); return -1 ; } stData* p2 =(stData*) mempool.ngx_palloc (512 ); if (p2 == nullptr ) { printf ("ngx_palloc 512 bytes fail..." ); return -1 ; } p2->ptr = (char *)malloc (12 ); strcpy (p2->ptr, "hello world" ); p2->pfile = fopen ("data.txt" , "w" ); ngx_pool_cleanup_s* c1 = mempool.ngx_pool_cleanup_add (sizeof (char *)); c1->handler = func1; c1->data = p2->ptr; ngx_pool_cleanup_s* c2 = mempool.ngx_pool_cleanup_add (sizeof (FILE*)); c2->handler = (ngx_pool_cleanup_pt)func2; c2->data = p2->pfile; mempool.ngx_destroy_pool (); return 0 ; }
4.改进 1.可以将create_pool放在构造函数中,将destroy_pool放在析构函数中,进一步进行封装
2.回调函数部分可以用bind绑定器或者function函数对象来实现
笔者只实现了第一点,实现第二点时,老是报错(读取访问权限冲突。 std::_Func_class<void,void * __ptr64>::_Getimpl(…) 返回 0xFFFFFFFFFFFFFFFF。完了写好之后再更新此文章)
pool.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 #pragma once #include <stdlib.h> #include <memory.h> #include <iostream> using namespace std;using u_char = unsigned char ;using ngx_uint_t = unsigned int ;struct ngx_pool_s ;typedef void (*ngx_pool_cleanup_pt) (void * data) ; struct ngx_pool_cleanup_s { ngx_pool_cleanup_pt handler; void * data; ngx_pool_cleanup_s* next; }; struct ngx_pool_large_s { ngx_pool_large_s* next; void * alloc; }; struct ngx_pool_data_t { u_char* last; u_char* end; ngx_pool_s* next; ngx_uint_t failed; } ; struct ngx_pool_s { ngx_pool_data_t d; size_t max; ngx_pool_s* current; ngx_pool_large_s* large; ngx_pool_cleanup_s* cleanup; }; #define ngx_align(d, a) (((d) + (a - 1)) & ~(a - 1)) #define NGX_ALIGNMENT sizeof(unsigned long) #define ngx_align_ptr(p, a) \ (u_char *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1)) #define ngx_memzero(buf, n) (void) memset(buf, 0, n) const int ngx_pagesize = 4096 ;const int NGX_MAX_ALLOC_FROM_POOL = ngx_pagesize - 1 ;const int NGX_DEFAULT_POOL_SIZE = 16 * 1024 ;const int NGX_POOL_ALIGNMENT = 16 ;const int NGX_MIN_POOL_SIZE = ngx_align ((sizeof (ngx_pool_s) + 2 * sizeof (ngx_pool_large_s)), NGX_POOL_ALIGNMENT); class ngx_mem_pool { public : ngx_mem_pool (size_t size) { if (this ->ngx_create_pool (size) == nullptr ) { cout << "ngx_create_pool fail..." << endl; exit (0 ); } } void * ngx_palloc (size_t size) ; void * ngx_pnalloc (size_t size) ; void * ngx_pcalloc (size_t size) ; void ngx_pfree (void * p) ; void ngx_reset_pool () ; ngx_pool_cleanup_s* ngx_pool_cleanup_add (size_t size) ; ~ngx_mem_pool () { this ->ngx_destroy_pool (); } private : ngx_pool_s* pool; void * ngx_palloc_small (size_t size, ngx_uint_t align) ; void * ngx_palloc_block (size_t size) ; void * ngx_palloc_large (size_t size) ; void * ngx_create_pool (size_t size) ; void ngx_destroy_pool () ; };
pool.cpp 这个没改动
text.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #include "pch.h" #include "ngx_mem_pool.h" #include <stdio.h> #include <string.h> typedef struct Data stData;struct Data { char * ptr; FILE* pfile; }; void func1 (void * p1) { char * p = (char *)p1; printf ("free ptr mem!" ); free (p); } void func2 (FILE* pf1) { FILE* pf = (FILE*)pf1; printf ("close file!" ); fclose (pf); } int main () { ngx_mem_pool mempool (512 ) ; void * p1 = mempool.ngx_palloc (128 ); if (p1 == nullptr ) { printf ("ngx_palloc 128 bytes fail..." ); return -1 ; } stData* p2 = (stData*)mempool.ngx_palloc (512 ); if (p2 == nullptr ) { printf ("ngx_palloc 512 bytes fail..." ); return -1 ; } p2->ptr = (char *)malloc (12 ); strcpy (p2->ptr, "hello world" ); p2->pfile = fopen ("data.txt" , "w" ); ngx_pool_cleanup_s* c1 = mempool.ngx_pool_cleanup_add (sizeof (char *)); c1->handler = func1; c1->data = p2->ptr; ngx_pool_cleanup_s* c2 = mempool.ngx_pool_cleanup_add (sizeof (FILE*)); c2->handler = (ngx_pool_cleanup_pt)func2; c2->data = p2->pfile; return 0 ; }
bind和function的改进还没完成(笔者标注一下不要在意)