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