bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / tomcat-connectors-1.2.32-src / native / common / jk_pool.c
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /***************************************************************************
19  * Description: Simple memory pool                                         *
20  * Author:      Gal Shachor <shachor@il.ibm.com>                           *
21  * Version:     $Revision: 466585 $                                           *
22  ***************************************************************************/
23
24 #include "jk_pool.h"
25
26 #define DEFAULT_DYNAMIC 10
27
28
29 static void *jk_pool_dyn_alloc(jk_pool_t *p, size_t size);
30
31
32 void jk_open_pool(jk_pool_t *p, jk_pool_atom_t *buf, size_t size)
33 {
34     p->pos = 0;
35     p->size = size;
36     p->buf = (char *)buf;
37
38     p->dyn_pos = 0;
39     p->dynamic = NULL;
40     p->dyn_size = 0;
41 }
42
43 void jk_close_pool(jk_pool_t *p)
44 {
45     jk_reset_pool(p);
46     if (p->dynamic) {
47         free(p->dynamic);
48     }
49 }
50
51 void jk_reset_pool(jk_pool_t *p)
52 {
53     if (p->dyn_pos && p->dynamic) {
54         size_t i;
55         for (i = 0; i < p->dyn_pos; i++) {
56             if (p->dynamic[i]) {
57                 free(p->dynamic[i]);
58             }
59         }
60     }
61
62     p->dyn_pos = 0;
63     p->pos = 0;
64 }
65
66 void *jk_pool_alloc(jk_pool_t *p, size_t size)
67 {
68     void *rc = NULL;
69
70     size = JK_ALIGN_DEFAULT(size);
71     if ((p->size - p->pos) >= size) {
72         rc = &(p->buf[p->pos]);
73         p->pos += size;
74     }
75     else {
76         rc = jk_pool_dyn_alloc(p, size);
77     }
78
79     return rc;
80 }
81
82 void *jk_pool_realloc(jk_pool_t *p, size_t sz, const void *old, size_t old_sz)
83 {
84     void *rc;
85
86     if (!p || (!old && old_sz)) {
87         return NULL;
88     }
89
90     rc = jk_pool_alloc(p, sz);
91     if (rc) {
92         memcpy(rc, old, old_sz);
93     }
94
95     return rc;
96 }
97
98 void *jk_pool_strdup(jk_pool_t *p, const char *s)
99 {
100     void *rc = NULL;
101     if (s && p) {
102         size_t size = strlen(s);
103
104         if (!size) {
105             return "";
106         }
107
108         size++;
109         rc = jk_pool_alloc(p, size);
110         if (rc) {
111             memcpy(rc, s, size);
112         }
113     }
114
115     return rc;
116 }
117
118 #if defined (DEBUG) || defined(_DEBUG)
119 static void jk_dump_pool(jk_pool_t *p, FILE * f)
120 {
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);
128
129     fflush(f);
130 }
131 #endif
132
133 static void *jk_pool_dyn_alloc(jk_pool_t *p, size_t size)
134 {
135     void *rc;
136
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 *));
140         if (new_dynamic) {
141             if (p->dynamic) {
142                 /* Copy old dynamic slots */
143                 memcpy(new_dynamic, p->dynamic, p->dyn_size * sizeof(void *));
144
145                 free(p->dynamic);
146             }
147
148             p->dynamic = new_dynamic;
149             p->dyn_size = new_dyn_size;
150         }
151         else {
152 #if defined (DEBUG) || defined(_DEBUG)
153             jk_dump_pool(p, stderr);
154 #endif            
155             return NULL;
156         }
157     }
158
159     rc = p->dynamic[p->dyn_pos] = malloc(size);
160     if (p->dynamic[p->dyn_pos]) {
161         p->dyn_pos++;
162     }
163
164     return rc;
165 }