Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2008 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 | |
Benny Prijono | d7c6d05 | 2008-07-12 09:31:34 +0000 | [diff] [blame] | 22 | #if PJ_HAS_POOL_ALT_API |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 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 | |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 44 | |
| 45 | int PJ_NO_MEMORY_EXCEPTION; |
| 46 | |
| 47 | |
Benny Prijono | d7c6d05 | 2008-07-12 09:31:34 +0000 | [diff] [blame] | 48 | PJ_DEF(int) pj_NO_MEMORY_EXCEPTION() |
| 49 | { |
| 50 | return PJ_NO_MEMORY_EXCEPTION; |
| 51 | } |
| 52 | |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 53 | /* Create pool */ |
| 54 | PJ_DEF(pj_pool_t*) pj_pool_create_imp( const char *file, int line, |
| 55 | void *factory, |
| 56 | const char *name, |
| 57 | pj_size_t initial_size, |
| 58 | pj_size_t increment_size, |
| 59 | pj_pool_callback *callback) |
| 60 | { |
| 61 | pj_pool_t *pool; |
| 62 | |
| 63 | PJ_UNUSED_ARG(file); |
| 64 | PJ_UNUSED_ARG(line); |
| 65 | PJ_UNUSED_ARG(factory); |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 66 | PJ_UNUSED_ARG(initial_size); |
| 67 | PJ_UNUSED_ARG(increment_size); |
| 68 | |
| 69 | pool = malloc(sizeof(struct pj_pool_t)); |
| 70 | if (!pool) |
| 71 | return NULL; |
| 72 | |
Benny Prijono | d7c6d05 | 2008-07-12 09:31:34 +0000 | [diff] [blame] | 73 | if (name) { |
| 74 | pj_ansi_strncpy(pool->obj_name, name, sizeof(pool->obj_name)); |
| 75 | pool->obj_name[sizeof(pool->obj_name)-1] = '\0'; |
| 76 | } else { |
| 77 | strcpy(pool->obj_name, "altpool"); |
| 78 | } |
| 79 | |
| 80 | pool->factory = NULL; |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 81 | pool->first_mem = NULL; |
| 82 | pool->used_size = 0; |
| 83 | pool->cb = callback; |
| 84 | |
| 85 | return pool; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /* Release pool */ |
| 90 | PJ_DEF(void) pj_pool_release_imp(pj_pool_t *pool) |
| 91 | { |
| 92 | pj_pool_reset(pool); |
| 93 | free(pool); |
| 94 | } |
| 95 | |
| 96 | /* Get pool name */ |
| 97 | PJ_DEF(const char*) pj_pool_getobjname_imp(pj_pool_t *pool) |
| 98 | { |
| 99 | PJ_UNUSED_ARG(pool); |
| 100 | return "pooldbg"; |
| 101 | } |
| 102 | |
| 103 | /* Reset pool */ |
| 104 | PJ_DEF(void) pj_pool_reset_imp(pj_pool_t *pool) |
| 105 | { |
| 106 | struct pj_pool_mem *mem; |
| 107 | |
| 108 | mem = pool->first_mem; |
| 109 | while (mem) { |
| 110 | struct pj_pool_mem *next = mem->next; |
| 111 | free(mem); |
| 112 | mem = next; |
| 113 | } |
| 114 | |
| 115 | pool->first_mem = NULL; |
| 116 | } |
| 117 | |
| 118 | /* Get capacity */ |
| 119 | PJ_DEF(pj_size_t) pj_pool_get_capacity_imp(pj_pool_t *pool) |
| 120 | { |
| 121 | PJ_UNUSED_ARG(pool); |
| 122 | |
| 123 | /* Unlimited capacity */ |
| 124 | return 0x7FFFFFFFUL; |
| 125 | } |
| 126 | |
| 127 | /* Get total used size */ |
| 128 | PJ_DEF(pj_size_t) pj_pool_get_used_size_imp(pj_pool_t *pool) |
| 129 | { |
| 130 | return pool->used_size; |
| 131 | } |
| 132 | |
| 133 | /* Allocate memory from the pool */ |
| 134 | PJ_DEF(void*) pj_pool_alloc_imp( const char *file, int line, |
| 135 | pj_pool_t *pool, pj_size_t sz) |
| 136 | { |
| 137 | struct pj_pool_mem *mem; |
| 138 | |
| 139 | PJ_UNUSED_ARG(file); |
| 140 | PJ_UNUSED_ARG(line); |
| 141 | |
| 142 | mem = malloc(sz + sizeof(struct pj_pool_mem)); |
| 143 | if (!mem) { |
| 144 | if (pool->cb) |
| 145 | (*pool->cb)(pool, sz); |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | mem->next = pool->first_mem; |
| 150 | pool->first_mem = mem; |
| 151 | |
| 152 | #ifdef TRACE_ |
| 153 | { |
| 154 | char msg[120]; |
| 155 | pj_ansi_sprintf(msg, "Mem %X (%d+%d bytes) allocated by %s:%d\r\n", |
| 156 | mem, sz, sizeof(struct pj_pool_mem), |
| 157 | file, line); |
| 158 | TRACE_(msg); |
| 159 | } |
| 160 | #endif |
| 161 | |
| 162 | return ((char*)mem) + sizeof(struct pj_pool_mem); |
| 163 | } |
| 164 | |
| 165 | /* Allocate memory from the pool and zero the memory */ |
| 166 | PJ_DEF(void*) pj_pool_calloc_imp( const char *file, int line, |
| 167 | pj_pool_t *pool, unsigned cnt, |
| 168 | unsigned elemsz) |
| 169 | { |
| 170 | void *mem; |
| 171 | |
| 172 | mem = pj_pool_alloc_imp(file, line, pool, cnt*elemsz); |
| 173 | if (!mem) |
| 174 | return NULL; |
| 175 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 176 | pj_bzero(mem, cnt*elemsz); |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 177 | return mem; |
| 178 | } |
| 179 | |
| 180 | /* Allocate memory from the pool and zero the memory */ |
| 181 | PJ_DEF(void*) pj_pool_zalloc_imp( const char *file, int line, |
| 182 | pj_pool_t *pool, pj_size_t sz) |
| 183 | { |
Benny Prijono | d7c6d05 | 2008-07-12 09:31:34 +0000 | [diff] [blame] | 184 | return pj_pool_calloc_imp(file, line, pool, 1, sz); |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
Benny Prijono | d7c6d05 | 2008-07-12 09:31:34 +0000 | [diff] [blame] | 188 | |
| 189 | #endif /* PJ_HAS_POOL_ALT_API */ |