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
22#define ALLOC_AMT (8192 - APR_MEMNODE_T_SIZE)
23
24typedef struct node_header_t {
25    apr_size_t size;
26    apr_bucket_alloc_t *alloc;
27    apr_memnode_t *memnode;
28    struct node_header_t *next;
29} node_header_t;
30
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)
33
34/** A list of free memory from which new buckets or private bucket
35 *  structures can be allocated.
36 */
37struct apr_bucket_alloc_t {
38    apr_pool_t *pool;
39    apr_allocator_t *allocator;
40    node_header_t *freelist;
41    apr_memnode_t *blocks;
42};
43
44static apr_status_t alloc_cleanup(void *data)
45{
46    apr_bucket_alloc_t *list = data;
47
48    apr_allocator_free(list->allocator, list->blocks);
49
50#if APR_POOL_DEBUG
51    if (list->pool && list->allocator != apr_pool_allocator_get(list->pool)) {
52        apr_allocator_destroy(list->allocator);
53    }
54#endif
55
56    return APR_SUCCESS;
57}
58
59APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create(apr_pool_t *p)
60{
61    apr_allocator_t *allocator = apr_pool_allocator_get(p);
62    apr_bucket_alloc_t *list;
63
64#if APR_POOL_DEBUG
65    /* may be NULL for debug mode. */
66    if (allocator == NULL) {
67        if (apr_allocator_create(&allocator) != APR_SUCCESS) {
68            apr_abortfunc_t fn = apr_pool_abort_get(p);
69            if (fn)
70                (fn)(APR_ENOMEM);
71            abort();
72        }
73    }
74#endif
75    list = apr_bucket_alloc_create_ex(allocator);
76    if (list == NULL) {
77            apr_abortfunc_t fn = apr_pool_abort_get(p);
78            if (fn)
79                (fn)(APR_ENOMEM);
80            abort();
81    }
82    list->pool = p;
83    apr_pool_cleanup_register(list->pool, list, alloc_cleanup,
84                              apr_pool_cleanup_null);
85
86    return list;
87}
88
89APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create_ex(
90                                             apr_allocator_t *allocator)
91{
92    apr_bucket_alloc_t *list;
93    apr_memnode_t *block;
94
95    block = apr_allocator_alloc(allocator, ALLOC_AMT);
96    if (!block) {
97        return NULL;
98    }
99    list = (apr_bucket_alloc_t *)block->first_avail;
100    list->pool = NULL;
101    list->allocator = allocator;
102    list->freelist = NULL;
103    list->blocks = block;
104    block->first_avail += APR_ALIGN_DEFAULT(sizeof(*list));
105
106    return list;
107}
108
109APU_DECLARE_NONSTD(void) apr_bucket_alloc_destroy(apr_bucket_alloc_t *list)
110{
111    if (list->pool) {
112        apr_pool_cleanup_kill(list->pool, list, alloc_cleanup);
113    }
114
115    apr_allocator_free(list->allocator, list->blocks);
116
117#if APR_POOL_DEBUG
118    if (list->pool && list->allocator != apr_pool_allocator_get(list->pool)) {
119        apr_allocator_destroy(list->allocator);
120    }
121#endif
122}
123
124APU_DECLARE_NONSTD(void *) apr_bucket_alloc(apr_size_t size,
125                                            apr_bucket_alloc_t *list)
126{
127    node_header_t *node;
128    apr_memnode_t *active = list->blocks;
129    char *endp;
130
131    size += SIZEOF_NODE_HEADER_T;
132    if (size <= SMALL_NODE_SIZE) {
133        if (list->freelist) {
134            node = list->freelist;
135            list->freelist = node->next;
136        }
137        else {
138            endp = active->first_avail + SMALL_NODE_SIZE;
139            if (endp >= active->endp) {
140                list->blocks = apr_allocator_alloc(list->allocator, ALLOC_AMT);
141                if (!list->blocks) {
142                    list->blocks = active;
143                    return NULL;
144                }
145                list->blocks->next = active;
146                active = list->blocks;
147                endp = active->first_avail + SMALL_NODE_SIZE;
148            }
149            node = (node_header_t *)active->first_avail;
150            node->alloc = list;
151            node->memnode = active;
152            node->size = SMALL_NODE_SIZE;
153            active->first_avail = endp;
154        }
155    }
156    else {
157        apr_memnode_t *memnode = apr_allocator_alloc(list->allocator, size);
158        if (!memnode) {
159            return NULL;
160        }
161        node = (node_header_t *)memnode->first_avail;
162        node->alloc = list;
163        node->memnode = memnode;
164        node->size = size;
165    }
166    return ((char *)node) + SIZEOF_NODE_HEADER_T;
167}
168
169#ifdef APR_BUCKET_DEBUG
170#if APR_HAVE_STDLIB_H
171#include <stdlib.h>
172#endif
173static void check_not_already_free(node_header_t *node)
174{
175    apr_bucket_alloc_t *list = node->alloc;
176    node_header_t *curr = list->freelist;
177
178    while (curr) {
179        if (node == curr) {
180            abort();
181        }
182        curr = curr->next;
183    }
184}
185#else
186#define check_not_already_free(node)
187#endif
188
189APU_DECLARE_NONSTD(void) apr_bucket_free(void *mem)
190{
191    node_header_t *node = (node_header_t *)((char *)mem - SIZEOF_NODE_HEADER_T);
192    apr_bucket_alloc_t *list = node->alloc;
193
194    if (node->size == SMALL_NODE_SIZE) {
195        check_not_already_free(node);
196        node->next = list->freelist;
197        list->freelist = node;
198    }
199    else {
200        apr_allocator_free(list->allocator, node->memnode);
201    }
202}
203