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 | #ifndef __PJ_POOL_ALT_H__ |
| 20 | #define __PJ_POOL_ALT_H__ |
| 21 | |
| 22 | #define __PJ_POOL_H__ |
| 23 | |
| 24 | |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 25 | /** |
| 26 | * The type for function to receive callback from the pool when it is unable |
| 27 | * to allocate memory. The elegant way to handle this condition is to throw |
| 28 | * exception, and this is what is expected by most of this library |
| 29 | * components. |
| 30 | */ |
| 31 | typedef void pj_pool_callback(pj_pool_t *pool, pj_size_t size); |
| 32 | |
Benny Prijono | d7c6d05 | 2008-07-12 09:31:34 +0000 | [diff] [blame^] | 33 | struct pj_pool_mem |
| 34 | { |
| 35 | struct pj_pool_mem *next; |
| 36 | |
| 37 | /* data follows immediately */ |
| 38 | }; |
| 39 | |
| 40 | |
| 41 | typedef struct pj_pool_t |
| 42 | { |
| 43 | struct pj_pool_mem *first_mem; |
| 44 | pj_pool_factory *factory; |
| 45 | char obj_name[32]; |
| 46 | pj_size_t used_size; |
| 47 | pj_pool_callback *cb; |
| 48 | } pj_pool_t; |
| 49 | |
| 50 | |
| 51 | |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 52 | /** |
| 53 | * This constant denotes the exception number that will be thrown by default |
| 54 | * memory factory policy when memory allocation fails. |
| 55 | */ |
| 56 | extern int PJ_NO_MEMORY_EXCEPTION; |
| 57 | |
| 58 | |
| 59 | |
| 60 | /* |
| 61 | * Declare all pool API as macro that calls the implementation |
| 62 | * function. |
| 63 | */ |
| 64 | #define pj_pool_create(fc,nm,init,inc,cb) \ |
| 65 | pj_pool_create_imp(__FILE__, __LINE__, fc, nm, init, inc, cb) |
| 66 | |
| 67 | #define pj_pool_release(pool) pj_pool_release_imp(pool) |
| 68 | #define pj_pool_getobjname(pool) pj_pool_getobjname_imp(pool) |
| 69 | #define pj_pool_reset(pool) pj_pool_reset_imp(pool) |
| 70 | #define pj_pool_get_capacity(pool) pj_pool_get_capacity_imp(pool) |
| 71 | #define pj_pool_get_used_size(pool) pj_pool_get_used_size_imp(pool) |
| 72 | #define pj_pool_alloc(pool,sz) \ |
| 73 | pj_pool_alloc_imp(__FILE__, __LINE__, pool, sz) |
| 74 | |
| 75 | #define pj_pool_calloc(pool,cnt,elem) \ |
| 76 | pj_pool_calloc_imp(__FILE__, __LINE__, pool, cnt, elem) |
| 77 | |
| 78 | #define pj_pool_zalloc(pool,sz) \ |
| 79 | pj_pool_zalloc_imp(__FILE__, __LINE__, pool, sz) |
| 80 | |
| 81 | |
| 82 | |
| 83 | /* |
| 84 | * Declare prototypes for pool implementation API. |
| 85 | */ |
| 86 | |
| 87 | /* Create pool */ |
| 88 | PJ_DECL(pj_pool_t*) pj_pool_create_imp(const char *file, int line, |
| 89 | void *factory, |
| 90 | const char *name, |
| 91 | pj_size_t initial_size, |
| 92 | pj_size_t increment_size, |
| 93 | pj_pool_callback *callback); |
| 94 | |
| 95 | /* Release pool */ |
| 96 | PJ_DECL(void) pj_pool_release_imp(pj_pool_t *pool); |
| 97 | |
| 98 | /* Get pool name */ |
| 99 | PJ_DECL(const char*) pj_pool_getobjname_imp(pj_pool_t *pool); |
| 100 | |
| 101 | /* Reset pool */ |
| 102 | PJ_DECL(void) pj_pool_reset_imp(pj_pool_t *pool); |
| 103 | |
| 104 | /* Get capacity */ |
| 105 | PJ_DECL(pj_size_t) pj_pool_get_capacity_imp(pj_pool_t *pool); |
| 106 | |
| 107 | /* Get total used size */ |
| 108 | PJ_DECL(pj_size_t) pj_pool_get_used_size_imp(pj_pool_t *pool); |
| 109 | |
| 110 | /* Allocate memory from the pool */ |
| 111 | PJ_DECL(void*) pj_pool_alloc_imp(const char *file, int line, |
| 112 | pj_pool_t *pool, pj_size_t sz); |
| 113 | |
| 114 | /* Allocate memory from the pool and zero the memory */ |
| 115 | PJ_DECL(void*) pj_pool_calloc_imp(const char *file, int line, |
| 116 | pj_pool_t *pool, unsigned cnt, |
| 117 | unsigned elemsz); |
| 118 | |
| 119 | /* Allocate memory from the pool and zero the memory */ |
| 120 | PJ_DECL(void*) pj_pool_zalloc_imp(const char *file, int line, |
| 121 | pj_pool_t *pool, pj_size_t sz); |
| 122 | |
| 123 | |
Benny Prijono | d7c6d05 | 2008-07-12 09:31:34 +0000 | [diff] [blame^] | 124 | #define PJ_POOL_ZALLOC_T(pool,type) \ |
| 125 | ((type*)pj_pool_zalloc(pool, sizeof(type))) |
| 126 | #define PJ_POOL_ALLOC_T(pool,type) \ |
| 127 | ((type*)pj_pool_alloc(pool, sizeof(type))) |
| 128 | #ifndef PJ_POOL_ALIGNMENT |
| 129 | # define PJ_POOL_ALIGNMENT 4 |
| 130 | #endif |
| 131 | |
| 132 | /** |
| 133 | * This structure declares pool factory interface. |
| 134 | */ |
| 135 | typedef struct pj_pool_factory_policy |
| 136 | { |
| 137 | /** |
| 138 | * Allocate memory block (for use by pool). This function is called |
| 139 | * by memory pool to allocate memory block. |
| 140 | * |
| 141 | * @param factory Pool factory. |
| 142 | * @param size The size of memory block to allocate. |
| 143 | * |
| 144 | * @return Memory block. |
| 145 | */ |
| 146 | void* (*block_alloc)(pj_pool_factory *factory, pj_size_t size); |
| 147 | |
| 148 | /** |
| 149 | * Free memory block. |
| 150 | * |
| 151 | * @param factory Pool factory. |
| 152 | * @param mem Memory block previously allocated by block_alloc(). |
| 153 | * @param size The size of memory block. |
| 154 | */ |
| 155 | void (*block_free)(pj_pool_factory *factory, void *mem, pj_size_t size); |
| 156 | |
| 157 | /** |
| 158 | * Default callback to be called when memory allocation fails. |
| 159 | */ |
| 160 | pj_pool_callback *callback; |
| 161 | |
| 162 | /** |
| 163 | * Option flags. |
| 164 | */ |
| 165 | unsigned flags; |
| 166 | |
| 167 | } pj_pool_factory_policy; |
| 168 | |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 169 | typedef struct pj_pool_factory |
| 170 | { |
Benny Prijono | d7c6d05 | 2008-07-12 09:31:34 +0000 | [diff] [blame^] | 171 | pj_pool_factory_policy policy; |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 172 | int dummy; |
| 173 | } pj_pool_factory; |
| 174 | |
| 175 | typedef struct pj_caching_pool |
| 176 | { |
| 177 | pj_pool_factory factory; |
| 178 | } pj_caching_pool; |
| 179 | |
| 180 | |
| 181 | #define pj_caching_pool_init( cp, pol, mac) |
| 182 | #define pj_caching_pool_destroy(cp) |
| 183 | #define pj_pool_factory_dump(pf, detail) |
| 184 | |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 185 | #endif /* __PJ_POOL_ALT_H__ */ |
| 186 | |