Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 4 | * |
| 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/string.h> |
| 21 | |
| 22 | #if PJ_POOL_DEBUG |
| 23 | |
| 24 | #if PJ_HAS_MALLOC_H |
| 25 | # include <malloc.h> |
| 26 | #endif |
| 27 | |
| 28 | |
| 29 | #if PJ_HAS_STDLIB_H |
| 30 | # include <stdlib.h> |
| 31 | #endif |
| 32 | |
| 33 | |
| 34 | #if defined(PJ_WIN32) && PJ_WIN32!=0 && defined(PJ_DEBUG) && PJ_DEBUG!=0 \ |
| 35 | && !PJ_NATIVE_STRING_IS_UNICODE |
| 36 | # include <windows.h> |
| 37 | # define TRACE_(msg) OutputDebugString(msg) |
| 38 | #endif |
| 39 | |
| 40 | /* Uncomment this to enable TRACE_ */ |
| 41 | //#undef TRACE_ |
| 42 | |
| 43 | |
| 44 | struct pj_pool_mem |
| 45 | { |
| 46 | struct pj_pool_mem *next; |
| 47 | |
| 48 | /* data follows immediately */ |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | struct pj_pool_t |
| 53 | { |
| 54 | struct pj_pool_mem *first_mem; |
| 55 | pj_size_t used_size; |
| 56 | pj_pool_callback *cb; |
| 57 | }; |
| 58 | |
| 59 | |
| 60 | int PJ_NO_MEMORY_EXCEPTION; |
| 61 | |
| 62 | |
| 63 | /* Create pool */ |
| 64 | PJ_DEF(pj_pool_t*) pj_pool_create_imp( const char *file, int line, |
| 65 | void *factory, |
| 66 | const char *name, |
| 67 | pj_size_t initial_size, |
| 68 | pj_size_t increment_size, |
| 69 | pj_pool_callback *callback) |
| 70 | { |
| 71 | pj_pool_t *pool; |
| 72 | |
| 73 | PJ_UNUSED_ARG(file); |
| 74 | PJ_UNUSED_ARG(line); |
| 75 | PJ_UNUSED_ARG(factory); |
| 76 | PJ_UNUSED_ARG(name); |
| 77 | PJ_UNUSED_ARG(initial_size); |
| 78 | PJ_UNUSED_ARG(increment_size); |
| 79 | |
| 80 | pool = malloc(sizeof(struct pj_pool_t)); |
| 81 | if (!pool) |
| 82 | return NULL; |
| 83 | |
| 84 | pool->first_mem = NULL; |
| 85 | pool->used_size = 0; |
| 86 | pool->cb = callback; |
| 87 | |
| 88 | return pool; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /* Release pool */ |
| 93 | PJ_DEF(void) pj_pool_release_imp(pj_pool_t *pool) |
| 94 | { |
| 95 | pj_pool_reset(pool); |
| 96 | free(pool); |
| 97 | } |
| 98 | |
| 99 | /* Get pool name */ |
| 100 | PJ_DEF(const char*) pj_pool_getobjname_imp(pj_pool_t *pool) |
| 101 | { |
| 102 | PJ_UNUSED_ARG(pool); |
| 103 | return "pooldbg"; |
| 104 | } |
| 105 | |
| 106 | /* Reset pool */ |
| 107 | PJ_DEF(void) pj_pool_reset_imp(pj_pool_t *pool) |
| 108 | { |
| 109 | struct pj_pool_mem *mem; |
| 110 | |
| 111 | mem = pool->first_mem; |
| 112 | while (mem) { |
| 113 | struct pj_pool_mem *next = mem->next; |
| 114 | free(mem); |
| 115 | mem = next; |
| 116 | } |
| 117 | |
| 118 | pool->first_mem = NULL; |
| 119 | } |
| 120 | |
| 121 | /* Get capacity */ |
| 122 | PJ_DEF(pj_size_t) pj_pool_get_capacity_imp(pj_pool_t *pool) |
| 123 | { |
| 124 | PJ_UNUSED_ARG(pool); |
| 125 | |
| 126 | /* Unlimited capacity */ |
| 127 | return 0x7FFFFFFFUL; |
| 128 | } |
| 129 | |
| 130 | /* Get total used size */ |
| 131 | PJ_DEF(pj_size_t) pj_pool_get_used_size_imp(pj_pool_t *pool) |
| 132 | { |
| 133 | return pool->used_size; |
| 134 | } |
| 135 | |
| 136 | /* Allocate memory from the pool */ |
| 137 | PJ_DEF(void*) pj_pool_alloc_imp( const char *file, int line, |
| 138 | pj_pool_t *pool, pj_size_t sz) |
| 139 | { |
| 140 | struct pj_pool_mem *mem; |
| 141 | |
| 142 | PJ_UNUSED_ARG(file); |
| 143 | PJ_UNUSED_ARG(line); |
| 144 | |
| 145 | mem = malloc(sz + sizeof(struct pj_pool_mem)); |
| 146 | if (!mem) { |
| 147 | if (pool->cb) |
| 148 | (*pool->cb)(pool, sz); |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | mem->next = pool->first_mem; |
| 153 | pool->first_mem = mem; |
| 154 | |
| 155 | #ifdef TRACE_ |
| 156 | { |
| 157 | char msg[120]; |
| 158 | pj_ansi_sprintf(msg, "Mem %X (%d+%d bytes) allocated by %s:%d\r\n", |
| 159 | mem, sz, sizeof(struct pj_pool_mem), |
| 160 | file, line); |
| 161 | TRACE_(msg); |
| 162 | } |
| 163 | #endif |
| 164 | |
| 165 | return ((char*)mem) + sizeof(struct pj_pool_mem); |
| 166 | } |
| 167 | |
| 168 | /* Allocate memory from the pool and zero the memory */ |
| 169 | PJ_DEF(void*) pj_pool_calloc_imp( const char *file, int line, |
| 170 | pj_pool_t *pool, unsigned cnt, |
| 171 | unsigned elemsz) |
| 172 | { |
| 173 | void *mem; |
| 174 | |
| 175 | mem = pj_pool_alloc_imp(file, line, pool, cnt*elemsz); |
| 176 | if (!mem) |
| 177 | return NULL; |
| 178 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 179 | pj_bzero(mem, cnt*elemsz); |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 180 | return mem; |
| 181 | } |
| 182 | |
| 183 | /* Allocate memory from the pool and zero the memory */ |
| 184 | PJ_DEF(void*) pj_pool_zalloc_imp( const char *file, int line, |
| 185 | pj_pool_t *pool, pj_size_t sz) |
| 186 | { |
| 187 | return pj_pool_calloc_imp(file, line, pool, 1, sz); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | #endif /* PJ_POOL_DEBUG */ |