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