Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 1 | /* $Id$ */
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 2 | /*
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 4 | *
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 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.
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 9 | *
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 10 | * This program is distributed in the hope that it will be useful,
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 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
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 18 | */
|
| 19 | #include <pj/pool.h>
|
| 20 |
|
| 21 | /* Only if we ARE debugging memory allocations. */
|
| 22 | #if PJ_POOL_DEBUG
|
| 23 |
|
| 24 | #include <pj/list.h>
|
| 25 | #include <pj/log.h>
|
| 26 |
|
| 27 | #include <stdlib.h>
|
| 28 | #include <stdio.h>
|
| 29 | #define WIN32_LEAN_AND_MEAN
|
| 30 | #include <windows.h>
|
| 31 |
|
| 32 | typedef struct memory_entry
|
| 33 | {
|
| 34 | PJ_DECL_LIST_MEMBER(struct memory_entry)
|
| 35 | void *ptr;
|
| 36 | char *file;
|
| 37 | int line;
|
| 38 | } memory_entry;
|
| 39 |
|
| 40 | struct pj_pool_t
|
| 41 | {
|
| 42 | char obj_name[32];
|
| 43 | HANDLE hHeap;
|
| 44 | memory_entry first;
|
| 45 | pj_size_t initial_size;
|
| 46 | pj_size_t increment;
|
| 47 | pj_size_t used_size;
|
| 48 | char *file;
|
| 49 | int line;
|
| 50 | };
|
| 51 |
|
| 52 | PJ_DEF(void) pj_pool_set_functions( void *(*malloc_func)(pj_size_t),
|
| 53 | void (*free_func)(void *ptr, pj_size_t))
|
| 54 | {
|
| 55 | /* Ignored. */
|
| 56 | PJ_CHECK_STACK();
|
| 57 | PJ_UNUSED_ARG(malloc_func)
|
| 58 | PJ_UNUSED_ARG(free_func)
|
| 59 | }
|
| 60 |
|
| 61 | PJ_DEF(pj_pool_t*) pj_pool_create_dbg( const char *name,
|
| 62 | pj_size_t initial_size,
|
| 63 | pj_size_t increment_size,
|
| 64 | pj_pool_callback *callback,
|
| 65 | char *file, int line)
|
| 66 | {
|
| 67 | pj_pool_t *pool;
|
| 68 | HANDLE hHeap;
|
| 69 |
|
| 70 | PJ_CHECK_STACK();
|
| 71 | PJ_UNUSED_ARG(callback)
|
| 72 |
|
| 73 | /* Create Win32 heap for the pool. */
|
| 74 | hHeap = HeapCreate(HEAP_GENERATE_EXCEPTIONS|HEAP_NO_SERIALIZE,
|
| 75 | initial_size, 0);
|
| 76 | if (!hHeap) {
|
| 77 | return NULL;
|
| 78 | }
|
| 79 |
|
| 80 |
|
| 81 | /* Create and initialize the pool structure. */
|
| 82 | pool = HeapAlloc(hHeap, HEAP_GENERATE_EXCEPTIONS|HEAP_NO_SERIALIZE,
|
| 83 | sizeof(*pool));
|
| 84 | memset(pool, 0, sizeof(*pool));
|
| 85 | pool->file = file;
|
| 86 | pool->line = line;
|
| 87 | pool->hHeap = hHeap;
|
| 88 | pool->initial_size = initial_size;
|
| 89 | pool->increment = increment_size;
|
| 90 | pool->used_size = 0;
|
| 91 |
|
| 92 | /* Set name. */
|
| 93 | if (name) {
|
| 94 | if (strchr(name, '%') != NULL) {
|
| 95 | sprintf(pool->obj_name, name, pool);
|
| 96 | } else {
|
| 97 | strncpy(pool->obj_name, name, PJ_MAX_OBJ_NAME);
|
| 98 | }
|
| 99 | } else {
|
| 100 | pool->obj_name[0] = '\0';
|
| 101 | }
|
| 102 |
|
| 103 | /* List pool's entry. */
|
| 104 | pj_list_init(&pool->first);
|
| 105 |
|
| 106 | PJ_LOG(3,(pool->obj_name, "Pool created"));
|
| 107 | return pool;
|
| 108 | }
|
| 109 |
|
| 110 | PJ_DEF(void) pj_pool_destroy( pj_pool_t *pool )
|
| 111 | {
|
| 112 | memory_entry *entry;
|
| 113 |
|
| 114 | PJ_CHECK_STACK();
|
| 115 |
|
| 116 | PJ_LOG(3,(pool->obj_name, "Destoying pool, init_size=%u, used=%u",
|
| 117 | pool->initial_size, pool->used_size));
|
| 118 |
|
| 119 | if (!HeapValidate( pool->hHeap, HEAP_NO_SERIALIZE, pool)) {
|
| 120 | PJ_LOG(2,(pool->obj_name, "Corrupted pool structure, allocated in %s:%d",
|
| 121 | pool->file, pool->line));
|
| 122 | }
|
| 123 |
|
| 124 | /* Validate all memory entries in the pool. */
|
| 125 | for (entry=pool->first.next; entry != &pool->first; entry = entry->next) {
|
| 126 | if (!HeapValidate( pool->hHeap, HEAP_NO_SERIALIZE, entry)) {
|
| 127 | PJ_LOG(2,(pool->obj_name, "Corrupted pool entry, allocated in %s:%d",
|
| 128 | entry->file, entry->line));
|
| 129 | }
|
| 130 |
|
| 131 | if (!HeapValidate( pool->hHeap, HEAP_NO_SERIALIZE, entry->ptr)) {
|
| 132 | PJ_LOG(2,(pool->obj_name, "Corrupted pool memory, allocated in %s:%d",
|
| 133 | entry->file, entry->line));
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | /* Destroy heap. */
|
| 138 | HeapDestroy(pool->hHeap);
|
| 139 | }
|
| 140 |
|
| 141 | PJ_DEF(void) pj_pool_reset( pj_pool_t *pool )
|
| 142 | {
|
| 143 | /* Do nothing. */
|
| 144 | PJ_CHECK_STACK();
|
| 145 | PJ_UNUSED_ARG(pool)
|
| 146 | }
|
| 147 |
|
| 148 | PJ_DEF(pj_size_t) pj_pool_get_capacity( pj_pool_t *pool )
|
| 149 | {
|
| 150 | PJ_CHECK_STACK();
|
| 151 | PJ_UNUSED_ARG(pool)
|
| 152 | return 0;
|
| 153 | }
|
| 154 |
|
| 155 | PJ_DEF(pj_size_t) pj_pool_get_used_size( pj_pool_t *pool )
|
| 156 | {
|
| 157 | PJ_CHECK_STACK();
|
| 158 | PJ_UNUSED_ARG(pool)
|
| 159 | return 0;
|
| 160 | }
|
| 161 |
|
| 162 | PJ_DEF(pj_size_t) pj_pool_get_request_count( pj_pool_t *pool )
|
| 163 | {
|
| 164 | PJ_CHECK_STACK();
|
| 165 | PJ_UNUSED_ARG(pool)
|
| 166 | return 0;
|
| 167 | }
|
| 168 |
|
| 169 | PJ_DEF(void*) pj_pool_alloc_dbg( pj_pool_t *pool, pj_size_t size,
|
| 170 | char *file, int line)
|
| 171 | {
|
| 172 | memory_entry *entry;
|
| 173 | int entry_size;
|
| 174 |
|
| 175 | PJ_CHECK_STACK();
|
| 176 |
|
| 177 | entry_size = sizeof(*entry);
|
| 178 | entry = HeapAlloc(pool->hHeap, HEAP_GENERATE_EXCEPTIONS|HEAP_NO_SERIALIZE,
|
| 179 | entry_size);
|
| 180 | entry->file = file;
|
| 181 | entry->line = line;
|
| 182 | entry->ptr = HeapAlloc(pool->hHeap, HEAP_GENERATE_EXCEPTIONS|HEAP_NO_SERIALIZE,
|
| 183 | size);
|
| 184 | pj_list_insert_before( &pool->first, entry);
|
| 185 |
|
| 186 | pool->used_size += size;
|
| 187 | return entry->ptr;
|
| 188 | }
|
| 189 |
|
| 190 | PJ_DEF(void*) pj_pool_calloc_dbg( pj_pool_t *pool, pj_size_t count, pj_size_t elem,
|
| 191 | char *file, int line)
|
| 192 | {
|
| 193 | void *ptr;
|
| 194 |
|
| 195 | PJ_CHECK_STACK();
|
| 196 |
|
| 197 | ptr = pj_pool_alloc_dbg(pool, count*elem, file, line);
|
| 198 | memset(ptr, 0, count*elem);
|
| 199 | return ptr;
|
| 200 | }
|
| 201 |
|
| 202 |
|
| 203 | PJ_DEF(void) pj_pool_pool_init( pj_pool_pool_t *pool_pool,
|
| 204 | pj_size_t max_capacity)
|
| 205 | {
|
| 206 | PJ_CHECK_STACK();
|
| 207 | PJ_UNUSED_ARG(pool_pool)
|
| 208 | PJ_UNUSED_ARG(max_capacity)
|
| 209 | }
|
| 210 |
|
| 211 | PJ_DEF(void) pj_pool_pool_destroy( pj_pool_pool_t *pool_pool )
|
| 212 | {
|
| 213 | PJ_CHECK_STACK();
|
| 214 | PJ_UNUSED_ARG(pool_pool)
|
| 215 | }
|
| 216 |
|
| 217 | PJ_DEF(pj_pool_t*) pj_pool_pool_create_pool( pj_pool_pool_t *pool_pool,
|
| 218 | const char *name,
|
| 219 | pj_size_t initial_size,
|
| 220 | pj_size_t increment_size,
|
| 221 | pj_pool_callback *callback)
|
| 222 | {
|
| 223 | PJ_CHECK_STACK();
|
| 224 | PJ_UNUSED_ARG(pool_pool)
|
| 225 | return pj_pool_create(name, initial_size, increment_size, callback);
|
| 226 | }
|
| 227 |
|
| 228 | PJ_DEF(void) pj_pool_pool_release_pool( pj_pool_pool_t *pool_pool,
|
| 229 | pj_pool_t *pool )
|
| 230 | {
|
| 231 | PJ_CHECK_STACK();
|
| 232 | PJ_UNUSED_ARG(pool_pool)
|
| 233 | pj_pool_destroy(pool);
|
| 234 | }
|
| 235 |
|
| 236 |
|
| 237 | #endif /* PJ_POOL_DEBUG */
|