1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "apr_buckets.h"
20 #include "apr_allocator.h"
22 #define ALLOC_AMT (8192 - APR_MEMNODE_T_SIZE)
24 typedef struct node_header_t {
26 apr_bucket_alloc_t *alloc;
27 apr_memnode_t *memnode;
28 struct node_header_t *next;
31 #define SIZEOF_NODE_HEADER_T APR_ALIGN_DEFAULT(sizeof(node_header_t))
32 #define SMALL_NODE_SIZE (APR_BUCKET_ALLOC_SIZE + SIZEOF_NODE_HEADER_T)
34 /** A list of free memory from which new buckets or private bucket
35 * structures can be allocated.
37 struct apr_bucket_alloc_t {
39 apr_allocator_t *allocator;
40 node_header_t *freelist;
41 apr_memnode_t *blocks;
44 static apr_status_t alloc_cleanup(void *data)
46 apr_bucket_alloc_t *list = data;
48 apr_allocator_free(list->allocator, list->blocks);
51 if (list->pool && list->allocator != apr_pool_allocator_get(list->pool)) {
52 apr_allocator_destroy(list->allocator);
59 APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create(apr_pool_t *p)
61 apr_allocator_t *allocator = apr_pool_allocator_get(p);
62 apr_bucket_alloc_t *list;
65 /* may be NULL for debug mode. */
66 if (allocator == NULL) {
67 if (apr_allocator_create(&allocator) != APR_SUCCESS) {
73 list = apr_bucket_alloc_create_ex(allocator);
75 apr_pool_cleanup_register(list->pool, list, alloc_cleanup,
76 apr_pool_cleanup_null);
81 APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create_ex(
82 apr_allocator_t *allocator)
84 apr_bucket_alloc_t *list;
87 block = apr_allocator_alloc(allocator, ALLOC_AMT);
91 list = (apr_bucket_alloc_t *)block->first_avail;
93 list->allocator = allocator;
94 list->freelist = NULL;
96 block->first_avail += APR_ALIGN_DEFAULT(sizeof(*list));
101 APU_DECLARE_NONSTD(void) apr_bucket_alloc_destroy(apr_bucket_alloc_t *list)
104 apr_pool_cleanup_kill(list->pool, list, alloc_cleanup);
107 apr_allocator_free(list->allocator, list->blocks);
110 if (list->pool && list->allocator != apr_pool_allocator_get(list->pool)) {
111 apr_allocator_destroy(list->allocator);
116 APU_DECLARE_NONSTD(void *) apr_bucket_alloc(apr_size_t size,
117 apr_bucket_alloc_t *list)
120 apr_memnode_t *active = list->blocks;
123 size += SIZEOF_NODE_HEADER_T;
124 if (size <= SMALL_NODE_SIZE) {
125 if (list->freelist) {
126 node = list->freelist;
127 list->freelist = node->next;
130 endp = active->first_avail + SMALL_NODE_SIZE;
131 if (endp >= active->endp) {
132 list->blocks = apr_allocator_alloc(list->allocator, ALLOC_AMT);
136 list->blocks->next = active;
137 active = list->blocks;
138 endp = active->first_avail + SMALL_NODE_SIZE;
140 node = (node_header_t *)active->first_avail;
142 node->memnode = active;
143 node->size = SMALL_NODE_SIZE;
144 active->first_avail = endp;
148 apr_memnode_t *memnode = apr_allocator_alloc(list->allocator, size);
152 node = (node_header_t *)memnode->first_avail;
154 node->memnode = memnode;
157 return ((char *)node) + SIZEOF_NODE_HEADER_T;
160 #ifdef APR_BUCKET_DEBUG
161 #if APR_HAVE_STDLIB_H
164 static void check_not_already_free(node_header_t *node)
166 apr_bucket_alloc_t *list = node->alloc;
167 node_header_t *curr = list->freelist;
177 #define check_not_already_free(node)
180 APU_DECLARE_NONSTD(void) apr_bucket_free(void *mem)
182 node_header_t *node = (node_header_t *)((char *)mem - SIZEOF_NODE_HEADER_T);
183 apr_bucket_alloc_t *list = node->alloc;
185 if (node->size == SMALL_NODE_SIZE) {
186 check_not_already_free(node);
187 node->next = list->freelist;
188 list->freelist = node;
191 apr_allocator_free(list->allocator, node->memnode);