blob: 5fa5ff741a4db2a9e51cc1590c5ac89e5cf8274d [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pj/pool.h>
20#include <pj/log.h>
21#include <pj/string.h>
22#include <pj/assert.h>
23#include <pj/os.h>
24
25static pj_pool_t* cpool_create_pool(pj_pool_factory *pf,
26 const char *name,
27 pj_size_t initial_size,
28 pj_size_t increment_sz,
29 pj_pool_callback *callback);
30static void cpool_release_pool(pj_pool_factory *pf, pj_pool_t *pool);
31static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail );
32
33static pj_size_t pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE] =
34{
35 256, 512, 1024, 2048, 4096, 8192, 12288, 16384,
36 20480, 24576, 28672, 32768, 40960, 49152, 57344, 65536
37};
38
39
40PJ_DEF(void) pj_caching_pool_init( pj_caching_pool *cp,
41 const pj_pool_factory_policy *policy,
42 pj_size_t max_capacity)
43{
44 int i;
45
46 PJ_CHECK_STACK();
47
48 pj_memset(cp, 0, sizeof(*cp));
49
50 cp->max_capacity = max_capacity;
51 pj_list_init(&cp->used_list);
52 for (i=0; i<PJ_CACHING_POOL_ARRAY_SIZE; ++i)
53 pj_list_init(&cp->free_list[i]);
54
55 pj_memcpy(&cp->factory.policy, policy, sizeof(pj_pool_factory_policy));
56 cp->factory.create_pool = &cpool_create_pool;
57 cp->factory.release_pool = &cpool_release_pool;
58 cp->factory.dump_status = &cpool_dump_status;
59}
60
61PJ_DEF(void) pj_caching_pool_destroy( pj_caching_pool *cp )
62{
63 int i;
64 pj_pool_t *pool;
65
66 PJ_CHECK_STACK();
67
68 /* Delete all pool in free list */
69 for (i=0; i < PJ_CACHING_POOL_ARRAY_SIZE; ++i) {
70 pj_pool_t *pool = cp->free_list[i].next;
71 pj_pool_t *next;
72 for (; pool != (void*)&cp->free_list[i]; pool = next) {
73 next = pool->next;
74 pj_list_erase(pool);
75 pj_pool_destroy_int(pool);
76 }
77 }
78
79 /* Delete all pools in used list */
80 pool = cp->used_list.next;
81 while (pool != (pj_pool_t*) &cp->used_list) {
82 pj_pool_t *next = pool->next;
83 pj_list_erase(pool);
84 pj_pool_destroy_int(pool);
85 pool = next;
86 }
87}
88
89static pj_pool_t* cpool_create_pool(pj_pool_factory *pf,
90 const char *name,
91 pj_size_t initial_size,
92 pj_size_t increment_sz,
93 pj_pool_callback *callback)
94{
95 pj_caching_pool *cp = (pj_caching_pool*)pf;
96 pj_pool_t *pool;
97 int idx;
98
99 PJ_CHECK_STACK();
100
101 /* Use pool factory's policy when callback is NULL */
102 if (callback == NULL) {
103 callback = pf->policy.callback;
104 }
105
106 /* Search the suitable size for the pool.
107 * We'll just do linear search to the size array, as the array size itself
108 * is only a few elements. Binary search I suspect will be less efficient
109 * for this purpose.
110 */
111 for (idx=0;
112 idx < PJ_CACHING_POOL_ARRAY_SIZE && pool_sizes[idx] < initial_size;
113 ++idx)
114 ;
115
116 /* Check whether there's a pool in the list. */
117 if (idx==PJ_CACHING_POOL_ARRAY_SIZE || pj_list_empty(&cp->free_list[idx])) {
118 /* No pool is available. */
119 /* Set minimum size. */
120 if (idx < PJ_CACHING_POOL_ARRAY_SIZE)
121 initial_size = pool_sizes[idx];
122
123 /* Create new pool */
124 pool = pj_pool_create_int(&cp->factory, name, initial_size,
125 increment_sz, callback);
126 if (!pool)
127 return NULL;
128
129 } else {
130 /* Get one pool from the list. */
131 pool = cp->free_list[idx].next;
132 pj_list_erase(pool);
133
134 /* Initialize the pool. */
135 pj_pool_init_int(pool, name, increment_sz, callback);
136
137 /* Update pool manager's free capacity. */
138 cp->capacity -= pj_pool_get_capacity(pool);
139
140 PJ_LOG(5, (pool->obj_name, "pool reused, size=%u", pool->capacity));
141 }
142
143 /* Put in used list. */
144 pj_list_insert_before( &cp->used_list, pool );
145
146 /* Increment used count. */
147 ++cp->used_count;
148 return pool;
149}
150
151static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool)
152{
153 pj_caching_pool *cp = (pj_caching_pool*)pf;
154 int i;
155
156 PJ_CHECK_STACK();
157
158 /* Erase from the used list. */
159 pj_list_erase(pool);
160
161 /* Decrement used count. */
162 --cp->used_count;
163
164 /* Destroy the pool if the size is greater than our size or if the total
165 * capacity in our recycle list (plus the size of the pool) exceeds
166 * maximum capacity.
167 . */
168 if (pool->capacity > pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE-1] ||
169 cp->capacity + pool->capacity > cp->max_capacity)
170 {
171 pj_pool_destroy_int(pool);
172 return;
173 }
174
175 /* Reset pool. */
176 PJ_LOG(4, (pool->obj_name, "recycle(): cap=%d, used=%d(%d%%)",
177 pool->capacity, pool->used_size, pool->used_size*100/pool->capacity));
178 pj_pool_reset(pool);
179
180 /*
181 * Otherwise put the pool in our recycle list.
182 */
183 for (i=0; i < PJ_CACHING_POOL_ARRAY_SIZE && pool_sizes[i] != pool->capacity; ++i)
184 ;
185
186 pj_assert( i != PJ_CACHING_POOL_ARRAY_SIZE );
187 if (i == PJ_CACHING_POOL_ARRAY_SIZE) {
188 /* Something has gone wrong with the pool. */
189 pj_pool_destroy_int(pool);
190 return;
191 }
192
193 pj_list_insert_after(&cp->free_list[i], pool);
194 cp->capacity += pool->capacity;
195}
196
197static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail )
198{
199#if PJ_LOG_MAX_LEVEL >= 3
200 pj_caching_pool *cp = (pj_caching_pool*)factory;
201 PJ_LOG(3,("cachpool", " Dumping caching pool:"));
202 PJ_LOG(3,("cachpool", " Capacity=%u, max_capacity=%u, used_cnt=%u", \
203 cp->capacity, cp->max_capacity, cp->used_count));
204 if (detail) {
205 pj_pool_t *pool = cp->used_list.next;
206 pj_uint32_t total_used = 0, total_capacity = 0;
207 PJ_LOG(3,("cachpool", " Dumping all active pools:"));
208 while (pool != (void*)&cp->used_list) {
209 PJ_LOG(3,("cachpool", " %12s: %8d of %8d (%d%%) used", pool->obj_name,
210 pool->used_size, pool->capacity,
211 pool->used_size*100/pool->capacity));
212 total_used += pool->used_size;
213 total_capacity += pool->capacity;
214 pool = pool->next;
215 }
216 PJ_LOG(3,("cachpool", " Total %9d of %9d (%d %%) used!",
217 total_used, total_capacity,
218 total_used * 100 / total_capacity));
219 }
220#endif
221}