|
Packit |
383869 |
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
Packit |
383869 |
* contributor license agreements. See the NOTICE file distributed with
|
|
Packit |
383869 |
* this work for additional information regarding copyright ownership.
|
|
Packit |
383869 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
Packit |
383869 |
* (the "License"); you may not use this file except in compliance with
|
|
Packit |
383869 |
* the License. You may obtain a copy of the License at
|
|
Packit |
383869 |
*
|
|
Packit |
383869 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
383869 |
*
|
|
Packit |
383869 |
* Unless required by applicable law or agreed to in writing, software
|
|
Packit |
383869 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
383869 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
383869 |
* See the License for the specific language governing permissions and
|
|
Packit |
383869 |
* limitations under the License.
|
|
Packit |
383869 |
*/
|
|
Packit |
383869 |
|
|
Packit |
383869 |
#include <stdlib.h>
|
|
Packit |
383869 |
|
|
Packit |
383869 |
#include "apr_buckets.h"
|
|
Packit |
383869 |
#include "apr_allocator.h"
|
|
Packit |
383869 |
#include "apr_version.h"
|
|
Packit |
383869 |
|
|
Packit |
383869 |
#define ALLOC_AMT (8192 - APR_MEMNODE_T_SIZE)
|
|
Packit |
383869 |
|
|
Packit |
383869 |
typedef struct node_header_t {
|
|
Packit |
383869 |
apr_size_t size;
|
|
Packit |
383869 |
apr_bucket_alloc_t *alloc;
|
|
Packit |
383869 |
apr_memnode_t *memnode;
|
|
Packit |
383869 |
struct node_header_t *next;
|
|
Packit |
383869 |
} node_header_t;
|
|
Packit |
383869 |
|
|
Packit |
383869 |
#define SIZEOF_NODE_HEADER_T APR_ALIGN_DEFAULT(sizeof(node_header_t))
|
|
Packit |
383869 |
#define SMALL_NODE_SIZE (APR_BUCKET_ALLOC_SIZE + SIZEOF_NODE_HEADER_T)
|
|
Packit |
383869 |
|
|
Packit |
383869 |
/** A list of free memory from which new buckets or private bucket
|
|
Packit |
383869 |
* structures can be allocated.
|
|
Packit |
383869 |
*/
|
|
Packit |
383869 |
struct apr_bucket_alloc_t {
|
|
Packit |
383869 |
apr_pool_t *pool;
|
|
Packit |
383869 |
apr_allocator_t *allocator;
|
|
Packit |
383869 |
node_header_t *freelist;
|
|
Packit |
383869 |
apr_memnode_t *blocks;
|
|
Packit |
383869 |
};
|
|
Packit |
383869 |
|
|
Packit |
383869 |
static apr_status_t alloc_cleanup(void *data)
|
|
Packit |
383869 |
{
|
|
Packit |
383869 |
apr_bucket_alloc_t *list = data;
|
|
Packit |
383869 |
|
|
Packit |
383869 |
apr_allocator_free(list->allocator, list->blocks);
|
|
Packit |
383869 |
|
|
Packit |
383869 |
#if APR_POOL_DEBUG
|
|
Packit |
383869 |
if (list->pool && list->allocator != apr_pool_allocator_get(list->pool)) {
|
|
Packit |
383869 |
apr_allocator_destroy(list->allocator);
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
#endif
|
|
Packit |
383869 |
|
|
Packit |
383869 |
return APR_SUCCESS;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
|
|
Packit |
383869 |
APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create(apr_pool_t *p)
|
|
Packit |
383869 |
{
|
|
Packit |
383869 |
apr_allocator_t *allocator = apr_pool_allocator_get(p);
|
|
Packit |
383869 |
apr_bucket_alloc_t *list;
|
|
Packit |
383869 |
|
|
Packit |
383869 |
#if APR_POOL_DEBUG
|
|
Packit |
383869 |
/* may be NULL for debug mode. */
|
|
Packit |
383869 |
if (allocator == NULL) {
|
|
Packit |
383869 |
if (apr_allocator_create(&allocator) != APR_SUCCESS) {
|
|
Packit |
383869 |
apr_abortfunc_t fn = apr_pool_abort_get(p);
|
|
Packit |
383869 |
if (fn)
|
|
Packit |
383869 |
(fn)(APR_ENOMEM);
|
|
Packit |
383869 |
abort();
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
#endif
|
|
Packit |
383869 |
list = apr_bucket_alloc_create_ex(allocator);
|
|
Packit |
383869 |
if (list == NULL) {
|
|
Packit |
383869 |
apr_abortfunc_t fn = apr_pool_abort_get(p);
|
|
Packit |
383869 |
if (fn)
|
|
Packit |
383869 |
(fn)(APR_ENOMEM);
|
|
Packit |
383869 |
abort();
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
list->pool = p;
|
|
Packit |
383869 |
apr_pool_cleanup_register(list->pool, list, alloc_cleanup,
|
|
Packit |
383869 |
apr_pool_cleanup_null);
|
|
Packit |
383869 |
|
|
Packit |
383869 |
return list;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
|
|
Packit |
383869 |
APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create_ex(
|
|
Packit |
383869 |
apr_allocator_t *allocator)
|
|
Packit |
383869 |
{
|
|
Packit |
383869 |
apr_bucket_alloc_t *list;
|
|
Packit |
383869 |
apr_memnode_t *block;
|
|
Packit |
383869 |
|
|
Packit |
383869 |
block = apr_allocator_alloc(allocator, ALLOC_AMT);
|
|
Packit |
383869 |
if (!block) {
|
|
Packit |
383869 |
return NULL;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
list = (apr_bucket_alloc_t *)block->first_avail;
|
|
Packit |
383869 |
list->pool = NULL;
|
|
Packit |
383869 |
list->allocator = allocator;
|
|
Packit |
383869 |
list->freelist = NULL;
|
|
Packit |
383869 |
list->blocks = block;
|
|
Packit |
383869 |
block->first_avail += APR_ALIGN_DEFAULT(sizeof(*list));
|
|
Packit |
383869 |
|
|
Packit |
383869 |
return list;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
|
|
Packit |
383869 |
APU_DECLARE_NONSTD(void) apr_bucket_alloc_destroy(apr_bucket_alloc_t *list)
|
|
Packit |
383869 |
{
|
|
Packit |
383869 |
if (list->pool) {
|
|
Packit |
383869 |
apr_pool_cleanup_kill(list->pool, list, alloc_cleanup);
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
|
|
Packit |
383869 |
apr_allocator_free(list->allocator, list->blocks);
|
|
Packit |
383869 |
|
|
Packit |
383869 |
#if APR_POOL_DEBUG
|
|
Packit |
383869 |
if (list->pool && list->allocator != apr_pool_allocator_get(list->pool)) {
|
|
Packit |
383869 |
apr_allocator_destroy(list->allocator);
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
#endif
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
|
|
Packit |
383869 |
APU_DECLARE_NONSTD(apr_size_t) apr_bucket_alloc_aligned_floor(apr_bucket_alloc_t *list,
|
|
Packit |
383869 |
apr_size_t size)
|
|
Packit |
383869 |
{
|
|
Packit |
383869 |
if (size <= SMALL_NODE_SIZE) {
|
|
Packit |
383869 |
size = SMALL_NODE_SIZE;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
else {
|
|
Packit |
383869 |
#if APR_VERSION_AT_LEAST(1,6,0)
|
|
Packit |
383869 |
if (size < APR_MEMNODE_T_SIZE) {
|
|
Packit |
383869 |
size = apr_allocator_align(list->allocator, 0);
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
else {
|
|
Packit |
383869 |
size = apr_allocator_align(list->allocator,
|
|
Packit |
383869 |
size - APR_MEMNODE_T_SIZE);
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
#else
|
|
Packit |
383869 |
/* Assumes the minimum (default) allocator's boundary of 4K and
|
|
Packit |
383869 |
* minimum (immutable before APR-1.6.x) allocation size of 8K,
|
|
Packit |
383869 |
* hence possibly (yet unlikely) under-estimating the floor...
|
|
Packit |
383869 |
*/
|
|
Packit |
383869 |
size = APR_ALIGN(size, 4096);
|
|
Packit |
383869 |
if (size < 8192) {
|
|
Packit |
383869 |
size = 8192;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
#endif
|
|
Packit |
383869 |
size -= APR_MEMNODE_T_SIZE;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
size -= SIZEOF_NODE_HEADER_T;
|
|
Packit |
383869 |
return size;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
|
|
Packit |
383869 |
APU_DECLARE_NONSTD(void *) apr_bucket_alloc(apr_size_t size,
|
|
Packit |
383869 |
apr_bucket_alloc_t *list)
|
|
Packit |
383869 |
{
|
|
Packit |
383869 |
node_header_t *node;
|
|
Packit |
383869 |
apr_memnode_t *active = list->blocks;
|
|
Packit |
383869 |
char *endp;
|
|
Packit |
383869 |
|
|
Packit |
383869 |
size += SIZEOF_NODE_HEADER_T;
|
|
Packit |
383869 |
if (size <= SMALL_NODE_SIZE) {
|
|
Packit |
383869 |
if (list->freelist) {
|
|
Packit |
383869 |
node = list->freelist;
|
|
Packit |
383869 |
list->freelist = node->next;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
else {
|
|
Packit |
383869 |
endp = active->first_avail + SMALL_NODE_SIZE;
|
|
Packit |
383869 |
if (endp >= active->endp) {
|
|
Packit |
383869 |
list->blocks = apr_allocator_alloc(list->allocator, ALLOC_AMT);
|
|
Packit |
383869 |
if (!list->blocks) {
|
|
Packit |
383869 |
list->blocks = active;
|
|
Packit |
383869 |
return NULL;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
list->blocks->next = active;
|
|
Packit |
383869 |
active = list->blocks;
|
|
Packit |
383869 |
endp = active->first_avail + SMALL_NODE_SIZE;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
node = (node_header_t *)active->first_avail;
|
|
Packit |
383869 |
node->alloc = list;
|
|
Packit |
383869 |
node->memnode = active;
|
|
Packit |
383869 |
node->size = SMALL_NODE_SIZE;
|
|
Packit |
383869 |
active->first_avail = endp;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
else {
|
|
Packit |
383869 |
apr_memnode_t *memnode = apr_allocator_alloc(list->allocator, size);
|
|
Packit |
383869 |
if (!memnode) {
|
|
Packit |
383869 |
return NULL;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
node = (node_header_t *)memnode->first_avail;
|
|
Packit |
383869 |
node->alloc = list;
|
|
Packit |
383869 |
node->memnode = memnode;
|
|
Packit |
383869 |
node->size = size;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
return ((char *)node) + SIZEOF_NODE_HEADER_T;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
|
|
Packit |
383869 |
#ifdef APR_BUCKET_DEBUG
|
|
Packit |
383869 |
#if APR_HAVE_STDLIB_H
|
|
Packit |
383869 |
#include <stdlib.h>
|
|
Packit |
383869 |
#endif
|
|
Packit |
383869 |
static void check_not_already_free(node_header_t *node)
|
|
Packit |
383869 |
{
|
|
Packit |
383869 |
apr_bucket_alloc_t *list = node->alloc;
|
|
Packit |
383869 |
node_header_t *curr = list->freelist;
|
|
Packit |
383869 |
|
|
Packit |
383869 |
while (curr) {
|
|
Packit |
383869 |
if (node == curr) {
|
|
Packit |
383869 |
abort();
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
curr = curr->next;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
#else
|
|
Packit |
383869 |
#define check_not_already_free(node)
|
|
Packit |
383869 |
#endif
|
|
Packit |
383869 |
|
|
Packit |
383869 |
APU_DECLARE_NONSTD(void) apr_bucket_free(void *mem)
|
|
Packit |
383869 |
{
|
|
Packit |
383869 |
node_header_t *node = (node_header_t *)((char *)mem - SIZEOF_NODE_HEADER_T);
|
|
Packit |
383869 |
apr_bucket_alloc_t *list = node->alloc;
|
|
Packit |
383869 |
|
|
Packit |
383869 |
if (node->size == SMALL_NODE_SIZE) {
|
|
Packit |
383869 |
check_not_already_free(node);
|
|
Packit |
383869 |
node->next = list->freelist;
|
|
Packit |
383869 |
list->freelist = node;
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
else {
|
|
Packit |
383869 |
apr_allocator_free(list->allocator, node->memnode);
|
|
Packit |
383869 |
}
|
|
Packit |
383869 |
}
|