Mercurial > hg > nginx
comparison src/core/ngx_palloc.c @ 931:87f456ff13b0
fix segfault in "out of memory" situation
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 18 Dec 2006 20:46:49 +0000 |
parents | 201d017ea470 |
children | 4d203f76b757 |
comparison
equal
deleted
inserted
replaced
930:45df22906c12 | 931:87f456ff13b0 |
---|---|
83 | 83 |
84 void * | 84 void * |
85 ngx_palloc(ngx_pool_t *pool, size_t size) | 85 ngx_palloc(ngx_pool_t *pool, size_t size) |
86 { | 86 { |
87 u_char *m; | 87 u_char *m; |
88 ngx_pool_t *p, *n; | 88 ngx_pool_t *p, *n, *current; |
89 ngx_pool_large_t *large; | 89 ngx_pool_large_t *large; |
90 | 90 |
91 if (size <= (size_t) NGX_MAX_ALLOC_FROM_POOL | 91 if (size <= (size_t) NGX_MAX_ALLOC_FROM_POOL |
92 && size <= (size_t) (pool->end - (u_char *) pool) | 92 && size <= (size_t) (pool->end - (u_char *) pool) |
93 - (size_t) ngx_align_ptr(sizeof(ngx_pool_t), NGX_ALIGNMENT)) | 93 - (size_t) ngx_align_ptr(sizeof(ngx_pool_t), NGX_ALIGNMENT)) |
94 { | 94 { |
95 for (p = pool->current; /* void */ ; p = p->next) { | 95 p = pool->current; |
96 current = p; | |
97 | |
98 for ( ;; ) { | |
96 | 99 |
97 if (size < sizeof(int) || (size & 1)) { | 100 if (size < sizeof(int) || (size & 1)) { |
98 m = p->last; | 101 m = p->last; |
99 | 102 |
100 } else { | 103 } else { |
106 | 109 |
107 return m; | 110 return m; |
108 } | 111 } |
109 | 112 |
110 if ((size_t) (p->end - m) < NGX_ALIGNMENT) { | 113 if ((size_t) (p->end - m) < NGX_ALIGNMENT) { |
111 pool->current = p->next; | 114 current = p->next; |
112 } | 115 } |
113 | 116 |
114 if (p->next == NULL) { | 117 if (p->next == NULL) { |
115 break; | 118 break; |
116 } | 119 } |
120 | |
121 p = p->next; | |
122 pool->current = current; | |
117 } | 123 } |
118 | 124 |
119 /* allocate a new pool block */ | 125 /* allocate a new pool block */ |
120 | 126 |
121 n = ngx_create_pool((size_t) (p->end - (u_char *) p), p->log); | 127 n = ngx_create_pool((size_t) (p->end - (u_char *) p), p->log); |
122 if (n == NULL) { | 128 if (n == NULL) { |
123 return NULL; | 129 return NULL; |
124 } | 130 } |
125 | 131 |
126 if (pool->current == NULL) { | 132 pool->current = current ? current : n; |
127 pool->current = n; | |
128 } | |
129 | 133 |
130 p->next = n; | 134 p->next = n; |
131 m = ngx_align_ptr(n->last, NGX_ALIGNMENT); | 135 m = ngx_align_ptr(n->last, NGX_ALIGNMENT); |
132 n->last = m + size; | 136 n->last = m + size; |
133 | 137 |