bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / tomcat-connectors-1.2.32-src / native / common / jk_context.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: Context handling (Autoconf)                                *
20  * Author:      Henri Gomez <hgomez@apache.org>                            *
21  * Version:     $Revision: 466585 $                                           *
22  ***************************************************************************/
23
24 #include "jk_global.h"
25 #include "jk_context.h"
26 #include "jk_ajp_common.h"
27
28
29 /*
30  * Set the virtual name of the context 
31  */
32
33 int context_set_virtual(jk_context_t *c, char *virt)
34 {
35     if (c) {
36
37         if (virt) {
38             c->virt = jk_pool_strdup(&c->p, virt);
39
40             if (!c->virt)
41                 return JK_FALSE;
42         }
43
44         return JK_TRUE;
45     }
46
47     return JK_FALSE;
48 }
49
50 /*
51  * Init the context info struct
52  */
53
54 int context_open(jk_context_t *c, char *virt)
55 {
56     if (c) {
57         jk_open_pool(&c->p, c->buf, sizeof(jk_pool_atom_t) * SMALL_POOL_SIZE);
58         c->size = 0;
59         c->capacity = 0;
60         c->contexts = NULL;
61
62         return context_set_virtual(c, virt);
63     }
64
65     return JK_FALSE;
66 }
67
68 /*
69  * Close the context info struct
70  */
71
72 int context_close(jk_context_t *c)
73 {
74     if (c) {
75
76         jk_close_pool(&c->p);
77         return JK_TRUE;
78     }
79
80     return JK_FALSE;
81 }
82
83 /*
84  * Allocate and open context
85  */
86
87 int context_alloc(jk_context_t **c, char *virt)
88 {
89     if (c)
90         return context_open(*c =
91                             (jk_context_t *)calloc(1, sizeof(jk_context_t)),
92                             virt);
93
94     return JK_FALSE;
95 }
96
97 /*
98  * Close and destroy context
99  */
100
101 int context_free(jk_context_t **c)
102 {
103     if (c && *c) {
104         context_close(*c);
105         free(*c);
106         *c = NULL;
107         return JK_TRUE;
108     }
109
110     return JK_FALSE;
111 }
112
113
114 /*
115  * Ensure there will be memory in context info to store Context Bases
116  */
117
118 static int context_realloc(jk_context_t *c)
119 {
120     if (c->size == c->capacity) {
121         jk_context_item_t **contexts;
122         int capacity = c->capacity + CBASE_INC_SIZE;
123
124         contexts =
125             (jk_context_item_t **)jk_pool_alloc(&c->p,
126                                                 sizeof(jk_context_item_t *) *
127                                                 capacity);
128
129         if (!contexts)
130             return JK_FALSE;
131
132         if (c->capacity && c->contexts)
133             memcpy(contexts, c->contexts,
134                    sizeof(jk_context_item_t *) * c->capacity);
135
136         c->contexts = contexts;
137         c->capacity = capacity;
138     }
139
140     return JK_TRUE;
141 }
142
143 /*
144  * Ensure there will be memory in context info to URIS
145  */
146
147 static int context_item_realloc(jk_context_t *c, jk_context_item_t *ci)
148 {
149     if (ci->size == ci->capacity) {
150         char **uris;
151         int capacity = ci->capacity + URI_INC_SIZE;
152
153         uris = (char **)jk_pool_alloc(&c->p, sizeof(char *) * capacity);
154
155         if (!uris)
156             return JK_FALSE;
157
158         memcpy(uris, ci->uris, sizeof(char *) * ci->capacity);
159
160         ci->uris = uris;
161         ci->capacity = capacity;
162     }
163
164     return JK_TRUE;
165 }
166
167
168 /*
169  * Locate a context base in context list
170  */
171
172 jk_context_item_t *context_find_base(jk_context_t *c, char *cbase)
173 {
174     int i;
175     jk_context_item_t *ci;
176
177     if (!c || !cbase)
178         return NULL;
179
180     for (i = 0; i < c->size; i++) {
181
182         ci = c->contexts[i];
183
184         if (!ci)
185             continue;
186
187         if (!strcmp(ci->cbase, cbase))
188             return ci;
189     }
190
191     return NULL;
192 }
193
194 /*
195  * Locate an URI in a context item
196  */
197
198 char *context_item_find_uri(jk_context_item_t *ci, char *uri)
199 {
200     int i;
201
202     if (!ci || !uri)
203         return NULL;
204
205     for (i = 0; i < ci->size; i++) {
206         if (!strcmp(ci->uris[i], uri))
207             return ci->uris[i];
208     }
209
210     return NULL;
211 }
212
213 void context_dump_uris(jk_context_t *c, char *cbase, FILE * f)
214 {
215     jk_context_item_t *ci;
216     int i;
217
218     ci = context_find_base(c, cbase);
219
220     if (!ci)
221         return;
222
223     for (i = 0; i < ci->size; i++)
224         fprintf(f, "/%s/%s\n", ci->cbase, ci->uris[i]);
225
226     fflush(f);
227 }
228
229
230 /*
231  * Add a new context item to context
232  */
233
234 jk_context_item_t *context_add_base(jk_context_t *c, char *cbase)
235 {
236     jk_context_item_t *ci;
237
238     if (!c || !cbase)
239         return NULL;
240
241     /* Check if the context base was not allready created */
242     ci = context_find_base(c, cbase);
243
244     if (ci)
245         return ci;
246
247     if (context_realloc(c) != JK_TRUE)
248         return NULL;
249
250     ci = (jk_context_item_t *)jk_pool_alloc(&c->p, sizeof(jk_context_item_t));
251
252     if (!ci)
253         return NULL;
254
255     c->contexts[c->size] = ci;
256     c->size++;
257     ci->cbase = jk_pool_strdup(&c->p, cbase);
258     ci->status = 0;
259     ci->size = 0;
260     ci->capacity = 0;
261     ci->uris = NULL;
262
263     return ci;
264 }
265
266 /*
267  * Add a new URI to a context item
268  */
269
270 int context_add_uri(jk_context_t *c, char *cbase, char *uri)
271 {
272     jk_context_item_t *ci;
273
274     if (!uri)
275         return JK_FALSE;
276
277     /* Get/Create the context base */
278     ci = context_add_base(c, cbase);
279
280     if (!ci)
281         return JK_FALSE;
282
283     if (context_item_find_uri(ci, uri) != NULL)
284         return JK_TRUE;
285
286     if (context_item_realloc(c, ci) == JK_FALSE)
287         return JK_FALSE;
288
289     ci->uris[ci->size] = jk_pool_strdup(&c->p, uri);
290
291     if (ci->uris[ci->size] == NULL)
292         return JK_FALSE;
293
294     ci->size++;
295     return JK_TRUE;
296 }