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