Mercurial > hg > nginx
comparison src/core/ngx_alloc.c @ 156:afc333135a6b
nginx-0.0.1-2003-10-23-10:13:16 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 23 Oct 2003 06:13:16 +0000 |
parents | b27548f540ad |
children | 2357fa41738a |
comparison
equal
deleted
inserted
replaced
155:46eb23d9471d | 156:afc333135a6b |
---|---|
18 #endif | 18 #endif |
19 | 19 |
20 return p; | 20 return p; |
21 } | 21 } |
22 | 22 |
23 | |
23 void *ngx_calloc(size_t size, ngx_log_t *log) | 24 void *ngx_calloc(size_t size, ngx_log_t *log) |
24 { | 25 { |
25 void *p; | 26 void *p; |
26 | 27 |
27 p = ngx_alloc(size, log); | 28 p = ngx_alloc(size, log); |
29 ngx_memzero(p, size); | 30 ngx_memzero(p, size); |
30 } | 31 } |
31 | 32 |
32 return p; | 33 return p; |
33 } | 34 } |
35 | |
34 | 36 |
35 ngx_pool_t *ngx_create_pool(size_t size, ngx_log_t *log) | 37 ngx_pool_t *ngx_create_pool(size_t size, ngx_log_t *log) |
36 { | 38 { |
37 ngx_pool_t *p; | 39 ngx_pool_t *p; |
38 | 40 |
45 p->log = log; | 47 p->log = log; |
46 | 48 |
47 return p; | 49 return p; |
48 } | 50 } |
49 | 51 |
52 | |
50 void ngx_destroy_pool(ngx_pool_t *pool) | 53 void ngx_destroy_pool(ngx_pool_t *pool) |
51 { | 54 { |
52 ngx_pool_t *p, *n; | 55 ngx_pool_t *p, *n; |
53 ngx_pool_large_t *l; | 56 ngx_pool_large_t *l; |
54 | 57 |
55 for (l = pool->large; l; l = l->next) { | 58 for (l = pool->large; l; l = l->next) { |
56 #if (NGX_DEBUG_ALLOC) | 59 #if (NGX_DEBUG_ALLOC) |
57 ngx_log_debug(pool->log, "free: %08x" _ l->alloc); | 60 ngx_log_debug(pool->log, "free: %08x" _ l->alloc); |
58 #endif | 61 #endif |
59 free(l->alloc); | 62 if (l->alloc) { |
63 free(l->alloc); | |
64 } | |
60 } | 65 } |
61 | 66 |
62 /* | 67 /* |
63 * we could allocate pool->log from this pool | 68 * we could allocate pool->log from this pool |
64 * so we can not use this log while free()ing the pool | 69 * so we can not use this log while free()ing the pool |
83 } | 88 } |
84 | 89 |
85 pool = NULL; | 90 pool = NULL; |
86 } | 91 } |
87 | 92 |
93 | |
88 void *ngx_palloc(ngx_pool_t *pool, size_t size) | 94 void *ngx_palloc(ngx_pool_t *pool, size_t size) |
89 { | 95 { |
90 char *m; | 96 char *m; |
91 ngx_pool_t *p, *n; | 97 ngx_pool_t *p, *n; |
92 ngx_pool_large_t *large, *last; | 98 ngx_pool_large_t *large, *last; |
105 if (n == NULL) { | 111 if (n == NULL) { |
106 break; | 112 break; |
107 } | 113 } |
108 } | 114 } |
109 | 115 |
110 /* alloc new pool block */ | 116 /* alloc a new pool block */ |
117 | |
111 ngx_test_null(n, ngx_create_pool(p->end - (char *) p, p->log), NULL); | 118 ngx_test_null(n, ngx_create_pool(p->end - (char *) p, p->log), NULL); |
112 p->next = n; | 119 p->next = n; |
113 m = n->last; | 120 m = n->last; |
114 n->last += size; | 121 n->last += size; |
122 | |
115 return m; | 123 return m; |
124 } | |
116 | 125 |
117 /* alloc large block */ | 126 /* alloc a large block */ |
118 } else { | |
119 large = NULL; | |
120 last = NULL; | |
121 | 127 |
122 if (pool->large) { | 128 large = NULL; |
123 for (last = pool->large; /* void */; last = last->next) { | 129 last = NULL; |
124 if (last->alloc == NULL) { | |
125 large = last; | |
126 last = NULL; | |
127 break; | |
128 } | |
129 | 130 |
130 if (last->next == NULL) { | 131 if (pool->large) { |
131 break; | 132 for (last = pool->large; /* void */; last = last->next) { |
132 } | 133 if (last->alloc == NULL) { |
134 large = last; | |
135 last = NULL; | |
136 break; | |
137 } | |
138 | |
139 if (last->next == NULL) { | |
140 break; | |
133 } | 141 } |
134 } | 142 } |
143 } | |
135 | 144 |
136 if (large == NULL) { | 145 if (large == NULL) { |
137 ngx_test_null(large, ngx_palloc(pool, sizeof(ngx_pool_large_t)), | 146 ngx_test_null(large, ngx_palloc(pool, sizeof(ngx_pool_large_t)), NULL); |
138 NULL); | 147 large->next = NULL; |
139 large->next = NULL; | 148 } |
149 | |
150 ngx_test_null(p, ngx_alloc(size, pool->log), NULL); | |
151 | |
152 if (pool->large == NULL) { | |
153 pool->large = large; | |
154 | |
155 } else if (last) { | |
156 last->next = large; | |
157 } | |
158 | |
159 large->alloc = p; | |
160 | |
161 return p; | |
162 } | |
163 | |
164 | |
165 void ngx_pfree(ngx_pool_t *pool, void *p) | |
166 { | |
167 ngx_pool_large_t *l; | |
168 | |
169 for (l = pool->large; l; l = l->next) { | |
170 if (p == l->alloc) { | |
171 #if (NGX_DEBUG_ALLOC) | |
172 ngx_log_debug(pool->log, "free: %08x" _ l->alloc); | |
173 #endif | |
174 free(l->alloc); | |
175 l->alloc = NULL; | |
140 } | 176 } |
141 | |
142 ngx_test_null(p, ngx_alloc(size, pool->log), NULL); | |
143 | |
144 if (pool->large == NULL) { | |
145 pool->large = large; | |
146 | |
147 } else if (last) { | |
148 last->next = large; | |
149 } | |
150 | |
151 large->alloc = p; | |
152 | |
153 return p; | |
154 } | 177 } |
155 } | 178 } |
179 | |
156 | 180 |
157 void *ngx_pcalloc(ngx_pool_t *pool, size_t size) | 181 void *ngx_pcalloc(ngx_pool_t *pool, size_t size) |
158 { | 182 { |
159 void *p; | 183 void *p; |
160 | 184 |