blob: 295d6b9aed53835291dfe01a27d364e27b11546a [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
Benny Prijono8ab968f2007-07-20 08:08:30 +000068 if (policy == NULL) {
69 policy = &pj_pool_factory_default_policy;
70 }
71
Benny Prijono9033e312005-11-21 02:08:39 +000072 pj_memcpy(&cp->factory.policy, policy, sizeof(pj_pool_factory_policy));
73 cp->factory.create_pool = &cpool_create_pool;
74 cp->factory.release_pool = &cpool_release_pool;
75 cp->factory.dump_status = &cpool_dump_status;
Benny Prijonoc6e165f2006-07-09 10:05:46 +000076 cp->factory.on_block_alloc = &cpool_on_block_alloc;
77 cp->factory.on_block_free = &cpool_on_block_free;
Benny Prijonoc8322f32006-02-26 21:18:42 +000078
Benny Prijono31333b62006-11-21 08:36:12 +000079 pool = pj_pool_create_on_buf("cachingpool", cp->pool_buf, sizeof(cp->pool_buf));
80 pj_lock_create_simple_mutex(pool, "cachingpool", &cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +000081}
82
83PJ_DEF(void) pj_caching_pool_destroy( pj_caching_pool *cp )
84{
85 int i;
86 pj_pool_t *pool;
87
88 PJ_CHECK_STACK();
89
90 /* Delete all pool in free list */
91 for (i=0; i < PJ_CACHING_POOL_ARRAY_SIZE; ++i) {
Benny Prijonof260e462007-04-30 21:03:32 +000092 pj_pool_t *pool = (pj_pool_t*) cp->free_list[i].next;
Benny Prijono9033e312005-11-21 02:08:39 +000093 pj_pool_t *next;
94 for (; pool != (void*)&cp->free_list[i]; pool = next) {
95 next = pool->next;
96 pj_list_erase(pool);
97 pj_pool_destroy_int(pool);
98 }
99 }
100
101 /* Delete all pools in used list */
Benny Prijonof260e462007-04-30 21:03:32 +0000102 pool = (pj_pool_t*) cp->used_list.next;
Benny Prijono9033e312005-11-21 02:08:39 +0000103 while (pool != (pj_pool_t*) &cp->used_list) {
104 pj_pool_t *next = pool->next;
105 pj_list_erase(pool);
Benny Prijonoba4abc92007-06-01 07:26:21 +0000106 PJ_LOG(4,(pool->obj_name,
107 "Pool is not released by application, releasing now"));
Benny Prijono9033e312005-11-21 02:08:39 +0000108 pj_pool_destroy_int(pool);
109 pool = next;
110 }
Benny Prijonoc8322f32006-02-26 21:18:42 +0000111
Benny Prijono31333b62006-11-21 08:36:12 +0000112 if (cp->lock) {
113 pj_lock_destroy(cp->lock);
114 pj_lock_create_null_mutex(NULL, "cachingpool", &cp->lock);
115 }
Benny Prijono9033e312005-11-21 02:08:39 +0000116}
117
118static pj_pool_t* cpool_create_pool(pj_pool_factory *pf,
119 const char *name,
120 pj_size_t initial_size,
121 pj_size_t increment_sz,
122 pj_pool_callback *callback)
123{
124 pj_caching_pool *cp = (pj_caching_pool*)pf;
125 pj_pool_t *pool;
126 int idx;
127
128 PJ_CHECK_STACK();
129
Benny Prijono31333b62006-11-21 08:36:12 +0000130 pj_lock_acquire(cp->lock);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000131
Benny Prijono9033e312005-11-21 02:08:39 +0000132 /* Use pool factory's policy when callback is NULL */
133 if (callback == NULL) {
134 callback = pf->policy.callback;
135 }
136
137 /* Search the suitable size for the pool.
138 * We'll just do linear search to the size array, as the array size itself
139 * is only a few elements. Binary search I suspect will be less efficient
140 * for this purpose.
141 */
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000142 if (initial_size <= pool_sizes[START_SIZE]) {
143 for (idx=START_SIZE-1;
144 idx >= 0 && pool_sizes[idx] >= initial_size;
145 --idx)
146 ;
147 ++idx;
148 } else {
149 for (idx=START_SIZE+1;
150 idx < PJ_CACHING_POOL_ARRAY_SIZE &&
151 pool_sizes[idx] < initial_size;
152 ++idx)
153 ;
154 }
Benny Prijono9033e312005-11-21 02:08:39 +0000155
156 /* Check whether there's a pool in the list. */
157 if (idx==PJ_CACHING_POOL_ARRAY_SIZE || pj_list_empty(&cp->free_list[idx])) {
158 /* No pool is available. */
159 /* Set minimum size. */
160 if (idx < PJ_CACHING_POOL_ARRAY_SIZE)
161 initial_size = pool_sizes[idx];
162
163 /* Create new pool */
164 pool = pj_pool_create_int(&cp->factory, name, initial_size,
165 increment_sz, callback);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000166 if (!pool) {
Benny Prijono31333b62006-11-21 08:36:12 +0000167 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000168 return NULL;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000169 }
Benny Prijono9033e312005-11-21 02:08:39 +0000170
171 } else {
172 /* Get one pool from the list. */
Benny Prijonof260e462007-04-30 21:03:32 +0000173 pool = (pj_pool_t*) cp->free_list[idx].next;
Benny Prijono9033e312005-11-21 02:08:39 +0000174 pj_list_erase(pool);
175
176 /* Initialize the pool. */
177 pj_pool_init_int(pool, name, increment_sz, callback);
178
179 /* Update pool manager's free capacity. */
180 cp->capacity -= pj_pool_get_capacity(pool);
181
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000182 PJ_LOG(6, (pool->obj_name, "pool reused, size=%u", pool->capacity));
Benny Prijono9033e312005-11-21 02:08:39 +0000183 }
184
185 /* Put in used list. */
186 pj_list_insert_before( &cp->used_list, pool );
187
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000188 /* Mark factory data */
Benny Prijono7db431e2006-07-23 14:38:49 +0000189 pool->factory_data = (void*) (long) idx;
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000190
Benny Prijono9033e312005-11-21 02:08:39 +0000191 /* Increment used count. */
192 ++cp->used_count;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000193
Benny Prijono31333b62006-11-21 08:36:12 +0000194 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000195 return pool;
196}
197
198static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool)
199{
200 pj_caching_pool *cp = (pj_caching_pool*)pf;
Benny Prijono8508aa02006-03-30 15:56:01 +0000201 unsigned pool_capacity;
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000202 unsigned i;
Benny Prijono9033e312005-11-21 02:08:39 +0000203
204 PJ_CHECK_STACK();
205
Benny Prijonoba4abc92007-06-01 07:26:21 +0000206 PJ_ASSERT_ON_FAIL(pf && pool, return);
207
Benny Prijono31333b62006-11-21 08:36:12 +0000208 pj_lock_acquire(cp->lock);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000209
Benny Prijonoba4abc92007-06-01 07:26:21 +0000210#if PJ_SAFE_POOL
211 /* Make sure pool is still in our used list */
212 if (pj_list_find_node(&cp->used_list, pool) != pool) {
213 pj_assert(!"Attempt to destroy pool that has been destroyed before");
214 return;
215 }
216#endif
217
Benny Prijono9033e312005-11-21 02:08:39 +0000218 /* Erase from the used list. */
219 pj_list_erase(pool);
220
221 /* Decrement used count. */
222 --cp->used_count;
223
Benny Prijono8508aa02006-03-30 15:56:01 +0000224 pool_capacity = pj_pool_get_capacity(pool);
225
Benny Prijono9033e312005-11-21 02:08:39 +0000226 /* Destroy the pool if the size is greater than our size or if the total
227 * capacity in our recycle list (plus the size of the pool) exceeds
228 * maximum capacity.
229 . */
Benny Prijono8508aa02006-03-30 15:56:01 +0000230 if (pool_capacity > pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE-1] ||
231 cp->capacity + pool_capacity > cp->max_capacity)
Benny Prijono9033e312005-11-21 02:08:39 +0000232 {
233 pj_pool_destroy_int(pool);
Benny Prijono31333b62006-11-21 08:36:12 +0000234 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000235 return;
236 }
237
238 /* Reset pool. */
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000239 PJ_LOG(6, (pool->obj_name, "recycle(): cap=%d, used=%d(%d%%)",
Benny Prijono8508aa02006-03-30 15:56:01 +0000240 pool_capacity, pj_pool_get_used_size(pool),
241 pj_pool_get_used_size(pool)*100/pool_capacity));
Benny Prijono9033e312005-11-21 02:08:39 +0000242 pj_pool_reset(pool);
243
Benny Prijono1479b652006-07-03 14:18:17 +0000244 pool_capacity = pj_pool_get_capacity(pool);
245
Benny Prijono9033e312005-11-21 02:08:39 +0000246 /*
247 * Otherwise put the pool in our recycle list.
248 */
Benny Prijono7db431e2006-07-23 14:38:49 +0000249 i = (unsigned) (unsigned long) pool->factory_data;
Benny Prijono9033e312005-11-21 02:08:39 +0000250
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000251 pj_assert(i<PJ_CACHING_POOL_ARRAY_SIZE);
252 if (i >= PJ_CACHING_POOL_ARRAY_SIZE ) {
Benny Prijono9033e312005-11-21 02:08:39 +0000253 /* Something has gone wrong with the pool. */
254 pj_pool_destroy_int(pool);
Benny Prijono31333b62006-11-21 08:36:12 +0000255 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000256 return;
257 }
258
259 pj_list_insert_after(&cp->free_list[i], pool);
Benny Prijono8508aa02006-03-30 15:56:01 +0000260 cp->capacity += pool_capacity;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000261
Benny Prijono31333b62006-11-21 08:36:12 +0000262 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000263}
264
265static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail )
266{
267#if PJ_LOG_MAX_LEVEL >= 3
268 pj_caching_pool *cp = (pj_caching_pool*)factory;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000269
Benny Prijono31333b62006-11-21 08:36:12 +0000270 pj_lock_acquire(cp->lock);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000271
Benny Prijono9033e312005-11-21 02:08:39 +0000272 PJ_LOG(3,("cachpool", " Dumping caching pool:"));
273 PJ_LOG(3,("cachpool", " Capacity=%u, max_capacity=%u, used_cnt=%u", \
274 cp->capacity, cp->max_capacity, cp->used_count));
275 if (detail) {
Benny Prijonof260e462007-04-30 21:03:32 +0000276 pj_pool_t *pool = (pj_pool_t*) cp->used_list.next;
Benny Prijono9033e312005-11-21 02:08:39 +0000277 pj_uint32_t total_used = 0, total_capacity = 0;
278 PJ_LOG(3,("cachpool", " Dumping all active pools:"));
279 while (pool != (void*)&cp->used_list) {
Benny Prijono8508aa02006-03-30 15:56:01 +0000280 unsigned pool_capacity = pj_pool_get_capacity(pool);
281 PJ_LOG(3,("cachpool", " %12s: %8d of %8d (%d%%) used",
282 pj_pool_getobjname(pool),
283 pj_pool_get_used_size(pool),
284 pool_capacity,
285 pj_pool_get_used_size(pool)*100/pool_capacity));
Benny Prijonod4934802005-11-21 16:58:03 +0000286 total_used += pj_pool_get_used_size(pool);
Benny Prijono8508aa02006-03-30 15:56:01 +0000287 total_capacity += pool_capacity;
Benny Prijono9033e312005-11-21 02:08:39 +0000288 pool = pool->next;
289 }
Benny Prijonoc50f81b2007-03-01 00:02:46 +0000290 if (total_capacity) {
291 PJ_LOG(3,("cachpool", " Total %9d of %9d (%d %%) used!",
292 total_used, total_capacity,
293 total_used * 100 / total_capacity));
294 }
Benny Prijono9033e312005-11-21 02:08:39 +0000295 }
Benny Prijonoc8322f32006-02-26 21:18:42 +0000296
Benny Prijono31333b62006-11-21 08:36:12 +0000297 pj_lock_release(cp->lock);
Benny Prijonoaf12bfc2005-11-23 11:23:12 +0000298#else
299 PJ_UNUSED_ARG(factory);
300 PJ_UNUSED_ARG(detail);
Benny Prijono9033e312005-11-21 02:08:39 +0000301#endif
302}
Benny Prijono8508aa02006-03-30 15:56:01 +0000303
Benny Prijonoc6e165f2006-07-09 10:05:46 +0000304
305static pj_bool_t cpool_on_block_alloc(pj_pool_factory *f, pj_size_t sz)
306{
307 pj_caching_pool *cp = (pj_caching_pool*)f;
308
309 //Can't lock because mutex is not recursive
310 //if (cp->mutex) pj_mutex_lock(cp->mutex);
311
312 cp->used_size += sz;
313 if (cp->used_size > cp->peak_used_size)
314 cp->peak_used_size = cp->used_size;
315
316 //if (cp->mutex) pj_mutex_unlock(cp->mutex);
317
318 return PJ_TRUE;
319}
320
321
322static void cpool_on_block_free(pj_pool_factory *f, pj_size_t sz)
323{
324 pj_caching_pool *cp = (pj_caching_pool*)f;
325
326 //pj_mutex_lock(cp->mutex);
327 cp->used_size -= sz;
328 //pj_mutex_unlock(cp->mutex);
329}
330
331
Benny Prijono8508aa02006-03-30 15:56:01 +0000332#endif /* PJ_HAS_POOL_ALT_API */
333