blob: 01c303d6447ced7201fed549b1f5615c2fd259cc [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) {
Benny Prijonof260e462007-04-30 21:03:32 +000088 pj_pool_t *pool = (pj_pool_t*) cp->free_list[i].next;
Benny Prijono9033e312005-11-21 02:08:39 +000089 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 */
Benny Prijonof260e462007-04-30 21:03:32 +000098 pool = (pj_pool_t*) cp->used_list.next;
Benny Prijono9033e312005-11-21 02:08:39 +000099 while (pool != (pj_pool_t*) &cp->used_list) {
100 pj_pool_t *next = pool->next;
101 pj_list_erase(pool);
Benny Prijonoba4abc92007-06-01 07:26:21 +0000102 PJ_LOG(4,(pool->obj_name,
103 "Pool is not released by application, releasing now"));
Benny Prijono9033e312005-11-21 02:08:39 +0000104 pj_pool_destroy_int(pool);
105 pool = next;
106 }
Benny Prijonoc8322f32006-02-26 21:18:42 +0000107
Benny Prijono31333b62006-11-21 08:36:12 +0000108 if (cp->lock) {
109 pj_lock_destroy(cp->lock);
110 pj_lock_create_null_mutex(NULL, "cachingpool", &cp->lock);
111 }
Benny Prijono9033e312005-11-21 02:08:39 +0000112}
113
114static pj_pool_t* cpool_create_pool(pj_pool_factory *pf,
115 const char *name,
116 pj_size_t initial_size,
117 pj_size_t increment_sz,
118 pj_pool_callback *callback)
119{
120 pj_caching_pool *cp = (pj_caching_pool*)pf;
121 pj_pool_t *pool;
122 int idx;
123
124 PJ_CHECK_STACK();
125
Benny Prijono31333b62006-11-21 08:36:12 +0000126 pj_lock_acquire(cp->lock);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000127
Benny Prijono9033e312005-11-21 02:08:39 +0000128 /* Use pool factory's policy when callback is NULL */
129 if (callback == NULL) {
130 callback = pf->policy.callback;
131 }
132
133 /* Search the suitable size for the pool.
134 * We'll just do linear search to the size array, as the array size itself
135 * is only a few elements. Binary search I suspect will be less efficient
136 * for this purpose.
137 */
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000138 if (initial_size <= pool_sizes[START_SIZE]) {
139 for (idx=START_SIZE-1;
140 idx >= 0 && pool_sizes[idx] >= initial_size;
141 --idx)
142 ;
143 ++idx;
144 } else {
145 for (idx=START_SIZE+1;
146 idx < PJ_CACHING_POOL_ARRAY_SIZE &&
147 pool_sizes[idx] < initial_size;
148 ++idx)
149 ;
150 }
Benny Prijono9033e312005-11-21 02:08:39 +0000151
152 /* Check whether there's a pool in the list. */
153 if (idx==PJ_CACHING_POOL_ARRAY_SIZE || pj_list_empty(&cp->free_list[idx])) {
154 /* No pool is available. */
155 /* Set minimum size. */
156 if (idx < PJ_CACHING_POOL_ARRAY_SIZE)
157 initial_size = pool_sizes[idx];
158
159 /* Create new pool */
160 pool = pj_pool_create_int(&cp->factory, name, initial_size,
161 increment_sz, callback);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000162 if (!pool) {
Benny Prijono31333b62006-11-21 08:36:12 +0000163 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000164 return NULL;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000165 }
Benny Prijono9033e312005-11-21 02:08:39 +0000166
167 } else {
168 /* Get one pool from the list. */
Benny Prijonof260e462007-04-30 21:03:32 +0000169 pool = (pj_pool_t*) cp->free_list[idx].next;
Benny Prijono9033e312005-11-21 02:08:39 +0000170 pj_list_erase(pool);
171
172 /* Initialize the pool. */
173 pj_pool_init_int(pool, name, increment_sz, callback);
174
175 /* Update pool manager's free capacity. */
176 cp->capacity -= pj_pool_get_capacity(pool);
177
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000178 PJ_LOG(6, (pool->obj_name, "pool reused, size=%u", pool->capacity));
Benny Prijono9033e312005-11-21 02:08:39 +0000179 }
180
181 /* Put in used list. */
182 pj_list_insert_before( &cp->used_list, pool );
183
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000184 /* Mark factory data */
Benny Prijono7db431e2006-07-23 14:38:49 +0000185 pool->factory_data = (void*) (long) idx;
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000186
Benny Prijono9033e312005-11-21 02:08:39 +0000187 /* Increment used count. */
188 ++cp->used_count;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000189
Benny Prijono31333b62006-11-21 08:36:12 +0000190 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000191 return pool;
192}
193
194static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool)
195{
196 pj_caching_pool *cp = (pj_caching_pool*)pf;
Benny Prijono8508aa02006-03-30 15:56:01 +0000197 unsigned pool_capacity;
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000198 unsigned i;
Benny Prijono9033e312005-11-21 02:08:39 +0000199
200 PJ_CHECK_STACK();
201
Benny Prijonoba4abc92007-06-01 07:26:21 +0000202 PJ_ASSERT_ON_FAIL(pf && pool, return);
203
Benny Prijono31333b62006-11-21 08:36:12 +0000204 pj_lock_acquire(cp->lock);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000205
Benny Prijonoba4abc92007-06-01 07:26:21 +0000206#if PJ_SAFE_POOL
207 /* Make sure pool is still in our used list */
208 if (pj_list_find_node(&cp->used_list, pool) != pool) {
209 pj_assert(!"Attempt to destroy pool that has been destroyed before");
210 return;
211 }
212#endif
213
Benny Prijono9033e312005-11-21 02:08:39 +0000214 /* Erase from the used list. */
215 pj_list_erase(pool);
216
217 /* Decrement used count. */
218 --cp->used_count;
219
Benny Prijono8508aa02006-03-30 15:56:01 +0000220 pool_capacity = pj_pool_get_capacity(pool);
221
Benny Prijono9033e312005-11-21 02:08:39 +0000222 /* Destroy the pool if the size is greater than our size or if the total
223 * capacity in our recycle list (plus the size of the pool) exceeds
224 * maximum capacity.
225 . */
Benny Prijono8508aa02006-03-30 15:56:01 +0000226 if (pool_capacity > pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE-1] ||
227 cp->capacity + pool_capacity > cp->max_capacity)
Benny Prijono9033e312005-11-21 02:08:39 +0000228 {
229 pj_pool_destroy_int(pool);
Benny Prijono31333b62006-11-21 08:36:12 +0000230 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000231 return;
232 }
233
234 /* Reset pool. */
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000235 PJ_LOG(6, (pool->obj_name, "recycle(): cap=%d, used=%d(%d%%)",
Benny Prijono8508aa02006-03-30 15:56:01 +0000236 pool_capacity, pj_pool_get_used_size(pool),
237 pj_pool_get_used_size(pool)*100/pool_capacity));
Benny Prijono9033e312005-11-21 02:08:39 +0000238 pj_pool_reset(pool);
239
Benny Prijono1479b652006-07-03 14:18:17 +0000240 pool_capacity = pj_pool_get_capacity(pool);
241
Benny Prijono9033e312005-11-21 02:08:39 +0000242 /*
243 * Otherwise put the pool in our recycle list.
244 */
Benny Prijono7db431e2006-07-23 14:38:49 +0000245 i = (unsigned) (unsigned long) pool->factory_data;
Benny Prijono9033e312005-11-21 02:08:39 +0000246
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000247 pj_assert(i<PJ_CACHING_POOL_ARRAY_SIZE);
248 if (i >= PJ_CACHING_POOL_ARRAY_SIZE ) {
Benny Prijono9033e312005-11-21 02:08:39 +0000249 /* Something has gone wrong with the pool. */
250 pj_pool_destroy_int(pool);
Benny Prijono31333b62006-11-21 08:36:12 +0000251 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000252 return;
253 }
254
255 pj_list_insert_after(&cp->free_list[i], pool);
Benny Prijono8508aa02006-03-30 15:56:01 +0000256 cp->capacity += pool_capacity;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000257
Benny Prijono31333b62006-11-21 08:36:12 +0000258 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000259}
260
261static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail )
262{
263#if PJ_LOG_MAX_LEVEL >= 3
264 pj_caching_pool *cp = (pj_caching_pool*)factory;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000265
Benny Prijono31333b62006-11-21 08:36:12 +0000266 pj_lock_acquire(cp->lock);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000267
Benny Prijono9033e312005-11-21 02:08:39 +0000268 PJ_LOG(3,("cachpool", " Dumping caching pool:"));
269 PJ_LOG(3,("cachpool", " Capacity=%u, max_capacity=%u, used_cnt=%u", \
270 cp->capacity, cp->max_capacity, cp->used_count));
271 if (detail) {
Benny Prijonof260e462007-04-30 21:03:32 +0000272 pj_pool_t *pool = (pj_pool_t*) cp->used_list.next;
Benny Prijono9033e312005-11-21 02:08:39 +0000273 pj_uint32_t total_used = 0, total_capacity = 0;
274 PJ_LOG(3,("cachpool", " Dumping all active pools:"));
275 while (pool != (void*)&cp->used_list) {
Benny Prijono8508aa02006-03-30 15:56:01 +0000276 unsigned pool_capacity = pj_pool_get_capacity(pool);
277 PJ_LOG(3,("cachpool", " %12s: %8d of %8d (%d%%) used",
278 pj_pool_getobjname(pool),
279 pj_pool_get_used_size(pool),
280 pool_capacity,
281 pj_pool_get_used_size(pool)*100/pool_capacity));
Benny Prijonod4934802005-11-21 16:58:03 +0000282 total_used += pj_pool_get_used_size(pool);
Benny Prijono8508aa02006-03-30 15:56:01 +0000283 total_capacity += pool_capacity;
Benny Prijono9033e312005-11-21 02:08:39 +0000284 pool = pool->next;
285 }
Benny Prijonoc50f81b2007-03-01 00:02:46 +0000286 if (total_capacity) {
287 PJ_LOG(3,("cachpool", " Total %9d of %9d (%d %%) used!",
288 total_used, total_capacity,
289 total_used * 100 / total_capacity));
290 }
Benny Prijono9033e312005-11-21 02:08:39 +0000291 }
Benny Prijonoc8322f32006-02-26 21:18:42 +0000292
Benny Prijono31333b62006-11-21 08:36:12 +0000293 pj_lock_release(cp->lock);
Benny Prijonoaf12bfc2005-11-23 11:23:12 +0000294#else
295 PJ_UNUSED_ARG(factory);
296 PJ_UNUSED_ARG(detail);
Benny Prijono9033e312005-11-21 02:08:39 +0000297#endif
298}
Benny Prijono8508aa02006-03-30 15:56:01 +0000299
Benny Prijonoc6e165f2006-07-09 10:05:46 +0000300
301static pj_bool_t cpool_on_block_alloc(pj_pool_factory *f, pj_size_t sz)
302{
303 pj_caching_pool *cp = (pj_caching_pool*)f;
304
305 //Can't lock because mutex is not recursive
306 //if (cp->mutex) pj_mutex_lock(cp->mutex);
307
308 cp->used_size += sz;
309 if (cp->used_size > cp->peak_used_size)
310 cp->peak_used_size = cp->used_size;
311
312 //if (cp->mutex) pj_mutex_unlock(cp->mutex);
313
314 return PJ_TRUE;
315}
316
317
318static void cpool_on_block_free(pj_pool_factory *f, pj_size_t sz)
319{
320 pj_caching_pool *cp = (pj_caching_pool*)f;
321
322 //pj_mutex_lock(cp->mutex);
323 cp->used_size -= sz;
324 //pj_mutex_unlock(cp->mutex);
325}
326
327
Benny Prijono8508aa02006-03-30 15:56:01 +0000328#endif /* PJ_HAS_POOL_ALT_API */
329