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