2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 /***************************************************************************
19 * Description: Simple memory pool *
20 * Author: Gal Shachor <shachor@il.ibm.com> *
21 * Version: $Revision: 466585 $ *
22 ***************************************************************************/
26 #define DEFAULT_DYNAMIC 10
29 static void *jk_pool_dyn_alloc(jk_pool_t *p, size_t size);
32 void jk_open_pool(jk_pool_t *p, jk_pool_atom_t *buf, size_t size)
43 void jk_close_pool(jk_pool_t *p)
51 void jk_reset_pool(jk_pool_t *p)
53 if (p->dyn_pos && p->dynamic) {
55 for (i = 0; i < p->dyn_pos; i++) {
66 void *jk_pool_alloc(jk_pool_t *p, size_t size)
70 size = JK_ALIGN_DEFAULT(size);
71 if ((p->size - p->pos) >= size) {
72 rc = &(p->buf[p->pos]);
76 rc = jk_pool_dyn_alloc(p, size);
82 void *jk_pool_realloc(jk_pool_t *p, size_t sz, const void *old, size_t old_sz)
86 if (!p || (!old && old_sz)) {
90 rc = jk_pool_alloc(p, sz);
92 memcpy(rc, old, old_sz);
98 void *jk_pool_strdup(jk_pool_t *p, const char *s)
102 size_t size = strlen(s);
109 rc = jk_pool_alloc(p, size);
118 #if defined (DEBUG) || defined(_DEBUG)
119 static void jk_dump_pool(jk_pool_t *p, FILE * f)
121 fprintf(f, "Dumping for pool [%p]\n", p);
122 fprintf(f, "size [%ld]\n", p->size);
123 fprintf(f, "pos [%ld]\n", p->pos);
124 fprintf(f, "buf [%p]\n", p->buf);
125 fprintf(f, "dyn_size [%ld]\n", p->dyn_size);
126 fprintf(f, "dyn_pos [%ld]\n", p->dyn_pos);
127 fprintf(f, "dynamic [%p]\n", p->dynamic);
133 static void *jk_pool_dyn_alloc(jk_pool_t *p, size_t size)
137 if (p->dyn_size == p->dyn_pos) {
138 size_t new_dyn_size = p->dyn_size * 2 + DEFAULT_DYNAMIC;
139 void **new_dynamic = (void **)malloc(new_dyn_size * sizeof(void *));
142 /* Copy old dynamic slots */
143 memcpy(new_dynamic, p->dynamic, p->dyn_size * sizeof(void *));
148 p->dynamic = new_dynamic;
149 p->dyn_size = new_dyn_size;
152 #if defined (DEBUG) || defined(_DEBUG)
153 jk_dump_pool(p, stderr);
159 rc = p->dynamic[p->dyn_pos] = malloc(size);
160 if (p->dynamic[p->dyn_pos]) {