blob: 35bc580b1d0b18d42eebc11aa5381dc04e7e8032 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00004 *
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>
Benny Prijono31333b62006-11-21 08:36:12 +000023#include <pj/lock.h>
Benny Prijono9033e312005-11-21 02:08:39 +000024#include <pj/os.h>
Benny Prijono31333b62006-11-21 08:36:12 +000025#include <pj/pool_buf.h>
Benny Prijono9033e312005-11-21 02:08:39 +000026
Benny Prijono8508aa02006-03-30 15:56:01 +000027#if !PJ_HAS_POOL_ALT_API
28
Benny Prijono9033e312005-11-21 02:08:39 +000029static pj_pool_t* cpool_create_pool(pj_pool_factory *pf,
30 const char *name,
31 pj_size_t initial_size,
32 pj_size_t increment_sz,
33 pj_pool_callback *callback);
34static void cpool_release_pool(pj_pool_factory *pf, pj_pool_t *pool);
35static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail );
Benny Prijonoc6e165f2006-07-09 10:05:46 +000036static pj_bool_t cpool_on_block_alloc(pj_pool_factory *f, pj_size_t sz);
37static void cpool_on_block_free(pj_pool_factory *f, pj_size_t sz);
38
Benny Prijono9033e312005-11-21 02:08:39 +000039
40static pj_size_t pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE] =
41{
42 256, 512, 1024, 2048, 4096, 8192, 12288, 16384,
43 20480, 24576, 28672, 32768, 40960, 49152, 57344, 65536
44};
45
Benny Prijono53e3ffd2006-07-06 14:27:31 +000046/* Index where the search for size should begin.
47 * Start with pool_sizes[5], which is 8192.
48 */
49#define START_SIZE 5
50
Benny Prijono9033e312005-11-21 02:08:39 +000051
52PJ_DEF(void) pj_caching_pool_init( pj_caching_pool *cp,
53 const pj_pool_factory_policy *policy,
54 pj_size_t max_capacity)
55{
56 int i;
Benny Prijono31333b62006-11-21 08:36:12 +000057 pj_pool_t *pool;
Benny Prijono9033e312005-11-21 02:08:39 +000058
59 PJ_CHECK_STACK();
60
Benny Prijonoac623b32006-07-03 15:19:31 +000061 pj_bzero(cp, sizeof(*cp));
Benny Prijono9033e312005-11-21 02:08:39 +000062
63 cp->max_capacity = max_capacity;
64 pj_list_init(&cp->used_list);
65 for (i=0; i<PJ_CACHING_POOL_ARRAY_SIZE; ++i)
66 pj_list_init(&cp->free_list[i]);
67
68 pj_memcpy(&cp->factory.policy, policy, sizeof(pj_pool_factory_policy));
69 cp->factory.create_pool = &cpool_create_pool;
70 cp->factory.release_pool = &cpool_release_pool;
71 cp->factory.dump_status = &cpool_dump_status;
Benny Prijonoc6e165f2006-07-09 10:05:46 +000072 cp->factory.on_block_alloc = &cpool_on_block_alloc;
73 cp->factory.on_block_free = &cpool_on_block_free;
Benny Prijonoc8322f32006-02-26 21:18:42 +000074
Benny Prijono31333b62006-11-21 08:36:12 +000075 pool = pj_pool_create_on_buf("cachingpool", cp->pool_buf, sizeof(cp->pool_buf));
76 pj_lock_create_simple_mutex(pool, "cachingpool", &cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +000077}
78
79PJ_DEF(void) pj_caching_pool_destroy( pj_caching_pool *cp )
80{
81 int i;
82 pj_pool_t *pool;
83
84 PJ_CHECK_STACK();
85
86 /* Delete all pool in free list */
87 for (i=0; i < PJ_CACHING_POOL_ARRAY_SIZE; ++i) {
88 pj_pool_t *pool = cp->free_list[i].next;
89 pj_pool_t *next;
90 for (; pool != (void*)&cp->free_list[i]; pool = next) {
91 next = pool->next;
92 pj_list_erase(pool);
93 pj_pool_destroy_int(pool);
94 }
95 }
96
97 /* Delete all pools in used list */
98 pool = cp->used_list.next;
99 while (pool != (pj_pool_t*) &cp->used_list) {
100 pj_pool_t *next = pool->next;
101 pj_list_erase(pool);
102 pj_pool_destroy_int(pool);
103 pool = next;
104 }
Benny Prijonoc8322f32006-02-26 21:18:42 +0000105
Benny Prijono31333b62006-11-21 08:36:12 +0000106 if (cp->lock) {
107 pj_lock_destroy(cp->lock);
108 pj_lock_create_null_mutex(NULL, "cachingpool", &cp->lock);
109 }
Benny Prijono9033e312005-11-21 02:08:39 +0000110}
111
112static pj_pool_t* cpool_create_pool(pj_pool_factory *pf,
113 const char *name,
114 pj_size_t initial_size,
115 pj_size_t increment_sz,
116 pj_pool_callback *callback)
117{
118 pj_caching_pool *cp = (pj_caching_pool*)pf;
119 pj_pool_t *pool;
120 int idx;
121
122 PJ_CHECK_STACK();
123
Benny Prijono31333b62006-11-21 08:36:12 +0000124 pj_lock_acquire(cp->lock);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000125
Benny Prijono9033e312005-11-21 02:08:39 +0000126 /* Use pool factory's policy when callback is NULL */
127 if (callback == NULL) {
128 callback = pf->policy.callback;
129 }
130
131 /* Search the suitable size for the pool.
132 * We'll just do linear search to the size array, as the array size itself
133 * is only a few elements. Binary search I suspect will be less efficient
134 * for this purpose.
135 */
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000136 if (initial_size <= pool_sizes[START_SIZE]) {
137 for (idx=START_SIZE-1;
138 idx >= 0 && pool_sizes[idx] >= initial_size;
139 --idx)
140 ;
141 ++idx;
142 } else {
143 for (idx=START_SIZE+1;
144 idx < PJ_CACHING_POOL_ARRAY_SIZE &&
145 pool_sizes[idx] < initial_size;
146 ++idx)
147 ;
148 }
Benny Prijono9033e312005-11-21 02:08:39 +0000149
150 /* Check whether there's a pool in the list. */
151 if (idx==PJ_CACHING_POOL_ARRAY_SIZE || pj_list_empty(&cp->free_list[idx])) {
152 /* No pool is available. */
153 /* Set minimum size. */
154 if (idx < PJ_CACHING_POOL_ARRAY_SIZE)
155 initial_size = pool_sizes[idx];
156
157 /* Create new pool */
158 pool = pj_pool_create_int(&cp->factory, name, initial_size,
159 increment_sz, callback);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000160 if (!pool) {
Benny Prijono31333b62006-11-21 08:36:12 +0000161 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000162 return NULL;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000163 }
Benny Prijono9033e312005-11-21 02:08:39 +0000164
165 } else {
166 /* Get one pool from the list. */
167 pool = cp->free_list[idx].next;
168 pj_list_erase(pool);
169
170 /* Initialize the pool. */
171 pj_pool_init_int(pool, name, increment_sz, callback);
172
173 /* Update pool manager's free capacity. */
174 cp->capacity -= pj_pool_get_capacity(pool);
175
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000176 PJ_LOG(6, (pool->obj_name, "pool reused, size=%u", pool->capacity));
Benny Prijono9033e312005-11-21 02:08:39 +0000177 }
178
179 /* Put in used list. */
180 pj_list_insert_before( &cp->used_list, pool );
181
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000182 /* Mark factory data */
Benny Prijono7db431e2006-07-23 14:38:49 +0000183 pool->factory_data = (void*) (long) idx;
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000184
Benny Prijono9033e312005-11-21 02:08:39 +0000185 /* Increment used count. */
186 ++cp->used_count;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000187
Benny Prijono31333b62006-11-21 08:36:12 +0000188 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000189 return pool;
190}
191
192static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool)
193{
194 pj_caching_pool *cp = (pj_caching_pool*)pf;
Benny Prijono8508aa02006-03-30 15:56:01 +0000195 unsigned pool_capacity;
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000196 unsigned i;
Benny Prijono9033e312005-11-21 02:08:39 +0000197
198 PJ_CHECK_STACK();
199
Benny Prijono31333b62006-11-21 08:36:12 +0000200 pj_lock_acquire(cp->lock);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000201
Benny Prijono9033e312005-11-21 02:08:39 +0000202 /* Erase from the used list. */
203 pj_list_erase(pool);
204
205 /* Decrement used count. */
206 --cp->used_count;
207
Benny Prijono8508aa02006-03-30 15:56:01 +0000208 pool_capacity = pj_pool_get_capacity(pool);
209
Benny Prijono9033e312005-11-21 02:08:39 +0000210 /* Destroy the pool if the size is greater than our size or if the total
211 * capacity in our recycle list (plus the size of the pool) exceeds
212 * maximum capacity.
213 . */
Benny Prijono8508aa02006-03-30 15:56:01 +0000214 if (pool_capacity > pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE-1] ||
215 cp->capacity + pool_capacity > cp->max_capacity)
Benny Prijono9033e312005-11-21 02:08:39 +0000216 {
217 pj_pool_destroy_int(pool);
Benny Prijono31333b62006-11-21 08:36:12 +0000218 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000219 return;
220 }
221
222 /* Reset pool. */
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000223 PJ_LOG(6, (pool->obj_name, "recycle(): cap=%d, used=%d(%d%%)",
Benny Prijono8508aa02006-03-30 15:56:01 +0000224 pool_capacity, pj_pool_get_used_size(pool),
225 pj_pool_get_used_size(pool)*100/pool_capacity));
Benny Prijono9033e312005-11-21 02:08:39 +0000226 pj_pool_reset(pool);
227
Benny Prijono1479b652006-07-03 14:18:17 +0000228 pool_capacity = pj_pool_get_capacity(pool);
229
Benny Prijono9033e312005-11-21 02:08:39 +0000230 /*
231 * Otherwise put the pool in our recycle list.
232 */
Benny Prijono7db431e2006-07-23 14:38:49 +0000233 i = (unsigned) (unsigned long) pool->factory_data;
Benny Prijono9033e312005-11-21 02:08:39 +0000234
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000235 pj_assert(i<PJ_CACHING_POOL_ARRAY_SIZE);
236 if (i >= PJ_CACHING_POOL_ARRAY_SIZE ) {
Benny Prijono9033e312005-11-21 02:08:39 +0000237 /* Something has gone wrong with the pool. */
238 pj_pool_destroy_int(pool);
Benny Prijono31333b62006-11-21 08:36:12 +0000239 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000240 return;
241 }
242
243 pj_list_insert_after(&cp->free_list[i], pool);
Benny Prijono8508aa02006-03-30 15:56:01 +0000244 cp->capacity += pool_capacity;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000245
Benny Prijono31333b62006-11-21 08:36:12 +0000246 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000247}
248
249static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail )
250{
251#if PJ_LOG_MAX_LEVEL >= 3
252 pj_caching_pool *cp = (pj_caching_pool*)factory;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000253
Benny Prijono31333b62006-11-21 08:36:12 +0000254 pj_lock_acquire(cp->lock);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000255
Benny Prijono9033e312005-11-21 02:08:39 +0000256 PJ_LOG(3,("cachpool", " Dumping caching pool:"));
257 PJ_LOG(3,("cachpool", " Capacity=%u, max_capacity=%u, used_cnt=%u", \
258 cp->capacity, cp->max_capacity, cp->used_count));
259 if (detail) {
260 pj_pool_t *pool = cp->used_list.next;
261 pj_uint32_t total_used = 0, total_capacity = 0;
262 PJ_LOG(3,("cachpool", " Dumping all active pools:"));
263 while (pool != (void*)&cp->used_list) {
Benny Prijono8508aa02006-03-30 15:56:01 +0000264 unsigned pool_capacity = pj_pool_get_capacity(pool);
265 PJ_LOG(3,("cachpool", " %12s: %8d of %8d (%d%%) used",
266 pj_pool_getobjname(pool),
267 pj_pool_get_used_size(pool),
268 pool_capacity,
269 pj_pool_get_used_size(pool)*100/pool_capacity));
Benny Prijonod4934802005-11-21 16:58:03 +0000270 total_used += pj_pool_get_used_size(pool);
Benny Prijono8508aa02006-03-30 15:56:01 +0000271 total_capacity += pool_capacity;
Benny Prijono9033e312005-11-21 02:08:39 +0000272 pool = pool->next;
273 }
Benny Prijonoc50f81b2007-03-01 00:02:46 +0000274 if (total_capacity) {
275 PJ_LOG(3,("cachpool", " Total %9d of %9d (%d %%) used!",
276 total_used, total_capacity,
277 total_used * 100 / total_capacity));
278 }
Benny Prijono9033e312005-11-21 02:08:39 +0000279 }
Benny Prijonoc8322f32006-02-26 21:18:42 +0000280
Benny Prijono31333b62006-11-21 08:36:12 +0000281 pj_lock_release(cp->lock);
Benny Prijonoaf12bfc2005-11-23 11:23:12 +0000282#else
283 PJ_UNUSED_ARG(factory);
284 PJ_UNUSED_ARG(detail);
Benny Prijono9033e312005-11-21 02:08:39 +0000285#endif
286}
Benny Prijono8508aa02006-03-30 15:56:01 +0000287
Benny Prijonoc6e165f2006-07-09 10:05:46 +0000288
289static pj_bool_t cpool_on_block_alloc(pj_pool_factory *f, pj_size_t sz)
290{
291 pj_caching_pool *cp = (pj_caching_pool*)f;
292
293 //Can't lock because mutex is not recursive
294 //if (cp->mutex) pj_mutex_lock(cp->mutex);
295
296 cp->used_size += sz;
297 if (cp->used_size > cp->peak_used_size)
298 cp->peak_used_size = cp->used_size;
299
300 //if (cp->mutex) pj_mutex_unlock(cp->mutex);
301
302 return PJ_TRUE;
303}
304
305
306static void cpool_on_block_free(pj_pool_factory *f, pj_size_t sz)
307{
308 pj_caching_pool *cp = (pj_caching_pool*)f;
309
310 //pj_mutex_lock(cp->mutex);
311 cp->used_size -= sz;
312 //pj_mutex_unlock(cp->mutex);
313}
314
315
Benny Prijono8508aa02006-03-30 15:56:01 +0000316#endif /* PJ_HAS_POOL_ALT_API */
317