~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Nginx/core/ngx_palloc.c

Version: ~ [ nginx-0.6.26 ] ~ [ nginx-0.5.35 ] ~ [ nginx-0.5.20 ] ~ [ nginx-0.5.19 ] ~

  1 
  2 /*
  3  * Copyright (C) Igor Sysoev
  4  */
  5 
  6 
  7 #include <ngx_config.h>
  8 #include <ngx_core.h>
  9 
 10 
 11 ngx_pool_t *
 12 ngx_create_pool(size_t size, ngx_log_t *log)
 13 {
 14     ngx_pool_t  *p;
 15 
 16     p = ngx_alloc(size, log);
 17     if (p == NULL) {
 18         return NULL;
 19     }
 20 
 21     p->last = (u_char *) p + sizeof(ngx_pool_t);
 22     p->end = (u_char *) p + size;
 23     p->current = p;
 24     p->chain = NULL;
 25     p->next = NULL;
 26     p->large = NULL;
 27     p->cleanup = NULL;
 28     p->log = log;
 29 
 30     return p;
 31 }
 32 
 33 
 34 void
 35 ngx_destroy_pool(ngx_pool_t *pool)
 36 {
 37     ngx_pool_t          *p, *n;
 38     ngx_pool_large_t    *l;
 39     ngx_pool_cleanup_t  *c;
 40 
 41     for (c = pool->cleanup; c; c = c->next) {
 42         if (c->handler) {
 43             ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
 44                            "run cleanup: %p", c);
 45             c->handler(c->data);
 46         }
 47     }
 48 
 49     for (l = pool->large; l; l = l->next) {
 50 
 51         ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc);
 52 
 53         if (l->alloc) {
 54             ngx_free(l->alloc);
 55         }
 56     }
 57 
 58 #if (NGX_DEBUG)
 59 
 60     /*
 61      * we could allocate the pool->log from this pool
 62      * so we can not use this log while the free()ing the pool
 63      */
 64 
 65     for (p = pool, n = pool->next; /* void */; p = n, n = n->next) {
 66         ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
 67                        "free: %p, unused: %uz", p, p->end - p->last);
 68 
 69         if (n == NULL) {
 70             break;
 71         }
 72     }
 73 
 74 #endif
 75 
 76     for (p = pool, n = pool->next; /* void */; p = n, n = n->next) {
 77         ngx_free(p);
 78 
 79         if (n == NULL) {
 80             break;
 81         }
 82     }
 83 }
 84 
 85 
 86 void *
 87 ngx_palloc(ngx_pool_t *pool, size_t size)
 88 {
 89     u_char            *m;
 90     ngx_pool_t        *p, *n, *current;
 91     ngx_pool_large_t  *large;
 92 
 93     if (size <= (size_t) NGX_MAX_ALLOC_FROM_POOL
 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;
 98         current = p;
 99 
100         for ( ;; ) {
101 
102 #if (NGX_HAVE_NONALIGNED)
103 
104             /*
105              * allow non-aligned memory blocks for small allocations (1, 2,
106              * or 3 bytes) and for odd length strings (struct's have aligned
107              * size)
108              */
109 
110             if (size < sizeof(int) || (size & 1)) {
111                 m = p->last;
112 
113             } else
114 #endif
115 
116             {
117                 m = ngx_align_ptr(p->last, NGX_ALIGNMENT);
118             }
119 
120             if ((size_t) (p->end - m) >= size) {
121                 p->last = m + size;
122 
123                 return m;
124             }
125 
126             if ((size_t) (p->end - m) < NGX_ALIGNMENT) {
127                 current = p->next;
128             }
129 
130             if (p->next == NULL) {
131                 break;
132             }
133 
134             p = p->next;
135             pool->current = current;
136         }
137 
138         /* allocate a new pool block */
139 
140         n = ngx_create_pool((size_t) (p->end - (u_char *) p), p->log);
141         if (n == NULL) {
142             return NULL;
143         }
144 
145         pool->current = current ? current : n;
146 
147         p->next = n;
148         m = ngx_align_ptr(n->last, NGX_ALIGNMENT);
149         n->last = m + size;
150 
151         return m;
152     }
153 
154 #if 0
155     p = ngx_memalign(ngx_pagesize, size, pool->log);
156     if (p == NULL) {
157         return NULL;
158     }
159 #else
160     p = ngx_alloc(size, pool->log);
161     if (p == NULL) {
162         return NULL;
163     }
164 #endif
165 
166     large = ngx_palloc(pool, sizeof(ngx_pool_large_t));
167     if (large == NULL) {
168         ngx_free(p);
169         return NULL;
170     }
171 
172     large->alloc = p;
173     large->next = pool->large;
174     pool->large = large;
175 
176     return p;
177 }
178 
179 
180 ngx_int_t
181 ngx_pfree(ngx_pool_t *pool, void *p)
182 {
183     ngx_pool_large_t  *l;
184 
185     for (l = pool->large; l; l = l->next) {
186         if (p == l->alloc) {
187             ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
188                            "free: %p", l->alloc);
189             ngx_free(l->alloc);
190             l->alloc = NULL;
191 
192             return NGX_OK;
193         }
194     }
195 
196     return NGX_DECLINED;
197 }
198 
199 
200 void *
201 ngx_pcalloc(ngx_pool_t *pool, size_t size)
202 {
203     void *p;
204 
205     p = ngx_palloc(pool, size);
206     if (p) {
207         ngx_memzero(p, size);
208     }
209 
210     return p;
211 }
212 
213 
214 ngx_pool_cleanup_t *
215 ngx_pool_cleanup_add(ngx_pool_t *p, size_t size)
216 {
217     ngx_pool_cleanup_t  *c;
218 
219     c = ngx_palloc(p, sizeof(ngx_pool_cleanup_t));
220     if (c == NULL) {
221         return NULL;
222     }
223 
224     if (size) {
225         c->data = ngx_palloc(p, size);
226         if (c->data == NULL) {
227             return NULL;
228         }
229 
230     } else {
231         c->data = NULL;
232     }
233 
234     c->handler = NULL;
235     c->next = p->cleanup;
236 
237     p->cleanup = c;
238 
239     ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, p->log, 0, "add cleanup: %p", c);
240 
241     return c;
242 }
243 
244 
245 void
246 ngx_pool_cleanup_file(void *data)
247 {
248     ngx_pool_cleanup_file_t  *c = data;
249 
250     ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, c->log, 0, "file cleanup: fd:%d",
251                    c->fd);
252 
253     if (ngx_close_file(c->fd) == NGX_FILE_ERROR) {
254         ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
255                       ngx_close_file_n " \"%s\" failed", c->name);
256     }
257 }
258 
259 
260 void
261 ngx_pool_delete_file(void *data)
262 {
263     ngx_pool_cleanup_file_t  *c = data;
264 
265     ngx_err_t  err;
266 
267     ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, c->log, 0, "file cleanup: fd:%d %s",
268                    c->fd, c->name);
269 
270     if (ngx_delete_file(c->name) == NGX_FILE_ERROR) {
271         err = ngx_errno;
272 
273         if (err != NGX_ENOENT) {
274             ngx_log_error(NGX_LOG_CRIT, c->log, err,
275                           ngx_delete_file_n " \"%s\" failed", c->name);
276         }
277     }
278 
279     if (ngx_close_file(c->fd) == NGX_FILE_ERROR) {
280         ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
281                       ngx_close_file_n " \"%s\" failed", c->name);
282     }
283 }
284 
285 
286 #if 0
287 
288 static void *
289 ngx_get_cached_block(size_t size)
290 {
291     void                     *p;
292     ngx_cached_block_slot_t  *slot;
293 
294     if (ngx_cycle->cache == NULL) {
295         return NULL;
296     }
297 
298     slot = &ngx_cycle->cache[(size + ngx_pagesize - 1) / ngx_pagesize];
299 
300     slot->tries++;
301 
302     if (slot->number) {
303         p = slot->block;
304         slot->block = slot->block->next;
305         slot->number--;
306         return p;
307     }
308 
309     return NULL;
310 }
311 
312 #endif
313 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.