~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr-util/buckets/apr_buckets_alloc.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
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
 
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
 
 
24
typedef 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
 */
 
37
struct 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
 
 
44
static 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
 
 
59
APU_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
            abort();
 
69
        }
 
70
    }
 
71
#endif
 
72
 
 
73
    list = apr_bucket_alloc_create_ex(allocator);
 
74
    list->pool = p;
 
75
    apr_pool_cleanup_register(list->pool, list, alloc_cleanup,
 
76
                              apr_pool_cleanup_null);
 
77
 
 
78
    return list;
 
79
}
 
80
 
 
81
APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create_ex(
 
82
                                             apr_allocator_t *allocator)
 
83
{
 
84
    apr_bucket_alloc_t *list;
 
85
    apr_memnode_t *block;
 
86
 
 
87
    block = apr_allocator_alloc(allocator, ALLOC_AMT);
 
88
    list = (apr_bucket_alloc_t *)block->first_avail;
 
89
    list->pool = NULL;
 
90
    list->allocator = allocator;
 
91
    list->freelist = NULL;
 
92
    list->blocks = block;
 
93
    block->first_avail += APR_ALIGN_DEFAULT(sizeof(*list));
 
94
 
 
95
    return list;
 
96
}
 
97
 
 
98
APU_DECLARE_NONSTD(void) apr_bucket_alloc_destroy(apr_bucket_alloc_t *list)
 
99
{
 
100
    if (list->pool) {
 
101
        apr_pool_cleanup_kill(list->pool, list, alloc_cleanup);
 
102
    }
 
103
 
 
104
    apr_allocator_free(list->allocator, list->blocks);
 
105
 
 
106
#if APR_POOL_DEBUG
 
107
    if (list->pool && list->allocator != apr_pool_allocator_get(list->pool)) {
 
108
        apr_allocator_destroy(list->allocator);
 
109
    }
 
110
#endif
 
111
}
 
112
 
 
113
APU_DECLARE_NONSTD(void *) apr_bucket_alloc(apr_size_t size, 
 
114
                                            apr_bucket_alloc_t *list)
 
115
{
 
116
    node_header_t *node;
 
117
    apr_memnode_t *active = list->blocks;
 
118
    char *endp;
 
119
 
 
120
    size += SIZEOF_NODE_HEADER_T;
 
121
    if (size <= SMALL_NODE_SIZE) {
 
122
        if (list->freelist) {
 
123
            node = list->freelist;
 
124
            list->freelist = node->next;
 
125
        }
 
126
        else {
 
127
            endp = active->first_avail + SMALL_NODE_SIZE;
 
128
            if (endp >= active->endp) {
 
129
                list->blocks = apr_allocator_alloc(list->allocator, ALLOC_AMT);
 
130
                list->blocks->next = active;
 
131
                active = list->blocks;
 
132
                endp = active->first_avail + SMALL_NODE_SIZE;
 
133
            }
 
134
            node = (node_header_t *)active->first_avail;
 
135
            node->alloc = list;
 
136
            node->memnode = active;
 
137
            node->size = SMALL_NODE_SIZE;
 
138
            active->first_avail = endp;
 
139
        }
 
140
    }
 
141
    else {
 
142
        apr_memnode_t *memnode = apr_allocator_alloc(list->allocator, size);
 
143
        node = (node_header_t *)memnode->first_avail;
 
144
        node->alloc = list;
 
145
        node->memnode = memnode;
 
146
        node->size = size;
 
147
    }
 
148
    return ((char *)node) + SIZEOF_NODE_HEADER_T;
 
149
}
 
150
 
 
151
#ifdef APR_BUCKET_DEBUG
 
152
#if APR_HAVE_STDLIB_H
 
153
#include <stdlib.h>
 
154
#endif
 
155
static void check_not_already_free(node_header_t *node)
 
156
{
 
157
    apr_bucket_alloc_t *list = node->alloc;
 
158
    node_header_t *curr = list->freelist;
 
159
 
 
160
    while (curr) {
 
161
        if (node == curr) {
 
162
            abort();
 
163
        }
 
164
        curr = curr->next;
 
165
    }
 
166
}
 
167
#else
 
168
#define check_not_already_free(node)
 
169
#endif
 
170
 
 
171
APU_DECLARE_NONSTD(void) apr_bucket_free(void *mem)
 
172
{
 
173
    node_header_t *node = (node_header_t *)((char *)mem - SIZEOF_NODE_HEADER_T);
 
174
    apr_bucket_alloc_t *list = node->alloc;
 
175
 
 
176
    if (node->size == SMALL_NODE_SIZE) {
 
177
        check_not_already_free(node);
 
178
        node->next = list->freelist;
 
179
        list->freelist = node;
 
180
    }
 
181
    else {
 
182
        apr_allocator_free(list->allocator, node->memnode);
 
183
    }
 
184
}