comparison src/core/ngx_palloc.c @ 382:984bb0b1399b NGINX_0_7_3

nginx 0.7.3 *) Change: the "rss" extension MIME type has been changed to "application/rss+xml". *) Change: now the "gzip_vary" directive turned on issues a "Vary: Accept-Encoding" header line for uncompressed responses too. *) Feature: now the "rewrite" directive does a redirect automatically if the "https://" protocol is used. *) Bugfix: the "proxy_pass" directive did not work with the HTTPS protocol; the bug had appeared in 0.6.9.
author Igor Sysoev <http://sysoev.ru>
date Mon, 23 Jun 2008 00:00:00 +0400
parents bc21d9cd9c54
children 0b6053502c55
comparison
equal deleted inserted replaced
381:23d1555141d9 382:984bb0b1399b
6 6
7 #include <ngx_config.h> 7 #include <ngx_config.h>
8 #include <ngx_core.h> 8 #include <ngx_core.h>
9 9
10 10
11 static void *ngx_palloc_block(ngx_pool_t *pool, size_t size);
12 static void *ngx_palloc_large(ngx_pool_t *pool, size_t size);
13
14
11 ngx_pool_t * 15 ngx_pool_t *
12 ngx_create_pool(size_t size, ngx_log_t *log) 16 ngx_create_pool(size_t size, ngx_log_t *log)
13 { 17 {
14 ngx_pool_t *p; 18 ngx_pool_t *p;
15 19
16 p = ngx_alloc(size, log); 20 p = ngx_alloc(size, log);
17 if (p == NULL) { 21 if (p == NULL) {
18 return NULL; 22 return NULL;
19 } 23 }
20 24
21 p->last = (u_char *) p + sizeof(ngx_pool_t); 25 p->d.last = (u_char *) p + sizeof(ngx_pool_t);
22 p->end = (u_char *) p + size; 26 p->d.end = (u_char *) p + size;
27 p->d.next = NULL;
28
29 size = size - sizeof(ngx_pool_t);
30 p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;
31
23 p->current = p; 32 p->current = p;
24 p->chain = NULL; 33 p->chain = NULL;
25 p->next = NULL;
26 p->large = NULL; 34 p->large = NULL;
27 p->cleanup = NULL; 35 p->cleanup = NULL;
28 p->log = log; 36 p->log = log;
29 37
30 return p; 38 return p;
60 /* 68 /*
61 * we could allocate the pool->log from this pool 69 * we could allocate the pool->log from this pool
62 * so we can not use this log while the free()ing the pool 70 * so we can not use this log while the free()ing the pool
63 */ 71 */
64 72
65 for (p = pool, n = pool->next; /* void */; p = n, n = n->next) { 73 for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
66 ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 74 ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
67 "free: %p, unused: %uz", p, p->end - p->last); 75 "free: %p, unused: %uz", p, p->d.end - p->d.last);
68 76
69 if (n == NULL) { 77 if (n == NULL) {
70 break; 78 break;
71 } 79 }
72 } 80 }
73 81
74 #endif 82 #endif
75 83
76 for (p = pool, n = pool->next; /* void */; p = n, n = n->next) { 84 for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
77 ngx_free(p); 85 ngx_free(p);
78 86
79 if (n == NULL) { 87 if (n == NULL) {
80 break; 88 break;
81 } 89 }
84 92
85 93
86 void * 94 void *
87 ngx_palloc(ngx_pool_t *pool, size_t size) 95 ngx_palloc(ngx_pool_t *pool, size_t size)
88 { 96 {
89 u_char *m; 97 u_char *m;
90 ngx_pool_t *p, *n, *current; 98 ngx_pool_t *p;
91 ngx_pool_large_t *large; 99
92 100 if (size <= pool->max) {
93 if (size <= (size_t) NGX_MAX_ALLOC_FROM_POOL 101
94 && size <= (size_t) (pool->end - (u_char *) pool)
95 - (size_t) ngx_align_ptr(sizeof(ngx_pool_t), NGX_ALIGNMENT))
96 {
97 p = pool->current; 102 p = pool->current;
98 current = p; 103
99 104 do {
100 for ( ;; ) { 105 m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT);
101 106
102 /* 107 if ((size_t) (p->d.end - m) >= size) {
103 * allow non-aligned memory blocks for small allocations (1, 2, 108 p->d.last = m + size;
104 * or 3 bytes) and for odd length strings (struct's have aligned
105 * size)
106 */
107
108 if (size < sizeof(int) || (size & 1)) {
109 m = p->last;
110
111 } else {
112 m = ngx_align_ptr(p->last, NGX_ALIGNMENT);
113 }
114
115 if ((size_t) (p->end - m) >= size) {
116 p->last = m + size;
117 109
118 return m; 110 return m;
119 } 111 }
120 112
121 if ((size_t) (p->end - m) < NGX_ALIGNMENT) { 113 p = p->d.next;
122 current = p->next; 114
115 } while (p);
116
117 return ngx_palloc_block(pool, size);
118 }
119
120 return ngx_palloc_large(pool, size);
121 }
122
123
124 void *
125 ngx_pnalloc(ngx_pool_t *pool, size_t size)
126 {
127 u_char *m;
128 ngx_pool_t *p;
129
130 if (size <= pool->max) {
131
132 p = pool->current;
133
134 do {
135 m = p->d.last;
136
137 if ((size_t) (p->d.end - m) >= size) {
138 p->d.last = m + size;
139
140 return m;
123 } 141 }
124 142
125 if (p->next == NULL) { 143 p = p->d.next;
126 break; 144
127 } 145 } while (p);
128 146
129 p = p->next; 147 return ngx_palloc_block(pool, size);
130 pool->current = current; 148 }
131 } 149
132 150 return ngx_palloc_large(pool, size);
133 /* allocate a new pool block */ 151 }
134 152
135 n = ngx_create_pool((size_t) (p->end - (u_char *) p), p->log); 153
136 if (n == NULL) { 154 static void *
137 return NULL; 155 ngx_palloc_block(ngx_pool_t *pool, size_t size)
138 } 156 {
139 157 u_char *m;
140 pool->current = current ? current : n; 158 size_t psize;
141 159 ngx_pool_t *p, *new, *current;
142 p->next = n; 160
143 m = ngx_align_ptr(n->last, NGX_ALIGNMENT); 161 psize = (size_t) (pool->d.end - (u_char *) pool);
144 n->last = m + size; 162
145 163 m = ngx_alloc(psize, pool->log);
146 return m; 164 if (m == NULL) {
147 } 165 return NULL;
166 }
167
168 new = (ngx_pool_t *) m;
169
170 new->d.end = m + psize;
171 new->d.next = NULL;
172
173 m += sizeof(ngx_pool_data_t);
174 new->d.last = m + size;
175
176 current = pool->current;
177
178 for (p = current; p->d.next; p = p->d.next) {
179 if ((size_t) (p->d.end - p->d.last) < NGX_ALIGNMENT) {
180 current = p->d.next;
181 }
182 }
183
184 p->d.next = new;
185
186 pool->current = current ? current : new;
187
188 return m;
189 }
190
191
192 static void *
193 ngx_palloc_large(ngx_pool_t *pool, size_t size)
194 {
195 void *p;
196 ngx_pool_large_t *large;
148 197
149 #if 0 198 #if 0
150 p = ngx_memalign(ngx_pagesize, size, pool->log); 199 p = ngx_memalign(ngx_pagesize, size, pool->log);
151 if (p == NULL) { 200 if (p == NULL) {
152 return NULL; 201 return NULL;
167 large->alloc = p; 216 large->alloc = p;
168 large->next = pool->large; 217 large->next = pool->large;
169 pool->large = large; 218 pool->large = large;
170 219
171 return p; 220 return p;
172 }
173
174
175 void *
176 ngx_palloc_aligned(ngx_pool_t *pool, size_t size)
177 {
178 if (size & 1) {
179 size++;
180 }
181
182 return ngx_palloc(pool, size);
183 } 221 }
184 222
185 223
186 ngx_int_t 224 ngx_int_t
187 ngx_pfree(ngx_pool_t *pool, void *p) 225 ngx_pfree(ngx_pool_t *pool, void *p)