Apache HTTPD
apr_buckets_alloc.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdlib.h>
18
19#include "apr_buckets.h"
20#include "apr_allocator.h"
21#include "apr_version.h"
22
23#define ALLOC_AMT (8192 - APR_MEMNODE_T_SIZE)
24
31
32#define SIZEOF_NODE_HEADER_T APR_ALIGN_DEFAULT(sizeof(node_header_t))
33#define SMALL_NODE_SIZE (APR_BUCKET_ALLOC_SIZE + SIZEOF_NODE_HEADER_T)
34
44
46{
48#if APR_POOL_DEBUG
50#endif
51
52#if APR_POOL_DEBUG
53 if (list->pool && list->allocator != apr_pool_allocator_get(list->pool)) {
54 allocator = list->allocator;
55 }
56#endif
57
58 apr_allocator_free(list->allocator, list->blocks);
59
60#if APR_POOL_DEBUG
61 if (allocator) {
63 }
64#endif
65
66 return APR_SUCCESS;
67}
68
70{
73
74#if APR_POOL_DEBUG
75 /* may be NULL for debug mode. */
76 if (allocator == NULL) {
79 if (fn)
80 (fn)(APR_ENOMEM);
81 abort();
82 }
83 }
84#endif
86 if (list == NULL) {
88 if (fn)
89 (fn)(APR_ENOMEM);
90 abort();
91 }
92 list->pool = p;
95
96 return list;
97}
98
101{
104
106 if (!block) {
107 return NULL;
108 }
109 list = (apr_bucket_alloc_t *)block->first_avail;
110 list->pool = NULL;
111 list->allocator = allocator;
112 list->freelist = NULL;
113 list->blocks = block;
114 block->first_avail += APR_ALIGN_DEFAULT(sizeof(*list));
115
116 return list;
117}
118
120{
121 if (list->pool) {
123 }
124
125 apr_allocator_free(list->allocator, list->blocks);
126
127#if APR_POOL_DEBUG
128 if (list->pool && list->allocator != apr_pool_allocator_get(list->pool)) {
129 apr_allocator_destroy(list->allocator);
130 }
131#endif
132}
133
136{
137 if (size <= SMALL_NODE_SIZE) {
139 }
140 else {
141#if APR_VERSION_AT_LEAST(1,6,0)
142 if (size < APR_MEMNODE_T_SIZE) {
143 size = apr_allocator_align(list->allocator, 0);
144 }
145 else {
146 size = apr_allocator_align(list->allocator,
148 }
149#else
150 /* Assumes the minimum (default) allocator's boundary of 4K and
151 * minimum (immutable before APR-1.6.x) allocation size of 8K,
152 * hence possibly (yet unlikely) under-estimating the floor...
153 */
154 size = APR_ALIGN(size, 4096);
155 if (size < 8192) {
156 size = 8192;
157 }
158#endif
160 }
162 return size;
163}
164
167{
168 node_header_t *node;
169 apr_memnode_t *active = list->blocks;
170 char *endp;
171
173 if (size <= SMALL_NODE_SIZE) {
174 if (list->freelist) {
175 node = list->freelist;
176 list->freelist = node->next;
177 }
178 else {
179 endp = active->first_avail + SMALL_NODE_SIZE;
180 if (endp >= active->endp) {
181 list->blocks = apr_allocator_alloc(list->allocator, ALLOC_AMT);
182 if (!list->blocks) {
183 list->blocks = active;
184 return NULL;
185 }
186 list->blocks->next = active;
187 active = list->blocks;
188 endp = active->first_avail + SMALL_NODE_SIZE;
189 }
190 node = (node_header_t *)active->first_avail;
191 node->alloc = list;
192 node->memnode = active;
193 node->size = SMALL_NODE_SIZE;
194 active->first_avail = endp;
195 }
196 }
197 else {
198 apr_memnode_t *memnode = apr_allocator_alloc(list->allocator, size);
199 if (!memnode) {
200 return NULL;
201 }
202 node = (node_header_t *)memnode->first_avail;
203 node->alloc = list;
204 node->memnode = memnode;
205 node->size = size;
206 }
207 return ((char *)node) + SIZEOF_NODE_HEADER_T;
208}
209
210#ifdef APR_BUCKET_DEBUG
211#if APR_HAVE_STDLIB_H
212#include <stdlib.h>
213#endif
214static void check_not_already_free(node_header_t *node)
215{
217 node_header_t *curr = list->freelist;
218
219 while (curr) {
220 if (node == curr) {
221 abort();
222 }
223 curr = curr->next;
224 }
225}
226#else
227#define check_not_already_free(node)
228#endif
229
231{
234
235 if (node->size == SMALL_NODE_SIZE) {
237 node->next = list->freelist;
238 list->freelist = node;
239 }
240 else {
241 apr_allocator_free(list->allocator, node->memnode);
242 }
243}
APR Internal Memory Allocation.
APR-UTIL Buckets/Bucket Brigades.
#define ALLOC_AMT
#define SMALL_NODE_SIZE
static apr_status_t alloc_cleanup(void *data)
#define check_not_already_free(node)
#define SIZEOF_NODE_HEADER_T
APR Versioning Interface.
#define APR_ENOMEM
Definition apr_errno.h:683
APU_DECLARE_NONSTD(void)
apr_size_t size
#define APR_MEMNODE_T_SIZE
const apr_array_header_t * list
Definition apr_cstr.h:105
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
void * data
#define APR_ALIGN(size, boundary)
#define APR_ALIGN_DEFAULT(size)
int(* apr_abortfunc_t)(int retcode)
Definition apr_pools.h:148
apr_abortfunc_t apr_allocator_t * allocator
Definition apr_pools.h:208
void * mem
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
apr_pool_t * pool
Definition apr_tables.h:64
apr_memnode_t * blocks
node_header_t * freelist
apr_allocator_t * allocator
struct node_header_t * next
apr_memnode_t * memnode
apr_bucket_alloc_t * alloc