comparison src/core/ngx_slab.c @ 468:56baf312c1b5 NGINX_0_7_46

nginx 0.7.46 *) Bugfix: the previous release tarball was incorrect.
author Igor Sysoev <http://sysoev.ru>
date Mon, 30 Mar 2009 00:00:00 +0400
parents 6281966854a5
children 549994537f15
comparison
equal deleted inserted replaced
467:d46142e61c30 468:56baf312c1b5
3 * Copyright (C) Igor Sysoev 3 * Copyright (C) Igor Sysoev
4 */ 4 */
5 5
6 #include <ngx_config.h> 6 #include <ngx_config.h>
7 #include <ngx_core.h> 7 #include <ngx_core.h>
8
9 /*
10
11 12
12 2048 2 11
13 1024 4 10
14 512 8 9
15 256 16 8
16
17 128 32 4 32 7
18
19 64 64 8 63 6 1
20 32 128 16 127 5 1
21 16 256 32 254 4 2
22 8 512 64 504 3 8
23
24 */
25 8
26 9
27 #define NGX_SLAB_PAGE_MASK 3 10 #define NGX_SLAB_PAGE_MASK 3
28 #define NGX_SLAB_PAGE 0 11 #define NGX_SLAB_PAGE 0
29 #define NGX_SLAB_BIG 1 12 #define NGX_SLAB_BIG 1
78 61
79 static ngx_slab_page_t *ngx_slab_alloc_pages(ngx_slab_pool_t *pool, 62 static ngx_slab_page_t *ngx_slab_alloc_pages(ngx_slab_pool_t *pool,
80 ngx_uint_t pages); 63 ngx_uint_t pages);
81 static void ngx_slab_free_pages(ngx_slab_pool_t *pool, ngx_slab_page_t *page, 64 static void ngx_slab_free_pages(ngx_slab_pool_t *pool, ngx_slab_page_t *page,
82 ngx_uint_t pages); 65 ngx_uint_t pages);
66 static void ngx_slab_error(ngx_slab_pool_t *pool, ngx_uint_t level,
67 char *text);
83 68
84 69
85 static ngx_uint_t ngx_slab_max_size; 70 static ngx_uint_t ngx_slab_max_size;
86 static ngx_uint_t ngx_slab_exact_size; 71 static ngx_uint_t ngx_slab_exact_size;
87 static ngx_uint_t ngx_slab_exact_shift; 72 static ngx_uint_t ngx_slab_exact_shift;
145 if (m > 0) { 130 if (m > 0) {
146 pages -= m; 131 pages -= m;
147 pool->pages->slab = pages; 132 pool->pages->slab = pages;
148 } 133 }
149 134
150 #if 0 135 pool->log_ctx = &pool->zero;
151 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, "slab: %p, %p, %ui, %d", 136 pool->zero = '\0';
152 pool, pool->start, pages,
153 (pool->end - pool->start) / ngx_pagesize - pages);
154 #endif
155 } 137 }
156 138
157 139
158 void * 140 void *
159 ngx_slab_alloc(ngx_slab_pool_t *pool, size_t size) 141 ngx_slab_alloc(ngx_slab_pool_t *pool, size_t size)
436 ngx_slab_page_t *slots, *page; 418 ngx_slab_page_t *slots, *page;
437 419
438 ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, ngx_cycle->log, 0, "slab free: %p", p); 420 ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, ngx_cycle->log, 0, "slab free: %p", p);
439 421
440 if ((u_char *) p < pool->start || (u_char *) p > pool->end) { 422 if ((u_char *) p < pool->start || (u_char *) p > pool->end) {
441 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, 423 ngx_slab_error(pool, NGX_LOG_ALERT, "ngx_slab_free(): outside of pool");
442 "ngx_slab_free(): outside of pool");
443 goto fail; 424 goto fail;
444 } 425 }
445 426
446 n = ((u_char *) p - pool->start) >> ngx_pagesize_shift; 427 n = ((u_char *) p - pool->start) >> ngx_pagesize_shift;
447 page = &pool->pages[n]; 428 page = &pool->pages[n];
585 if ((uintptr_t) p & (ngx_pagesize - 1)) { 566 if ((uintptr_t) p & (ngx_pagesize - 1)) {
586 goto wrong_chunk; 567 goto wrong_chunk;
587 } 568 }
588 569
589 if (slab == NGX_SLAB_PAGE_FREE) { 570 if (slab == NGX_SLAB_PAGE_FREE) {
590 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, 571 ngx_slab_error(pool, NGX_LOG_ALERT,
591 "ngx_slab_free(): page is already free"); 572 "ngx_slab_free(): page is already free");
592 goto fail; 573 goto fail;
593 } 574 }
594 575
595 if (slab == NGX_SLAB_PAGE_BUSY) { 576 if (slab == NGX_SLAB_PAGE_BUSY) {
596 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, 577 ngx_slab_error(pool, NGX_LOG_ALERT,
597 "ngx_slab_free(): pointer to wrong page"); 578 "ngx_slab_free(): pointer to wrong page");
598 goto fail; 579 goto fail;
599 } 580 }
600 581
601 n = ((u_char *) p - pool->start) >> ngx_pagesize_shift; 582 n = ((u_char *) p - pool->start) >> ngx_pagesize_shift;
602 size = slab & ~NGX_SLAB_PAGE_START; 583 size = slab & ~NGX_SLAB_PAGE_START;
618 599
619 return; 600 return;
620 601
621 wrong_chunk: 602 wrong_chunk:
622 603
623 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, 604 ngx_slab_error(pool, NGX_LOG_ALERT,
624 "ngx_slab_free(): pointer to wrong chunk"); 605 "ngx_slab_free(): pointer to wrong chunk");
625 606
626 goto fail; 607 goto fail;
627 608
628 chunk_already_free: 609 chunk_already_free:
629 610
630 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, 611 ngx_slab_error(pool, NGX_LOG_ALERT,
631 "ngx_slab_free(): chunk is already free"); 612 "ngx_slab_free(): chunk is already free");
632 613
633 fail: 614 fail:
634 615
635 return; 616 return;
636 } 617 }
677 658
678 return page; 659 return page;
679 } 660 }
680 } 661 }
681 662
682 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, NGX_ENOMEM, 663 ngx_slab_error(pool, NGX_LOG_CRIT, "ngx_slab_alloc() failed: no memory");
683 "ngx_slab_alloc(): failed");
684 664
685 return NULL; 665 return NULL;
686 } 666 }
687 667
688 668
709 689
710 page->next->prev = (uintptr_t) page; 690 page->next->prev = (uintptr_t) page;
711 691
712 pool->free.next = page; 692 pool->free.next = page;
713 } 693 }
694
695
696 static void
697 ngx_slab_error(ngx_slab_pool_t *pool, ngx_uint_t level, char *text)
698 {
699 ngx_log_error(level, ngx_cycle->log, 0, "%s%s", text, pool->log_ctx);
700 }