~ubuntu-branches/ubuntu/oneiric/libapache-mod-jk/oneiric

« back to all changes in this revision

Viewing changes to jk/native/common/jk_pool.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2006-08-05 16:30:53 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060805163053-myf66gm6j1a21ps6
Tags: 1:1.2.18-1ubuntu1
Merge from Debian unstable.

Show diffs side-by-side

added added

removed removed

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