blob: 48941e23da5e9666aa57d59d4d83953ca5ed0fc7 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pj/pool.h>
21#include <pj/log.h>
22#include <pj/string.h>
23#include <pj/assert.h>
Benny Prijono31333b62006-11-21 08:36:12 +000024#include <pj/lock.h>
Benny Prijono9033e312005-11-21 02:08:39 +000025#include <pj/os.h>
Benny Prijono31333b62006-11-21 08:36:12 +000026#include <pj/pool_buf.h>
Benny Prijono9033e312005-11-21 02:08:39 +000027
Benny Prijono8508aa02006-03-30 15:56:01 +000028#if !PJ_HAS_POOL_ALT_API
29
Benny Prijono9033e312005-11-21 02:08:39 +000030static pj_pool_t* cpool_create_pool(pj_pool_factory *pf,
31 const char *name,
32 pj_size_t initial_size,
33 pj_size_t increment_sz,
34 pj_pool_callback *callback);
35static void cpool_release_pool(pj_pool_factory *pf, pj_pool_t *pool);
36static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail );
Benny Prijonoc6e165f2006-07-09 10:05:46 +000037static pj_bool_t cpool_on_block_alloc(pj_pool_factory *f, pj_size_t sz);
38static void cpool_on_block_free(pj_pool_factory *f, pj_size_t sz);
39
Benny Prijono9033e312005-11-21 02:08:39 +000040
41static pj_size_t pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE] =
42{
43 256, 512, 1024, 2048, 4096, 8192, 12288, 16384,
44 20480, 24576, 28672, 32768, 40960, 49152, 57344, 65536
45};
46
Benny Prijono53e3ffd2006-07-06 14:27:31 +000047/* Index where the search for size should begin.
48 * Start with pool_sizes[5], which is 8192.
49 */
50#define START_SIZE 5
51
Benny Prijono9033e312005-11-21 02:08:39 +000052
53PJ_DEF(void) pj_caching_pool_init( pj_caching_pool *cp,
54 const pj_pool_factory_policy *policy,
55 pj_size_t max_capacity)
56{
57 int i;
Benny Prijono31333b62006-11-21 08:36:12 +000058 pj_pool_t *pool;
Benny Prijono9033e312005-11-21 02:08:39 +000059
60 PJ_CHECK_STACK();
61
Benny Prijonoac623b32006-07-03 15:19:31 +000062 pj_bzero(cp, sizeof(*cp));
Benny Prijono9033e312005-11-21 02:08:39 +000063
64 cp->max_capacity = max_capacity;
65 pj_list_init(&cp->used_list);
66 for (i=0; i<PJ_CACHING_POOL_ARRAY_SIZE; ++i)
67 pj_list_init(&cp->free_list[i]);
68
Benny Prijono8ab968f2007-07-20 08:08:30 +000069 if (policy == NULL) {
70 policy = &pj_pool_factory_default_policy;
71 }
72
Benny Prijono9033e312005-11-21 02:08:39 +000073 pj_memcpy(&cp->factory.policy, policy, sizeof(pj_pool_factory_policy));
74 cp->factory.create_pool = &cpool_create_pool;
75 cp->factory.release_pool = &cpool_release_pool;
76 cp->factory.dump_status = &cpool_dump_status;
Benny Prijonoc6e165f2006-07-09 10:05:46 +000077 cp->factory.on_block_alloc = &cpool_on_block_alloc;
78 cp->factory.on_block_free = &cpool_on_block_free;
Benny Prijonoc8322f32006-02-26 21:18:42 +000079
Benny Prijono31333b62006-11-21 08:36:12 +000080 pool = pj_pool_create_on_buf("cachingpool", cp->pool_buf, sizeof(cp->pool_buf));
81 pj_lock_create_simple_mutex(pool, "cachingpool", &cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +000082}
83
84PJ_DEF(void) pj_caching_pool_destroy( pj_caching_pool *cp )
85{
86 int i;
87 pj_pool_t *pool;
88
89 PJ_CHECK_STACK();
90
91 /* Delete all pool in free list */
92 for (i=0; i < PJ_CACHING_POOL_ARRAY_SIZE; ++i) {
Benny Prijonof260e462007-04-30 21:03:32 +000093 pj_pool_t *pool = (pj_pool_t*) cp->free_list[i].next;
Benny Prijono9033e312005-11-21 02:08:39 +000094 pj_pool_t *next;
95 for (; pool != (void*)&cp->free_list[i]; pool = next) {
96 next = pool->next;
97 pj_list_erase(pool);
98 pj_pool_destroy_int(pool);
99 }
100 }
101
102 /* Delete all pools in used list */
Benny Prijonof260e462007-04-30 21:03:32 +0000103 pool = (pj_pool_t*) cp->used_list.next;
Benny Prijono9033e312005-11-21 02:08:39 +0000104 while (pool != (pj_pool_t*) &cp->used_list) {
105 pj_pool_t *next = pool->next;
106 pj_list_erase(pool);
Benny Prijonoba4abc92007-06-01 07:26:21 +0000107 PJ_LOG(4,(pool->obj_name,
108 "Pool is not released by application, releasing now"));
Benny Prijono9033e312005-11-21 02:08:39 +0000109 pj_pool_destroy_int(pool);
110 pool = next;
111 }
Benny Prijonoc8322f32006-02-26 21:18:42 +0000112
Benny Prijono31333b62006-11-21 08:36:12 +0000113 if (cp->lock) {
114 pj_lock_destroy(cp->lock);
115 pj_lock_create_null_mutex(NULL, "cachingpool", &cp->lock);
116 }
Benny Prijono9033e312005-11-21 02:08:39 +0000117}
118
119static pj_pool_t* cpool_create_pool(pj_pool_factory *pf,
120 const char *name,
121 pj_size_t initial_size,
122 pj_size_t increment_sz,
123 pj_pool_callback *callback)
124{
125 pj_caching_pool *cp = (pj_caching_pool*)pf;
126 pj_pool_t *pool;
127 int idx;
128
129 PJ_CHECK_STACK();
130
Benny Prijono31333b62006-11-21 08:36:12 +0000131 pj_lock_acquire(cp->lock);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000132
Benny Prijono9033e312005-11-21 02:08:39 +0000133 /* Use pool factory's policy when callback is NULL */
134 if (callback == NULL) {
135 callback = pf->policy.callback;
136 }
137
138 /* Search the suitable size for the pool.
139 * We'll just do linear search to the size array, as the array size itself
140 * is only a few elements. Binary search I suspect will be less efficient
141 * for this purpose.
142 */
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000143 if (initial_size <= pool_sizes[START_SIZE]) {
144 for (idx=START_SIZE-1;
145 idx >= 0 && pool_sizes[idx] >= initial_size;
146 --idx)
147 ;
148 ++idx;
149 } else {
150 for (idx=START_SIZE+1;
151 idx < PJ_CACHING_POOL_ARRAY_SIZE &&
152 pool_sizes[idx] < initial_size;
153 ++idx)
154 ;
155 }
Benny Prijono9033e312005-11-21 02:08:39 +0000156
157 /* Check whether there's a pool in the list. */
158 if (idx==PJ_CACHING_POOL_ARRAY_SIZE || pj_list_empty(&cp->free_list[idx])) {
159 /* No pool is available. */
160 /* Set minimum size. */
161 if (idx < PJ_CACHING_POOL_ARRAY_SIZE)
162 initial_size = pool_sizes[idx];
163
164 /* Create new pool */
165 pool = pj_pool_create_int(&cp->factory, name, initial_size,
166 increment_sz, callback);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000167 if (!pool) {
Benny Prijono31333b62006-11-21 08:36:12 +0000168 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000169 return NULL;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000170 }
Benny Prijono9033e312005-11-21 02:08:39 +0000171
172 } else {
173 /* Get one pool from the list. */
Benny Prijonof260e462007-04-30 21:03:32 +0000174 pool = (pj_pool_t*) cp->free_list[idx].next;
Benny Prijono9033e312005-11-21 02:08:39 +0000175 pj_list_erase(pool);
176
177 /* Initialize the pool. */
178 pj_pool_init_int(pool, name, increment_sz, callback);
179
180 /* Update pool manager's free capacity. */
181 cp->capacity -= pj_pool_get_capacity(pool);
182
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000183 PJ_LOG(6, (pool->obj_name, "pool reused, size=%u", pool->capacity));
Benny Prijono9033e312005-11-21 02:08:39 +0000184 }
185
186 /* Put in used list. */
187 pj_list_insert_before( &cp->used_list, pool );
188
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000189 /* Mark factory data */
Benny Prijono7db431e2006-07-23 14:38:49 +0000190 pool->factory_data = (void*) (long) idx;
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000191
Benny Prijono9033e312005-11-21 02:08:39 +0000192 /* Increment used count. */
193 ++cp->used_count;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000194
Benny Prijono31333b62006-11-21 08:36:12 +0000195 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000196 return pool;
197}
198
199static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool)
200{
201 pj_caching_pool *cp = (pj_caching_pool*)pf;
Benny Prijono8508aa02006-03-30 15:56:01 +0000202 unsigned pool_capacity;
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000203 unsigned i;
Benny Prijono9033e312005-11-21 02:08:39 +0000204
205 PJ_CHECK_STACK();
206
Benny Prijonoba4abc92007-06-01 07:26:21 +0000207 PJ_ASSERT_ON_FAIL(pf && pool, return);
208
Benny Prijono31333b62006-11-21 08:36:12 +0000209 pj_lock_acquire(cp->lock);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000210
Benny Prijonoba4abc92007-06-01 07:26:21 +0000211#if PJ_SAFE_POOL
212 /* Make sure pool is still in our used list */
213 if (pj_list_find_node(&cp->used_list, pool) != pool) {
214 pj_assert(!"Attempt to destroy pool that has been destroyed before");
215 return;
216 }
217#endif
218
Benny Prijono9033e312005-11-21 02:08:39 +0000219 /* Erase from the used list. */
220 pj_list_erase(pool);
221
222 /* Decrement used count. */
223 --cp->used_count;
224
Benny Prijono8508aa02006-03-30 15:56:01 +0000225 pool_capacity = pj_pool_get_capacity(pool);
226
Benny Prijono9033e312005-11-21 02:08:39 +0000227 /* Destroy the pool if the size is greater than our size or if the total
228 * capacity in our recycle list (plus the size of the pool) exceeds
229 * maximum capacity.
230 . */
Benny Prijono8508aa02006-03-30 15:56:01 +0000231 if (pool_capacity > pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE-1] ||
232 cp->capacity + pool_capacity > cp->max_capacity)
Benny Prijono9033e312005-11-21 02:08:39 +0000233 {
234 pj_pool_destroy_int(pool);
Benny Prijono31333b62006-11-21 08:36:12 +0000235 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000236 return;
237 }
238
239 /* Reset pool. */
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000240 PJ_LOG(6, (pool->obj_name, "recycle(): cap=%d, used=%d(%d%%)",
Benny Prijono8508aa02006-03-30 15:56:01 +0000241 pool_capacity, pj_pool_get_used_size(pool),
242 pj_pool_get_used_size(pool)*100/pool_capacity));
Benny Prijono9033e312005-11-21 02:08:39 +0000243 pj_pool_reset(pool);
244
Benny Prijono1479b652006-07-03 14:18:17 +0000245 pool_capacity = pj_pool_get_capacity(pool);
246
Benny Prijono9033e312005-11-21 02:08:39 +0000247 /*
248 * Otherwise put the pool in our recycle list.
249 */
Benny Prijono7db431e2006-07-23 14:38:49 +0000250 i = (unsigned) (unsigned long) pool->factory_data;
Benny Prijono9033e312005-11-21 02:08:39 +0000251
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000252 pj_assert(i<PJ_CACHING_POOL_ARRAY_SIZE);
253 if (i >= PJ_CACHING_POOL_ARRAY_SIZE ) {
Benny Prijono9033e312005-11-21 02:08:39 +0000254 /* Something has gone wrong with the pool. */
255 pj_pool_destroy_int(pool);
Benny Prijono31333b62006-11-21 08:36:12 +0000256 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000257 return;
258 }
259
260 pj_list_insert_after(&cp->free_list[i], pool);
Benny Prijono8508aa02006-03-30 15:56:01 +0000261 cp->capacity += pool_capacity;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000262
Benny Prijono31333b62006-11-21 08:36:12 +0000263 pj_lock_release(cp->lock);
Benny Prijono9033e312005-11-21 02:08:39 +0000264}
265
266static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail )
267{
268#if PJ_LOG_MAX_LEVEL >= 3
269 pj_caching_pool *cp = (pj_caching_pool*)factory;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000270
Benny Prijono31333b62006-11-21 08:36:12 +0000271 pj_lock_acquire(cp->lock);
Benny Prijonoc8322f32006-02-26 21:18:42 +0000272
Benny Prijono9033e312005-11-21 02:08:39 +0000273 PJ_LOG(3,("cachpool", " Dumping caching pool:"));
274 PJ_LOG(3,("cachpool", " Capacity=%u, max_capacity=%u, used_cnt=%u", \
275 cp->capacity, cp->max_capacity, cp->used_count));
276 if (detail) {
Benny Prijonof260e462007-04-30 21:03:32 +0000277 pj_pool_t *pool = (pj_pool_t*) cp->used_list.next;
Benny Prijono9033e312005-11-21 02:08:39 +0000278 pj_uint32_t total_used = 0, total_capacity = 0;
279 PJ_LOG(3,("cachpool", " Dumping all active pools:"));
280 while (pool != (void*)&cp->used_list) {
Benny Prijono8508aa02006-03-30 15:56:01 +0000281 unsigned pool_capacity = pj_pool_get_capacity(pool);
Benny Prijonod7d58ca2009-08-12 17:24:40 +0000282 PJ_LOG(3,("cachpool", " %16s: %8d of %8d (%d%%) used",
Benny Prijono8508aa02006-03-30 15:56:01 +0000283 pj_pool_getobjname(pool),
284 pj_pool_get_used_size(pool),
285 pool_capacity,
286 pj_pool_get_used_size(pool)*100/pool_capacity));
Benny Prijonod4934802005-11-21 16:58:03 +0000287 total_used += pj_pool_get_used_size(pool);
Benny Prijono8508aa02006-03-30 15:56:01 +0000288 total_capacity += pool_capacity;
Benny Prijono9033e312005-11-21 02:08:39 +0000289 pool = pool->next;
290 }
Benny Prijonoc50f81b2007-03-01 00:02:46 +0000291 if (total_capacity) {
292 PJ_LOG(3,("cachpool", " Total %9d of %9d (%d %%) used!",
293 total_used, total_capacity,
294 total_used * 100 / total_capacity));
295 }
Benny Prijono9033e312005-11-21 02:08:39 +0000296 }
Benny Prijonoc8322f32006-02-26 21:18:42 +0000297
Benny Prijono31333b62006-11-21 08:36:12 +0000298 pj_lock_release(cp->lock);
Benny Prijonoaf12bfc2005-11-23 11:23:12 +0000299#else
300 PJ_UNUSED_ARG(factory);
301 PJ_UNUSED_ARG(detail);
Benny Prijono9033e312005-11-21 02:08:39 +0000302#endif
303}
Benny Prijono8508aa02006-03-30 15:56:01 +0000304
Benny Prijonoc6e165f2006-07-09 10:05:46 +0000305
306static pj_bool_t cpool_on_block_alloc(pj_pool_factory *f, pj_size_t sz)
307{
308 pj_caching_pool *cp = (pj_caching_pool*)f;
309
310 //Can't lock because mutex is not recursive
311 //if (cp->mutex) pj_mutex_lock(cp->mutex);
312
313 cp->used_size += sz;
314 if (cp->used_size > cp->peak_used_size)
315 cp->peak_used_size = cp->used_size;
316
317 //if (cp->mutex) pj_mutex_unlock(cp->mutex);
318
319 return PJ_TRUE;
320}
321
322
323static void cpool_on_block_free(pj_pool_factory *f, pj_size_t sz)
324{
325 pj_caching_pool *cp = (pj_caching_pool*)f;
326
327 //pj_mutex_lock(cp->mutex);
328 cp->used_size -= sz;
329 //pj_mutex_unlock(cp->mutex);
330}
331
332
Benny Prijono8508aa02006-03-30 15:56:01 +0000333#endif /* PJ_HAS_POOL_ALT_API */
334