2
* Copyright 1999-2004 The Apache Software Foundation
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.
17
/***************************************************************************
18
* Description: Simple memory pool *
19
* Author: Gal Shachor <shachor@il.ibm.com> *
20
* Version: $Revision: 1.14 $ *
21
***************************************************************************/
25
#define DEFAULT_DYNAMIC 10
28
static void *jk_pool_dyn_alloc(jk_pool_t *p, size_t size);
31
void jk_open_pool(jk_pool_t *p, jk_pool_atom_t *buf, size_t size)
42
void jk_close_pool(jk_pool_t *p)
50
void jk_reset_pool(jk_pool_t *p)
52
if (p->dyn_pos && p->dynamic) {
54
for (i = 0; i < p->dyn_pos; i++) {
65
void *jk_pool_alloc(jk_pool_t *p, size_t size)
69
size = JK_ALIGN_DEFAULT(size);
70
if ((p->size - p->pos) >= size) {
71
rc = &(p->buf[p->pos]);
75
rc = jk_pool_dyn_alloc(p, size);
81
void *jk_pool_realloc(jk_pool_t *p, size_t sz, const void *old, size_t old_sz)
85
if (!p || (!old && old_sz)) {
89
rc = jk_pool_alloc(p, sz);
91
memcpy(rc, old, old_sz);
97
void *jk_pool_strdup(jk_pool_t *p, const char *s)
101
size_t size = strlen(s);
108
rc = jk_pool_alloc(p, size);
117
#if defined (DEBUG) || defined(_DEBUG)
118
static void jk_dump_pool(jk_pool_t *p, FILE * f)
120
fprintf(f, "Dumping for pool [%p]\n", p);
121
fprintf(f, "size [%ld]\n", p->size);
122
fprintf(f, "pos [%ld]\n", p->pos);
123
fprintf(f, "buf [%p]\n", p->buf);
124
fprintf(f, "dyn_size [%ld]\n", p->dyn_size);
125
fprintf(f, "dyn_pos [%ld]\n", p->dyn_pos);
126
fprintf(f, "dynamic [%p]\n", p->dynamic);
132
static void *jk_pool_dyn_alloc(jk_pool_t *p, size_t size)
136
if (p->dyn_size == p->dyn_pos) {
137
size_t new_dyn_size = p->dyn_size * 2 + DEFAULT_DYNAMIC;
138
void **new_dynamic = (void **)malloc(new_dyn_size * sizeof(void *));
141
/* Copy old dynamic slots */
142
memcpy(new_dynamic, p->dynamic, p->dyn_size * sizeof(void *));
147
p->dynamic = new_dynamic;
148
p->dyn_size = new_dyn_size;
151
#if defined (DEBUG) || defined(_DEBUG)
152
jk_dump_pool(p, stderr);
158
rc = p->dynamic[p->dyn_pos] = malloc(size);
159
if (p->dynamic[p->dyn_pos]) {