├── gen-patch ├── .gitignore ├── README.md ├── nginx-1.0.0-no_pool.patch ├── nginx-1.0.2-no_pool.patch ├── nginx-1.0.4-no_pool.patch ├── nginx-1.0.5-no_pool.patch ├── nginx-1.1.0-no_pool.patch ├── nginx-1.1.2-no_pool.patch ├── nginx-0.8.41-no_pool.patch ├── nginx-0.8.53-no_pool.patch ├── nginx-0.8.54-no_pool.patch ├── nginx-0.8.55-no_pool.patch ├── nginx-1.0.6-no_pool.patch ├── nginx-1.0.8-no_pool.patch ├── nginx-1.0.9-no_pool.patch ├── nginx-1.1.4-no_pool.patch ├── nginx-1.1.5-no_pool.patch └── nginx-1.0.10-no_pool.patch /gen-patch: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | diff -urx '*~' -x '*.swp' $1/src $1-patched/src 4 | 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .libs 2 | *.swp 3 | *.slo 4 | *.la 5 | *.swo 6 | *.lo 7 | *.mobi 8 | genmobi.sh 9 | *~ 10 | *.o 11 | print.txt 12 | .rsync 13 | *.tar.gz 14 | dist 15 | build[789] 16 | build 17 | tags 18 | update-readme 19 | *.tmp 20 | test/Makefile 21 | test/blib 22 | test.sh 23 | t.sh 24 | t/t.sh 25 | test/t/servroot/ 26 | releng 27 | reset 28 | *.t_ 29 | reindex 30 | src/location.h 31 | src/filter.c 32 | src/subrequest.h 33 | src/sleep.h 34 | src/util.c 35 | src/echo.c 36 | src/info.c 37 | src/util.h 38 | src/var.h 39 | src/filter.h 40 | src/sleep.c 41 | src/var.c 42 | src/timer.c 43 | src/module.h 44 | src/echo.h 45 | src/info.h 46 | src/foreach.c 47 | src/location.c 48 | src/timer.h 49 | src/module.c 50 | src/subrequest.c 51 | src/handler.h 52 | src/foreach.h 53 | src/handler.c 54 | nginx 55 | *.html 56 | ctags 57 | t/servroot 58 | all.sh 59 | buildroot/ 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Name 2 | ==== 3 | 4 | no-pool-nginx - NGINX patch to disable NGINX's memory pool for debugging. 5 | 6 | Description 7 | =========== 8 | 9 | This is a set of a patch files to the nginx source. It replaces nginx's pool machanism 10 | with plain malloc & free to help tools like valgrind's memcheck to detect 11 | memory issues more reliably. 12 | 13 | Usage 14 | ===== 15 | 16 | ```bash 17 | cd nginx-VERSION/ 18 | patch -p1 < /path/to/nginx-VERSION-no_pool.patch 19 | ./configure 20 | make 21 | make install 22 | ``` 23 | 24 | Copyright & License 25 | =================== 26 | 27 | This program is licenced under the BSD licence. 28 | 29 | Copyright (c) 2010-2017, Yichun "agentzh" Zhang , OpenResty Inc. 30 | 31 | Copyright (c) 2010, Shrimp . 32 | 33 | All rights reserved. 34 | 35 | Redistribution and use in source and binary forms, with or without 36 | modification, are permitted provided that the following conditions 37 | are met: 38 | 39 | * Redistributions of source code must retain the above copyright 40 | notice, this list of conditions and the following disclaimer. 41 | 42 | * Redistributions in binary form must reproduce the above copyright 43 | notice, this list of conditions and the following disclaimer in the 44 | documentation and/or other materials provided with the distribution. 45 | 46 | * Neither the name of the Taobao Inc. nor the names of its 47 | contributors may be used to endorse or promote products derived from 48 | this software without specific prior written permission. 49 | 50 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 51 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 52 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 53 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 54 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 55 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 56 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 57 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 58 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 59 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 60 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 | 62 | -------------------------------------------------------------------------------- /nginx-1.0.0-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-1.0.0/src/core/nginx.h nginx-1.0.0-no-pool/src/core/nginx.h 2 | --- nginx-1.0.0/src/core/nginx.h 2011-04-07 17:08:52.000000000 +0800 3 | +++ nginx-1.0.0-no-pool/src/core/nginx.h 2011-06-30 17:40:27.050946999 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 1000000 8 | -#define NGINX_VERSION "1.0.0" 9 | +#define NGINX_VERSION "1.0.0 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-1.0.0/src/core/ngx_array.c nginx-1.0.0-no-pool/src/core/ngx_array.c 14 | --- nginx-1.0.0/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-1.0.0-no-pool/src/core/ngx_array.c 2011-06-30 17:40:27.050946999 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -112,31 +94,16 @@ 72 | 73 | p = a->pool; 74 | 75 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 76 | - && p->d.last + size <= p->d.end) 77 | - { 78 | - /* 79 | - * the array allocation is the last in the pool 80 | - * and there is space for new allocation 81 | - */ 82 | - 83 | - p->d.last += size; 84 | - a->nalloc += n; 85 | - 86 | - } else { 87 | - /* allocate a new array */ 88 | - 89 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 90 | - 91 | - new = ngx_palloc(p, nalloc * a->size); 92 | - if (new == NULL) { 93 | - return NULL; 94 | - } 95 | - 96 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 97 | - a->elts = new; 98 | - a->nalloc = nalloc; 99 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 100 | + 101 | + new = ngx_palloc(p, nalloc * a->size); 102 | + if (new == NULL) { 103 | + return NULL; 104 | } 105 | + 106 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 107 | + a->elts = new; 108 | + a->nalloc = nalloc; 109 | } 110 | 111 | elt = (u_char *) a->elts + a->size * a->nelts; 112 | diff -ur nginx-1.0.0/src/core/ngx_palloc.c nginx-1.0.0-no-pool/src/core/ngx_palloc.c 113 | --- nginx-1.0.0/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 114 | +++ nginx-1.0.0-no-pool/src/core/ngx_palloc.c 2011-06-30 17:40:54.610946999 +0800 115 | @@ -8,24 +8,31 @@ 116 | #include 117 | 118 | 119 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 120 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 121 | 122 | 123 | ngx_pool_t * 124 | ngx_create_pool(size_t size, ngx_log_t *log) 125 | { 126 | - ngx_pool_t *p; 127 | + ngx_pool_t *p; 128 | + ngx_pool_data_t *d; 129 | 130 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 131 | + size = sizeof(ngx_pool_t); 132 | + p = ngx_alloc(size, log); 133 | if (p == NULL) { 134 | return NULL; 135 | } 136 | 137 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 138 | - p->d.end = (u_char *) p + size; 139 | - p->d.next = NULL; 140 | - p->d.failed = 0; 141 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 142 | + 143 | + if (d == NULL) { 144 | + return NULL; 145 | + } 146 | + 147 | + d->next = d; 148 | + d->prev = d; 149 | + d->alloc = NULL; 150 | + p->d = d; 151 | 152 | size = size - sizeof(ngx_pool_t); 153 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 154 | @@ -43,7 +50,7 @@ 155 | void 156 | ngx_destroy_pool(ngx_pool_t *pool) 157 | { 158 | - ngx_pool_t *p, *n; 159 | + ngx_pool_data_t *d, *n; 160 | ngx_pool_large_t *l; 161 | ngx_pool_cleanup_t *c; 162 | 163 | @@ -55,7 +62,7 @@ 164 | } 165 | } 166 | 167 | - for (l = pool->large; l; l = l->next) { 168 | + for (l = pool->large; l ; l = l->next) { 169 | 170 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 171 | 172 | @@ -71,34 +78,45 @@ 173 | * so we can not use this log while the free()ing the pool 174 | */ 175 | 176 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 177 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 178 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 179 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 180 | + "free: %p", d); 181 | 182 | - if (n == NULL) { 183 | + if (n == pool->d) { 184 | break; 185 | } 186 | } 187 | 188 | #endif 189 | + if (pool->d->next == pool->d) { 190 | + ngx_free(pool->d); 191 | + } else { 192 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 193 | + if (d->alloc) { 194 | + ngx_free(d->alloc); 195 | + } 196 | + ngx_free(d); 197 | 198 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 199 | - ngx_free(p); 200 | - 201 | - if (n == NULL) { 202 | - break; 203 | + if (n == pool->d) { 204 | + break; 205 | + } 206 | } 207 | } 208 | + 209 | + ngx_free(pool); 210 | } 211 | 212 | 213 | void 214 | ngx_reset_pool(ngx_pool_t *pool) 215 | { 216 | - ngx_pool_t *p; 217 | - ngx_pool_large_t *l; 218 | + ngx_pool_data_t *p, *tmp; 219 | + ngx_pool_large_t *l; 220 | + 221 | + for (l = pool->large; l ; l = l->next) { 222 | + 223 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 224 | 225 | - for (l = pool->large; l; l = l->next) { 226 | if (l->alloc) { 227 | ngx_free(l->alloc); 228 | } 229 | @@ -106,109 +124,64 @@ 230 | 231 | pool->large = NULL; 232 | 233 | - for (p = pool; p; p = p->d.next) { 234 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 235 | + p = pool->d->next; 236 | + while (p != pool->d) { 237 | + tmp = p; 238 | + ngx_free(p->alloc); 239 | + p->prev->next = p->next; 240 | + p->next->prev = p->prev; 241 | + p = p->next; 242 | + ngx_free(tmp); 243 | } 244 | -} 245 | 246 | + ngx_free(pool->d->alloc); 247 | + pool->d->alloc = NULL; 248 | +} 249 | 250 | void * 251 | -ngx_palloc(ngx_pool_t *pool, size_t size) 252 | +ngx_malloc(ngx_pool_t *pool, size_t size) 253 | { 254 | - u_char *m; 255 | - ngx_pool_t *p; 256 | - 257 | - if (size <= pool->max) { 258 | - 259 | - p = pool->current; 260 | + ngx_pool_data_t *new; 261 | + void *m; 262 | 263 | - do { 264 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 265 | - 266 | - if ((size_t) (p->d.end - m) >= size) { 267 | - p->d.last = m + size; 268 | - 269 | - return m; 270 | - } 271 | - 272 | - p = p->d.next; 273 | - 274 | - } while (p); 275 | + m = ngx_alloc(size, pool->log); 276 | + if (m == NULL) { 277 | + return NULL; 278 | + } 279 | 280 | - return ngx_palloc_block(pool, size); 281 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 282 | + if (new == NULL){ 283 | + ngx_free(m); 284 | + return NULL; 285 | } 286 | 287 | - return ngx_palloc_large(pool, size); 288 | + new->alloc = m; 289 | + new->next = pool->d; 290 | + new->prev = pool->d->prev; 291 | + pool->d->prev->next = new; 292 | + pool->d->prev = new; 293 | + return m; 294 | } 295 | 296 | - 297 | void * 298 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 299 | +ngx_palloc(ngx_pool_t *pool, size_t size) 300 | { 301 | - u_char *m; 302 | - ngx_pool_t *p; 303 | - 304 | - if (size <= pool->max) { 305 | - 306 | - p = pool->current; 307 | - 308 | - do { 309 | - m = p->d.last; 310 | - 311 | - if ((size_t) (p->d.end - m) >= size) { 312 | - p->d.last = m + size; 313 | - 314 | - return m; 315 | - } 316 | - 317 | - p = p->d.next; 318 | - 319 | - } while (p); 320 | - 321 | - return ngx_palloc_block(pool, size); 322 | + if (size <= 1024) { 323 | + return ngx_malloc(pool, size); 324 | } 325 | 326 | return ngx_palloc_large(pool, size); 327 | } 328 | 329 | 330 | -static void * 331 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 332 | +void * 333 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 334 | { 335 | - u_char *m; 336 | - size_t psize; 337 | - ngx_pool_t *p, *new, *current; 338 | - 339 | - psize = (size_t) (pool->d.end - (u_char *) pool); 340 | - 341 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 342 | - if (m == NULL) { 343 | - return NULL; 344 | + if (size <= 1024) { 345 | + return ngx_malloc(pool, size); 346 | } 347 | 348 | - new = (ngx_pool_t *) m; 349 | - 350 | - new->d.end = m + psize; 351 | - new->d.next = NULL; 352 | - new->d.failed = 0; 353 | - 354 | - m += sizeof(ngx_pool_data_t); 355 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 356 | - new->d.last = m + size; 357 | - 358 | - current = pool->current; 359 | - 360 | - for (p = current; p->d.next; p = p->d.next) { 361 | - if (p->d.failed++ > 4) { 362 | - current = p->d.next; 363 | - } 364 | - } 365 | - 366 | - p->d.next = new; 367 | - 368 | - pool->current = current ? current : new; 369 | - 370 | - return m; 371 | + return ngx_palloc_large(pool, size); 372 | } 373 | 374 | 375 | @@ -226,18 +199,7 @@ 376 | 377 | n = 0; 378 | 379 | - for (large = pool->large; large; large = large->next) { 380 | - if (large->alloc == NULL) { 381 | - large->alloc = p; 382 | - return p; 383 | - } 384 | - 385 | - if (n++ > 3) { 386 | - break; 387 | - } 388 | - } 389 | - 390 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 391 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 392 | if (large == NULL) { 393 | ngx_free(p); 394 | return NULL; 395 | @@ -262,7 +224,7 @@ 396 | return NULL; 397 | } 398 | 399 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 400 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 401 | if (large == NULL) { 402 | ngx_free(p); 403 | return NULL; 404 | @@ -279,17 +241,41 @@ 405 | ngx_int_t 406 | ngx_pfree(ngx_pool_t *pool, void *p) 407 | { 408 | - ngx_pool_large_t *l; 409 | + ngx_pool_large_t *l; 410 | + ngx_pool_large_t *ll; 411 | + ngx_pool_data_t *d, *n; 412 | 413 | - for (l = pool->large; l; l = l->next) { 414 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 415 | if (p == l->alloc) { 416 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 417 | "free: %p", l->alloc); 418 | ngx_free(l->alloc); 419 | l->alloc = NULL; 420 | + if (l == pool->large) { 421 | + pool->large = NULL; 422 | + } 423 | + 424 | + ll->next = l->next; 425 | + p = l; 426 | + break; 427 | + } 428 | + } 429 | 430 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 431 | + if (p == d->alloc) { 432 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 433 | + if (d->alloc) { 434 | + ngx_free(d->alloc); 435 | + } 436 | + d->alloc = NULL; 437 | + d->prev->next = d->next; 438 | + d->next->prev = d->prev; 439 | + ngx_free(d); 440 | return NGX_OK; 441 | } 442 | + if (d->next == pool->d) { 443 | + break; 444 | + } 445 | } 446 | 447 | return NGX_DECLINED; 448 | Only in nginx-1.0.0-no-pool/src/core: ngx_palloc.c~ 449 | diff -ur nginx-1.0.0/src/core/ngx_palloc.h nginx-1.0.0-no-pool/src/core/ngx_palloc.h 450 | --- nginx-1.0.0/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 451 | +++ nginx-1.0.0-no-pool/src/core/ngx_palloc.h 2011-06-30 17:40:27.050946999 +0800 452 | @@ -38,6 +38,7 @@ 453 | 454 | 455 | typedef struct ngx_pool_large_s ngx_pool_large_t; 456 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 457 | 458 | struct ngx_pool_large_s { 459 | ngx_pool_large_t *next; 460 | @@ -45,16 +46,15 @@ 461 | }; 462 | 463 | 464 | -typedef struct { 465 | - u_char *last; 466 | - u_char *end; 467 | - ngx_pool_t *next; 468 | - ngx_uint_t failed; 469 | -} ngx_pool_data_t; 470 | +struct ngx_pool_data_s{ 471 | + ngx_pool_data_t *next; 472 | + ngx_pool_data_t *prev; 473 | + void *alloc; 474 | +}; 475 | 476 | 477 | struct ngx_pool_s { 478 | - ngx_pool_data_t d; 479 | + ngx_pool_data_t *d; 480 | size_t max; 481 | ngx_pool_t *current; 482 | ngx_chain_t *chain; 483 | -------------------------------------------------------------------------------- /nginx-1.0.2-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-1.0.2/src/core/nginx.h nginx-1.0.2-no-pool/src/core/nginx.h 2 | --- nginx-1.0.2/src/core/nginx.h 2011-05-04 20:20:32.000000000 +0800 3 | +++ nginx-1.0.2-no-pool/src/core/nginx.h 2011-06-30 17:42:03.710946999 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 1000002 8 | -#define NGINX_VERSION "1.0.2" 9 | +#define NGINX_VERSION "1.0.2 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-1.0.2/src/core/ngx_array.c nginx-1.0.2-no-pool/src/core/ngx_array.c 14 | --- nginx-1.0.2/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-1.0.2-no-pool/src/core/ngx_array.c 2011-06-30 17:42:03.710946999 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -112,31 +94,16 @@ 72 | 73 | p = a->pool; 74 | 75 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 76 | - && p->d.last + size <= p->d.end) 77 | - { 78 | - /* 79 | - * the array allocation is the last in the pool 80 | - * and there is space for new allocation 81 | - */ 82 | - 83 | - p->d.last += size; 84 | - a->nalloc += n; 85 | - 86 | - } else { 87 | - /* allocate a new array */ 88 | - 89 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 90 | - 91 | - new = ngx_palloc(p, nalloc * a->size); 92 | - if (new == NULL) { 93 | - return NULL; 94 | - } 95 | - 96 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 97 | - a->elts = new; 98 | - a->nalloc = nalloc; 99 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 100 | + 101 | + new = ngx_palloc(p, nalloc * a->size); 102 | + if (new == NULL) { 103 | + return NULL; 104 | } 105 | + 106 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 107 | + a->elts = new; 108 | + a->nalloc = nalloc; 109 | } 110 | 111 | elt = (u_char *) a->elts + a->size * a->nelts; 112 | diff -ur nginx-1.0.2/src/core/ngx_palloc.c nginx-1.0.2-no-pool/src/core/ngx_palloc.c 113 | --- nginx-1.0.2/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 114 | +++ nginx-1.0.2-no-pool/src/core/ngx_palloc.c 2011-06-30 17:42:33.250946999 +0800 115 | @@ -8,24 +8,31 @@ 116 | #include 117 | 118 | 119 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 120 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 121 | 122 | 123 | ngx_pool_t * 124 | ngx_create_pool(size_t size, ngx_log_t *log) 125 | { 126 | - ngx_pool_t *p; 127 | + ngx_pool_t *p; 128 | + ngx_pool_data_t *d; 129 | 130 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 131 | + size = sizeof(ngx_pool_t); 132 | + p = ngx_alloc(size, log); 133 | if (p == NULL) { 134 | return NULL; 135 | } 136 | 137 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 138 | - p->d.end = (u_char *) p + size; 139 | - p->d.next = NULL; 140 | - p->d.failed = 0; 141 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 142 | + 143 | + if (d == NULL) { 144 | + return NULL; 145 | + } 146 | + 147 | + d->next = d; 148 | + d->prev = d; 149 | + d->alloc = NULL; 150 | + p->d = d; 151 | 152 | size = size - sizeof(ngx_pool_t); 153 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 154 | @@ -43,7 +50,7 @@ 155 | void 156 | ngx_destroy_pool(ngx_pool_t *pool) 157 | { 158 | - ngx_pool_t *p, *n; 159 | + ngx_pool_data_t *d, *n; 160 | ngx_pool_large_t *l; 161 | ngx_pool_cleanup_t *c; 162 | 163 | @@ -55,7 +62,7 @@ 164 | } 165 | } 166 | 167 | - for (l = pool->large; l; l = l->next) { 168 | + for (l = pool->large; l ; l = l->next) { 169 | 170 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 171 | 172 | @@ -71,34 +78,45 @@ 173 | * so we can not use this log while the free()ing the pool 174 | */ 175 | 176 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 177 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 178 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 179 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 180 | + "free: %p", d); 181 | 182 | - if (n == NULL) { 183 | + if (n == pool->d) { 184 | break; 185 | } 186 | } 187 | 188 | #endif 189 | + if (pool->d->next == pool->d) { 190 | + ngx_free(pool->d); 191 | + } else { 192 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 193 | + if (d->alloc) { 194 | + ngx_free(d->alloc); 195 | + } 196 | + ngx_free(d); 197 | 198 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 199 | - ngx_free(p); 200 | - 201 | - if (n == NULL) { 202 | - break; 203 | + if (n == pool->d) { 204 | + break; 205 | + } 206 | } 207 | } 208 | + 209 | + ngx_free(pool); 210 | } 211 | 212 | 213 | void 214 | ngx_reset_pool(ngx_pool_t *pool) 215 | { 216 | - ngx_pool_t *p; 217 | - ngx_pool_large_t *l; 218 | + ngx_pool_data_t *p, *tmp; 219 | + ngx_pool_large_t *l; 220 | + 221 | + for (l = pool->large; l ; l = l->next) { 222 | + 223 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 224 | 225 | - for (l = pool->large; l; l = l->next) { 226 | if (l->alloc) { 227 | ngx_free(l->alloc); 228 | } 229 | @@ -106,109 +124,64 @@ 230 | 231 | pool->large = NULL; 232 | 233 | - for (p = pool; p; p = p->d.next) { 234 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 235 | + p = pool->d->next; 236 | + while (p != pool->d) { 237 | + tmp = p; 238 | + ngx_free(p->alloc); 239 | + p->prev->next = p->next; 240 | + p->next->prev = p->prev; 241 | + p = p->next; 242 | + ngx_free(tmp); 243 | } 244 | -} 245 | 246 | + ngx_free(pool->d->alloc); 247 | + pool->d->alloc = NULL; 248 | +} 249 | 250 | void * 251 | -ngx_palloc(ngx_pool_t *pool, size_t size) 252 | +ngx_malloc(ngx_pool_t *pool, size_t size) 253 | { 254 | - u_char *m; 255 | - ngx_pool_t *p; 256 | - 257 | - if (size <= pool->max) { 258 | - 259 | - p = pool->current; 260 | + ngx_pool_data_t *new; 261 | + void *m; 262 | 263 | - do { 264 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 265 | - 266 | - if ((size_t) (p->d.end - m) >= size) { 267 | - p->d.last = m + size; 268 | - 269 | - return m; 270 | - } 271 | - 272 | - p = p->d.next; 273 | - 274 | - } while (p); 275 | + m = ngx_alloc(size, pool->log); 276 | + if (m == NULL) { 277 | + return NULL; 278 | + } 279 | 280 | - return ngx_palloc_block(pool, size); 281 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 282 | + if (new == NULL){ 283 | + ngx_free(m); 284 | + return NULL; 285 | } 286 | 287 | - return ngx_palloc_large(pool, size); 288 | + new->alloc = m; 289 | + new->next = pool->d; 290 | + new->prev = pool->d->prev; 291 | + pool->d->prev->next = new; 292 | + pool->d->prev = new; 293 | + return m; 294 | } 295 | 296 | - 297 | void * 298 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 299 | +ngx_palloc(ngx_pool_t *pool, size_t size) 300 | { 301 | - u_char *m; 302 | - ngx_pool_t *p; 303 | - 304 | - if (size <= pool->max) { 305 | - 306 | - p = pool->current; 307 | - 308 | - do { 309 | - m = p->d.last; 310 | - 311 | - if ((size_t) (p->d.end - m) >= size) { 312 | - p->d.last = m + size; 313 | - 314 | - return m; 315 | - } 316 | - 317 | - p = p->d.next; 318 | - 319 | - } while (p); 320 | - 321 | - return ngx_palloc_block(pool, size); 322 | + if (size <= 1024) { 323 | + return ngx_malloc(pool, size); 324 | } 325 | 326 | return ngx_palloc_large(pool, size); 327 | } 328 | 329 | 330 | -static void * 331 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 332 | +void * 333 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 334 | { 335 | - u_char *m; 336 | - size_t psize; 337 | - ngx_pool_t *p, *new, *current; 338 | - 339 | - psize = (size_t) (pool->d.end - (u_char *) pool); 340 | - 341 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 342 | - if (m == NULL) { 343 | - return NULL; 344 | + if (size <= 1024) { 345 | + return ngx_malloc(pool, size); 346 | } 347 | 348 | - new = (ngx_pool_t *) m; 349 | - 350 | - new->d.end = m + psize; 351 | - new->d.next = NULL; 352 | - new->d.failed = 0; 353 | - 354 | - m += sizeof(ngx_pool_data_t); 355 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 356 | - new->d.last = m + size; 357 | - 358 | - current = pool->current; 359 | - 360 | - for (p = current; p->d.next; p = p->d.next) { 361 | - if (p->d.failed++ > 4) { 362 | - current = p->d.next; 363 | - } 364 | - } 365 | - 366 | - p->d.next = new; 367 | - 368 | - pool->current = current ? current : new; 369 | - 370 | - return m; 371 | + return ngx_palloc_large(pool, size); 372 | } 373 | 374 | 375 | @@ -226,18 +199,7 @@ 376 | 377 | n = 0; 378 | 379 | - for (large = pool->large; large; large = large->next) { 380 | - if (large->alloc == NULL) { 381 | - large->alloc = p; 382 | - return p; 383 | - } 384 | - 385 | - if (n++ > 3) { 386 | - break; 387 | - } 388 | - } 389 | - 390 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 391 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 392 | if (large == NULL) { 393 | ngx_free(p); 394 | return NULL; 395 | @@ -262,7 +224,7 @@ 396 | return NULL; 397 | } 398 | 399 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 400 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 401 | if (large == NULL) { 402 | ngx_free(p); 403 | return NULL; 404 | @@ -279,17 +241,41 @@ 405 | ngx_int_t 406 | ngx_pfree(ngx_pool_t *pool, void *p) 407 | { 408 | - ngx_pool_large_t *l; 409 | + ngx_pool_large_t *l; 410 | + ngx_pool_large_t *ll; 411 | + ngx_pool_data_t *d, *n; 412 | 413 | - for (l = pool->large; l; l = l->next) { 414 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 415 | if (p == l->alloc) { 416 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 417 | "free: %p", l->alloc); 418 | ngx_free(l->alloc); 419 | l->alloc = NULL; 420 | + if (l == pool->large) { 421 | + pool->large = NULL; 422 | + } 423 | + 424 | + ll->next = l->next; 425 | + p = l; 426 | + break; 427 | + } 428 | + } 429 | 430 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 431 | + if (p == d->alloc) { 432 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 433 | + if (d->alloc) { 434 | + ngx_free(d->alloc); 435 | + } 436 | + d->alloc = NULL; 437 | + d->prev->next = d->next; 438 | + d->next->prev = d->prev; 439 | + ngx_free(d); 440 | return NGX_OK; 441 | } 442 | + if (d->next == pool->d) { 443 | + break; 444 | + } 445 | } 446 | 447 | return NGX_DECLINED; 448 | Only in nginx-1.0.2-no-pool/src/core: ngx_palloc.c~ 449 | diff -ur nginx-1.0.2/src/core/ngx_palloc.h nginx-1.0.2-no-pool/src/core/ngx_palloc.h 450 | --- nginx-1.0.2/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 451 | +++ nginx-1.0.2-no-pool/src/core/ngx_palloc.h 2011-06-30 17:42:03.710946999 +0800 452 | @@ -38,6 +38,7 @@ 453 | 454 | 455 | typedef struct ngx_pool_large_s ngx_pool_large_t; 456 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 457 | 458 | struct ngx_pool_large_s { 459 | ngx_pool_large_t *next; 460 | @@ -45,16 +46,15 @@ 461 | }; 462 | 463 | 464 | -typedef struct { 465 | - u_char *last; 466 | - u_char *end; 467 | - ngx_pool_t *next; 468 | - ngx_uint_t failed; 469 | -} ngx_pool_data_t; 470 | +struct ngx_pool_data_s{ 471 | + ngx_pool_data_t *next; 472 | + ngx_pool_data_t *prev; 473 | + void *alloc; 474 | +}; 475 | 476 | 477 | struct ngx_pool_s { 478 | - ngx_pool_data_t d; 479 | + ngx_pool_data_t *d; 480 | size_t max; 481 | ngx_pool_t *current; 482 | ngx_chain_t *chain; 483 | -------------------------------------------------------------------------------- /nginx-1.0.4-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-1.0.4/src/core/nginx.h nginx-1.0.4-no-pool/src/core/nginx.h 2 | --- nginx-1.0.4/src/core/nginx.h 2011-05-26 15:31:40.000000000 +0800 3 | +++ nginx-1.0.4-no-pool/src/core/nginx.h 2011-06-30 17:00:43.540946999 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 1000004 8 | -#define NGINX_VERSION "1.0.4" 9 | +#define NGINX_VERSION "1.0.4 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-1.0.4/src/core/ngx_array.c nginx-1.0.4-no-pool/src/core/ngx_array.c 14 | --- nginx-1.0.4/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-1.0.4-no-pool/src/core/ngx_array.c 2011-06-30 17:00:43.540946999 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -112,31 +94,16 @@ 72 | 73 | p = a->pool; 74 | 75 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 76 | - && p->d.last + size <= p->d.end) 77 | - { 78 | - /* 79 | - * the array allocation is the last in the pool 80 | - * and there is space for new allocation 81 | - */ 82 | - 83 | - p->d.last += size; 84 | - a->nalloc += n; 85 | - 86 | - } else { 87 | - /* allocate a new array */ 88 | - 89 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 90 | - 91 | - new = ngx_palloc(p, nalloc * a->size); 92 | - if (new == NULL) { 93 | - return NULL; 94 | - } 95 | - 96 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 97 | - a->elts = new; 98 | - a->nalloc = nalloc; 99 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 100 | + 101 | + new = ngx_palloc(p, nalloc * a->size); 102 | + if (new == NULL) { 103 | + return NULL; 104 | } 105 | + 106 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 107 | + a->elts = new; 108 | + a->nalloc = nalloc; 109 | } 110 | 111 | elt = (u_char *) a->elts + a->size * a->nelts; 112 | diff -ur nginx-1.0.4/src/core/ngx_palloc.c nginx-1.0.4-no-pool/src/core/ngx_palloc.c 113 | --- nginx-1.0.4/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 114 | +++ nginx-1.0.4-no-pool/src/core/ngx_palloc.c 2011-06-30 17:06:36.060946999 +0800 115 | @@ -8,24 +8,31 @@ 116 | #include 117 | 118 | 119 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 120 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 121 | 122 | 123 | ngx_pool_t * 124 | ngx_create_pool(size_t size, ngx_log_t *log) 125 | { 126 | - ngx_pool_t *p; 127 | + ngx_pool_t *p; 128 | + ngx_pool_data_t *d; 129 | 130 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 131 | + size = sizeof(ngx_pool_t); 132 | + p = ngx_alloc(size, log); 133 | if (p == NULL) { 134 | return NULL; 135 | } 136 | 137 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 138 | - p->d.end = (u_char *) p + size; 139 | - p->d.next = NULL; 140 | - p->d.failed = 0; 141 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 142 | + 143 | + if (d == NULL) { 144 | + return NULL; 145 | + } 146 | + 147 | + d->next = d; 148 | + d->prev = d; 149 | + d->alloc = NULL; 150 | + p->d = d; 151 | 152 | size = size - sizeof(ngx_pool_t); 153 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 154 | @@ -43,7 +50,7 @@ 155 | void 156 | ngx_destroy_pool(ngx_pool_t *pool) 157 | { 158 | - ngx_pool_t *p, *n; 159 | + ngx_pool_data_t *d, *n; 160 | ngx_pool_large_t *l; 161 | ngx_pool_cleanup_t *c; 162 | 163 | @@ -55,7 +62,7 @@ 164 | } 165 | } 166 | 167 | - for (l = pool->large; l; l = l->next) { 168 | + for (l = pool->large; l ; l = l->next) { 169 | 170 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 171 | 172 | @@ -71,34 +78,45 @@ 173 | * so we can not use this log while the free()ing the pool 174 | */ 175 | 176 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 177 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 178 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 179 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 180 | + "free: %p", d); 181 | 182 | - if (n == NULL) { 183 | + if (n == pool->d) { 184 | break; 185 | } 186 | } 187 | 188 | #endif 189 | + if (pool->d->next == pool->d) { 190 | + ngx_free(pool->d); 191 | + } else { 192 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 193 | + if (d->alloc) { 194 | + ngx_free(d->alloc); 195 | + } 196 | + ngx_free(d); 197 | 198 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 199 | - ngx_free(p); 200 | - 201 | - if (n == NULL) { 202 | - break; 203 | + if (n == pool->d) { 204 | + break; 205 | + } 206 | } 207 | } 208 | + 209 | + ngx_free(pool); 210 | } 211 | 212 | 213 | void 214 | ngx_reset_pool(ngx_pool_t *pool) 215 | { 216 | - ngx_pool_t *p; 217 | - ngx_pool_large_t *l; 218 | + ngx_pool_data_t *p, *tmp; 219 | + ngx_pool_large_t *l; 220 | + 221 | + for (l = pool->large; l ; l = l->next) { 222 | + 223 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 224 | 225 | - for (l = pool->large; l; l = l->next) { 226 | if (l->alloc) { 227 | ngx_free(l->alloc); 228 | } 229 | @@ -106,109 +124,65 @@ 230 | 231 | pool->large = NULL; 232 | 233 | - for (p = pool; p; p = p->d.next) { 234 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 235 | + p = pool->d->next; 236 | + while (p != pool->d) { 237 | + tmp = p; 238 | + ngx_free(p->alloc); 239 | + p->prev->next = p->next; 240 | + p->next->prev = p->prev; 241 | + p = p->next; 242 | + ngx_free(tmp); 243 | } 244 | -} 245 | 246 | + ngx_free(pool->d->alloc); 247 | + pool->d->alloc = NULL; 248 | + 249 | +} 250 | 251 | void * 252 | -ngx_palloc(ngx_pool_t *pool, size_t size) 253 | +ngx_malloc(ngx_pool_t *pool, size_t size) 254 | { 255 | - u_char *m; 256 | - ngx_pool_t *p; 257 | - 258 | - if (size <= pool->max) { 259 | + ngx_pool_data_t *new; 260 | + void *m; 261 | 262 | - p = pool->current; 263 | - 264 | - do { 265 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 266 | - 267 | - if ((size_t) (p->d.end - m) >= size) { 268 | - p->d.last = m + size; 269 | - 270 | - return m; 271 | - } 272 | - 273 | - p = p->d.next; 274 | - 275 | - } while (p); 276 | + m = ngx_alloc(size, pool->log); 277 | + if (m == NULL) { 278 | + return NULL; 279 | + } 280 | 281 | - return ngx_palloc_block(pool, size); 282 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 283 | + if (new == NULL){ 284 | + ngx_free(m); 285 | + return NULL; 286 | } 287 | 288 | - return ngx_palloc_large(pool, size); 289 | + new->alloc = m; 290 | + new->next = pool->d; 291 | + new->prev = pool->d->prev; 292 | + pool->d->prev->next = new; 293 | + pool->d->prev = new; 294 | + return m; 295 | } 296 | 297 | - 298 | void * 299 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 300 | +ngx_palloc(ngx_pool_t *pool, size_t size) 301 | { 302 | - u_char *m; 303 | - ngx_pool_t *p; 304 | - 305 | - if (size <= pool->max) { 306 | - 307 | - p = pool->current; 308 | - 309 | - do { 310 | - m = p->d.last; 311 | - 312 | - if ((size_t) (p->d.end - m) >= size) { 313 | - p->d.last = m + size; 314 | - 315 | - return m; 316 | - } 317 | - 318 | - p = p->d.next; 319 | - 320 | - } while (p); 321 | - 322 | - return ngx_palloc_block(pool, size); 323 | + if (size <= 1024) { 324 | + return ngx_malloc(pool, size); 325 | } 326 | 327 | return ngx_palloc_large(pool, size); 328 | } 329 | 330 | 331 | -static void * 332 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 333 | +void * 334 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 335 | { 336 | - u_char *m; 337 | - size_t psize; 338 | - ngx_pool_t *p, *new, *current; 339 | - 340 | - psize = (size_t) (pool->d.end - (u_char *) pool); 341 | - 342 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 343 | - if (m == NULL) { 344 | - return NULL; 345 | + if (size <= 1024) { 346 | + return ngx_malloc(pool, size); 347 | } 348 | 349 | - new = (ngx_pool_t *) m; 350 | - 351 | - new->d.end = m + psize; 352 | - new->d.next = NULL; 353 | - new->d.failed = 0; 354 | - 355 | - m += sizeof(ngx_pool_data_t); 356 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 357 | - new->d.last = m + size; 358 | - 359 | - current = pool->current; 360 | - 361 | - for (p = current; p->d.next; p = p->d.next) { 362 | - if (p->d.failed++ > 4) { 363 | - current = p->d.next; 364 | - } 365 | - } 366 | - 367 | - p->d.next = new; 368 | - 369 | - pool->current = current ? current : new; 370 | - 371 | - return m; 372 | + return ngx_palloc_large(pool, size); 373 | } 374 | 375 | 376 | @@ -226,18 +200,7 @@ 377 | 378 | n = 0; 379 | 380 | - for (large = pool->large; large; large = large->next) { 381 | - if (large->alloc == NULL) { 382 | - large->alloc = p; 383 | - return p; 384 | - } 385 | - 386 | - if (n++ > 3) { 387 | - break; 388 | - } 389 | - } 390 | - 391 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 392 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 393 | if (large == NULL) { 394 | ngx_free(p); 395 | return NULL; 396 | @@ -262,7 +225,7 @@ 397 | return NULL; 398 | } 399 | 400 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 401 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 402 | if (large == NULL) { 403 | ngx_free(p); 404 | return NULL; 405 | @@ -279,17 +242,41 @@ 406 | ngx_int_t 407 | ngx_pfree(ngx_pool_t *pool, void *p) 408 | { 409 | - ngx_pool_large_t *l; 410 | + ngx_pool_large_t *l; 411 | + ngx_pool_large_t *ll; 412 | + ngx_pool_data_t *d, *n; 413 | 414 | - for (l = pool->large; l; l = l->next) { 415 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 416 | if (p == l->alloc) { 417 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 418 | "free: %p", l->alloc); 419 | ngx_free(l->alloc); 420 | l->alloc = NULL; 421 | + if (l == pool->large) { 422 | + pool->large = NULL; 423 | + } 424 | + 425 | + ll->next = l->next; 426 | + p = l; 427 | + break; 428 | + } 429 | + } 430 | 431 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 432 | + if (p == d->alloc) { 433 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 434 | + if (d->alloc) { 435 | + ngx_free(d->alloc); 436 | + } 437 | + d->alloc = NULL; 438 | + d->prev->next = d->next; 439 | + d->next->prev = d->prev; 440 | + ngx_free(d); 441 | return NGX_OK; 442 | } 443 | + if (d->next == pool->d) { 444 | + break; 445 | + } 446 | } 447 | 448 | return NGX_DECLINED; 449 | Only in nginx-1.0.4-no-pool/src/core: ngx_palloc.c~ 450 | diff -ur nginx-1.0.4/src/core/ngx_palloc.h nginx-1.0.4-no-pool/src/core/ngx_palloc.h 451 | --- nginx-1.0.4/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 452 | +++ nginx-1.0.4-no-pool/src/core/ngx_palloc.h 2011-06-30 17:00:43.540946999 +0800 453 | @@ -38,6 +38,7 @@ 454 | 455 | 456 | typedef struct ngx_pool_large_s ngx_pool_large_t; 457 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 458 | 459 | struct ngx_pool_large_s { 460 | ngx_pool_large_t *next; 461 | @@ -45,16 +46,15 @@ 462 | }; 463 | 464 | 465 | -typedef struct { 466 | - u_char *last; 467 | - u_char *end; 468 | - ngx_pool_t *next; 469 | - ngx_uint_t failed; 470 | -} ngx_pool_data_t; 471 | +struct ngx_pool_data_s{ 472 | + ngx_pool_data_t *next; 473 | + ngx_pool_data_t *prev; 474 | + void *alloc; 475 | +}; 476 | 477 | 478 | struct ngx_pool_s { 479 | - ngx_pool_data_t d; 480 | + ngx_pool_data_t *d; 481 | size_t max; 482 | ngx_pool_t *current; 483 | ngx_chain_t *chain; 484 | -------------------------------------------------------------------------------- /nginx-1.0.5-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-1.0.5/src/core/nginx.h nginx-1.0.5-no-pool/src/core/nginx.h 2 | --- nginx-1.0.5/src/core/nginx.h 2011-05-26 15:31:40.000000000 +0800 3 | +++ nginx-1.0.5-no-pool/src/core/nginx.h 2011-06-30 17:00:43.540946999 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 1000005 8 | -#define NGINX_VERSION "1.0.5" 9 | +#define NGINX_VERSION "1.0.5 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-1.0.5/src/core/ngx_array.c nginx-1.0.5-no-pool/src/core/ngx_array.c 14 | --- nginx-1.0.5/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-1.0.5-no-pool/src/core/ngx_array.c 2011-06-30 17:00:43.540946999 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -112,31 +94,16 @@ 72 | 73 | p = a->pool; 74 | 75 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 76 | - && p->d.last + size <= p->d.end) 77 | - { 78 | - /* 79 | - * the array allocation is the last in the pool 80 | - * and there is space for new allocation 81 | - */ 82 | - 83 | - p->d.last += size; 84 | - a->nalloc += n; 85 | - 86 | - } else { 87 | - /* allocate a new array */ 88 | - 89 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 90 | - 91 | - new = ngx_palloc(p, nalloc * a->size); 92 | - if (new == NULL) { 93 | - return NULL; 94 | - } 95 | - 96 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 97 | - a->elts = new; 98 | - a->nalloc = nalloc; 99 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 100 | + 101 | + new = ngx_palloc(p, nalloc * a->size); 102 | + if (new == NULL) { 103 | + return NULL; 104 | } 105 | + 106 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 107 | + a->elts = new; 108 | + a->nalloc = nalloc; 109 | } 110 | 111 | elt = (u_char *) a->elts + a->size * a->nelts; 112 | diff -ur nginx-1.0.5/src/core/ngx_palloc.c nginx-1.0.5-no-pool/src/core/ngx_palloc.c 113 | --- nginx-1.0.5/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 114 | +++ nginx-1.0.5-no-pool/src/core/ngx_palloc.c 2011-06-30 17:06:36.060946999 +0800 115 | @@ -8,24 +8,31 @@ 116 | #include 117 | 118 | 119 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 120 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 121 | 122 | 123 | ngx_pool_t * 124 | ngx_create_pool(size_t size, ngx_log_t *log) 125 | { 126 | - ngx_pool_t *p; 127 | + ngx_pool_t *p; 128 | + ngx_pool_data_t *d; 129 | 130 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 131 | + size = sizeof(ngx_pool_t); 132 | + p = ngx_alloc(size, log); 133 | if (p == NULL) { 134 | return NULL; 135 | } 136 | 137 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 138 | - p->d.end = (u_char *) p + size; 139 | - p->d.next = NULL; 140 | - p->d.failed = 0; 141 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 142 | + 143 | + if (d == NULL) { 144 | + return NULL; 145 | + } 146 | + 147 | + d->next = d; 148 | + d->prev = d; 149 | + d->alloc = NULL; 150 | + p->d = d; 151 | 152 | size = size - sizeof(ngx_pool_t); 153 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 154 | @@ -43,7 +50,7 @@ 155 | void 156 | ngx_destroy_pool(ngx_pool_t *pool) 157 | { 158 | - ngx_pool_t *p, *n; 159 | + ngx_pool_data_t *d, *n; 160 | ngx_pool_large_t *l; 161 | ngx_pool_cleanup_t *c; 162 | 163 | @@ -55,7 +62,7 @@ 164 | } 165 | } 166 | 167 | - for (l = pool->large; l; l = l->next) { 168 | + for (l = pool->large; l ; l = l->next) { 169 | 170 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 171 | 172 | @@ -71,34 +78,45 @@ 173 | * so we can not use this log while the free()ing the pool 174 | */ 175 | 176 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 177 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 178 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 179 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 180 | + "free: %p", d); 181 | 182 | - if (n == NULL) { 183 | + if (n == pool->d) { 184 | break; 185 | } 186 | } 187 | 188 | #endif 189 | + if (pool->d->next == pool->d) { 190 | + ngx_free(pool->d); 191 | + } else { 192 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 193 | + if (d->alloc) { 194 | + ngx_free(d->alloc); 195 | + } 196 | + ngx_free(d); 197 | 198 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 199 | - ngx_free(p); 200 | - 201 | - if (n == NULL) { 202 | - break; 203 | + if (n == pool->d) { 204 | + break; 205 | + } 206 | } 207 | } 208 | + 209 | + ngx_free(pool); 210 | } 211 | 212 | 213 | void 214 | ngx_reset_pool(ngx_pool_t *pool) 215 | { 216 | - ngx_pool_t *p; 217 | - ngx_pool_large_t *l; 218 | + ngx_pool_data_t *p, *tmp; 219 | + ngx_pool_large_t *l; 220 | + 221 | + for (l = pool->large; l ; l = l->next) { 222 | + 223 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 224 | 225 | - for (l = pool->large; l; l = l->next) { 226 | if (l->alloc) { 227 | ngx_free(l->alloc); 228 | } 229 | @@ -106,109 +124,65 @@ 230 | 231 | pool->large = NULL; 232 | 233 | - for (p = pool; p; p = p->d.next) { 234 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 235 | + p = pool->d->next; 236 | + while (p != pool->d) { 237 | + tmp = p; 238 | + ngx_free(p->alloc); 239 | + p->prev->next = p->next; 240 | + p->next->prev = p->prev; 241 | + p = p->next; 242 | + ngx_free(tmp); 243 | } 244 | -} 245 | 246 | + ngx_free(pool->d->alloc); 247 | + pool->d->alloc = NULL; 248 | + 249 | +} 250 | 251 | void * 252 | -ngx_palloc(ngx_pool_t *pool, size_t size) 253 | +ngx_malloc(ngx_pool_t *pool, size_t size) 254 | { 255 | - u_char *m; 256 | - ngx_pool_t *p; 257 | - 258 | - if (size <= pool->max) { 259 | + ngx_pool_data_t *new; 260 | + void *m; 261 | 262 | - p = pool->current; 263 | - 264 | - do { 265 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 266 | - 267 | - if ((size_t) (p->d.end - m) >= size) { 268 | - p->d.last = m + size; 269 | - 270 | - return m; 271 | - } 272 | - 273 | - p = p->d.next; 274 | - 275 | - } while (p); 276 | + m = ngx_alloc(size, pool->log); 277 | + if (m == NULL) { 278 | + return NULL; 279 | + } 280 | 281 | - return ngx_palloc_block(pool, size); 282 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 283 | + if (new == NULL){ 284 | + ngx_free(m); 285 | + return NULL; 286 | } 287 | 288 | - return ngx_palloc_large(pool, size); 289 | + new->alloc = m; 290 | + new->next = pool->d; 291 | + new->prev = pool->d->prev; 292 | + pool->d->prev->next = new; 293 | + pool->d->prev = new; 294 | + return m; 295 | } 296 | 297 | - 298 | void * 299 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 300 | +ngx_palloc(ngx_pool_t *pool, size_t size) 301 | { 302 | - u_char *m; 303 | - ngx_pool_t *p; 304 | - 305 | - if (size <= pool->max) { 306 | - 307 | - p = pool->current; 308 | - 309 | - do { 310 | - m = p->d.last; 311 | - 312 | - if ((size_t) (p->d.end - m) >= size) { 313 | - p->d.last = m + size; 314 | - 315 | - return m; 316 | - } 317 | - 318 | - p = p->d.next; 319 | - 320 | - } while (p); 321 | - 322 | - return ngx_palloc_block(pool, size); 323 | + if (size <= 1024) { 324 | + return ngx_malloc(pool, size); 325 | } 326 | 327 | return ngx_palloc_large(pool, size); 328 | } 329 | 330 | 331 | -static void * 332 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 333 | +void * 334 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 335 | { 336 | - u_char *m; 337 | - size_t psize; 338 | - ngx_pool_t *p, *new, *current; 339 | - 340 | - psize = (size_t) (pool->d.end - (u_char *) pool); 341 | - 342 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 343 | - if (m == NULL) { 344 | - return NULL; 345 | + if (size <= 1024) { 346 | + return ngx_malloc(pool, size); 347 | } 348 | 349 | - new = (ngx_pool_t *) m; 350 | - 351 | - new->d.end = m + psize; 352 | - new->d.next = NULL; 353 | - new->d.failed = 0; 354 | - 355 | - m += sizeof(ngx_pool_data_t); 356 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 357 | - new->d.last = m + size; 358 | - 359 | - current = pool->current; 360 | - 361 | - for (p = current; p->d.next; p = p->d.next) { 362 | - if (p->d.failed++ > 4) { 363 | - current = p->d.next; 364 | - } 365 | - } 366 | - 367 | - p->d.next = new; 368 | - 369 | - pool->current = current ? current : new; 370 | - 371 | - return m; 372 | + return ngx_palloc_large(pool, size); 373 | } 374 | 375 | 376 | @@ -226,18 +200,7 @@ 377 | 378 | n = 0; 379 | 380 | - for (large = pool->large; large; large = large->next) { 381 | - if (large->alloc == NULL) { 382 | - large->alloc = p; 383 | - return p; 384 | - } 385 | - 386 | - if (n++ > 3) { 387 | - break; 388 | - } 389 | - } 390 | - 391 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 392 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 393 | if (large == NULL) { 394 | ngx_free(p); 395 | return NULL; 396 | @@ -262,7 +225,7 @@ 397 | return NULL; 398 | } 399 | 400 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 401 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 402 | if (large == NULL) { 403 | ngx_free(p); 404 | return NULL; 405 | @@ -279,17 +242,41 @@ 406 | ngx_int_t 407 | ngx_pfree(ngx_pool_t *pool, void *p) 408 | { 409 | - ngx_pool_large_t *l; 410 | + ngx_pool_large_t *l; 411 | + ngx_pool_large_t *ll; 412 | + ngx_pool_data_t *d, *n; 413 | 414 | - for (l = pool->large; l; l = l->next) { 415 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 416 | if (p == l->alloc) { 417 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 418 | "free: %p", l->alloc); 419 | ngx_free(l->alloc); 420 | l->alloc = NULL; 421 | + if (l == pool->large) { 422 | + pool->large = NULL; 423 | + } 424 | + 425 | + ll->next = l->next; 426 | + p = l; 427 | + break; 428 | + } 429 | + } 430 | 431 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 432 | + if (p == d->alloc) { 433 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 434 | + if (d->alloc) { 435 | + ngx_free(d->alloc); 436 | + } 437 | + d->alloc = NULL; 438 | + d->prev->next = d->next; 439 | + d->next->prev = d->prev; 440 | + ngx_free(d); 441 | return NGX_OK; 442 | } 443 | + if (d->next == pool->d) { 444 | + break; 445 | + } 446 | } 447 | 448 | return NGX_DECLINED; 449 | Only in nginx-1.0.5-no-pool/src/core: ngx_palloc.c~ 450 | diff -ur nginx-1.0.5/src/core/ngx_palloc.h nginx-1.0.5-no-pool/src/core/ngx_palloc.h 451 | --- nginx-1.0.5/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 452 | +++ nginx-1.0.5-no-pool/src/core/ngx_palloc.h 2011-06-30 17:00:43.540946999 +0800 453 | @@ -38,6 +38,7 @@ 454 | 455 | 456 | typedef struct ngx_pool_large_s ngx_pool_large_t; 457 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 458 | 459 | struct ngx_pool_large_s { 460 | ngx_pool_large_t *next; 461 | @@ -45,16 +46,15 @@ 462 | }; 463 | 464 | 465 | -typedef struct { 466 | - u_char *last; 467 | - u_char *end; 468 | - ngx_pool_t *next; 469 | - ngx_uint_t failed; 470 | -} ngx_pool_data_t; 471 | +struct ngx_pool_data_s{ 472 | + ngx_pool_data_t *next; 473 | + ngx_pool_data_t *prev; 474 | + void *alloc; 475 | +}; 476 | 477 | 478 | struct ngx_pool_s { 479 | - ngx_pool_data_t d; 480 | + ngx_pool_data_t *d; 481 | size_t max; 482 | ngx_pool_t *current; 483 | ngx_chain_t *chain; 484 | -------------------------------------------------------------------------------- /nginx-1.1.0-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-1.1.0/src/core/nginx.h nginx-1.1.0-no-pool/src/core/nginx.h 2 | --- nginx-1.1.0/src/core/nginx.h 2011-05-26 15:31:40.000000000 +0800 3 | +++ nginx-1.1.0-no-pool/src/core/nginx.h 2011-06-30 17:00:43.540946999 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 1001000 8 | -#define NGINX_VERSION "1.1.0" 9 | +#define NGINX_VERSION "1.1.0 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-1.1.0/src/core/ngx_array.c nginx-1.1.0-no-pool/src/core/ngx_array.c 14 | --- nginx-1.1.0/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-1.1.0-no-pool/src/core/ngx_array.c 2011-06-30 17:00:43.540946999 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -112,31 +94,16 @@ 72 | 73 | p = a->pool; 74 | 75 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 76 | - && p->d.last + size <= p->d.end) 77 | - { 78 | - /* 79 | - * the array allocation is the last in the pool 80 | - * and there is space for new allocation 81 | - */ 82 | - 83 | - p->d.last += size; 84 | - a->nalloc += n; 85 | - 86 | - } else { 87 | - /* allocate a new array */ 88 | - 89 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 90 | - 91 | - new = ngx_palloc(p, nalloc * a->size); 92 | - if (new == NULL) { 93 | - return NULL; 94 | - } 95 | - 96 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 97 | - a->elts = new; 98 | - a->nalloc = nalloc; 99 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 100 | + 101 | + new = ngx_palloc(p, nalloc * a->size); 102 | + if (new == NULL) { 103 | + return NULL; 104 | } 105 | + 106 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 107 | + a->elts = new; 108 | + a->nalloc = nalloc; 109 | } 110 | 111 | elt = (u_char *) a->elts + a->size * a->nelts; 112 | diff -ur nginx-1.1.0/src/core/ngx_palloc.c nginx-1.1.0-no-pool/src/core/ngx_palloc.c 113 | --- nginx-1.1.0/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 114 | +++ nginx-1.1.0-no-pool/src/core/ngx_palloc.c 2011-06-30 17:06:36.060946999 +0800 115 | @@ -8,24 +8,31 @@ 116 | #include 117 | 118 | 119 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 120 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 121 | 122 | 123 | ngx_pool_t * 124 | ngx_create_pool(size_t size, ngx_log_t *log) 125 | { 126 | - ngx_pool_t *p; 127 | + ngx_pool_t *p; 128 | + ngx_pool_data_t *d; 129 | 130 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 131 | + size = sizeof(ngx_pool_t); 132 | + p = ngx_alloc(size, log); 133 | if (p == NULL) { 134 | return NULL; 135 | } 136 | 137 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 138 | - p->d.end = (u_char *) p + size; 139 | - p->d.next = NULL; 140 | - p->d.failed = 0; 141 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 142 | + 143 | + if (d == NULL) { 144 | + return NULL; 145 | + } 146 | + 147 | + d->next = d; 148 | + d->prev = d; 149 | + d->alloc = NULL; 150 | + p->d = d; 151 | 152 | size = size - sizeof(ngx_pool_t); 153 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 154 | @@ -43,7 +50,7 @@ 155 | void 156 | ngx_destroy_pool(ngx_pool_t *pool) 157 | { 158 | - ngx_pool_t *p, *n; 159 | + ngx_pool_data_t *d, *n; 160 | ngx_pool_large_t *l; 161 | ngx_pool_cleanup_t *c; 162 | 163 | @@ -55,7 +62,7 @@ 164 | } 165 | } 166 | 167 | - for (l = pool->large; l; l = l->next) { 168 | + for (l = pool->large; l ; l = l->next) { 169 | 170 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 171 | 172 | @@ -71,34 +78,45 @@ 173 | * so we can not use this log while the free()ing the pool 174 | */ 175 | 176 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 177 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 178 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 179 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 180 | + "free: %p", d); 181 | 182 | - if (n == NULL) { 183 | + if (n == pool->d) { 184 | break; 185 | } 186 | } 187 | 188 | #endif 189 | + if (pool->d->next == pool->d) { 190 | + ngx_free(pool->d); 191 | + } else { 192 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 193 | + if (d->alloc) { 194 | + ngx_free(d->alloc); 195 | + } 196 | + ngx_free(d); 197 | 198 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 199 | - ngx_free(p); 200 | - 201 | - if (n == NULL) { 202 | - break; 203 | + if (n == pool->d) { 204 | + break; 205 | + } 206 | } 207 | } 208 | + 209 | + ngx_free(pool); 210 | } 211 | 212 | 213 | void 214 | ngx_reset_pool(ngx_pool_t *pool) 215 | { 216 | - ngx_pool_t *p; 217 | - ngx_pool_large_t *l; 218 | + ngx_pool_data_t *p, *tmp; 219 | + ngx_pool_large_t *l; 220 | + 221 | + for (l = pool->large; l ; l = l->next) { 222 | + 223 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 224 | 225 | - for (l = pool->large; l; l = l->next) { 226 | if (l->alloc) { 227 | ngx_free(l->alloc); 228 | } 229 | @@ -106,109 +124,65 @@ 230 | 231 | pool->large = NULL; 232 | 233 | - for (p = pool; p; p = p->d.next) { 234 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 235 | + p = pool->d->next; 236 | + while (p != pool->d) { 237 | + tmp = p; 238 | + ngx_free(p->alloc); 239 | + p->prev->next = p->next; 240 | + p->next->prev = p->prev; 241 | + p = p->next; 242 | + ngx_free(tmp); 243 | } 244 | -} 245 | 246 | + ngx_free(pool->d->alloc); 247 | + pool->d->alloc = NULL; 248 | + 249 | +} 250 | 251 | void * 252 | -ngx_palloc(ngx_pool_t *pool, size_t size) 253 | +ngx_malloc(ngx_pool_t *pool, size_t size) 254 | { 255 | - u_char *m; 256 | - ngx_pool_t *p; 257 | - 258 | - if (size <= pool->max) { 259 | + ngx_pool_data_t *new; 260 | + void *m; 261 | 262 | - p = pool->current; 263 | - 264 | - do { 265 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 266 | - 267 | - if ((size_t) (p->d.end - m) >= size) { 268 | - p->d.last = m + size; 269 | - 270 | - return m; 271 | - } 272 | - 273 | - p = p->d.next; 274 | - 275 | - } while (p); 276 | + m = ngx_alloc(size, pool->log); 277 | + if (m == NULL) { 278 | + return NULL; 279 | + } 280 | 281 | - return ngx_palloc_block(pool, size); 282 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 283 | + if (new == NULL){ 284 | + ngx_free(m); 285 | + return NULL; 286 | } 287 | 288 | - return ngx_palloc_large(pool, size); 289 | + new->alloc = m; 290 | + new->next = pool->d; 291 | + new->prev = pool->d->prev; 292 | + pool->d->prev->next = new; 293 | + pool->d->prev = new; 294 | + return m; 295 | } 296 | 297 | - 298 | void * 299 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 300 | +ngx_palloc(ngx_pool_t *pool, size_t size) 301 | { 302 | - u_char *m; 303 | - ngx_pool_t *p; 304 | - 305 | - if (size <= pool->max) { 306 | - 307 | - p = pool->current; 308 | - 309 | - do { 310 | - m = p->d.last; 311 | - 312 | - if ((size_t) (p->d.end - m) >= size) { 313 | - p->d.last = m + size; 314 | - 315 | - return m; 316 | - } 317 | - 318 | - p = p->d.next; 319 | - 320 | - } while (p); 321 | - 322 | - return ngx_palloc_block(pool, size); 323 | + if (size <= 1024) { 324 | + return ngx_malloc(pool, size); 325 | } 326 | 327 | return ngx_palloc_large(pool, size); 328 | } 329 | 330 | 331 | -static void * 332 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 333 | +void * 334 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 335 | { 336 | - u_char *m; 337 | - size_t psize; 338 | - ngx_pool_t *p, *new, *current; 339 | - 340 | - psize = (size_t) (pool->d.end - (u_char *) pool); 341 | - 342 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 343 | - if (m == NULL) { 344 | - return NULL; 345 | + if (size <= 1024) { 346 | + return ngx_malloc(pool, size); 347 | } 348 | 349 | - new = (ngx_pool_t *) m; 350 | - 351 | - new->d.end = m + psize; 352 | - new->d.next = NULL; 353 | - new->d.failed = 0; 354 | - 355 | - m += sizeof(ngx_pool_data_t); 356 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 357 | - new->d.last = m + size; 358 | - 359 | - current = pool->current; 360 | - 361 | - for (p = current; p->d.next; p = p->d.next) { 362 | - if (p->d.failed++ > 4) { 363 | - current = p->d.next; 364 | - } 365 | - } 366 | - 367 | - p->d.next = new; 368 | - 369 | - pool->current = current ? current : new; 370 | - 371 | - return m; 372 | + return ngx_palloc_large(pool, size); 373 | } 374 | 375 | 376 | @@ -226,18 +200,7 @@ 377 | 378 | n = 0; 379 | 380 | - for (large = pool->large; large; large = large->next) { 381 | - if (large->alloc == NULL) { 382 | - large->alloc = p; 383 | - return p; 384 | - } 385 | - 386 | - if (n++ > 3) { 387 | - break; 388 | - } 389 | - } 390 | - 391 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 392 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 393 | if (large == NULL) { 394 | ngx_free(p); 395 | return NULL; 396 | @@ -262,7 +225,7 @@ 397 | return NULL; 398 | } 399 | 400 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 401 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 402 | if (large == NULL) { 403 | ngx_free(p); 404 | return NULL; 405 | @@ -279,17 +242,41 @@ 406 | ngx_int_t 407 | ngx_pfree(ngx_pool_t *pool, void *p) 408 | { 409 | - ngx_pool_large_t *l; 410 | + ngx_pool_large_t *l; 411 | + ngx_pool_large_t *ll; 412 | + ngx_pool_data_t *d, *n; 413 | 414 | - for (l = pool->large; l; l = l->next) { 415 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 416 | if (p == l->alloc) { 417 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 418 | "free: %p", l->alloc); 419 | ngx_free(l->alloc); 420 | l->alloc = NULL; 421 | + if (l == pool->large) { 422 | + pool->large = NULL; 423 | + } 424 | + 425 | + ll->next = l->next; 426 | + p = l; 427 | + break; 428 | + } 429 | + } 430 | 431 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 432 | + if (p == d->alloc) { 433 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 434 | + if (d->alloc) { 435 | + ngx_free(d->alloc); 436 | + } 437 | + d->alloc = NULL; 438 | + d->prev->next = d->next; 439 | + d->next->prev = d->prev; 440 | + ngx_free(d); 441 | return NGX_OK; 442 | } 443 | + if (d->next == pool->d) { 444 | + break; 445 | + } 446 | } 447 | 448 | return NGX_DECLINED; 449 | Only in nginx-1.1.0-no-pool/src/core: ngx_palloc.c~ 450 | diff -ur nginx-1.1.0/src/core/ngx_palloc.h nginx-1.1.0-no-pool/src/core/ngx_palloc.h 451 | --- nginx-1.1.0/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 452 | +++ nginx-1.1.0-no-pool/src/core/ngx_palloc.h 2011-06-30 17:00:43.540946999 +0800 453 | @@ -38,6 +38,7 @@ 454 | 455 | 456 | typedef struct ngx_pool_large_s ngx_pool_large_t; 457 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 458 | 459 | struct ngx_pool_large_s { 460 | ngx_pool_large_t *next; 461 | @@ -45,16 +46,15 @@ 462 | }; 463 | 464 | 465 | -typedef struct { 466 | - u_char *last; 467 | - u_char *end; 468 | - ngx_pool_t *next; 469 | - ngx_uint_t failed; 470 | -} ngx_pool_data_t; 471 | +struct ngx_pool_data_s{ 472 | + ngx_pool_data_t *next; 473 | + ngx_pool_data_t *prev; 474 | + void *alloc; 475 | +}; 476 | 477 | 478 | struct ngx_pool_s { 479 | - ngx_pool_data_t d; 480 | + ngx_pool_data_t *d; 481 | size_t max; 482 | ngx_pool_t *current; 483 | ngx_chain_t *chain; 484 | -------------------------------------------------------------------------------- /nginx-1.1.2-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-1.1.2/src/core/nginx.h nginx-1.1.2-no-pool/src/core/nginx.h 2 | --- nginx-1.1.2/src/core/nginx.h 2011-05-26 15:31:40.000000000 +0800 3 | +++ nginx-1.1.2-no-pool/src/core/nginx.h 2011-06-30 17:00:43.540946999 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 1001002 8 | -#define NGINX_VERSION "1.1.2" 9 | +#define NGINX_VERSION "1.1.2 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-1.1.2/src/core/ngx_array.c nginx-1.1.2-no-pool/src/core/ngx_array.c 14 | --- nginx-1.1.2/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-1.1.2-no-pool/src/core/ngx_array.c 2011-06-30 17:00:43.540946999 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -112,31 +94,16 @@ 72 | 73 | p = a->pool; 74 | 75 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 76 | - && p->d.last + size <= p->d.end) 77 | - { 78 | - /* 79 | - * the array allocation is the last in the pool 80 | - * and there is space for new allocation 81 | - */ 82 | - 83 | - p->d.last += size; 84 | - a->nalloc += n; 85 | - 86 | - } else { 87 | - /* allocate a new array */ 88 | - 89 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 90 | - 91 | - new = ngx_palloc(p, nalloc * a->size); 92 | - if (new == NULL) { 93 | - return NULL; 94 | - } 95 | - 96 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 97 | - a->elts = new; 98 | - a->nalloc = nalloc; 99 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 100 | + 101 | + new = ngx_palloc(p, nalloc * a->size); 102 | + if (new == NULL) { 103 | + return NULL; 104 | } 105 | + 106 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 107 | + a->elts = new; 108 | + a->nalloc = nalloc; 109 | } 110 | 111 | elt = (u_char *) a->elts + a->size * a->nelts; 112 | diff -ur nginx-1.1.2/src/core/ngx_palloc.c nginx-1.1.2-no-pool/src/core/ngx_palloc.c 113 | --- nginx-1.1.2/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 114 | +++ nginx-1.1.2-no-pool/src/core/ngx_palloc.c 2011-06-30 17:06:36.060946999 +0800 115 | @@ -8,24 +8,31 @@ 116 | #include 117 | 118 | 119 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 120 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 121 | 122 | 123 | ngx_pool_t * 124 | ngx_create_pool(size_t size, ngx_log_t *log) 125 | { 126 | - ngx_pool_t *p; 127 | + ngx_pool_t *p; 128 | + ngx_pool_data_t *d; 129 | 130 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 131 | + size = sizeof(ngx_pool_t); 132 | + p = ngx_alloc(size, log); 133 | if (p == NULL) { 134 | return NULL; 135 | } 136 | 137 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 138 | - p->d.end = (u_char *) p + size; 139 | - p->d.next = NULL; 140 | - p->d.failed = 0; 141 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 142 | + 143 | + if (d == NULL) { 144 | + return NULL; 145 | + } 146 | + 147 | + d->next = d; 148 | + d->prev = d; 149 | + d->alloc = NULL; 150 | + p->d = d; 151 | 152 | size = size - sizeof(ngx_pool_t); 153 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 154 | @@ -43,7 +50,7 @@ 155 | void 156 | ngx_destroy_pool(ngx_pool_t *pool) 157 | { 158 | - ngx_pool_t *p, *n; 159 | + ngx_pool_data_t *d, *n; 160 | ngx_pool_large_t *l; 161 | ngx_pool_cleanup_t *c; 162 | 163 | @@ -55,7 +62,7 @@ 164 | } 165 | } 166 | 167 | - for (l = pool->large; l; l = l->next) { 168 | + for (l = pool->large; l ; l = l->next) { 169 | 170 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 171 | 172 | @@ -71,34 +78,45 @@ 173 | * so we can not use this log while the free()ing the pool 174 | */ 175 | 176 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 177 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 178 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 179 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 180 | + "free: %p", d); 181 | 182 | - if (n == NULL) { 183 | + if (n == pool->d) { 184 | break; 185 | } 186 | } 187 | 188 | #endif 189 | + if (pool->d->next == pool->d) { 190 | + ngx_free(pool->d); 191 | + } else { 192 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 193 | + if (d->alloc) { 194 | + ngx_free(d->alloc); 195 | + } 196 | + ngx_free(d); 197 | 198 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 199 | - ngx_free(p); 200 | - 201 | - if (n == NULL) { 202 | - break; 203 | + if (n == pool->d) { 204 | + break; 205 | + } 206 | } 207 | } 208 | + 209 | + ngx_free(pool); 210 | } 211 | 212 | 213 | void 214 | ngx_reset_pool(ngx_pool_t *pool) 215 | { 216 | - ngx_pool_t *p; 217 | - ngx_pool_large_t *l; 218 | + ngx_pool_data_t *p, *tmp; 219 | + ngx_pool_large_t *l; 220 | + 221 | + for (l = pool->large; l ; l = l->next) { 222 | + 223 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 224 | 225 | - for (l = pool->large; l; l = l->next) { 226 | if (l->alloc) { 227 | ngx_free(l->alloc); 228 | } 229 | @@ -106,109 +124,65 @@ 230 | 231 | pool->large = NULL; 232 | 233 | - for (p = pool; p; p = p->d.next) { 234 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 235 | + p = pool->d->next; 236 | + while (p != pool->d) { 237 | + tmp = p; 238 | + ngx_free(p->alloc); 239 | + p->prev->next = p->next; 240 | + p->next->prev = p->prev; 241 | + p = p->next; 242 | + ngx_free(tmp); 243 | } 244 | -} 245 | 246 | + ngx_free(pool->d->alloc); 247 | + pool->d->alloc = NULL; 248 | + 249 | +} 250 | 251 | void * 252 | -ngx_palloc(ngx_pool_t *pool, size_t size) 253 | +ngx_malloc(ngx_pool_t *pool, size_t size) 254 | { 255 | - u_char *m; 256 | - ngx_pool_t *p; 257 | - 258 | - if (size <= pool->max) { 259 | + ngx_pool_data_t *new; 260 | + void *m; 261 | 262 | - p = pool->current; 263 | - 264 | - do { 265 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 266 | - 267 | - if ((size_t) (p->d.end - m) >= size) { 268 | - p->d.last = m + size; 269 | - 270 | - return m; 271 | - } 272 | - 273 | - p = p->d.next; 274 | - 275 | - } while (p); 276 | + m = ngx_alloc(size, pool->log); 277 | + if (m == NULL) { 278 | + return NULL; 279 | + } 280 | 281 | - return ngx_palloc_block(pool, size); 282 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 283 | + if (new == NULL){ 284 | + ngx_free(m); 285 | + return NULL; 286 | } 287 | 288 | - return ngx_palloc_large(pool, size); 289 | + new->alloc = m; 290 | + new->next = pool->d; 291 | + new->prev = pool->d->prev; 292 | + pool->d->prev->next = new; 293 | + pool->d->prev = new; 294 | + return m; 295 | } 296 | 297 | - 298 | void * 299 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 300 | +ngx_palloc(ngx_pool_t *pool, size_t size) 301 | { 302 | - u_char *m; 303 | - ngx_pool_t *p; 304 | - 305 | - if (size <= pool->max) { 306 | - 307 | - p = pool->current; 308 | - 309 | - do { 310 | - m = p->d.last; 311 | - 312 | - if ((size_t) (p->d.end - m) >= size) { 313 | - p->d.last = m + size; 314 | - 315 | - return m; 316 | - } 317 | - 318 | - p = p->d.next; 319 | - 320 | - } while (p); 321 | - 322 | - return ngx_palloc_block(pool, size); 323 | + if (size <= 1024) { 324 | + return ngx_malloc(pool, size); 325 | } 326 | 327 | return ngx_palloc_large(pool, size); 328 | } 329 | 330 | 331 | -static void * 332 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 333 | +void * 334 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 335 | { 336 | - u_char *m; 337 | - size_t psize; 338 | - ngx_pool_t *p, *new, *current; 339 | - 340 | - psize = (size_t) (pool->d.end - (u_char *) pool); 341 | - 342 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 343 | - if (m == NULL) { 344 | - return NULL; 345 | + if (size <= 1024) { 346 | + return ngx_malloc(pool, size); 347 | } 348 | 349 | - new = (ngx_pool_t *) m; 350 | - 351 | - new->d.end = m + psize; 352 | - new->d.next = NULL; 353 | - new->d.failed = 0; 354 | - 355 | - m += sizeof(ngx_pool_data_t); 356 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 357 | - new->d.last = m + size; 358 | - 359 | - current = pool->current; 360 | - 361 | - for (p = current; p->d.next; p = p->d.next) { 362 | - if (p->d.failed++ > 4) { 363 | - current = p->d.next; 364 | - } 365 | - } 366 | - 367 | - p->d.next = new; 368 | - 369 | - pool->current = current ? current : new; 370 | - 371 | - return m; 372 | + return ngx_palloc_large(pool, size); 373 | } 374 | 375 | 376 | @@ -226,18 +200,7 @@ 377 | 378 | n = 0; 379 | 380 | - for (large = pool->large; large; large = large->next) { 381 | - if (large->alloc == NULL) { 382 | - large->alloc = p; 383 | - return p; 384 | - } 385 | - 386 | - if (n++ > 3) { 387 | - break; 388 | - } 389 | - } 390 | - 391 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 392 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 393 | if (large == NULL) { 394 | ngx_free(p); 395 | return NULL; 396 | @@ -262,7 +225,7 @@ 397 | return NULL; 398 | } 399 | 400 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 401 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 402 | if (large == NULL) { 403 | ngx_free(p); 404 | return NULL; 405 | @@ -279,17 +242,41 @@ 406 | ngx_int_t 407 | ngx_pfree(ngx_pool_t *pool, void *p) 408 | { 409 | - ngx_pool_large_t *l; 410 | + ngx_pool_large_t *l; 411 | + ngx_pool_large_t *ll; 412 | + ngx_pool_data_t *d, *n; 413 | 414 | - for (l = pool->large; l; l = l->next) { 415 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 416 | if (p == l->alloc) { 417 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 418 | "free: %p", l->alloc); 419 | ngx_free(l->alloc); 420 | l->alloc = NULL; 421 | + if (l == pool->large) { 422 | + pool->large = NULL; 423 | + } 424 | + 425 | + ll->next = l->next; 426 | + p = l; 427 | + break; 428 | + } 429 | + } 430 | 431 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 432 | + if (p == d->alloc) { 433 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 434 | + if (d->alloc) { 435 | + ngx_free(d->alloc); 436 | + } 437 | + d->alloc = NULL; 438 | + d->prev->next = d->next; 439 | + d->next->prev = d->prev; 440 | + ngx_free(d); 441 | return NGX_OK; 442 | } 443 | + if (d->next == pool->d) { 444 | + break; 445 | + } 446 | } 447 | 448 | return NGX_DECLINED; 449 | Only in nginx-1.1.2-no-pool/src/core: ngx_palloc.c~ 450 | diff -ur nginx-1.1.2/src/core/ngx_palloc.h nginx-1.1.2-no-pool/src/core/ngx_palloc.h 451 | --- nginx-1.1.2/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 452 | +++ nginx-1.1.2-no-pool/src/core/ngx_palloc.h 2011-06-30 17:00:43.540946999 +0800 453 | @@ -38,6 +38,7 @@ 454 | 455 | 456 | typedef struct ngx_pool_large_s ngx_pool_large_t; 457 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 458 | 459 | struct ngx_pool_large_s { 460 | ngx_pool_large_t *next; 461 | @@ -45,16 +46,15 @@ 462 | }; 463 | 464 | 465 | -typedef struct { 466 | - u_char *last; 467 | - u_char *end; 468 | - ngx_pool_t *next; 469 | - ngx_uint_t failed; 470 | -} ngx_pool_data_t; 471 | +struct ngx_pool_data_s{ 472 | + ngx_pool_data_t *next; 473 | + ngx_pool_data_t *prev; 474 | + void *alloc; 475 | +}; 476 | 477 | 478 | struct ngx_pool_s { 479 | - ngx_pool_data_t d; 480 | + ngx_pool_data_t *d; 481 | size_t max; 482 | ngx_pool_t *current; 483 | ngx_chain_t *chain; 484 | -------------------------------------------------------------------------------- /nginx-0.8.41-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-0.8.41/src/core/nginx.h nginx-0.8.41-no-pool/src/core/nginx.h 2 | --- nginx-0.8.41/src/core/nginx.h 2010-06-07 21:34:56.000000000 +0800 3 | +++ nginx-0.8.41-no-pool/src/core/nginx.h 2011-06-30 17:35:18.860946999 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 8041 8 | -#define NGINX_VERSION "0.8.41" 9 | +#define NGINX_VERSION "0.8.41 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-0.8.41/src/core/ngx_array.c nginx-0.8.41-no-pool/src/core/ngx_array.c 14 | --- nginx-0.8.41/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-0.8.41-no-pool/src/core/ngx_array.c 2011-06-30 17:35:18.860946999 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -112,31 +94,16 @@ 72 | 73 | p = a->pool; 74 | 75 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 76 | - && p->d.last + size <= p->d.end) 77 | - { 78 | - /* 79 | - * the array allocation is the last in the pool 80 | - * and there is space for new allocation 81 | - */ 82 | - 83 | - p->d.last += size; 84 | - a->nalloc += n; 85 | - 86 | - } else { 87 | - /* allocate a new array */ 88 | - 89 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 90 | - 91 | - new = ngx_palloc(p, nalloc * a->size); 92 | - if (new == NULL) { 93 | - return NULL; 94 | - } 95 | - 96 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 97 | - a->elts = new; 98 | - a->nalloc = nalloc; 99 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 100 | + 101 | + new = ngx_palloc(p, nalloc * a->size); 102 | + if (new == NULL) { 103 | + return NULL; 104 | } 105 | + 106 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 107 | + a->elts = new; 108 | + a->nalloc = nalloc; 109 | } 110 | 111 | elt = (u_char *) a->elts + a->size * a->nelts; 112 | diff -ur nginx-0.8.41/src/core/ngx_palloc.c nginx-0.8.41-no-pool/src/core/ngx_palloc.c 113 | --- nginx-0.8.41/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 114 | +++ nginx-0.8.41-no-pool/src/core/ngx_palloc.c 2011-06-30 17:36:33.480946999 +0800 115 | @@ -8,24 +8,31 @@ 116 | #include 117 | 118 | 119 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 120 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 121 | 122 | 123 | ngx_pool_t * 124 | ngx_create_pool(size_t size, ngx_log_t *log) 125 | { 126 | - ngx_pool_t *p; 127 | + ngx_pool_t *p; 128 | + ngx_pool_data_t *d; 129 | 130 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 131 | + size = sizeof(ngx_pool_t); 132 | + p = ngx_alloc(size, log); 133 | if (p == NULL) { 134 | return NULL; 135 | } 136 | 137 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 138 | - p->d.end = (u_char *) p + size; 139 | - p->d.next = NULL; 140 | - p->d.failed = 0; 141 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 142 | + 143 | + if (d == NULL) { 144 | + return NULL; 145 | + } 146 | + 147 | + d->next = d; 148 | + d->prev = d; 149 | + d->alloc = NULL; 150 | + p->d = d; 151 | 152 | size = size - sizeof(ngx_pool_t); 153 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 154 | @@ -43,7 +50,7 @@ 155 | void 156 | ngx_destroy_pool(ngx_pool_t *pool) 157 | { 158 | - ngx_pool_t *p, *n; 159 | + ngx_pool_data_t *d, *n; 160 | ngx_pool_large_t *l; 161 | ngx_pool_cleanup_t *c; 162 | 163 | @@ -55,7 +62,7 @@ 164 | } 165 | } 166 | 167 | - for (l = pool->large; l; l = l->next) { 168 | + for (l = pool->large; l ; l = l->next) { 169 | 170 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 171 | 172 | @@ -71,34 +78,45 @@ 173 | * so we can not use this log while the free()ing the pool 174 | */ 175 | 176 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 177 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 178 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 179 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 180 | + "free: %p", d); 181 | 182 | - if (n == NULL) { 183 | + if (n == pool->d) { 184 | break; 185 | } 186 | } 187 | 188 | #endif 189 | + if (pool->d->next == pool->d) { 190 | + ngx_free(pool->d); 191 | + } else { 192 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 193 | + if (d->alloc) { 194 | + ngx_free(d->alloc); 195 | + } 196 | + ngx_free(d); 197 | 198 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 199 | - ngx_free(p); 200 | - 201 | - if (n == NULL) { 202 | - break; 203 | + if (n == pool->d) { 204 | + break; 205 | + } 206 | } 207 | } 208 | + 209 | + ngx_free(pool); 210 | } 211 | 212 | 213 | void 214 | ngx_reset_pool(ngx_pool_t *pool) 215 | { 216 | - ngx_pool_t *p; 217 | - ngx_pool_large_t *l; 218 | + ngx_pool_data_t *p, *tmp; 219 | + ngx_pool_large_t *l; 220 | + 221 | + for (l = pool->large; l ; l = l->next) { 222 | + 223 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 224 | 225 | - for (l = pool->large; l; l = l->next) { 226 | if (l->alloc) { 227 | ngx_free(l->alloc); 228 | } 229 | @@ -106,109 +124,64 @@ 230 | 231 | pool->large = NULL; 232 | 233 | - for (p = pool; p; p = p->d.next) { 234 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 235 | + p = pool->d->next; 236 | + while (p != pool->d) { 237 | + tmp = p; 238 | + ngx_free(p->alloc); 239 | + p->prev->next = p->next; 240 | + p->next->prev = p->prev; 241 | + p = p->next; 242 | + ngx_free(tmp); 243 | } 244 | -} 245 | 246 | + ngx_free(pool->d->alloc); 247 | + pool->d->alloc = NULL; 248 | +} 249 | 250 | void * 251 | -ngx_palloc(ngx_pool_t *pool, size_t size) 252 | +ngx_malloc(ngx_pool_t *pool, size_t size) 253 | { 254 | - u_char *m; 255 | - ngx_pool_t *p; 256 | - 257 | - if (size <= pool->max) { 258 | - 259 | - p = pool->current; 260 | + ngx_pool_data_t *new; 261 | + void *m; 262 | 263 | - do { 264 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 265 | - 266 | - if ((size_t) (p->d.end - m) >= size) { 267 | - p->d.last = m + size; 268 | - 269 | - return m; 270 | - } 271 | - 272 | - p = p->d.next; 273 | - 274 | - } while (p); 275 | + m = ngx_alloc(size, pool->log); 276 | + if (m == NULL) { 277 | + return NULL; 278 | + } 279 | 280 | - return ngx_palloc_block(pool, size); 281 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 282 | + if (new == NULL){ 283 | + ngx_free(m); 284 | + return NULL; 285 | } 286 | 287 | - return ngx_palloc_large(pool, size); 288 | + new->alloc = m; 289 | + new->next = pool->d; 290 | + new->prev = pool->d->prev; 291 | + pool->d->prev->next = new; 292 | + pool->d->prev = new; 293 | + return m; 294 | } 295 | 296 | - 297 | void * 298 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 299 | +ngx_palloc(ngx_pool_t *pool, size_t size) 300 | { 301 | - u_char *m; 302 | - ngx_pool_t *p; 303 | - 304 | - if (size <= pool->max) { 305 | - 306 | - p = pool->current; 307 | - 308 | - do { 309 | - m = p->d.last; 310 | - 311 | - if ((size_t) (p->d.end - m) >= size) { 312 | - p->d.last = m + size; 313 | - 314 | - return m; 315 | - } 316 | - 317 | - p = p->d.next; 318 | - 319 | - } while (p); 320 | - 321 | - return ngx_palloc_block(pool, size); 322 | + if (size <= 1024) { 323 | + return ngx_malloc(pool, size); 324 | } 325 | 326 | return ngx_palloc_large(pool, size); 327 | } 328 | 329 | 330 | -static void * 331 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 332 | +void * 333 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 334 | { 335 | - u_char *m; 336 | - size_t psize; 337 | - ngx_pool_t *p, *new, *current; 338 | - 339 | - psize = (size_t) (pool->d.end - (u_char *) pool); 340 | - 341 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 342 | - if (m == NULL) { 343 | - return NULL; 344 | + if (size <= 1024) { 345 | + return ngx_malloc(pool, size); 346 | } 347 | 348 | - new = (ngx_pool_t *) m; 349 | - 350 | - new->d.end = m + psize; 351 | - new->d.next = NULL; 352 | - new->d.failed = 0; 353 | - 354 | - m += sizeof(ngx_pool_data_t); 355 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 356 | - new->d.last = m + size; 357 | - 358 | - current = pool->current; 359 | - 360 | - for (p = current; p->d.next; p = p->d.next) { 361 | - if (p->d.failed++ > 4) { 362 | - current = p->d.next; 363 | - } 364 | - } 365 | - 366 | - p->d.next = new; 367 | - 368 | - pool->current = current ? current : new; 369 | - 370 | - return m; 371 | + return ngx_palloc_large(pool, size); 372 | } 373 | 374 | 375 | @@ -226,18 +199,7 @@ 376 | 377 | n = 0; 378 | 379 | - for (large = pool->large; large; large = large->next) { 380 | - if (large->alloc == NULL) { 381 | - large->alloc = p; 382 | - return p; 383 | - } 384 | - 385 | - if (n++ > 3) { 386 | - break; 387 | - } 388 | - } 389 | - 390 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 391 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 392 | if (large == NULL) { 393 | ngx_free(p); 394 | return NULL; 395 | @@ -262,7 +224,7 @@ 396 | return NULL; 397 | } 398 | 399 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 400 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 401 | if (large == NULL) { 402 | ngx_free(p); 403 | return NULL; 404 | @@ -279,17 +241,41 @@ 405 | ngx_int_t 406 | ngx_pfree(ngx_pool_t *pool, void *p) 407 | { 408 | - ngx_pool_large_t *l; 409 | + ngx_pool_large_t *l; 410 | + ngx_pool_large_t *ll; 411 | + ngx_pool_data_t *d, *n; 412 | 413 | - for (l = pool->large; l; l = l->next) { 414 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 415 | if (p == l->alloc) { 416 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 417 | "free: %p", l->alloc); 418 | ngx_free(l->alloc); 419 | l->alloc = NULL; 420 | + if (l == pool->large) { 421 | + pool->large = NULL; 422 | + } 423 | + 424 | + ll->next = l->next; 425 | + p = l; 426 | + break; 427 | + } 428 | + } 429 | 430 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 431 | + if (p == d->alloc) { 432 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 433 | + if (d->alloc) { 434 | + ngx_free(d->alloc); 435 | + } 436 | + d->alloc = NULL; 437 | + d->prev->next = d->next; 438 | + d->next->prev = d->prev; 439 | + ngx_free(d); 440 | return NGX_OK; 441 | } 442 | + if (d->next == pool->d) { 443 | + break; 444 | + } 445 | } 446 | 447 | return NGX_DECLINED; 448 | Only in nginx-0.8.41-no-pool/src/core: ngx_palloc.c~ 449 | diff -ur nginx-0.8.41/src/core/ngx_palloc.h nginx-0.8.41-no-pool/src/core/ngx_palloc.h 450 | --- nginx-0.8.41/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 451 | +++ nginx-0.8.41-no-pool/src/core/ngx_palloc.h 2011-06-30 17:35:18.860946999 +0800 452 | @@ -38,6 +38,7 @@ 453 | 454 | 455 | typedef struct ngx_pool_large_s ngx_pool_large_t; 456 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 457 | 458 | struct ngx_pool_large_s { 459 | ngx_pool_large_t *next; 460 | @@ -45,16 +46,15 @@ 461 | }; 462 | 463 | 464 | -typedef struct { 465 | - u_char *last; 466 | - u_char *end; 467 | - ngx_pool_t *next; 468 | - ngx_uint_t failed; 469 | -} ngx_pool_data_t; 470 | +struct ngx_pool_data_s{ 471 | + ngx_pool_data_t *next; 472 | + ngx_pool_data_t *prev; 473 | + void *alloc; 474 | +}; 475 | 476 | 477 | struct ngx_pool_s { 478 | - ngx_pool_data_t d; 479 | + ngx_pool_data_t *d; 480 | size_t max; 481 | ngx_pool_t *current; 482 | ngx_chain_t *chain; 483 | -------------------------------------------------------------------------------- /nginx-0.8.53-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-0.8.53/src/core/nginx.h nginx-0.8.53-no-pool/src/core/nginx.h 2 | --- nginx-0.8.53/src/core/nginx.h 2010-10-04 21:50:09.000000000 +0800 3 | +++ nginx-0.8.53-no-pool/src/core/nginx.h 2011-06-30 17:38:26.300946999 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 8053 8 | -#define NGINX_VERSION "0.8.53" 9 | +#define NGINX_VERSION "0.8.53 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-0.8.53/src/core/ngx_array.c nginx-0.8.53-no-pool/src/core/ngx_array.c 14 | --- nginx-0.8.53/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-0.8.53-no-pool/src/core/ngx_array.c 2011-06-30 17:38:26.300946999 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -112,31 +94,16 @@ 72 | 73 | p = a->pool; 74 | 75 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 76 | - && p->d.last + size <= p->d.end) 77 | - { 78 | - /* 79 | - * the array allocation is the last in the pool 80 | - * and there is space for new allocation 81 | - */ 82 | - 83 | - p->d.last += size; 84 | - a->nalloc += n; 85 | - 86 | - } else { 87 | - /* allocate a new array */ 88 | - 89 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 90 | - 91 | - new = ngx_palloc(p, nalloc * a->size); 92 | - if (new == NULL) { 93 | - return NULL; 94 | - } 95 | - 96 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 97 | - a->elts = new; 98 | - a->nalloc = nalloc; 99 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 100 | + 101 | + new = ngx_palloc(p, nalloc * a->size); 102 | + if (new == NULL) { 103 | + return NULL; 104 | } 105 | + 106 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 107 | + a->elts = new; 108 | + a->nalloc = nalloc; 109 | } 110 | 111 | elt = (u_char *) a->elts + a->size * a->nelts; 112 | diff -ur nginx-0.8.53/src/core/ngx_palloc.c nginx-0.8.53-no-pool/src/core/ngx_palloc.c 113 | --- nginx-0.8.53/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 114 | +++ nginx-0.8.53-no-pool/src/core/ngx_palloc.c 2011-06-30 17:39:09.810946999 +0800 115 | @@ -8,24 +8,31 @@ 116 | #include 117 | 118 | 119 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 120 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 121 | 122 | 123 | ngx_pool_t * 124 | ngx_create_pool(size_t size, ngx_log_t *log) 125 | { 126 | - ngx_pool_t *p; 127 | + ngx_pool_t *p; 128 | + ngx_pool_data_t *d; 129 | 130 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 131 | + size = sizeof(ngx_pool_t); 132 | + p = ngx_alloc(size, log); 133 | if (p == NULL) { 134 | return NULL; 135 | } 136 | 137 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 138 | - p->d.end = (u_char *) p + size; 139 | - p->d.next = NULL; 140 | - p->d.failed = 0; 141 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 142 | + 143 | + if (d == NULL) { 144 | + return NULL; 145 | + } 146 | + 147 | + d->next = d; 148 | + d->prev = d; 149 | + d->alloc = NULL; 150 | + p->d = d; 151 | 152 | size = size - sizeof(ngx_pool_t); 153 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 154 | @@ -43,7 +50,7 @@ 155 | void 156 | ngx_destroy_pool(ngx_pool_t *pool) 157 | { 158 | - ngx_pool_t *p, *n; 159 | + ngx_pool_data_t *d, *n; 160 | ngx_pool_large_t *l; 161 | ngx_pool_cleanup_t *c; 162 | 163 | @@ -55,7 +62,7 @@ 164 | } 165 | } 166 | 167 | - for (l = pool->large; l; l = l->next) { 168 | + for (l = pool->large; l ; l = l->next) { 169 | 170 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 171 | 172 | @@ -71,34 +78,45 @@ 173 | * so we can not use this log while the free()ing the pool 174 | */ 175 | 176 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 177 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 178 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 179 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 180 | + "free: %p", d); 181 | 182 | - if (n == NULL) { 183 | + if (n == pool->d) { 184 | break; 185 | } 186 | } 187 | 188 | #endif 189 | + if (pool->d->next == pool->d) { 190 | + ngx_free(pool->d); 191 | + } else { 192 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 193 | + if (d->alloc) { 194 | + ngx_free(d->alloc); 195 | + } 196 | + ngx_free(d); 197 | 198 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 199 | - ngx_free(p); 200 | - 201 | - if (n == NULL) { 202 | - break; 203 | + if (n == pool->d) { 204 | + break; 205 | + } 206 | } 207 | } 208 | + 209 | + ngx_free(pool); 210 | } 211 | 212 | 213 | void 214 | ngx_reset_pool(ngx_pool_t *pool) 215 | { 216 | - ngx_pool_t *p; 217 | - ngx_pool_large_t *l; 218 | + ngx_pool_data_t *p, *tmp; 219 | + ngx_pool_large_t *l; 220 | + 221 | + for (l = pool->large; l ; l = l->next) { 222 | + 223 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 224 | 225 | - for (l = pool->large; l; l = l->next) { 226 | if (l->alloc) { 227 | ngx_free(l->alloc); 228 | } 229 | @@ -106,109 +124,64 @@ 230 | 231 | pool->large = NULL; 232 | 233 | - for (p = pool; p; p = p->d.next) { 234 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 235 | + p = pool->d->next; 236 | + while (p != pool->d) { 237 | + tmp = p; 238 | + ngx_free(p->alloc); 239 | + p->prev->next = p->next; 240 | + p->next->prev = p->prev; 241 | + p = p->next; 242 | + ngx_free(tmp); 243 | } 244 | -} 245 | 246 | + ngx_free(pool->d->alloc); 247 | + pool->d->alloc = NULL; 248 | +} 249 | 250 | void * 251 | -ngx_palloc(ngx_pool_t *pool, size_t size) 252 | +ngx_malloc(ngx_pool_t *pool, size_t size) 253 | { 254 | - u_char *m; 255 | - ngx_pool_t *p; 256 | - 257 | - if (size <= pool->max) { 258 | - 259 | - p = pool->current; 260 | + ngx_pool_data_t *new; 261 | + void *m; 262 | 263 | - do { 264 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 265 | - 266 | - if ((size_t) (p->d.end - m) >= size) { 267 | - p->d.last = m + size; 268 | - 269 | - return m; 270 | - } 271 | - 272 | - p = p->d.next; 273 | - 274 | - } while (p); 275 | + m = ngx_alloc(size, pool->log); 276 | + if (m == NULL) { 277 | + return NULL; 278 | + } 279 | 280 | - return ngx_palloc_block(pool, size); 281 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 282 | + if (new == NULL){ 283 | + ngx_free(m); 284 | + return NULL; 285 | } 286 | 287 | - return ngx_palloc_large(pool, size); 288 | + new->alloc = m; 289 | + new->next = pool->d; 290 | + new->prev = pool->d->prev; 291 | + pool->d->prev->next = new; 292 | + pool->d->prev = new; 293 | + return m; 294 | } 295 | 296 | - 297 | void * 298 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 299 | +ngx_palloc(ngx_pool_t *pool, size_t size) 300 | { 301 | - u_char *m; 302 | - ngx_pool_t *p; 303 | - 304 | - if (size <= pool->max) { 305 | - 306 | - p = pool->current; 307 | - 308 | - do { 309 | - m = p->d.last; 310 | - 311 | - if ((size_t) (p->d.end - m) >= size) { 312 | - p->d.last = m + size; 313 | - 314 | - return m; 315 | - } 316 | - 317 | - p = p->d.next; 318 | - 319 | - } while (p); 320 | - 321 | - return ngx_palloc_block(pool, size); 322 | + if (size <= 1024) { 323 | + return ngx_malloc(pool, size); 324 | } 325 | 326 | return ngx_palloc_large(pool, size); 327 | } 328 | 329 | 330 | -static void * 331 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 332 | +void * 333 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 334 | { 335 | - u_char *m; 336 | - size_t psize; 337 | - ngx_pool_t *p, *new, *current; 338 | - 339 | - psize = (size_t) (pool->d.end - (u_char *) pool); 340 | - 341 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 342 | - if (m == NULL) { 343 | - return NULL; 344 | + if (size <= 1024) { 345 | + return ngx_malloc(pool, size); 346 | } 347 | 348 | - new = (ngx_pool_t *) m; 349 | - 350 | - new->d.end = m + psize; 351 | - new->d.next = NULL; 352 | - new->d.failed = 0; 353 | - 354 | - m += sizeof(ngx_pool_data_t); 355 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 356 | - new->d.last = m + size; 357 | - 358 | - current = pool->current; 359 | - 360 | - for (p = current; p->d.next; p = p->d.next) { 361 | - if (p->d.failed++ > 4) { 362 | - current = p->d.next; 363 | - } 364 | - } 365 | - 366 | - p->d.next = new; 367 | - 368 | - pool->current = current ? current : new; 369 | - 370 | - return m; 371 | + return ngx_palloc_large(pool, size); 372 | } 373 | 374 | 375 | @@ -226,18 +199,7 @@ 376 | 377 | n = 0; 378 | 379 | - for (large = pool->large; large; large = large->next) { 380 | - if (large->alloc == NULL) { 381 | - large->alloc = p; 382 | - return p; 383 | - } 384 | - 385 | - if (n++ > 3) { 386 | - break; 387 | - } 388 | - } 389 | - 390 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 391 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 392 | if (large == NULL) { 393 | ngx_free(p); 394 | return NULL; 395 | @@ -262,7 +224,7 @@ 396 | return NULL; 397 | } 398 | 399 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 400 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 401 | if (large == NULL) { 402 | ngx_free(p); 403 | return NULL; 404 | @@ -279,17 +241,41 @@ 405 | ngx_int_t 406 | ngx_pfree(ngx_pool_t *pool, void *p) 407 | { 408 | - ngx_pool_large_t *l; 409 | + ngx_pool_large_t *l; 410 | + ngx_pool_large_t *ll; 411 | + ngx_pool_data_t *d, *n; 412 | 413 | - for (l = pool->large; l; l = l->next) { 414 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 415 | if (p == l->alloc) { 416 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 417 | "free: %p", l->alloc); 418 | ngx_free(l->alloc); 419 | l->alloc = NULL; 420 | + if (l == pool->large) { 421 | + pool->large = NULL; 422 | + } 423 | + 424 | + ll->next = l->next; 425 | + p = l; 426 | + break; 427 | + } 428 | + } 429 | 430 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 431 | + if (p == d->alloc) { 432 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 433 | + if (d->alloc) { 434 | + ngx_free(d->alloc); 435 | + } 436 | + d->alloc = NULL; 437 | + d->prev->next = d->next; 438 | + d->next->prev = d->prev; 439 | + ngx_free(d); 440 | return NGX_OK; 441 | } 442 | + if (d->next == pool->d) { 443 | + break; 444 | + } 445 | } 446 | 447 | return NGX_DECLINED; 448 | Only in nginx-0.8.53-no-pool/src/core: ngx_palloc.c~ 449 | diff -ur nginx-0.8.53/src/core/ngx_palloc.h nginx-0.8.53-no-pool/src/core/ngx_palloc.h 450 | --- nginx-0.8.53/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 451 | +++ nginx-0.8.53-no-pool/src/core/ngx_palloc.h 2011-06-30 17:38:26.300946999 +0800 452 | @@ -38,6 +38,7 @@ 453 | 454 | 455 | typedef struct ngx_pool_large_s ngx_pool_large_t; 456 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 457 | 458 | struct ngx_pool_large_s { 459 | ngx_pool_large_t *next; 460 | @@ -45,16 +46,15 @@ 461 | }; 462 | 463 | 464 | -typedef struct { 465 | - u_char *last; 466 | - u_char *end; 467 | - ngx_pool_t *next; 468 | - ngx_uint_t failed; 469 | -} ngx_pool_data_t; 470 | +struct ngx_pool_data_s{ 471 | + ngx_pool_data_t *next; 472 | + ngx_pool_data_t *prev; 473 | + void *alloc; 474 | +}; 475 | 476 | 477 | struct ngx_pool_s { 478 | - ngx_pool_data_t d; 479 | + ngx_pool_data_t *d; 480 | size_t max; 481 | ngx_pool_t *current; 482 | ngx_chain_t *chain; 483 | -------------------------------------------------------------------------------- /nginx-0.8.54-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-0.8.54/src/core/nginx.h nginx-0.8.54-no-pool/src/core/nginx.h 2 | --- nginx-0.8.54/src/core/nginx.h 2010-12-14 18:33:37.000000000 +0800 3 | +++ nginx-0.8.54-no-pool/src/core/nginx.h 2011-06-30 17:15:35.410946999 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 8054 8 | -#define NGINX_VERSION "0.8.54" 9 | +#define NGINX_VERSION "0.8.54 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-0.8.54/src/core/ngx_array.c nginx-0.8.54-no-pool/src/core/ngx_array.c 14 | --- nginx-0.8.54/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-0.8.54-no-pool/src/core/ngx_array.c 2011-06-30 17:15:35.410946999 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -112,31 +94,16 @@ 72 | 73 | p = a->pool; 74 | 75 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 76 | - && p->d.last + size <= p->d.end) 77 | - { 78 | - /* 79 | - * the array allocation is the last in the pool 80 | - * and there is space for new allocation 81 | - */ 82 | - 83 | - p->d.last += size; 84 | - a->nalloc += n; 85 | - 86 | - } else { 87 | - /* allocate a new array */ 88 | - 89 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 90 | - 91 | - new = ngx_palloc(p, nalloc * a->size); 92 | - if (new == NULL) { 93 | - return NULL; 94 | - } 95 | - 96 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 97 | - a->elts = new; 98 | - a->nalloc = nalloc; 99 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 100 | + 101 | + new = ngx_palloc(p, nalloc * a->size); 102 | + if (new == NULL) { 103 | + return NULL; 104 | } 105 | + 106 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 107 | + a->elts = new; 108 | + a->nalloc = nalloc; 109 | } 110 | 111 | elt = (u_char *) a->elts + a->size * a->nelts; 112 | diff -ur nginx-0.8.54/src/core/ngx_palloc.c nginx-0.8.54-no-pool/src/core/ngx_palloc.c 113 | --- nginx-0.8.54/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 114 | +++ nginx-0.8.54-no-pool/src/core/ngx_palloc.c 2011-06-30 17:18:19.520946999 +0800 115 | @@ -8,24 +8,31 @@ 116 | #include 117 | 118 | 119 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 120 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 121 | 122 | 123 | ngx_pool_t * 124 | ngx_create_pool(size_t size, ngx_log_t *log) 125 | { 126 | - ngx_pool_t *p; 127 | + ngx_pool_t *p; 128 | + ngx_pool_data_t *d; 129 | 130 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 131 | + size = sizeof(ngx_pool_t); 132 | + p = ngx_alloc(size, log); 133 | if (p == NULL) { 134 | return NULL; 135 | } 136 | 137 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 138 | - p->d.end = (u_char *) p + size; 139 | - p->d.next = NULL; 140 | - p->d.failed = 0; 141 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 142 | + 143 | + if (d == NULL) { 144 | + return NULL; 145 | + } 146 | + 147 | + d->next = d; 148 | + d->prev = d; 149 | + d->alloc = NULL; 150 | + p->d = d; 151 | 152 | size = size - sizeof(ngx_pool_t); 153 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 154 | @@ -43,7 +50,7 @@ 155 | void 156 | ngx_destroy_pool(ngx_pool_t *pool) 157 | { 158 | - ngx_pool_t *p, *n; 159 | + ngx_pool_data_t *d, *n; 160 | ngx_pool_large_t *l; 161 | ngx_pool_cleanup_t *c; 162 | 163 | @@ -55,7 +62,7 @@ 164 | } 165 | } 166 | 167 | - for (l = pool->large; l; l = l->next) { 168 | + for (l = pool->large; l ; l = l->next) { 169 | 170 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 171 | 172 | @@ -71,34 +78,45 @@ 173 | * so we can not use this log while the free()ing the pool 174 | */ 175 | 176 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 177 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 178 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 179 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 180 | + "free: %p", d); 181 | 182 | - if (n == NULL) { 183 | + if (n == pool->d) { 184 | break; 185 | } 186 | } 187 | 188 | #endif 189 | + if (pool->d->next == pool->d) { 190 | + ngx_free(pool->d); 191 | + } else { 192 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 193 | + if (d->alloc) { 194 | + ngx_free(d->alloc); 195 | + } 196 | + ngx_free(d); 197 | 198 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 199 | - ngx_free(p); 200 | - 201 | - if (n == NULL) { 202 | - break; 203 | + if (n == pool->d) { 204 | + break; 205 | + } 206 | } 207 | } 208 | + 209 | + ngx_free(pool); 210 | } 211 | 212 | 213 | void 214 | ngx_reset_pool(ngx_pool_t *pool) 215 | { 216 | - ngx_pool_t *p; 217 | - ngx_pool_large_t *l; 218 | + ngx_pool_data_t *p, *tmp; 219 | + ngx_pool_large_t *l; 220 | + 221 | + for (l = pool->large; l ; l = l->next) { 222 | + 223 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 224 | 225 | - for (l = pool->large; l; l = l->next) { 226 | if (l->alloc) { 227 | ngx_free(l->alloc); 228 | } 229 | @@ -106,109 +124,64 @@ 230 | 231 | pool->large = NULL; 232 | 233 | - for (p = pool; p; p = p->d.next) { 234 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 235 | + p = pool->d->next; 236 | + while (p != pool->d) { 237 | + tmp = p; 238 | + ngx_free(p->alloc); 239 | + p->prev->next = p->next; 240 | + p->next->prev = p->prev; 241 | + p = p->next; 242 | + ngx_free(tmp); 243 | } 244 | -} 245 | 246 | + ngx_free(pool->d->alloc); 247 | + pool->d->alloc = NULL; 248 | +} 249 | 250 | void * 251 | -ngx_palloc(ngx_pool_t *pool, size_t size) 252 | +ngx_malloc(ngx_pool_t *pool, size_t size) 253 | { 254 | - u_char *m; 255 | - ngx_pool_t *p; 256 | - 257 | - if (size <= pool->max) { 258 | - 259 | - p = pool->current; 260 | + ngx_pool_data_t *new; 261 | + void *m; 262 | 263 | - do { 264 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 265 | - 266 | - if ((size_t) (p->d.end - m) >= size) { 267 | - p->d.last = m + size; 268 | - 269 | - return m; 270 | - } 271 | - 272 | - p = p->d.next; 273 | - 274 | - } while (p); 275 | + m = ngx_alloc(size, pool->log); 276 | + if (m == NULL) { 277 | + return NULL; 278 | + } 279 | 280 | - return ngx_palloc_block(pool, size); 281 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 282 | + if (new == NULL){ 283 | + ngx_free(m); 284 | + return NULL; 285 | } 286 | 287 | - return ngx_palloc_large(pool, size); 288 | + new->alloc = m; 289 | + new->next = pool->d; 290 | + new->prev = pool->d->prev; 291 | + pool->d->prev->next = new; 292 | + pool->d->prev = new; 293 | + return m; 294 | } 295 | 296 | - 297 | void * 298 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 299 | +ngx_palloc(ngx_pool_t *pool, size_t size) 300 | { 301 | - u_char *m; 302 | - ngx_pool_t *p; 303 | - 304 | - if (size <= pool->max) { 305 | - 306 | - p = pool->current; 307 | - 308 | - do { 309 | - m = p->d.last; 310 | - 311 | - if ((size_t) (p->d.end - m) >= size) { 312 | - p->d.last = m + size; 313 | - 314 | - return m; 315 | - } 316 | - 317 | - p = p->d.next; 318 | - 319 | - } while (p); 320 | - 321 | - return ngx_palloc_block(pool, size); 322 | + if (size <= 1024) { 323 | + return ngx_malloc(pool, size); 324 | } 325 | 326 | return ngx_palloc_large(pool, size); 327 | } 328 | 329 | 330 | -static void * 331 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 332 | +void * 333 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 334 | { 335 | - u_char *m; 336 | - size_t psize; 337 | - ngx_pool_t *p, *new, *current; 338 | - 339 | - psize = (size_t) (pool->d.end - (u_char *) pool); 340 | - 341 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 342 | - if (m == NULL) { 343 | - return NULL; 344 | + if (size <= 1024) { 345 | + return ngx_malloc(pool, size); 346 | } 347 | 348 | - new = (ngx_pool_t *) m; 349 | - 350 | - new->d.end = m + psize; 351 | - new->d.next = NULL; 352 | - new->d.failed = 0; 353 | - 354 | - m += sizeof(ngx_pool_data_t); 355 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 356 | - new->d.last = m + size; 357 | - 358 | - current = pool->current; 359 | - 360 | - for (p = current; p->d.next; p = p->d.next) { 361 | - if (p->d.failed++ > 4) { 362 | - current = p->d.next; 363 | - } 364 | - } 365 | - 366 | - p->d.next = new; 367 | - 368 | - pool->current = current ? current : new; 369 | - 370 | - return m; 371 | + return ngx_palloc_large(pool, size); 372 | } 373 | 374 | 375 | @@ -226,18 +199,7 @@ 376 | 377 | n = 0; 378 | 379 | - for (large = pool->large; large; large = large->next) { 380 | - if (large->alloc == NULL) { 381 | - large->alloc = p; 382 | - return p; 383 | - } 384 | - 385 | - if (n++ > 3) { 386 | - break; 387 | - } 388 | - } 389 | - 390 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 391 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 392 | if (large == NULL) { 393 | ngx_free(p); 394 | return NULL; 395 | @@ -262,7 +224,7 @@ 396 | return NULL; 397 | } 398 | 399 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 400 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 401 | if (large == NULL) { 402 | ngx_free(p); 403 | return NULL; 404 | @@ -279,17 +241,41 @@ 405 | ngx_int_t 406 | ngx_pfree(ngx_pool_t *pool, void *p) 407 | { 408 | - ngx_pool_large_t *l; 409 | + ngx_pool_large_t *l; 410 | + ngx_pool_large_t *ll; 411 | + ngx_pool_data_t *d, *n; 412 | 413 | - for (l = pool->large; l; l = l->next) { 414 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 415 | if (p == l->alloc) { 416 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 417 | "free: %p", l->alloc); 418 | ngx_free(l->alloc); 419 | l->alloc = NULL; 420 | + if (l == pool->large) { 421 | + pool->large = NULL; 422 | + } 423 | + 424 | + ll->next = l->next; 425 | + p = l; 426 | + break; 427 | + } 428 | + } 429 | 430 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 431 | + if (p == d->alloc) { 432 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 433 | + if (d->alloc) { 434 | + ngx_free(d->alloc); 435 | + } 436 | + d->alloc = NULL; 437 | + d->prev->next = d->next; 438 | + d->next->prev = d->prev; 439 | + ngx_free(d); 440 | return NGX_OK; 441 | } 442 | + if (d->next == pool->d) { 443 | + break; 444 | + } 445 | } 446 | 447 | return NGX_DECLINED; 448 | Only in nginx-0.8.54-no-pool/src/core: ngx_palloc.c~ 449 | diff -ur nginx-0.8.54/src/core/ngx_palloc.h nginx-0.8.54-no-pool/src/core/ngx_palloc.h 450 | --- nginx-0.8.54/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 451 | +++ nginx-0.8.54-no-pool/src/core/ngx_palloc.h 2011-06-30 17:15:35.410946999 +0800 452 | @@ -38,6 +38,7 @@ 453 | 454 | 455 | typedef struct ngx_pool_large_s ngx_pool_large_t; 456 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 457 | 458 | struct ngx_pool_large_s { 459 | ngx_pool_large_t *next; 460 | @@ -45,16 +46,15 @@ 461 | }; 462 | 463 | 464 | -typedef struct { 465 | - u_char *last; 466 | - u_char *end; 467 | - ngx_pool_t *next; 468 | - ngx_uint_t failed; 469 | -} ngx_pool_data_t; 470 | +struct ngx_pool_data_s{ 471 | + ngx_pool_data_t *next; 472 | + ngx_pool_data_t *prev; 473 | + void *alloc; 474 | +}; 475 | 476 | 477 | struct ngx_pool_s { 478 | - ngx_pool_data_t d; 479 | + ngx_pool_data_t *d; 480 | size_t max; 481 | ngx_pool_t *current; 482 | ngx_chain_t *chain; 483 | -------------------------------------------------------------------------------- /nginx-0.8.55-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-0.8.55/src/core/nginx.h nginx-0.8.55-no-pool/src/core/nginx.h 2 | --- nginx-0.8.55/src/core/nginx.h 2010-12-14 18:33:37.000000000 +0800 3 | +++ nginx-0.8.55-no-pool/src/core/nginx.h 2011-06-30 17:15:35.410946999 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 8055 8 | -#define NGINX_VERSION "0.8.55" 9 | +#define NGINX_VERSION "0.8.55 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-0.8.55/src/core/ngx_array.c nginx-0.8.55-no-pool/src/core/ngx_array.c 14 | --- nginx-0.8.55/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-0.8.55-no-pool/src/core/ngx_array.c 2011-06-30 17:15:35.410946999 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -112,31 +94,16 @@ 72 | 73 | p = a->pool; 74 | 75 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 76 | - && p->d.last + size <= p->d.end) 77 | - { 78 | - /* 79 | - * the array allocation is the last in the pool 80 | - * and there is space for new allocation 81 | - */ 82 | - 83 | - p->d.last += size; 84 | - a->nalloc += n; 85 | - 86 | - } else { 87 | - /* allocate a new array */ 88 | - 89 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 90 | - 91 | - new = ngx_palloc(p, nalloc * a->size); 92 | - if (new == NULL) { 93 | - return NULL; 94 | - } 95 | - 96 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 97 | - a->elts = new; 98 | - a->nalloc = nalloc; 99 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 100 | + 101 | + new = ngx_palloc(p, nalloc * a->size); 102 | + if (new == NULL) { 103 | + return NULL; 104 | } 105 | + 106 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 107 | + a->elts = new; 108 | + a->nalloc = nalloc; 109 | } 110 | 111 | elt = (u_char *) a->elts + a->size * a->nelts; 112 | diff -ur nginx-0.8.55/src/core/ngx_palloc.c nginx-0.8.55-no-pool/src/core/ngx_palloc.c 113 | --- nginx-0.8.55/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 114 | +++ nginx-0.8.55-no-pool/src/core/ngx_palloc.c 2011-06-30 17:18:19.520946999 +0800 115 | @@ -8,24 +8,31 @@ 116 | #include 117 | 118 | 119 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 120 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 121 | 122 | 123 | ngx_pool_t * 124 | ngx_create_pool(size_t size, ngx_log_t *log) 125 | { 126 | - ngx_pool_t *p; 127 | + ngx_pool_t *p; 128 | + ngx_pool_data_t *d; 129 | 130 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 131 | + size = sizeof(ngx_pool_t); 132 | + p = ngx_alloc(size, log); 133 | if (p == NULL) { 134 | return NULL; 135 | } 136 | 137 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 138 | - p->d.end = (u_char *) p + size; 139 | - p->d.next = NULL; 140 | - p->d.failed = 0; 141 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 142 | + 143 | + if (d == NULL) { 144 | + return NULL; 145 | + } 146 | + 147 | + d->next = d; 148 | + d->prev = d; 149 | + d->alloc = NULL; 150 | + p->d = d; 151 | 152 | size = size - sizeof(ngx_pool_t); 153 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 154 | @@ -43,7 +50,7 @@ 155 | void 156 | ngx_destroy_pool(ngx_pool_t *pool) 157 | { 158 | - ngx_pool_t *p, *n; 159 | + ngx_pool_data_t *d, *n; 160 | ngx_pool_large_t *l; 161 | ngx_pool_cleanup_t *c; 162 | 163 | @@ -55,7 +62,7 @@ 164 | } 165 | } 166 | 167 | - for (l = pool->large; l; l = l->next) { 168 | + for (l = pool->large; l ; l = l->next) { 169 | 170 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 171 | 172 | @@ -71,34 +78,45 @@ 173 | * so we can not use this log while the free()ing the pool 174 | */ 175 | 176 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 177 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 178 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 179 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 180 | + "free: %p", d); 181 | 182 | - if (n == NULL) { 183 | + if (n == pool->d) { 184 | break; 185 | } 186 | } 187 | 188 | #endif 189 | + if (pool->d->next == pool->d) { 190 | + ngx_free(pool->d); 191 | + } else { 192 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 193 | + if (d->alloc) { 194 | + ngx_free(d->alloc); 195 | + } 196 | + ngx_free(d); 197 | 198 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 199 | - ngx_free(p); 200 | - 201 | - if (n == NULL) { 202 | - break; 203 | + if (n == pool->d) { 204 | + break; 205 | + } 206 | } 207 | } 208 | + 209 | + ngx_free(pool); 210 | } 211 | 212 | 213 | void 214 | ngx_reset_pool(ngx_pool_t *pool) 215 | { 216 | - ngx_pool_t *p; 217 | - ngx_pool_large_t *l; 218 | + ngx_pool_data_t *p, *tmp; 219 | + ngx_pool_large_t *l; 220 | + 221 | + for (l = pool->large; l ; l = l->next) { 222 | + 223 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 224 | 225 | - for (l = pool->large; l; l = l->next) { 226 | if (l->alloc) { 227 | ngx_free(l->alloc); 228 | } 229 | @@ -106,109 +124,64 @@ 230 | 231 | pool->large = NULL; 232 | 233 | - for (p = pool; p; p = p->d.next) { 234 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 235 | + p = pool->d->next; 236 | + while (p != pool->d) { 237 | + tmp = p; 238 | + ngx_free(p->alloc); 239 | + p->prev->next = p->next; 240 | + p->next->prev = p->prev; 241 | + p = p->next; 242 | + ngx_free(tmp); 243 | } 244 | -} 245 | 246 | + ngx_free(pool->d->alloc); 247 | + pool->d->alloc = NULL; 248 | +} 249 | 250 | void * 251 | -ngx_palloc(ngx_pool_t *pool, size_t size) 252 | +ngx_malloc(ngx_pool_t *pool, size_t size) 253 | { 254 | - u_char *m; 255 | - ngx_pool_t *p; 256 | - 257 | - if (size <= pool->max) { 258 | - 259 | - p = pool->current; 260 | + ngx_pool_data_t *new; 261 | + void *m; 262 | 263 | - do { 264 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 265 | - 266 | - if ((size_t) (p->d.end - m) >= size) { 267 | - p->d.last = m + size; 268 | - 269 | - return m; 270 | - } 271 | - 272 | - p = p->d.next; 273 | - 274 | - } while (p); 275 | + m = ngx_alloc(size, pool->log); 276 | + if (m == NULL) { 277 | + return NULL; 278 | + } 279 | 280 | - return ngx_palloc_block(pool, size); 281 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 282 | + if (new == NULL){ 283 | + ngx_free(m); 284 | + return NULL; 285 | } 286 | 287 | - return ngx_palloc_large(pool, size); 288 | + new->alloc = m; 289 | + new->next = pool->d; 290 | + new->prev = pool->d->prev; 291 | + pool->d->prev->next = new; 292 | + pool->d->prev = new; 293 | + return m; 294 | } 295 | 296 | - 297 | void * 298 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 299 | +ngx_palloc(ngx_pool_t *pool, size_t size) 300 | { 301 | - u_char *m; 302 | - ngx_pool_t *p; 303 | - 304 | - if (size <= pool->max) { 305 | - 306 | - p = pool->current; 307 | - 308 | - do { 309 | - m = p->d.last; 310 | - 311 | - if ((size_t) (p->d.end - m) >= size) { 312 | - p->d.last = m + size; 313 | - 314 | - return m; 315 | - } 316 | - 317 | - p = p->d.next; 318 | - 319 | - } while (p); 320 | - 321 | - return ngx_palloc_block(pool, size); 322 | + if (size <= 1024) { 323 | + return ngx_malloc(pool, size); 324 | } 325 | 326 | return ngx_palloc_large(pool, size); 327 | } 328 | 329 | 330 | -static void * 331 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 332 | +void * 333 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 334 | { 335 | - u_char *m; 336 | - size_t psize; 337 | - ngx_pool_t *p, *new, *current; 338 | - 339 | - psize = (size_t) (pool->d.end - (u_char *) pool); 340 | - 341 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 342 | - if (m == NULL) { 343 | - return NULL; 344 | + if (size <= 1024) { 345 | + return ngx_malloc(pool, size); 346 | } 347 | 348 | - new = (ngx_pool_t *) m; 349 | - 350 | - new->d.end = m + psize; 351 | - new->d.next = NULL; 352 | - new->d.failed = 0; 353 | - 354 | - m += sizeof(ngx_pool_data_t); 355 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 356 | - new->d.last = m + size; 357 | - 358 | - current = pool->current; 359 | - 360 | - for (p = current; p->d.next; p = p->d.next) { 361 | - if (p->d.failed++ > 4) { 362 | - current = p->d.next; 363 | - } 364 | - } 365 | - 366 | - p->d.next = new; 367 | - 368 | - pool->current = current ? current : new; 369 | - 370 | - return m; 371 | + return ngx_palloc_large(pool, size); 372 | } 373 | 374 | 375 | @@ -226,18 +199,7 @@ 376 | 377 | n = 0; 378 | 379 | - for (large = pool->large; large; large = large->next) { 380 | - if (large->alloc == NULL) { 381 | - large->alloc = p; 382 | - return p; 383 | - } 384 | - 385 | - if (n++ > 3) { 386 | - break; 387 | - } 388 | - } 389 | - 390 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 391 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 392 | if (large == NULL) { 393 | ngx_free(p); 394 | return NULL; 395 | @@ -262,7 +224,7 @@ 396 | return NULL; 397 | } 398 | 399 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 400 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 401 | if (large == NULL) { 402 | ngx_free(p); 403 | return NULL; 404 | @@ -279,17 +241,41 @@ 405 | ngx_int_t 406 | ngx_pfree(ngx_pool_t *pool, void *p) 407 | { 408 | - ngx_pool_large_t *l; 409 | + ngx_pool_large_t *l; 410 | + ngx_pool_large_t *ll; 411 | + ngx_pool_data_t *d, *n; 412 | 413 | - for (l = pool->large; l; l = l->next) { 414 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 415 | if (p == l->alloc) { 416 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 417 | "free: %p", l->alloc); 418 | ngx_free(l->alloc); 419 | l->alloc = NULL; 420 | + if (l == pool->large) { 421 | + pool->large = NULL; 422 | + } 423 | + 424 | + ll->next = l->next; 425 | + p = l; 426 | + break; 427 | + } 428 | + } 429 | 430 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 431 | + if (p == d->alloc) { 432 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 433 | + if (d->alloc) { 434 | + ngx_free(d->alloc); 435 | + } 436 | + d->alloc = NULL; 437 | + d->prev->next = d->next; 438 | + d->next->prev = d->prev; 439 | + ngx_free(d); 440 | return NGX_OK; 441 | } 442 | + if (d->next == pool->d) { 443 | + break; 444 | + } 445 | } 446 | 447 | return NGX_DECLINED; 448 | Only in nginx-0.8.55-no-pool/src/core: ngx_palloc.c~ 449 | diff -ur nginx-0.8.55/src/core/ngx_palloc.h nginx-0.8.55-no-pool/src/core/ngx_palloc.h 450 | --- nginx-0.8.55/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 451 | +++ nginx-0.8.55-no-pool/src/core/ngx_palloc.h 2011-06-30 17:15:35.410946999 +0800 452 | @@ -38,6 +38,7 @@ 453 | 454 | 455 | typedef struct ngx_pool_large_s ngx_pool_large_t; 456 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 457 | 458 | struct ngx_pool_large_s { 459 | ngx_pool_large_t *next; 460 | @@ -45,16 +46,15 @@ 461 | }; 462 | 463 | 464 | -typedef struct { 465 | - u_char *last; 466 | - u_char *end; 467 | - ngx_pool_t *next; 468 | - ngx_uint_t failed; 469 | -} ngx_pool_data_t; 470 | +struct ngx_pool_data_s{ 471 | + ngx_pool_data_t *next; 472 | + ngx_pool_data_t *prev; 473 | + void *alloc; 474 | +}; 475 | 476 | 477 | struct ngx_pool_s { 478 | - ngx_pool_data_t d; 479 | + ngx_pool_data_t *d; 480 | size_t max; 481 | ngx_pool_t *current; 482 | ngx_chain_t *chain; 483 | -------------------------------------------------------------------------------- /nginx-1.0.6-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-1.0.6/src/core/nginx.h nginx-1.0.6-patched/src/core/nginx.h 2 | --- nginx-1.0.6/src/core/nginx.h 2011-08-29 17:30:22.000000000 +0800 3 | +++ nginx-1.0.6-patched/src/core/nginx.h 2011-09-13 12:11:03.135622101 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 1000006 8 | -#define NGINX_VERSION "1.0.6" 9 | +#define NGINX_VERSION "1.0.6 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-1.0.6/src/core/ngx_array.c nginx-1.0.6-patched/src/core/ngx_array.c 14 | --- nginx-1.0.6/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-1.0.6-patched/src/core/ngx_array.c 2011-09-14 12:02:56.263128538 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -100,43 +82,25 @@ 72 | ngx_array_push_n(ngx_array_t *a, ngx_uint_t n) 73 | { 74 | void *elt, *new; 75 | - size_t size; 76 | ngx_uint_t nalloc; 77 | ngx_pool_t *p; 78 | 79 | - size = n * a->size; 80 | - 81 | if (a->nelts + n > a->nalloc) { 82 | 83 | /* the array is full */ 84 | 85 | p = a->pool; 86 | 87 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 88 | - && p->d.last + size <= p->d.end) 89 | - { 90 | - /* 91 | - * the array allocation is the last in the pool 92 | - * and there is space for new allocation 93 | - */ 94 | - 95 | - p->d.last += size; 96 | - a->nalloc += n; 97 | - 98 | - } else { 99 | - /* allocate a new array */ 100 | - 101 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 102 | - 103 | - new = ngx_palloc(p, nalloc * a->size); 104 | - if (new == NULL) { 105 | - return NULL; 106 | - } 107 | - 108 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 109 | - a->elts = new; 110 | - a->nalloc = nalloc; 111 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 112 | + 113 | + new = ngx_palloc(p, nalloc * a->size); 114 | + if (new == NULL) { 115 | + return NULL; 116 | } 117 | + 118 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 119 | + a->elts = new; 120 | + a->nalloc = nalloc; 121 | } 122 | 123 | elt = (u_char *) a->elts + a->size * a->nelts; 124 | diff -ur nginx-1.0.6/src/core/ngx_palloc.c nginx-1.0.6-patched/src/core/ngx_palloc.c 125 | --- nginx-1.0.6/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 126 | +++ nginx-1.0.6-patched/src/core/ngx_palloc.c 2011-09-14 12:03:41.663126519 +0800 127 | @@ -8,24 +8,31 @@ 128 | #include 129 | 130 | 131 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 132 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 133 | 134 | 135 | ngx_pool_t * 136 | ngx_create_pool(size_t size, ngx_log_t *log) 137 | { 138 | - ngx_pool_t *p; 139 | + ngx_pool_t *p; 140 | + ngx_pool_data_t *d; 141 | 142 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 143 | + size = sizeof(ngx_pool_t); 144 | + p = ngx_alloc(size, log); 145 | if (p == NULL) { 146 | return NULL; 147 | } 148 | 149 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 150 | - p->d.end = (u_char *) p + size; 151 | - p->d.next = NULL; 152 | - p->d.failed = 0; 153 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 154 | + 155 | + if (d == NULL) { 156 | + return NULL; 157 | + } 158 | + 159 | + d->next = d; 160 | + d->prev = d; 161 | + d->alloc = NULL; 162 | + p->d = d; 163 | 164 | size = size - sizeof(ngx_pool_t); 165 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 166 | @@ -43,7 +50,7 @@ 167 | void 168 | ngx_destroy_pool(ngx_pool_t *pool) 169 | { 170 | - ngx_pool_t *p, *n; 171 | + ngx_pool_data_t *d, *n; 172 | ngx_pool_large_t *l; 173 | ngx_pool_cleanup_t *c; 174 | 175 | @@ -55,7 +62,7 @@ 176 | } 177 | } 178 | 179 | - for (l = pool->large; l; l = l->next) { 180 | + for (l = pool->large; l ; l = l->next) { 181 | 182 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 183 | 184 | @@ -71,34 +78,45 @@ 185 | * so we can not use this log while the free()ing the pool 186 | */ 187 | 188 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 189 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 190 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 191 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 192 | + "free: %p", d); 193 | 194 | - if (n == NULL) { 195 | + if (n == pool->d) { 196 | break; 197 | } 198 | } 199 | 200 | #endif 201 | + if (pool->d->next == pool->d) { 202 | + ngx_free(pool->d); 203 | + } else { 204 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 205 | + if (d->alloc) { 206 | + ngx_free(d->alloc); 207 | + } 208 | + ngx_free(d); 209 | 210 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 211 | - ngx_free(p); 212 | - 213 | - if (n == NULL) { 214 | - break; 215 | + if (n == pool->d) { 216 | + break; 217 | + } 218 | } 219 | } 220 | + 221 | + ngx_free(pool); 222 | } 223 | 224 | 225 | void 226 | ngx_reset_pool(ngx_pool_t *pool) 227 | { 228 | - ngx_pool_t *p; 229 | - ngx_pool_large_t *l; 230 | + ngx_pool_data_t *p, *tmp; 231 | + ngx_pool_large_t *l; 232 | + 233 | + for (l = pool->large; l ; l = l->next) { 234 | + 235 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 236 | 237 | - for (l = pool->large; l; l = l->next) { 238 | if (l->alloc) { 239 | ngx_free(l->alloc); 240 | } 241 | @@ -106,109 +124,65 @@ 242 | 243 | pool->large = NULL; 244 | 245 | - for (p = pool; p; p = p->d.next) { 246 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 247 | + p = pool->d->next; 248 | + while (p != pool->d) { 249 | + tmp = p; 250 | + ngx_free(p->alloc); 251 | + p->prev->next = p->next; 252 | + p->next->prev = p->prev; 253 | + p = p->next; 254 | + ngx_free(tmp); 255 | } 256 | -} 257 | 258 | + ngx_free(pool->d->alloc); 259 | + pool->d->alloc = NULL; 260 | + 261 | +} 262 | 263 | void * 264 | -ngx_palloc(ngx_pool_t *pool, size_t size) 265 | +ngx_malloc(ngx_pool_t *pool, size_t size) 266 | { 267 | - u_char *m; 268 | - ngx_pool_t *p; 269 | - 270 | - if (size <= pool->max) { 271 | + ngx_pool_data_t *new; 272 | + void *m; 273 | 274 | - p = pool->current; 275 | - 276 | - do { 277 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 278 | - 279 | - if ((size_t) (p->d.end - m) >= size) { 280 | - p->d.last = m + size; 281 | - 282 | - return m; 283 | - } 284 | - 285 | - p = p->d.next; 286 | - 287 | - } while (p); 288 | + m = ngx_alloc(size, pool->log); 289 | + if (m == NULL) { 290 | + return NULL; 291 | + } 292 | 293 | - return ngx_palloc_block(pool, size); 294 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 295 | + if (new == NULL){ 296 | + ngx_free(m); 297 | + return NULL; 298 | } 299 | 300 | - return ngx_palloc_large(pool, size); 301 | + new->alloc = m; 302 | + new->next = pool->d; 303 | + new->prev = pool->d->prev; 304 | + pool->d->prev->next = new; 305 | + pool->d->prev = new; 306 | + return m; 307 | } 308 | 309 | - 310 | void * 311 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 312 | +ngx_palloc(ngx_pool_t *pool, size_t size) 313 | { 314 | - u_char *m; 315 | - ngx_pool_t *p; 316 | - 317 | - if (size <= pool->max) { 318 | - 319 | - p = pool->current; 320 | - 321 | - do { 322 | - m = p->d.last; 323 | - 324 | - if ((size_t) (p->d.end - m) >= size) { 325 | - p->d.last = m + size; 326 | - 327 | - return m; 328 | - } 329 | - 330 | - p = p->d.next; 331 | - 332 | - } while (p); 333 | - 334 | - return ngx_palloc_block(pool, size); 335 | + if (size <= 1024) { 336 | + return ngx_malloc(pool, size); 337 | } 338 | 339 | return ngx_palloc_large(pool, size); 340 | } 341 | 342 | 343 | -static void * 344 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 345 | +void * 346 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 347 | { 348 | - u_char *m; 349 | - size_t psize; 350 | - ngx_pool_t *p, *new, *current; 351 | - 352 | - psize = (size_t) (pool->d.end - (u_char *) pool); 353 | - 354 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 355 | - if (m == NULL) { 356 | - return NULL; 357 | - } 358 | - 359 | - new = (ngx_pool_t *) m; 360 | - 361 | - new->d.end = m + psize; 362 | - new->d.next = NULL; 363 | - new->d.failed = 0; 364 | - 365 | - m += sizeof(ngx_pool_data_t); 366 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 367 | - new->d.last = m + size; 368 | - 369 | - current = pool->current; 370 | - 371 | - for (p = current; p->d.next; p = p->d.next) { 372 | - if (p->d.failed++ > 4) { 373 | - current = p->d.next; 374 | - } 375 | + if (size <= 1024) { 376 | + return ngx_malloc(pool, size); 377 | } 378 | 379 | - p->d.next = new; 380 | - 381 | - pool->current = current ? current : new; 382 | - 383 | - return m; 384 | + return ngx_palloc_large(pool, size); 385 | } 386 | 387 | 388 | @@ -216,7 +190,6 @@ 389 | ngx_palloc_large(ngx_pool_t *pool, size_t size) 390 | { 391 | void *p; 392 | - ngx_uint_t n; 393 | ngx_pool_large_t *large; 394 | 395 | p = ngx_alloc(size, pool->log); 396 | @@ -224,20 +197,7 @@ 397 | return NULL; 398 | } 399 | 400 | - n = 0; 401 | - 402 | - for (large = pool->large; large; large = large->next) { 403 | - if (large->alloc == NULL) { 404 | - large->alloc = p; 405 | - return p; 406 | - } 407 | - 408 | - if (n++ > 3) { 409 | - break; 410 | - } 411 | - } 412 | - 413 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 414 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 415 | if (large == NULL) { 416 | ngx_free(p); 417 | return NULL; 418 | @@ -262,7 +222,7 @@ 419 | return NULL; 420 | } 421 | 422 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 423 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 424 | if (large == NULL) { 425 | ngx_free(p); 426 | return NULL; 427 | @@ -279,17 +239,41 @@ 428 | ngx_int_t 429 | ngx_pfree(ngx_pool_t *pool, void *p) 430 | { 431 | - ngx_pool_large_t *l; 432 | + ngx_pool_large_t *l; 433 | + ngx_pool_large_t *ll; 434 | + ngx_pool_data_t *d, *n; 435 | 436 | - for (l = pool->large; l; l = l->next) { 437 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 438 | if (p == l->alloc) { 439 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 440 | "free: %p", l->alloc); 441 | ngx_free(l->alloc); 442 | l->alloc = NULL; 443 | + if (l == pool->large) { 444 | + pool->large = NULL; 445 | + } 446 | + 447 | + ll->next = l->next; 448 | + p = l; 449 | + break; 450 | + } 451 | + } 452 | 453 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 454 | + if (p == d->alloc) { 455 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 456 | + if (d->alloc) { 457 | + ngx_free(d->alloc); 458 | + } 459 | + d->alloc = NULL; 460 | + d->prev->next = d->next; 461 | + d->next->prev = d->prev; 462 | + ngx_free(d); 463 | return NGX_OK; 464 | } 465 | + if (d->next == pool->d) { 466 | + break; 467 | + } 468 | } 469 | 470 | return NGX_DECLINED; 471 | diff -ur nginx-1.0.6/src/core/ngx_palloc.h nginx-1.0.6-patched/src/core/ngx_palloc.h 472 | --- nginx-1.0.6/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 473 | +++ nginx-1.0.6-patched/src/core/ngx_palloc.h 2011-09-13 12:11:03.155622101 +0800 474 | @@ -38,6 +38,7 @@ 475 | 476 | 477 | typedef struct ngx_pool_large_s ngx_pool_large_t; 478 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 479 | 480 | struct ngx_pool_large_s { 481 | ngx_pool_large_t *next; 482 | @@ -45,16 +46,15 @@ 483 | }; 484 | 485 | 486 | -typedef struct { 487 | - u_char *last; 488 | - u_char *end; 489 | - ngx_pool_t *next; 490 | - ngx_uint_t failed; 491 | -} ngx_pool_data_t; 492 | +struct ngx_pool_data_s{ 493 | + ngx_pool_data_t *next; 494 | + ngx_pool_data_t *prev; 495 | + void *alloc; 496 | +}; 497 | 498 | 499 | struct ngx_pool_s { 500 | - ngx_pool_data_t d; 501 | + ngx_pool_data_t *d; 502 | size_t max; 503 | ngx_pool_t *current; 504 | ngx_chain_t *chain; 505 | -------------------------------------------------------------------------------- /nginx-1.0.8-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-1.0.8/src/core/nginx.h nginx-1.0.8-patched/src/core/nginx.h 2 | --- nginx-1.0.8/src/core/nginx.h 2011-08-29 17:30:22.000000000 +0800 3 | +++ nginx-1.0.8-patched/src/core/nginx.h 2011-09-13 12:11:03.135622101 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 1000008 8 | -#define NGINX_VERSION "1.0.8" 9 | +#define NGINX_VERSION "1.0.8 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-1.0.8/src/core/ngx_array.c nginx-1.0.8-patched/src/core/ngx_array.c 14 | --- nginx-1.0.8/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-1.0.8-patched/src/core/ngx_array.c 2011-09-14 12:02:56.263128538 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -100,43 +82,25 @@ 72 | ngx_array_push_n(ngx_array_t *a, ngx_uint_t n) 73 | { 74 | void *elt, *new; 75 | - size_t size; 76 | ngx_uint_t nalloc; 77 | ngx_pool_t *p; 78 | 79 | - size = n * a->size; 80 | - 81 | if (a->nelts + n > a->nalloc) { 82 | 83 | /* the array is full */ 84 | 85 | p = a->pool; 86 | 87 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 88 | - && p->d.last + size <= p->d.end) 89 | - { 90 | - /* 91 | - * the array allocation is the last in the pool 92 | - * and there is space for new allocation 93 | - */ 94 | - 95 | - p->d.last += size; 96 | - a->nalloc += n; 97 | - 98 | - } else { 99 | - /* allocate a new array */ 100 | - 101 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 102 | - 103 | - new = ngx_palloc(p, nalloc * a->size); 104 | - if (new == NULL) { 105 | - return NULL; 106 | - } 107 | - 108 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 109 | - a->elts = new; 110 | - a->nalloc = nalloc; 111 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 112 | + 113 | + new = ngx_palloc(p, nalloc * a->size); 114 | + if (new == NULL) { 115 | + return NULL; 116 | } 117 | + 118 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 119 | + a->elts = new; 120 | + a->nalloc = nalloc; 121 | } 122 | 123 | elt = (u_char *) a->elts + a->size * a->nelts; 124 | diff -ur nginx-1.0.8/src/core/ngx_palloc.c nginx-1.0.8-patched/src/core/ngx_palloc.c 125 | --- nginx-1.0.8/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 126 | +++ nginx-1.0.8-patched/src/core/ngx_palloc.c 2011-09-14 12:03:41.663126519 +0800 127 | @@ -8,24 +8,31 @@ 128 | #include 129 | 130 | 131 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 132 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 133 | 134 | 135 | ngx_pool_t * 136 | ngx_create_pool(size_t size, ngx_log_t *log) 137 | { 138 | - ngx_pool_t *p; 139 | + ngx_pool_t *p; 140 | + ngx_pool_data_t *d; 141 | 142 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 143 | + size = sizeof(ngx_pool_t); 144 | + p = ngx_alloc(size, log); 145 | if (p == NULL) { 146 | return NULL; 147 | } 148 | 149 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 150 | - p->d.end = (u_char *) p + size; 151 | - p->d.next = NULL; 152 | - p->d.failed = 0; 153 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 154 | + 155 | + if (d == NULL) { 156 | + return NULL; 157 | + } 158 | + 159 | + d->next = d; 160 | + d->prev = d; 161 | + d->alloc = NULL; 162 | + p->d = d; 163 | 164 | size = size - sizeof(ngx_pool_t); 165 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 166 | @@ -43,7 +50,7 @@ 167 | void 168 | ngx_destroy_pool(ngx_pool_t *pool) 169 | { 170 | - ngx_pool_t *p, *n; 171 | + ngx_pool_data_t *d, *n; 172 | ngx_pool_large_t *l; 173 | ngx_pool_cleanup_t *c; 174 | 175 | @@ -55,7 +62,7 @@ 176 | } 177 | } 178 | 179 | - for (l = pool->large; l; l = l->next) { 180 | + for (l = pool->large; l ; l = l->next) { 181 | 182 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 183 | 184 | @@ -71,34 +78,45 @@ 185 | * so we can not use this log while the free()ing the pool 186 | */ 187 | 188 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 189 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 190 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 191 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 192 | + "free: %p", d); 193 | 194 | - if (n == NULL) { 195 | + if (n == pool->d) { 196 | break; 197 | } 198 | } 199 | 200 | #endif 201 | + if (pool->d->next == pool->d) { 202 | + ngx_free(pool->d); 203 | + } else { 204 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 205 | + if (d->alloc) { 206 | + ngx_free(d->alloc); 207 | + } 208 | + ngx_free(d); 209 | 210 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 211 | - ngx_free(p); 212 | - 213 | - if (n == NULL) { 214 | - break; 215 | + if (n == pool->d) { 216 | + break; 217 | + } 218 | } 219 | } 220 | + 221 | + ngx_free(pool); 222 | } 223 | 224 | 225 | void 226 | ngx_reset_pool(ngx_pool_t *pool) 227 | { 228 | - ngx_pool_t *p; 229 | - ngx_pool_large_t *l; 230 | + ngx_pool_data_t *p, *tmp; 231 | + ngx_pool_large_t *l; 232 | + 233 | + for (l = pool->large; l ; l = l->next) { 234 | + 235 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 236 | 237 | - for (l = pool->large; l; l = l->next) { 238 | if (l->alloc) { 239 | ngx_free(l->alloc); 240 | } 241 | @@ -106,109 +124,65 @@ 242 | 243 | pool->large = NULL; 244 | 245 | - for (p = pool; p; p = p->d.next) { 246 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 247 | + p = pool->d->next; 248 | + while (p != pool->d) { 249 | + tmp = p; 250 | + ngx_free(p->alloc); 251 | + p->prev->next = p->next; 252 | + p->next->prev = p->prev; 253 | + p = p->next; 254 | + ngx_free(tmp); 255 | } 256 | -} 257 | 258 | + ngx_free(pool->d->alloc); 259 | + pool->d->alloc = NULL; 260 | + 261 | +} 262 | 263 | void * 264 | -ngx_palloc(ngx_pool_t *pool, size_t size) 265 | +ngx_malloc(ngx_pool_t *pool, size_t size) 266 | { 267 | - u_char *m; 268 | - ngx_pool_t *p; 269 | - 270 | - if (size <= pool->max) { 271 | + ngx_pool_data_t *new; 272 | + void *m; 273 | 274 | - p = pool->current; 275 | - 276 | - do { 277 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 278 | - 279 | - if ((size_t) (p->d.end - m) >= size) { 280 | - p->d.last = m + size; 281 | - 282 | - return m; 283 | - } 284 | - 285 | - p = p->d.next; 286 | - 287 | - } while (p); 288 | + m = ngx_alloc(size, pool->log); 289 | + if (m == NULL) { 290 | + return NULL; 291 | + } 292 | 293 | - return ngx_palloc_block(pool, size); 294 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 295 | + if (new == NULL){ 296 | + ngx_free(m); 297 | + return NULL; 298 | } 299 | 300 | - return ngx_palloc_large(pool, size); 301 | + new->alloc = m; 302 | + new->next = pool->d; 303 | + new->prev = pool->d->prev; 304 | + pool->d->prev->next = new; 305 | + pool->d->prev = new; 306 | + return m; 307 | } 308 | 309 | - 310 | void * 311 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 312 | +ngx_palloc(ngx_pool_t *pool, size_t size) 313 | { 314 | - u_char *m; 315 | - ngx_pool_t *p; 316 | - 317 | - if (size <= pool->max) { 318 | - 319 | - p = pool->current; 320 | - 321 | - do { 322 | - m = p->d.last; 323 | - 324 | - if ((size_t) (p->d.end - m) >= size) { 325 | - p->d.last = m + size; 326 | - 327 | - return m; 328 | - } 329 | - 330 | - p = p->d.next; 331 | - 332 | - } while (p); 333 | - 334 | - return ngx_palloc_block(pool, size); 335 | + if (size <= 1024) { 336 | + return ngx_malloc(pool, size); 337 | } 338 | 339 | return ngx_palloc_large(pool, size); 340 | } 341 | 342 | 343 | -static void * 344 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 345 | +void * 346 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 347 | { 348 | - u_char *m; 349 | - size_t psize; 350 | - ngx_pool_t *p, *new, *current; 351 | - 352 | - psize = (size_t) (pool->d.end - (u_char *) pool); 353 | - 354 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 355 | - if (m == NULL) { 356 | - return NULL; 357 | - } 358 | - 359 | - new = (ngx_pool_t *) m; 360 | - 361 | - new->d.end = m + psize; 362 | - new->d.next = NULL; 363 | - new->d.failed = 0; 364 | - 365 | - m += sizeof(ngx_pool_data_t); 366 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 367 | - new->d.last = m + size; 368 | - 369 | - current = pool->current; 370 | - 371 | - for (p = current; p->d.next; p = p->d.next) { 372 | - if (p->d.failed++ > 4) { 373 | - current = p->d.next; 374 | - } 375 | + if (size <= 1024) { 376 | + return ngx_malloc(pool, size); 377 | } 378 | 379 | - p->d.next = new; 380 | - 381 | - pool->current = current ? current : new; 382 | - 383 | - return m; 384 | + return ngx_palloc_large(pool, size); 385 | } 386 | 387 | 388 | @@ -216,7 +190,6 @@ 389 | ngx_palloc_large(ngx_pool_t *pool, size_t size) 390 | { 391 | void *p; 392 | - ngx_uint_t n; 393 | ngx_pool_large_t *large; 394 | 395 | p = ngx_alloc(size, pool->log); 396 | @@ -224,20 +197,7 @@ 397 | return NULL; 398 | } 399 | 400 | - n = 0; 401 | - 402 | - for (large = pool->large; large; large = large->next) { 403 | - if (large->alloc == NULL) { 404 | - large->alloc = p; 405 | - return p; 406 | - } 407 | - 408 | - if (n++ > 3) { 409 | - break; 410 | - } 411 | - } 412 | - 413 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 414 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 415 | if (large == NULL) { 416 | ngx_free(p); 417 | return NULL; 418 | @@ -262,7 +222,7 @@ 419 | return NULL; 420 | } 421 | 422 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 423 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 424 | if (large == NULL) { 425 | ngx_free(p); 426 | return NULL; 427 | @@ -279,17 +239,41 @@ 428 | ngx_int_t 429 | ngx_pfree(ngx_pool_t *pool, void *p) 430 | { 431 | - ngx_pool_large_t *l; 432 | + ngx_pool_large_t *l; 433 | + ngx_pool_large_t *ll; 434 | + ngx_pool_data_t *d, *n; 435 | 436 | - for (l = pool->large; l; l = l->next) { 437 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 438 | if (p == l->alloc) { 439 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 440 | "free: %p", l->alloc); 441 | ngx_free(l->alloc); 442 | l->alloc = NULL; 443 | + if (l == pool->large) { 444 | + pool->large = NULL; 445 | + } 446 | + 447 | + ll->next = l->next; 448 | + p = l; 449 | + break; 450 | + } 451 | + } 452 | 453 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 454 | + if (p == d->alloc) { 455 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 456 | + if (d->alloc) { 457 | + ngx_free(d->alloc); 458 | + } 459 | + d->alloc = NULL; 460 | + d->prev->next = d->next; 461 | + d->next->prev = d->prev; 462 | + ngx_free(d); 463 | return NGX_OK; 464 | } 465 | + if (d->next == pool->d) { 466 | + break; 467 | + } 468 | } 469 | 470 | return NGX_DECLINED; 471 | diff -ur nginx-1.0.8/src/core/ngx_palloc.h nginx-1.0.8-patched/src/core/ngx_palloc.h 472 | --- nginx-1.0.8/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 473 | +++ nginx-1.0.8-patched/src/core/ngx_palloc.h 2011-09-13 12:11:03.155622101 +0800 474 | @@ -38,6 +38,7 @@ 475 | 476 | 477 | typedef struct ngx_pool_large_s ngx_pool_large_t; 478 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 479 | 480 | struct ngx_pool_large_s { 481 | ngx_pool_large_t *next; 482 | @@ -45,16 +46,15 @@ 483 | }; 484 | 485 | 486 | -typedef struct { 487 | - u_char *last; 488 | - u_char *end; 489 | - ngx_pool_t *next; 490 | - ngx_uint_t failed; 491 | -} ngx_pool_data_t; 492 | +struct ngx_pool_data_s{ 493 | + ngx_pool_data_t *next; 494 | + ngx_pool_data_t *prev; 495 | + void *alloc; 496 | +}; 497 | 498 | 499 | struct ngx_pool_s { 500 | - ngx_pool_data_t d; 501 | + ngx_pool_data_t *d; 502 | size_t max; 503 | ngx_pool_t *current; 504 | ngx_chain_t *chain; 505 | -------------------------------------------------------------------------------- /nginx-1.0.9-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-1.0.9/src/core/nginx.h nginx-1.0.9-patched/src/core/nginx.h 2 | --- nginx-1.0.9/src/core/nginx.h 2011-08-29 17:30:22.000000000 +0800 3 | +++ nginx-1.0.9-patched/src/core/nginx.h 2011-09-13 12:11:03.135622101 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 1000009 8 | -#define NGINX_VERSION "1.0.9" 9 | +#define NGINX_VERSION "1.0.9 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-1.0.9/src/core/ngx_array.c nginx-1.0.9-patched/src/core/ngx_array.c 14 | --- nginx-1.0.9/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-1.0.9-patched/src/core/ngx_array.c 2011-09-14 12:02:56.263128538 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -100,43 +82,25 @@ 72 | ngx_array_push_n(ngx_array_t *a, ngx_uint_t n) 73 | { 74 | void *elt, *new; 75 | - size_t size; 76 | ngx_uint_t nalloc; 77 | ngx_pool_t *p; 78 | 79 | - size = n * a->size; 80 | - 81 | if (a->nelts + n > a->nalloc) { 82 | 83 | /* the array is full */ 84 | 85 | p = a->pool; 86 | 87 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 88 | - && p->d.last + size <= p->d.end) 89 | - { 90 | - /* 91 | - * the array allocation is the last in the pool 92 | - * and there is space for new allocation 93 | - */ 94 | - 95 | - p->d.last += size; 96 | - a->nalloc += n; 97 | - 98 | - } else { 99 | - /* allocate a new array */ 100 | - 101 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 102 | - 103 | - new = ngx_palloc(p, nalloc * a->size); 104 | - if (new == NULL) { 105 | - return NULL; 106 | - } 107 | - 108 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 109 | - a->elts = new; 110 | - a->nalloc = nalloc; 111 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 112 | + 113 | + new = ngx_palloc(p, nalloc * a->size); 114 | + if (new == NULL) { 115 | + return NULL; 116 | } 117 | + 118 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 119 | + a->elts = new; 120 | + a->nalloc = nalloc; 121 | } 122 | 123 | elt = (u_char *) a->elts + a->size * a->nelts; 124 | diff -ur nginx-1.0.9/src/core/ngx_palloc.c nginx-1.0.9-patched/src/core/ngx_palloc.c 125 | --- nginx-1.0.9/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 126 | +++ nginx-1.0.9-patched/src/core/ngx_palloc.c 2011-09-14 12:03:41.663126519 +0800 127 | @@ -8,24 +8,31 @@ 128 | #include 129 | 130 | 131 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 132 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 133 | 134 | 135 | ngx_pool_t * 136 | ngx_create_pool(size_t size, ngx_log_t *log) 137 | { 138 | - ngx_pool_t *p; 139 | + ngx_pool_t *p; 140 | + ngx_pool_data_t *d; 141 | 142 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 143 | + size = sizeof(ngx_pool_t); 144 | + p = ngx_alloc(size, log); 145 | if (p == NULL) { 146 | return NULL; 147 | } 148 | 149 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 150 | - p->d.end = (u_char *) p + size; 151 | - p->d.next = NULL; 152 | - p->d.failed = 0; 153 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 154 | + 155 | + if (d == NULL) { 156 | + return NULL; 157 | + } 158 | + 159 | + d->next = d; 160 | + d->prev = d; 161 | + d->alloc = NULL; 162 | + p->d = d; 163 | 164 | size = size - sizeof(ngx_pool_t); 165 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 166 | @@ -43,7 +50,7 @@ 167 | void 168 | ngx_destroy_pool(ngx_pool_t *pool) 169 | { 170 | - ngx_pool_t *p, *n; 171 | + ngx_pool_data_t *d, *n; 172 | ngx_pool_large_t *l; 173 | ngx_pool_cleanup_t *c; 174 | 175 | @@ -55,7 +62,7 @@ 176 | } 177 | } 178 | 179 | - for (l = pool->large; l; l = l->next) { 180 | + for (l = pool->large; l ; l = l->next) { 181 | 182 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 183 | 184 | @@ -71,34 +78,45 @@ 185 | * so we can not use this log while the free()ing the pool 186 | */ 187 | 188 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 189 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 190 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 191 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 192 | + "free: %p", d); 193 | 194 | - if (n == NULL) { 195 | + if (n == pool->d) { 196 | break; 197 | } 198 | } 199 | 200 | #endif 201 | + if (pool->d->next == pool->d) { 202 | + ngx_free(pool->d); 203 | + } else { 204 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 205 | + if (d->alloc) { 206 | + ngx_free(d->alloc); 207 | + } 208 | + ngx_free(d); 209 | 210 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 211 | - ngx_free(p); 212 | - 213 | - if (n == NULL) { 214 | - break; 215 | + if (n == pool->d) { 216 | + break; 217 | + } 218 | } 219 | } 220 | + 221 | + ngx_free(pool); 222 | } 223 | 224 | 225 | void 226 | ngx_reset_pool(ngx_pool_t *pool) 227 | { 228 | - ngx_pool_t *p; 229 | - ngx_pool_large_t *l; 230 | + ngx_pool_data_t *p, *tmp; 231 | + ngx_pool_large_t *l; 232 | + 233 | + for (l = pool->large; l ; l = l->next) { 234 | + 235 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 236 | 237 | - for (l = pool->large; l; l = l->next) { 238 | if (l->alloc) { 239 | ngx_free(l->alloc); 240 | } 241 | @@ -106,109 +124,65 @@ 242 | 243 | pool->large = NULL; 244 | 245 | - for (p = pool; p; p = p->d.next) { 246 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 247 | + p = pool->d->next; 248 | + while (p != pool->d) { 249 | + tmp = p; 250 | + ngx_free(p->alloc); 251 | + p->prev->next = p->next; 252 | + p->next->prev = p->prev; 253 | + p = p->next; 254 | + ngx_free(tmp); 255 | } 256 | -} 257 | 258 | + ngx_free(pool->d->alloc); 259 | + pool->d->alloc = NULL; 260 | + 261 | +} 262 | 263 | void * 264 | -ngx_palloc(ngx_pool_t *pool, size_t size) 265 | +ngx_malloc(ngx_pool_t *pool, size_t size) 266 | { 267 | - u_char *m; 268 | - ngx_pool_t *p; 269 | - 270 | - if (size <= pool->max) { 271 | + ngx_pool_data_t *new; 272 | + void *m; 273 | 274 | - p = pool->current; 275 | - 276 | - do { 277 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 278 | - 279 | - if ((size_t) (p->d.end - m) >= size) { 280 | - p->d.last = m + size; 281 | - 282 | - return m; 283 | - } 284 | - 285 | - p = p->d.next; 286 | - 287 | - } while (p); 288 | + m = ngx_alloc(size, pool->log); 289 | + if (m == NULL) { 290 | + return NULL; 291 | + } 292 | 293 | - return ngx_palloc_block(pool, size); 294 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 295 | + if (new == NULL){ 296 | + ngx_free(m); 297 | + return NULL; 298 | } 299 | 300 | - return ngx_palloc_large(pool, size); 301 | + new->alloc = m; 302 | + new->next = pool->d; 303 | + new->prev = pool->d->prev; 304 | + pool->d->prev->next = new; 305 | + pool->d->prev = new; 306 | + return m; 307 | } 308 | 309 | - 310 | void * 311 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 312 | +ngx_palloc(ngx_pool_t *pool, size_t size) 313 | { 314 | - u_char *m; 315 | - ngx_pool_t *p; 316 | - 317 | - if (size <= pool->max) { 318 | - 319 | - p = pool->current; 320 | - 321 | - do { 322 | - m = p->d.last; 323 | - 324 | - if ((size_t) (p->d.end - m) >= size) { 325 | - p->d.last = m + size; 326 | - 327 | - return m; 328 | - } 329 | - 330 | - p = p->d.next; 331 | - 332 | - } while (p); 333 | - 334 | - return ngx_palloc_block(pool, size); 335 | + if (size <= 1024) { 336 | + return ngx_malloc(pool, size); 337 | } 338 | 339 | return ngx_palloc_large(pool, size); 340 | } 341 | 342 | 343 | -static void * 344 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 345 | +void * 346 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 347 | { 348 | - u_char *m; 349 | - size_t psize; 350 | - ngx_pool_t *p, *new, *current; 351 | - 352 | - psize = (size_t) (pool->d.end - (u_char *) pool); 353 | - 354 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 355 | - if (m == NULL) { 356 | - return NULL; 357 | - } 358 | - 359 | - new = (ngx_pool_t *) m; 360 | - 361 | - new->d.end = m + psize; 362 | - new->d.next = NULL; 363 | - new->d.failed = 0; 364 | - 365 | - m += sizeof(ngx_pool_data_t); 366 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 367 | - new->d.last = m + size; 368 | - 369 | - current = pool->current; 370 | - 371 | - for (p = current; p->d.next; p = p->d.next) { 372 | - if (p->d.failed++ > 4) { 373 | - current = p->d.next; 374 | - } 375 | + if (size <= 1024) { 376 | + return ngx_malloc(pool, size); 377 | } 378 | 379 | - p->d.next = new; 380 | - 381 | - pool->current = current ? current : new; 382 | - 383 | - return m; 384 | + return ngx_palloc_large(pool, size); 385 | } 386 | 387 | 388 | @@ -216,7 +190,6 @@ 389 | ngx_palloc_large(ngx_pool_t *pool, size_t size) 390 | { 391 | void *p; 392 | - ngx_uint_t n; 393 | ngx_pool_large_t *large; 394 | 395 | p = ngx_alloc(size, pool->log); 396 | @@ -224,20 +197,7 @@ 397 | return NULL; 398 | } 399 | 400 | - n = 0; 401 | - 402 | - for (large = pool->large; large; large = large->next) { 403 | - if (large->alloc == NULL) { 404 | - large->alloc = p; 405 | - return p; 406 | - } 407 | - 408 | - if (n++ > 3) { 409 | - break; 410 | - } 411 | - } 412 | - 413 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 414 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 415 | if (large == NULL) { 416 | ngx_free(p); 417 | return NULL; 418 | @@ -262,7 +222,7 @@ 419 | return NULL; 420 | } 421 | 422 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 423 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 424 | if (large == NULL) { 425 | ngx_free(p); 426 | return NULL; 427 | @@ -279,17 +239,41 @@ 428 | ngx_int_t 429 | ngx_pfree(ngx_pool_t *pool, void *p) 430 | { 431 | - ngx_pool_large_t *l; 432 | + ngx_pool_large_t *l; 433 | + ngx_pool_large_t *ll; 434 | + ngx_pool_data_t *d, *n; 435 | 436 | - for (l = pool->large; l; l = l->next) { 437 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 438 | if (p == l->alloc) { 439 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 440 | "free: %p", l->alloc); 441 | ngx_free(l->alloc); 442 | l->alloc = NULL; 443 | + if (l == pool->large) { 444 | + pool->large = NULL; 445 | + } 446 | + 447 | + ll->next = l->next; 448 | + p = l; 449 | + break; 450 | + } 451 | + } 452 | 453 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 454 | + if (p == d->alloc) { 455 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 456 | + if (d->alloc) { 457 | + ngx_free(d->alloc); 458 | + } 459 | + d->alloc = NULL; 460 | + d->prev->next = d->next; 461 | + d->next->prev = d->prev; 462 | + ngx_free(d); 463 | return NGX_OK; 464 | } 465 | + if (d->next == pool->d) { 466 | + break; 467 | + } 468 | } 469 | 470 | return NGX_DECLINED; 471 | diff -ur nginx-1.0.9/src/core/ngx_palloc.h nginx-1.0.9-patched/src/core/ngx_palloc.h 472 | --- nginx-1.0.9/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 473 | +++ nginx-1.0.9-patched/src/core/ngx_palloc.h 2011-09-13 12:11:03.155622101 +0800 474 | @@ -38,6 +38,7 @@ 475 | 476 | 477 | typedef struct ngx_pool_large_s ngx_pool_large_t; 478 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 479 | 480 | struct ngx_pool_large_s { 481 | ngx_pool_large_t *next; 482 | @@ -45,16 +46,15 @@ 483 | }; 484 | 485 | 486 | -typedef struct { 487 | - u_char *last; 488 | - u_char *end; 489 | - ngx_pool_t *next; 490 | - ngx_uint_t failed; 491 | -} ngx_pool_data_t; 492 | +struct ngx_pool_data_s{ 493 | + ngx_pool_data_t *next; 494 | + ngx_pool_data_t *prev; 495 | + void *alloc; 496 | +}; 497 | 498 | 499 | struct ngx_pool_s { 500 | - ngx_pool_data_t d; 501 | + ngx_pool_data_t *d; 502 | size_t max; 503 | ngx_pool_t *current; 504 | ngx_chain_t *chain; 505 | -------------------------------------------------------------------------------- /nginx-1.1.4-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-1.1.4/src/core/nginx.h nginx-1.1.4-patched/src/core/nginx.h 2 | --- nginx-1.1.4/src/core/nginx.h 2011-08-29 17:30:22.000000000 +0800 3 | +++ nginx-1.1.4-patched/src/core/nginx.h 2011-09-13 12:11:03.135622101 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 1001004 8 | -#define NGINX_VERSION "1.1.4" 9 | +#define NGINX_VERSION "1.1.4 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-1.1.4/src/core/ngx_array.c nginx-1.1.4-patched/src/core/ngx_array.c 14 | --- nginx-1.1.4/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-1.1.4-patched/src/core/ngx_array.c 2011-09-14 12:02:56.263128538 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -100,43 +82,25 @@ 72 | ngx_array_push_n(ngx_array_t *a, ngx_uint_t n) 73 | { 74 | void *elt, *new; 75 | - size_t size; 76 | ngx_uint_t nalloc; 77 | ngx_pool_t *p; 78 | 79 | - size = n * a->size; 80 | - 81 | if (a->nelts + n > a->nalloc) { 82 | 83 | /* the array is full */ 84 | 85 | p = a->pool; 86 | 87 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 88 | - && p->d.last + size <= p->d.end) 89 | - { 90 | - /* 91 | - * the array allocation is the last in the pool 92 | - * and there is space for new allocation 93 | - */ 94 | - 95 | - p->d.last += size; 96 | - a->nalloc += n; 97 | - 98 | - } else { 99 | - /* allocate a new array */ 100 | - 101 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 102 | - 103 | - new = ngx_palloc(p, nalloc * a->size); 104 | - if (new == NULL) { 105 | - return NULL; 106 | - } 107 | - 108 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 109 | - a->elts = new; 110 | - a->nalloc = nalloc; 111 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 112 | + 113 | + new = ngx_palloc(p, nalloc * a->size); 114 | + if (new == NULL) { 115 | + return NULL; 116 | } 117 | + 118 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 119 | + a->elts = new; 120 | + a->nalloc = nalloc; 121 | } 122 | 123 | elt = (u_char *) a->elts + a->size * a->nelts; 124 | diff -ur nginx-1.1.4/src/core/ngx_palloc.c nginx-1.1.4-patched/src/core/ngx_palloc.c 125 | --- nginx-1.1.4/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 126 | +++ nginx-1.1.4-patched/src/core/ngx_palloc.c 2011-09-14 12:03:41.663126519 +0800 127 | @@ -8,24 +8,31 @@ 128 | #include 129 | 130 | 131 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 132 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 133 | 134 | 135 | ngx_pool_t * 136 | ngx_create_pool(size_t size, ngx_log_t *log) 137 | { 138 | - ngx_pool_t *p; 139 | + ngx_pool_t *p; 140 | + ngx_pool_data_t *d; 141 | 142 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 143 | + size = sizeof(ngx_pool_t); 144 | + p = ngx_alloc(size, log); 145 | if (p == NULL) { 146 | return NULL; 147 | } 148 | 149 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 150 | - p->d.end = (u_char *) p + size; 151 | - p->d.next = NULL; 152 | - p->d.failed = 0; 153 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 154 | + 155 | + if (d == NULL) { 156 | + return NULL; 157 | + } 158 | + 159 | + d->next = d; 160 | + d->prev = d; 161 | + d->alloc = NULL; 162 | + p->d = d; 163 | 164 | size = size - sizeof(ngx_pool_t); 165 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 166 | @@ -43,7 +50,7 @@ 167 | void 168 | ngx_destroy_pool(ngx_pool_t *pool) 169 | { 170 | - ngx_pool_t *p, *n; 171 | + ngx_pool_data_t *d, *n; 172 | ngx_pool_large_t *l; 173 | ngx_pool_cleanup_t *c; 174 | 175 | @@ -55,7 +62,7 @@ 176 | } 177 | } 178 | 179 | - for (l = pool->large; l; l = l->next) { 180 | + for (l = pool->large; l ; l = l->next) { 181 | 182 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 183 | 184 | @@ -71,34 +78,45 @@ 185 | * so we can not use this log while the free()ing the pool 186 | */ 187 | 188 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 189 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 190 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 191 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 192 | + "free: %p", d); 193 | 194 | - if (n == NULL) { 195 | + if (n == pool->d) { 196 | break; 197 | } 198 | } 199 | 200 | #endif 201 | + if (pool->d->next == pool->d) { 202 | + ngx_free(pool->d); 203 | + } else { 204 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 205 | + if (d->alloc) { 206 | + ngx_free(d->alloc); 207 | + } 208 | + ngx_free(d); 209 | 210 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 211 | - ngx_free(p); 212 | - 213 | - if (n == NULL) { 214 | - break; 215 | + if (n == pool->d) { 216 | + break; 217 | + } 218 | } 219 | } 220 | + 221 | + ngx_free(pool); 222 | } 223 | 224 | 225 | void 226 | ngx_reset_pool(ngx_pool_t *pool) 227 | { 228 | - ngx_pool_t *p; 229 | - ngx_pool_large_t *l; 230 | + ngx_pool_data_t *p, *tmp; 231 | + ngx_pool_large_t *l; 232 | + 233 | + for (l = pool->large; l ; l = l->next) { 234 | + 235 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 236 | 237 | - for (l = pool->large; l; l = l->next) { 238 | if (l->alloc) { 239 | ngx_free(l->alloc); 240 | } 241 | @@ -106,109 +124,65 @@ 242 | 243 | pool->large = NULL; 244 | 245 | - for (p = pool; p; p = p->d.next) { 246 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 247 | + p = pool->d->next; 248 | + while (p != pool->d) { 249 | + tmp = p; 250 | + ngx_free(p->alloc); 251 | + p->prev->next = p->next; 252 | + p->next->prev = p->prev; 253 | + p = p->next; 254 | + ngx_free(tmp); 255 | } 256 | -} 257 | 258 | + ngx_free(pool->d->alloc); 259 | + pool->d->alloc = NULL; 260 | + 261 | +} 262 | 263 | void * 264 | -ngx_palloc(ngx_pool_t *pool, size_t size) 265 | +ngx_malloc(ngx_pool_t *pool, size_t size) 266 | { 267 | - u_char *m; 268 | - ngx_pool_t *p; 269 | - 270 | - if (size <= pool->max) { 271 | + ngx_pool_data_t *new; 272 | + void *m; 273 | 274 | - p = pool->current; 275 | - 276 | - do { 277 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 278 | - 279 | - if ((size_t) (p->d.end - m) >= size) { 280 | - p->d.last = m + size; 281 | - 282 | - return m; 283 | - } 284 | - 285 | - p = p->d.next; 286 | - 287 | - } while (p); 288 | + m = ngx_alloc(size, pool->log); 289 | + if (m == NULL) { 290 | + return NULL; 291 | + } 292 | 293 | - return ngx_palloc_block(pool, size); 294 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 295 | + if (new == NULL){ 296 | + ngx_free(m); 297 | + return NULL; 298 | } 299 | 300 | - return ngx_palloc_large(pool, size); 301 | + new->alloc = m; 302 | + new->next = pool->d; 303 | + new->prev = pool->d->prev; 304 | + pool->d->prev->next = new; 305 | + pool->d->prev = new; 306 | + return m; 307 | } 308 | 309 | - 310 | void * 311 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 312 | +ngx_palloc(ngx_pool_t *pool, size_t size) 313 | { 314 | - u_char *m; 315 | - ngx_pool_t *p; 316 | - 317 | - if (size <= pool->max) { 318 | - 319 | - p = pool->current; 320 | - 321 | - do { 322 | - m = p->d.last; 323 | - 324 | - if ((size_t) (p->d.end - m) >= size) { 325 | - p->d.last = m + size; 326 | - 327 | - return m; 328 | - } 329 | - 330 | - p = p->d.next; 331 | - 332 | - } while (p); 333 | - 334 | - return ngx_palloc_block(pool, size); 335 | + if (size <= 1024) { 336 | + return ngx_malloc(pool, size); 337 | } 338 | 339 | return ngx_palloc_large(pool, size); 340 | } 341 | 342 | 343 | -static void * 344 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 345 | +void * 346 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 347 | { 348 | - u_char *m; 349 | - size_t psize; 350 | - ngx_pool_t *p, *new, *current; 351 | - 352 | - psize = (size_t) (pool->d.end - (u_char *) pool); 353 | - 354 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 355 | - if (m == NULL) { 356 | - return NULL; 357 | - } 358 | - 359 | - new = (ngx_pool_t *) m; 360 | - 361 | - new->d.end = m + psize; 362 | - new->d.next = NULL; 363 | - new->d.failed = 0; 364 | - 365 | - m += sizeof(ngx_pool_data_t); 366 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 367 | - new->d.last = m + size; 368 | - 369 | - current = pool->current; 370 | - 371 | - for (p = current; p->d.next; p = p->d.next) { 372 | - if (p->d.failed++ > 4) { 373 | - current = p->d.next; 374 | - } 375 | + if (size <= 1024) { 376 | + return ngx_malloc(pool, size); 377 | } 378 | 379 | - p->d.next = new; 380 | - 381 | - pool->current = current ? current : new; 382 | - 383 | - return m; 384 | + return ngx_palloc_large(pool, size); 385 | } 386 | 387 | 388 | @@ -216,7 +190,6 @@ 389 | ngx_palloc_large(ngx_pool_t *pool, size_t size) 390 | { 391 | void *p; 392 | - ngx_uint_t n; 393 | ngx_pool_large_t *large; 394 | 395 | p = ngx_alloc(size, pool->log); 396 | @@ -224,20 +197,7 @@ 397 | return NULL; 398 | } 399 | 400 | - n = 0; 401 | - 402 | - for (large = pool->large; large; large = large->next) { 403 | - if (large->alloc == NULL) { 404 | - large->alloc = p; 405 | - return p; 406 | - } 407 | - 408 | - if (n++ > 3) { 409 | - break; 410 | - } 411 | - } 412 | - 413 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 414 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 415 | if (large == NULL) { 416 | ngx_free(p); 417 | return NULL; 418 | @@ -262,7 +222,7 @@ 419 | return NULL; 420 | } 421 | 422 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 423 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 424 | if (large == NULL) { 425 | ngx_free(p); 426 | return NULL; 427 | @@ -279,17 +239,41 @@ 428 | ngx_int_t 429 | ngx_pfree(ngx_pool_t *pool, void *p) 430 | { 431 | - ngx_pool_large_t *l; 432 | + ngx_pool_large_t *l; 433 | + ngx_pool_large_t *ll; 434 | + ngx_pool_data_t *d, *n; 435 | 436 | - for (l = pool->large; l; l = l->next) { 437 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 438 | if (p == l->alloc) { 439 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 440 | "free: %p", l->alloc); 441 | ngx_free(l->alloc); 442 | l->alloc = NULL; 443 | + if (l == pool->large) { 444 | + pool->large = NULL; 445 | + } 446 | + 447 | + ll->next = l->next; 448 | + p = l; 449 | + break; 450 | + } 451 | + } 452 | 453 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 454 | + if (p == d->alloc) { 455 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 456 | + if (d->alloc) { 457 | + ngx_free(d->alloc); 458 | + } 459 | + d->alloc = NULL; 460 | + d->prev->next = d->next; 461 | + d->next->prev = d->prev; 462 | + ngx_free(d); 463 | return NGX_OK; 464 | } 465 | + if (d->next == pool->d) { 466 | + break; 467 | + } 468 | } 469 | 470 | return NGX_DECLINED; 471 | diff -ur nginx-1.1.4/src/core/ngx_palloc.h nginx-1.1.4-patched/src/core/ngx_palloc.h 472 | --- nginx-1.1.4/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 473 | +++ nginx-1.1.4-patched/src/core/ngx_palloc.h 2011-09-13 12:11:03.155622101 +0800 474 | @@ -38,6 +38,7 @@ 475 | 476 | 477 | typedef struct ngx_pool_large_s ngx_pool_large_t; 478 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 479 | 480 | struct ngx_pool_large_s { 481 | ngx_pool_large_t *next; 482 | @@ -45,16 +46,15 @@ 483 | }; 484 | 485 | 486 | -typedef struct { 487 | - u_char *last; 488 | - u_char *end; 489 | - ngx_pool_t *next; 490 | - ngx_uint_t failed; 491 | -} ngx_pool_data_t; 492 | +struct ngx_pool_data_s{ 493 | + ngx_pool_data_t *next; 494 | + ngx_pool_data_t *prev; 495 | + void *alloc; 496 | +}; 497 | 498 | 499 | struct ngx_pool_s { 500 | - ngx_pool_data_t d; 501 | + ngx_pool_data_t *d; 502 | size_t max; 503 | ngx_pool_t *current; 504 | ngx_chain_t *chain; 505 | -------------------------------------------------------------------------------- /nginx-1.1.5-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-1.1.5/src/core/nginx.h nginx-1.1.5-patched/src/core/nginx.h 2 | --- nginx-1.1.5/src/core/nginx.h 2011-08-29 17:30:22.000000000 +0800 3 | +++ nginx-1.1.5-patched/src/core/nginx.h 2011-09-13 12:11:03.135622101 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 1001005 8 | -#define NGINX_VERSION "1.1.5" 9 | +#define NGINX_VERSION "1.1.5 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-1.1.5/src/core/ngx_array.c nginx-1.1.5-patched/src/core/ngx_array.c 14 | --- nginx-1.1.5/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-1.1.5-patched/src/core/ngx_array.c 2011-09-14 12:02:56.263128538 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -100,43 +82,25 @@ 72 | ngx_array_push_n(ngx_array_t *a, ngx_uint_t n) 73 | { 74 | void *elt, *new; 75 | - size_t size; 76 | ngx_uint_t nalloc; 77 | ngx_pool_t *p; 78 | 79 | - size = n * a->size; 80 | - 81 | if (a->nelts + n > a->nalloc) { 82 | 83 | /* the array is full */ 84 | 85 | p = a->pool; 86 | 87 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 88 | - && p->d.last + size <= p->d.end) 89 | - { 90 | - /* 91 | - * the array allocation is the last in the pool 92 | - * and there is space for new allocation 93 | - */ 94 | - 95 | - p->d.last += size; 96 | - a->nalloc += n; 97 | - 98 | - } else { 99 | - /* allocate a new array */ 100 | - 101 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 102 | - 103 | - new = ngx_palloc(p, nalloc * a->size); 104 | - if (new == NULL) { 105 | - return NULL; 106 | - } 107 | - 108 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 109 | - a->elts = new; 110 | - a->nalloc = nalloc; 111 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 112 | + 113 | + new = ngx_palloc(p, nalloc * a->size); 114 | + if (new == NULL) { 115 | + return NULL; 116 | } 117 | + 118 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 119 | + a->elts = new; 120 | + a->nalloc = nalloc; 121 | } 122 | 123 | elt = (u_char *) a->elts + a->size * a->nelts; 124 | diff -ur nginx-1.1.5/src/core/ngx_palloc.c nginx-1.1.5-patched/src/core/ngx_palloc.c 125 | --- nginx-1.1.5/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 126 | +++ nginx-1.1.5-patched/src/core/ngx_palloc.c 2011-09-14 12:03:41.663126519 +0800 127 | @@ -8,24 +8,31 @@ 128 | #include 129 | 130 | 131 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 132 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 133 | 134 | 135 | ngx_pool_t * 136 | ngx_create_pool(size_t size, ngx_log_t *log) 137 | { 138 | - ngx_pool_t *p; 139 | + ngx_pool_t *p; 140 | + ngx_pool_data_t *d; 141 | 142 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 143 | + size = sizeof(ngx_pool_t); 144 | + p = ngx_alloc(size, log); 145 | if (p == NULL) { 146 | return NULL; 147 | } 148 | 149 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 150 | - p->d.end = (u_char *) p + size; 151 | - p->d.next = NULL; 152 | - p->d.failed = 0; 153 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 154 | + 155 | + if (d == NULL) { 156 | + return NULL; 157 | + } 158 | + 159 | + d->next = d; 160 | + d->prev = d; 161 | + d->alloc = NULL; 162 | + p->d = d; 163 | 164 | size = size - sizeof(ngx_pool_t); 165 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 166 | @@ -43,7 +50,7 @@ 167 | void 168 | ngx_destroy_pool(ngx_pool_t *pool) 169 | { 170 | - ngx_pool_t *p, *n; 171 | + ngx_pool_data_t *d, *n; 172 | ngx_pool_large_t *l; 173 | ngx_pool_cleanup_t *c; 174 | 175 | @@ -55,7 +62,7 @@ 176 | } 177 | } 178 | 179 | - for (l = pool->large; l; l = l->next) { 180 | + for (l = pool->large; l ; l = l->next) { 181 | 182 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 183 | 184 | @@ -71,34 +78,45 @@ 185 | * so we can not use this log while the free()ing the pool 186 | */ 187 | 188 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 189 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 190 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 191 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 192 | + "free: %p", d); 193 | 194 | - if (n == NULL) { 195 | + if (n == pool->d) { 196 | break; 197 | } 198 | } 199 | 200 | #endif 201 | + if (pool->d->next == pool->d) { 202 | + ngx_free(pool->d); 203 | + } else { 204 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 205 | + if (d->alloc) { 206 | + ngx_free(d->alloc); 207 | + } 208 | + ngx_free(d); 209 | 210 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 211 | - ngx_free(p); 212 | - 213 | - if (n == NULL) { 214 | - break; 215 | + if (n == pool->d) { 216 | + break; 217 | + } 218 | } 219 | } 220 | + 221 | + ngx_free(pool); 222 | } 223 | 224 | 225 | void 226 | ngx_reset_pool(ngx_pool_t *pool) 227 | { 228 | - ngx_pool_t *p; 229 | - ngx_pool_large_t *l; 230 | + ngx_pool_data_t *p, *tmp; 231 | + ngx_pool_large_t *l; 232 | + 233 | + for (l = pool->large; l ; l = l->next) { 234 | + 235 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 236 | 237 | - for (l = pool->large; l; l = l->next) { 238 | if (l->alloc) { 239 | ngx_free(l->alloc); 240 | } 241 | @@ -106,109 +124,65 @@ 242 | 243 | pool->large = NULL; 244 | 245 | - for (p = pool; p; p = p->d.next) { 246 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 247 | + p = pool->d->next; 248 | + while (p != pool->d) { 249 | + tmp = p; 250 | + ngx_free(p->alloc); 251 | + p->prev->next = p->next; 252 | + p->next->prev = p->prev; 253 | + p = p->next; 254 | + ngx_free(tmp); 255 | } 256 | -} 257 | 258 | + ngx_free(pool->d->alloc); 259 | + pool->d->alloc = NULL; 260 | + 261 | +} 262 | 263 | void * 264 | -ngx_palloc(ngx_pool_t *pool, size_t size) 265 | +ngx_malloc(ngx_pool_t *pool, size_t size) 266 | { 267 | - u_char *m; 268 | - ngx_pool_t *p; 269 | - 270 | - if (size <= pool->max) { 271 | + ngx_pool_data_t *new; 272 | + void *m; 273 | 274 | - p = pool->current; 275 | - 276 | - do { 277 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 278 | - 279 | - if ((size_t) (p->d.end - m) >= size) { 280 | - p->d.last = m + size; 281 | - 282 | - return m; 283 | - } 284 | - 285 | - p = p->d.next; 286 | - 287 | - } while (p); 288 | + m = ngx_alloc(size, pool->log); 289 | + if (m == NULL) { 290 | + return NULL; 291 | + } 292 | 293 | - return ngx_palloc_block(pool, size); 294 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 295 | + if (new == NULL){ 296 | + ngx_free(m); 297 | + return NULL; 298 | } 299 | 300 | - return ngx_palloc_large(pool, size); 301 | + new->alloc = m; 302 | + new->next = pool->d; 303 | + new->prev = pool->d->prev; 304 | + pool->d->prev->next = new; 305 | + pool->d->prev = new; 306 | + return m; 307 | } 308 | 309 | - 310 | void * 311 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 312 | +ngx_palloc(ngx_pool_t *pool, size_t size) 313 | { 314 | - u_char *m; 315 | - ngx_pool_t *p; 316 | - 317 | - if (size <= pool->max) { 318 | - 319 | - p = pool->current; 320 | - 321 | - do { 322 | - m = p->d.last; 323 | - 324 | - if ((size_t) (p->d.end - m) >= size) { 325 | - p->d.last = m + size; 326 | - 327 | - return m; 328 | - } 329 | - 330 | - p = p->d.next; 331 | - 332 | - } while (p); 333 | - 334 | - return ngx_palloc_block(pool, size); 335 | + if (size <= 1024) { 336 | + return ngx_malloc(pool, size); 337 | } 338 | 339 | return ngx_palloc_large(pool, size); 340 | } 341 | 342 | 343 | -static void * 344 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 345 | +void * 346 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 347 | { 348 | - u_char *m; 349 | - size_t psize; 350 | - ngx_pool_t *p, *new, *current; 351 | - 352 | - psize = (size_t) (pool->d.end - (u_char *) pool); 353 | - 354 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 355 | - if (m == NULL) { 356 | - return NULL; 357 | - } 358 | - 359 | - new = (ngx_pool_t *) m; 360 | - 361 | - new->d.end = m + psize; 362 | - new->d.next = NULL; 363 | - new->d.failed = 0; 364 | - 365 | - m += sizeof(ngx_pool_data_t); 366 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 367 | - new->d.last = m + size; 368 | - 369 | - current = pool->current; 370 | - 371 | - for (p = current; p->d.next; p = p->d.next) { 372 | - if (p->d.failed++ > 4) { 373 | - current = p->d.next; 374 | - } 375 | + if (size <= 1024) { 376 | + return ngx_malloc(pool, size); 377 | } 378 | 379 | - p->d.next = new; 380 | - 381 | - pool->current = current ? current : new; 382 | - 383 | - return m; 384 | + return ngx_palloc_large(pool, size); 385 | } 386 | 387 | 388 | @@ -216,7 +190,6 @@ 389 | ngx_palloc_large(ngx_pool_t *pool, size_t size) 390 | { 391 | void *p; 392 | - ngx_uint_t n; 393 | ngx_pool_large_t *large; 394 | 395 | p = ngx_alloc(size, pool->log); 396 | @@ -224,20 +197,7 @@ 397 | return NULL; 398 | } 399 | 400 | - n = 0; 401 | - 402 | - for (large = pool->large; large; large = large->next) { 403 | - if (large->alloc == NULL) { 404 | - large->alloc = p; 405 | - return p; 406 | - } 407 | - 408 | - if (n++ > 3) { 409 | - break; 410 | - } 411 | - } 412 | - 413 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 414 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 415 | if (large == NULL) { 416 | ngx_free(p); 417 | return NULL; 418 | @@ -262,7 +222,7 @@ 419 | return NULL; 420 | } 421 | 422 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 423 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 424 | if (large == NULL) { 425 | ngx_free(p); 426 | return NULL; 427 | @@ -279,17 +239,41 @@ 428 | ngx_int_t 429 | ngx_pfree(ngx_pool_t *pool, void *p) 430 | { 431 | - ngx_pool_large_t *l; 432 | + ngx_pool_large_t *l; 433 | + ngx_pool_large_t *ll; 434 | + ngx_pool_data_t *d, *n; 435 | 436 | - for (l = pool->large; l; l = l->next) { 437 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 438 | if (p == l->alloc) { 439 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 440 | "free: %p", l->alloc); 441 | ngx_free(l->alloc); 442 | l->alloc = NULL; 443 | + if (l == pool->large) { 444 | + pool->large = NULL; 445 | + } 446 | + 447 | + ll->next = l->next; 448 | + p = l; 449 | + break; 450 | + } 451 | + } 452 | 453 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 454 | + if (p == d->alloc) { 455 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 456 | + if (d->alloc) { 457 | + ngx_free(d->alloc); 458 | + } 459 | + d->alloc = NULL; 460 | + d->prev->next = d->next; 461 | + d->next->prev = d->prev; 462 | + ngx_free(d); 463 | return NGX_OK; 464 | } 465 | + if (d->next == pool->d) { 466 | + break; 467 | + } 468 | } 469 | 470 | return NGX_DECLINED; 471 | diff -ur nginx-1.1.5/src/core/ngx_palloc.h nginx-1.1.5-patched/src/core/ngx_palloc.h 472 | --- nginx-1.1.5/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 473 | +++ nginx-1.1.5-patched/src/core/ngx_palloc.h 2011-09-13 12:11:03.155622101 +0800 474 | @@ -38,6 +38,7 @@ 475 | 476 | 477 | typedef struct ngx_pool_large_s ngx_pool_large_t; 478 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 479 | 480 | struct ngx_pool_large_s { 481 | ngx_pool_large_t *next; 482 | @@ -45,16 +46,15 @@ 483 | }; 484 | 485 | 486 | -typedef struct { 487 | - u_char *last; 488 | - u_char *end; 489 | - ngx_pool_t *next; 490 | - ngx_uint_t failed; 491 | -} ngx_pool_data_t; 492 | +struct ngx_pool_data_s{ 493 | + ngx_pool_data_t *next; 494 | + ngx_pool_data_t *prev; 495 | + void *alloc; 496 | +}; 497 | 498 | 499 | struct ngx_pool_s { 500 | - ngx_pool_data_t d; 501 | + ngx_pool_data_t *d; 502 | size_t max; 503 | ngx_pool_t *current; 504 | ngx_chain_t *chain; 505 | -------------------------------------------------------------------------------- /nginx-1.0.10-no_pool.patch: -------------------------------------------------------------------------------- 1 | diff -ur nginx-1.0.10/src/core/nginx.h nginx-1.0.10-patched/src/core/nginx.h 2 | --- nginx-1.0.10/src/core/nginx.h 2011-08-29 17:30:22.000000000 +0800 3 | +++ nginx-1.0.10-patched/src/core/nginx.h 2011-09-13 12:11:03.135622101 +0800 4 | @@ -9,7 +9,7 @@ 5 | 6 | 7 | #define nginx_version 1000010 8 | -#define NGINX_VERSION "1.0.10" 9 | +#define NGINX_VERSION "1.0.10 (without pool)" 10 | #define NGINX_VER "nginx/" NGINX_VERSION 11 | 12 | #define NGINX_VAR "NGINX" 13 | diff -ur nginx-1.0.10/src/core/ngx_array.c nginx-1.0.10-patched/src/core/ngx_array.c 14 | --- nginx-1.0.10/src/core/ngx_array.c 2008-06-17 23:00:30.000000000 +0800 15 | +++ nginx-1.0.10-patched/src/core/ngx_array.c 2011-09-14 12:02:56.263128538 +0800 16 | @@ -39,13 +39,7 @@ 17 | 18 | p = a->pool; 19 | 20 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { 21 | - p->d.last -= a->size * a->nalloc; 22 | - } 23 | - 24 | - if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { 25 | - p->d.last = (u_char *) a; 26 | - } 27 | + ngx_pfree(p, a); 28 | } 29 | 30 | 31 | @@ -64,29 +58,17 @@ 32 | 33 | p = a->pool; 34 | 35 | - if ((u_char *) a->elts + size == p->d.last 36 | - && p->d.last + a->size <= p->d.end) 37 | - { 38 | - /* 39 | - * the array allocation is the last in the pool 40 | - * and there is space for new allocation 41 | - */ 42 | - 43 | - p->d.last += a->size; 44 | - a->nalloc++; 45 | - 46 | - } else { 47 | - /* allocate a new array */ 48 | - 49 | - new = ngx_palloc(p, 2 * size); 50 | - if (new == NULL) { 51 | - return NULL; 52 | - } 53 | - 54 | - ngx_memcpy(new, a->elts, size); 55 | - a->elts = new; 56 | - a->nalloc *= 2; 57 | + /* allocate a new array */ 58 | + 59 | + new = ngx_palloc(p, 2 * size); 60 | + if (new == NULL) { 61 | + return NULL; 62 | } 63 | + 64 | + ngx_memcpy(new, a->elts, size); 65 | + a->elts = new; 66 | + a->nalloc *= 2; 67 | + 68 | } 69 | 70 | elt = (u_char *) a->elts + a->size * a->nelts; 71 | @@ -100,43 +82,25 @@ 72 | ngx_array_push_n(ngx_array_t *a, ngx_uint_t n) 73 | { 74 | void *elt, *new; 75 | - size_t size; 76 | ngx_uint_t nalloc; 77 | ngx_pool_t *p; 78 | 79 | - size = n * a->size; 80 | - 81 | if (a->nelts + n > a->nalloc) { 82 | 83 | /* the array is full */ 84 | 85 | p = a->pool; 86 | 87 | - if ((u_char *) a->elts + a->size * a->nalloc == p->d.last 88 | - && p->d.last + size <= p->d.end) 89 | - { 90 | - /* 91 | - * the array allocation is the last in the pool 92 | - * and there is space for new allocation 93 | - */ 94 | - 95 | - p->d.last += size; 96 | - a->nalloc += n; 97 | - 98 | - } else { 99 | - /* allocate a new array */ 100 | - 101 | - nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 102 | - 103 | - new = ngx_palloc(p, nalloc * a->size); 104 | - if (new == NULL) { 105 | - return NULL; 106 | - } 107 | - 108 | - ngx_memcpy(new, a->elts, a->nelts * a->size); 109 | - a->elts = new; 110 | - a->nalloc = nalloc; 111 | + nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); 112 | + 113 | + new = ngx_palloc(p, nalloc * a->size); 114 | + if (new == NULL) { 115 | + return NULL; 116 | } 117 | + 118 | + ngx_memcpy(new, a->elts, a->nelts * a->size); 119 | + a->elts = new; 120 | + a->nalloc = nalloc; 121 | } 122 | 123 | elt = (u_char *) a->elts + a->size * a->nelts; 124 | diff -ur nginx-1.0.10/src/core/ngx_palloc.c nginx-1.0.10-patched/src/core/ngx_palloc.c 125 | --- nginx-1.0.10/src/core/ngx_palloc.c 2009-12-17 20:25:46.000000000 +0800 126 | +++ nginx-1.0.10-patched/src/core/ngx_palloc.c 2011-09-14 12:03:41.663126519 +0800 127 | @@ -8,24 +8,31 @@ 128 | #include 129 | 130 | 131 | -static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); 132 | static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); 133 | 134 | 135 | ngx_pool_t * 136 | ngx_create_pool(size_t size, ngx_log_t *log) 137 | { 138 | - ngx_pool_t *p; 139 | + ngx_pool_t *p; 140 | + ngx_pool_data_t *d; 141 | 142 | - p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); 143 | + size = sizeof(ngx_pool_t); 144 | + p = ngx_alloc(size, log); 145 | if (p == NULL) { 146 | return NULL; 147 | } 148 | 149 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 150 | - p->d.end = (u_char *) p + size; 151 | - p->d.next = NULL; 152 | - p->d.failed = 0; 153 | + d = ngx_alloc(sizeof(ngx_pool_data_t), log); 154 | + 155 | + if (d == NULL) { 156 | + return NULL; 157 | + } 158 | + 159 | + d->next = d; 160 | + d->prev = d; 161 | + d->alloc = NULL; 162 | + p->d = d; 163 | 164 | size = size - sizeof(ngx_pool_t); 165 | p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL; 166 | @@ -43,7 +50,7 @@ 167 | void 168 | ngx_destroy_pool(ngx_pool_t *pool) 169 | { 170 | - ngx_pool_t *p, *n; 171 | + ngx_pool_data_t *d, *n; 172 | ngx_pool_large_t *l; 173 | ngx_pool_cleanup_t *c; 174 | 175 | @@ -55,7 +62,7 @@ 176 | } 177 | } 178 | 179 | - for (l = pool->large; l; l = l->next) { 180 | + for (l = pool->large; l ; l = l->next) { 181 | 182 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 183 | 184 | @@ -71,34 +78,45 @@ 185 | * so we can not use this log while the free()ing the pool 186 | */ 187 | 188 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 189 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 190 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 191 | - "free: %p, unused: %uz", p, p->d.end - p->d.last); 192 | + "free: %p", d); 193 | 194 | - if (n == NULL) { 195 | + if (n == pool->d) { 196 | break; 197 | } 198 | } 199 | 200 | #endif 201 | + if (pool->d->next == pool->d) { 202 | + ngx_free(pool->d); 203 | + } else { 204 | + for (d = pool->d, n = d->next; ; d = n, n = n->next) { 205 | + if (d->alloc) { 206 | + ngx_free(d->alloc); 207 | + } 208 | + ngx_free(d); 209 | 210 | - for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 211 | - ngx_free(p); 212 | - 213 | - if (n == NULL) { 214 | - break; 215 | + if (n == pool->d) { 216 | + break; 217 | + } 218 | } 219 | } 220 | + 221 | + ngx_free(pool); 222 | } 223 | 224 | 225 | void 226 | ngx_reset_pool(ngx_pool_t *pool) 227 | { 228 | - ngx_pool_t *p; 229 | - ngx_pool_large_t *l; 230 | + ngx_pool_data_t *p, *tmp; 231 | + ngx_pool_large_t *l; 232 | + 233 | + for (l = pool->large; l ; l = l->next) { 234 | + 235 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 236 | 237 | - for (l = pool->large; l; l = l->next) { 238 | if (l->alloc) { 239 | ngx_free(l->alloc); 240 | } 241 | @@ -106,109 +124,65 @@ 242 | 243 | pool->large = NULL; 244 | 245 | - for (p = pool; p; p = p->d.next) { 246 | - p->d.last = (u_char *) p + sizeof(ngx_pool_t); 247 | + p = pool->d->next; 248 | + while (p != pool->d) { 249 | + tmp = p; 250 | + ngx_free(p->alloc); 251 | + p->prev->next = p->next; 252 | + p->next->prev = p->prev; 253 | + p = p->next; 254 | + ngx_free(tmp); 255 | } 256 | -} 257 | 258 | + ngx_free(pool->d->alloc); 259 | + pool->d->alloc = NULL; 260 | + 261 | +} 262 | 263 | void * 264 | -ngx_palloc(ngx_pool_t *pool, size_t size) 265 | +ngx_malloc(ngx_pool_t *pool, size_t size) 266 | { 267 | - u_char *m; 268 | - ngx_pool_t *p; 269 | - 270 | - if (size <= pool->max) { 271 | + ngx_pool_data_t *new; 272 | + void *m; 273 | 274 | - p = pool->current; 275 | - 276 | - do { 277 | - m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); 278 | - 279 | - if ((size_t) (p->d.end - m) >= size) { 280 | - p->d.last = m + size; 281 | - 282 | - return m; 283 | - } 284 | - 285 | - p = p->d.next; 286 | - 287 | - } while (p); 288 | + m = ngx_alloc(size, pool->log); 289 | + if (m == NULL) { 290 | + return NULL; 291 | + } 292 | 293 | - return ngx_palloc_block(pool, size); 294 | + new = ngx_alloc(sizeof(ngx_pool_data_t), pool->log); 295 | + if (new == NULL){ 296 | + ngx_free(m); 297 | + return NULL; 298 | } 299 | 300 | - return ngx_palloc_large(pool, size); 301 | + new->alloc = m; 302 | + new->next = pool->d; 303 | + new->prev = pool->d->prev; 304 | + pool->d->prev->next = new; 305 | + pool->d->prev = new; 306 | + return m; 307 | } 308 | 309 | - 310 | void * 311 | -ngx_pnalloc(ngx_pool_t *pool, size_t size) 312 | +ngx_palloc(ngx_pool_t *pool, size_t size) 313 | { 314 | - u_char *m; 315 | - ngx_pool_t *p; 316 | - 317 | - if (size <= pool->max) { 318 | - 319 | - p = pool->current; 320 | - 321 | - do { 322 | - m = p->d.last; 323 | - 324 | - if ((size_t) (p->d.end - m) >= size) { 325 | - p->d.last = m + size; 326 | - 327 | - return m; 328 | - } 329 | - 330 | - p = p->d.next; 331 | - 332 | - } while (p); 333 | - 334 | - return ngx_palloc_block(pool, size); 335 | + if (size <= 1024) { 336 | + return ngx_malloc(pool, size); 337 | } 338 | 339 | return ngx_palloc_large(pool, size); 340 | } 341 | 342 | 343 | -static void * 344 | -ngx_palloc_block(ngx_pool_t *pool, size_t size) 345 | +void * 346 | +ngx_pnalloc(ngx_pool_t *pool, size_t size) 347 | { 348 | - u_char *m; 349 | - size_t psize; 350 | - ngx_pool_t *p, *new, *current; 351 | - 352 | - psize = (size_t) (pool->d.end - (u_char *) pool); 353 | - 354 | - m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); 355 | - if (m == NULL) { 356 | - return NULL; 357 | - } 358 | - 359 | - new = (ngx_pool_t *) m; 360 | - 361 | - new->d.end = m + psize; 362 | - new->d.next = NULL; 363 | - new->d.failed = 0; 364 | - 365 | - m += sizeof(ngx_pool_data_t); 366 | - m = ngx_align_ptr(m, NGX_ALIGNMENT); 367 | - new->d.last = m + size; 368 | - 369 | - current = pool->current; 370 | - 371 | - for (p = current; p->d.next; p = p->d.next) { 372 | - if (p->d.failed++ > 4) { 373 | - current = p->d.next; 374 | - } 375 | + if (size <= 1024) { 376 | + return ngx_malloc(pool, size); 377 | } 378 | 379 | - p->d.next = new; 380 | - 381 | - pool->current = current ? current : new; 382 | - 383 | - return m; 384 | + return ngx_palloc_large(pool, size); 385 | } 386 | 387 | 388 | @@ -216,7 +190,6 @@ 389 | ngx_palloc_large(ngx_pool_t *pool, size_t size) 390 | { 391 | void *p; 392 | - ngx_uint_t n; 393 | ngx_pool_large_t *large; 394 | 395 | p = ngx_alloc(size, pool->log); 396 | @@ -224,20 +197,7 @@ 397 | return NULL; 398 | } 399 | 400 | - n = 0; 401 | - 402 | - for (large = pool->large; large; large = large->next) { 403 | - if (large->alloc == NULL) { 404 | - large->alloc = p; 405 | - return p; 406 | - } 407 | - 408 | - if (n++ > 3) { 409 | - break; 410 | - } 411 | - } 412 | - 413 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 414 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 415 | if (large == NULL) { 416 | ngx_free(p); 417 | return NULL; 418 | @@ -262,7 +222,7 @@ 419 | return NULL; 420 | } 421 | 422 | - large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); 423 | + large = ngx_malloc(pool, sizeof(ngx_pool_large_t)); 424 | if (large == NULL) { 425 | ngx_free(p); 426 | return NULL; 427 | @@ -279,17 +239,41 @@ 428 | ngx_int_t 429 | ngx_pfree(ngx_pool_t *pool, void *p) 430 | { 431 | - ngx_pool_large_t *l; 432 | + ngx_pool_large_t *l; 433 | + ngx_pool_large_t *ll; 434 | + ngx_pool_data_t *d, *n; 435 | 436 | - for (l = pool->large; l; l = l->next) { 437 | + for (l = pool->large, ll = l; l; ll = l, l = l->next) { 438 | if (p == l->alloc) { 439 | ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 440 | "free: %p", l->alloc); 441 | ngx_free(l->alloc); 442 | l->alloc = NULL; 443 | + if (l == pool->large) { 444 | + pool->large = NULL; 445 | + } 446 | + 447 | + ll->next = l->next; 448 | + p = l; 449 | + break; 450 | + } 451 | + } 452 | 453 | + for (d = pool->d, n = d->next; ; d = n, n = d->next) { 454 | + if (p == d->alloc) { 455 | + ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", d->alloc); 456 | + if (d->alloc) { 457 | + ngx_free(d->alloc); 458 | + } 459 | + d->alloc = NULL; 460 | + d->prev->next = d->next; 461 | + d->next->prev = d->prev; 462 | + ngx_free(d); 463 | return NGX_OK; 464 | } 465 | + if (d->next == pool->d) { 466 | + break; 467 | + } 468 | } 469 | 470 | return NGX_DECLINED; 471 | diff -ur nginx-1.0.10/src/core/ngx_palloc.h nginx-1.0.10-patched/src/core/ngx_palloc.h 472 | --- nginx-1.0.10/src/core/ngx_palloc.h 2009-12-17 20:25:46.000000000 +0800 473 | +++ nginx-1.0.10-patched/src/core/ngx_palloc.h 2011-09-13 12:11:03.155622101 +0800 474 | @@ -38,6 +38,7 @@ 475 | 476 | 477 | typedef struct ngx_pool_large_s ngx_pool_large_t; 478 | +typedef struct ngx_pool_data_s ngx_pool_data_t; 479 | 480 | struct ngx_pool_large_s { 481 | ngx_pool_large_t *next; 482 | @@ -45,16 +46,15 @@ 483 | }; 484 | 485 | 486 | -typedef struct { 487 | - u_char *last; 488 | - u_char *end; 489 | - ngx_pool_t *next; 490 | - ngx_uint_t failed; 491 | -} ngx_pool_data_t; 492 | +struct ngx_pool_data_s{ 493 | + ngx_pool_data_t *next; 494 | + ngx_pool_data_t *prev; 495 | + void *alloc; 496 | +}; 497 | 498 | 499 | struct ngx_pool_s { 500 | - ngx_pool_data_t d; 501 | + ngx_pool_data_t *d; 502 | size_t max; 503 | ngx_pool_t *current; 504 | ngx_chain_t *chain; 505 | --------------------------------------------------------------------------------