blob: 55a9a0b6bad8f759b714daf561b2cadbdd9cc483 [file] [log] [blame]
Benny Prijono8508aa02006-03-30 15:56:01 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono8508aa02006-03-30 15:56:01 +00004 *
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 Prijono8508aa02006-03-30 15:56:01 +000025/**
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 */
31typedef void pj_pool_callback(pj_pool_t *pool, pj_size_t size);
32
Benny Prijonod7c6d052008-07-12 09:31:34 +000033struct pj_pool_mem
34{
35 struct pj_pool_mem *next;
36
37 /* data follows immediately */
38};
39
40
Benny Prijono07895192008-10-02 22:13:01 +000041struct pj_pool_t
Benny Prijonod7c6d052008-07-12 09:31:34 +000042{
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 Prijono07895192008-10-02 22:13:01 +000048};
Benny Prijonod7c6d052008-07-12 09:31:34 +000049
50
Benny Prijono40032fd2008-07-14 16:48:13 +000051#define PJ_POOL_SIZE (sizeof(struct pj_pool_t))
Benny Prijonod7c6d052008-07-12 09:31:34 +000052
Benny Prijono8508aa02006-03-30 15:56:01 +000053/**
54 * This constant denotes the exception number that will be thrown by default
55 * memory factory policy when memory allocation fails.
56 */
57extern 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 */
89PJ_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 */
97PJ_DECL(void) pj_pool_release_imp(pj_pool_t *pool);
98
99/* Get pool name */
100PJ_DECL(const char*) pj_pool_getobjname_imp(pj_pool_t *pool);
101
102/* Reset pool */
103PJ_DECL(void) pj_pool_reset_imp(pj_pool_t *pool);
104
105/* Get capacity */
106PJ_DECL(pj_size_t) pj_pool_get_capacity_imp(pj_pool_t *pool);
107
108/* Get total used size */
109PJ_DECL(pj_size_t) pj_pool_get_used_size_imp(pj_pool_t *pool);
110
111/* Allocate memory from the pool */
112PJ_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 */
116PJ_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 */
121PJ_DECL(void*) pj_pool_zalloc_imp(const char *file, int line,
122 pj_pool_t *pool, pj_size_t sz);
123
124
Benny Prijonod7c6d052008-07-12 09:31:34 +0000125#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 */
136typedef 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 Prijono07895192008-10-02 22:13:01 +0000170struct pj_pool_factory
Benny Prijono8508aa02006-03-30 15:56:01 +0000171{
Benny Prijonod7c6d052008-07-12 09:31:34 +0000172 pj_pool_factory_policy policy;
Benny Prijono8508aa02006-03-30 15:56:01 +0000173 int dummy;
Benny Prijono07895192008-10-02 22:13:01 +0000174};
Benny Prijono8508aa02006-03-30 15:56:01 +0000175
Benny Prijono07895192008-10-02 22:13:01 +0000176struct pj_caching_pool
Benny Prijono8508aa02006-03-30 15:56:01 +0000177{
178 pj_pool_factory factory;
Benny Prijono8508aa02006-03-30 15:56:01 +0000179
Benny Prijono07895192008-10-02 22:13:01 +0000180 /* 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 */
187typedef struct pj_pool_block
188{
189 int dummy;
190} pj_pool_block;
Benny Prijono8508aa02006-03-30 15:56:01 +0000191
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 Prijono8508aa02006-03-30 15:56:01 +0000196#endif /* __PJ_POOL_ALT_H__ */
197