Debian Codegraph @

Xpjproject/pjlib/include/pj/pool_alt.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 PJ_BEGIN_DECL
25
26 /**
27 * The type for function to receive callback from the pool when it is unable
28 * to allocate memory. The elegant way to handle this condition is to throw
29 * exception, and this is what is expected by most of this library
30 * components.
31 */
32 typedef void pj_pool_callback(pj_pool_t *pool, pj_size_t size);
33
34 struct pj_pool_mem
35 {
36 struct pj_pool_mem *next;
37
38 /* data follows immediately */
39 };
40
41
42 struct pj_pool_t
43 {
44 struct pj_pool_mem *first_mem;
45 pj_pool_factory *factory;
46 char obj_name[32];
47 pj_size_t used_size;
48 pj_pool_callback *cb;
49 pj_size_t alignment;
50 };
51
52
53 #define PJ_POOL_SIZE (sizeof(struct pj_pool_t))
54
55 /**
56 * This constant denotes the exception number that will be thrown by default
57 * memory factory policy when memory allocation fails.
58 */
59 PJ_DECL_DATA(int) PJ_NO_MEMORY_EXCEPTION;
60
61 /**
62 * Get #PJ_NO_MEMORY_EXCEPTION constant.
63 */
64 PJ_DECL(int) pj_NO_MEMORY_EXCEPTION(void);
65
66
67 /*
68 * Declare all pool API as macro that calls the implementation
69 * function.
70 */
71 #define pj_pool_create(fc,nm,init,inc,cb) \
72 pj_pool_create_imp(__FILE__, __LINE__, fc, nm, init, inc, 0, cb)
73 #define pj_pool_aligned_create(fc,nm,init,inc,alg,cb) \
74 pj_pool_create_imp(__FILE__, __LINE__, fc, nm, init, inc, alg, cb)
75
76 #define pj_pool_release(pool) pj_pool_release_imp(pool)
77 #define pj_pool_safe_release(pool) pj_pool_safe_release_imp(pool)
78 #define pj_pool_secure_release(pool) pj_pool_secure_release_imp(pool)
79 #define pj_pool_getobjname(pool) pj_pool_getobjname_imp(pool)
80 #define pj_pool_reset(pool) pj_pool_reset_imp(pool)
81 #define pj_pool_get_capacity(pool) pj_pool_get_capacity_imp(pool)
82 #define pj_pool_get_used_size(pool) pj_pool_get_used_size_imp(pool)
83 #define pj_pool_alloc(pool,sz) \
84 pj_pool_alloc_imp(__FILE__, __LINE__, pool, 0, sz)
85 #define pj_pool_aligned_alloc(pool, alignment, sz) \
86 pj_pool_alloc_imp(__FILE__, __LINE__, pool, alignment, sz)
87
88 #define pj_pool_calloc(pool,cnt,elem) \
89 pj_pool_calloc_imp(__FILE__, __LINE__, pool, cnt, elem)
90
91 #define pj_pool_zalloc(pool,sz) \
92 pj_pool_zalloc_imp(__FILE__, __LINE__, pool, sz)
93
94
95
96 /*
97 * Declare prototypes for pool implementation API.
98 */
99
100 /* Create pool */
101 PJ_DECL(pj_pool_t*) pj_pool_create_imp(const char *file, int line,
102 void *factory,
103 const char *name,
104 pj_size_t initial_size,
105 pj_size_t increment_size,
106 pj_size_t alignment,
107 pj_pool_callback *callback);
108
109 /* Release pool */
110 PJ_DECL(void) pj_pool_release_imp(pj_pool_t *pool);
111
112 /* Safe release pool */
113 PJ_DECL(void) pj_pool_safe_release_imp(pj_pool_t **pool);
114
115 /* Secure release pool */
116 PJ_DECL(void) pj_pool_secure_release_imp(pj_pool_t **pool);
117
118 /* Get pool name */
119 PJ_DECL(const char*) pj_pool_getobjname_imp(pj_pool_t *pool);
120
121 /* Reset pool */
122 PJ_DECL(void) pj_pool_reset_imp(pj_pool_t *pool);
123
124 /* Get capacity */
125 PJ_DECL(pj_size_t) pj_pool_get_capacity_imp(pj_pool_t *pool);
126
127 /* Get total used size */
128 PJ_DECL(pj_size_t) pj_pool_get_used_size_imp(pj_pool_t *pool);
129
130 /* Allocate memory from the pool */
131 PJ_DECL(void*) pj_pool_alloc_imp(const char *file, int line,
132 pj_pool_t *pool, pj_size_t alignment,
133 pj_size_t sz);
134
135 /* Allocate memory from the pool and zero the memory */
136 PJ_DECL(void*) pj_pool_calloc_imp(const char *file, int line,
137 pj_pool_t *pool, unsigned cnt,
138 unsigned elemsz);
139
140 /* Allocate memory from the pool and zero the memory */
141 PJ_DECL(void*) pj_pool_zalloc_imp(const char *file, int line,
142 pj_pool_t *pool, pj_size_t sz);
143
144
145 #define PJ_POOL_ZALLOC_T(pool,type) \
146 ((type*)pj_pool_zalloc(pool, sizeof(type)))
147 #define PJ_POOL_ALLOC_T(pool,type) \
148 ((type*)pj_pool_alloc(pool, sizeof(type)))
149 #ifndef PJ_POOL_ALIGNMENT
150 # define PJ_POOL_ALIGNMENT 4
151 #endif
152
153 /**
154 * This structure declares pool factory interface.
155 */
156 typedef struct pj_pool_factory_policy
157 {
158 /**
159 * Allocate memory block (for use by pool). This function is called
160 * by memory pool to allocate memory block.
161 *
162 * @param factory Pool factory.
163 * @param size The size of memory block to allocate.
164 *
165 * @return Memory block.
166 */
167 void* (*block_alloc)(pj_pool_factory *factory, pj_size_t size);
168
169 /**
170 * Free memory block.
171 *
172 * @param factory Pool factory.
173 * @param mem Memory block previously allocated by block_alloc().
174 * @param size The size of memory block.
175 */
176 void (*block_free)(pj_pool_factory *factory, void *mem, pj_size_t size);
177
178 /**
179 * Default callback to be called when memory allocation fails.
180 */
181 pj_pool_callback *callback;
182
183 /**
184 * Option flags.
185 */
186 unsigned flags;
187
188 } pj_pool_factory_policy;
189
190 struct pj_pool_factory
191 {
192 pj_pool_factory_policy policy;
193 int dummy;
194 };
195
196 struct pj_caching_pool
197 {
198 pj_pool_factory factory;
199
200 /* just to make it compilable */
201 unsigned used_count;
202 unsigned used_size;
203 unsigned peak_used_size;
204 };
205
206 /* just to make it compilable */
207 typedef struct pj_pool_block
208 {
209 int dummy;
210 } pj_pool_block;
211
212 #define pj_caching_pool_init( cp, pol, mac)
213 #define pj_caching_pool_destroy(cp)
214 #define pj_pool_factory_dump(pf, detail)
215
216 PJ_END_DECL
217
218 #endif /* __PJ_POOL_ALT_H__ */
219