blob: 52f030c61fbb03cd47efad8f7923ad259c5b7a27 [file] [log] [blame]
Benny Prijono8508aa02006-03-30 15:56:01 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono8508aa02006-03-30 15:56:01 +00005 *
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#ifndef __PJ_POOL_ALT_H__
21#define __PJ_POOL_ALT_H__
22
23#define __PJ_POOL_H__
24
25
Benny Prijono8508aa02006-03-30 15:56:01 +000026/**
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 */
32typedef void pj_pool_callback(pj_pool_t *pool, pj_size_t size);
33
Benny Prijonod7c6d052008-07-12 09:31:34 +000034struct pj_pool_mem
35{
36 struct pj_pool_mem *next;
37
38 /* data follows immediately */
39};
40
41
Benny Prijono07895192008-10-02 22:13:01 +000042struct pj_pool_t
Benny Prijonod7c6d052008-07-12 09:31:34 +000043{
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;
Benny Prijono07895192008-10-02 22:13:01 +000049};
Benny Prijonod7c6d052008-07-12 09:31:34 +000050
51
Benny Prijono40032fd2008-07-14 16:48:13 +000052#define PJ_POOL_SIZE (sizeof(struct pj_pool_t))
Benny Prijonod7c6d052008-07-12 09:31:34 +000053
Benny Prijono8508aa02006-03-30 15:56:01 +000054/**
55 * This constant denotes the exception number that will be thrown by default
56 * memory factory policy when memory allocation fails.
57 */
58extern int PJ_NO_MEMORY_EXCEPTION;
59
60
61
62/*
63 * Declare all pool API as macro that calls the implementation
64 * function.
65 */
66#define pj_pool_create(fc,nm,init,inc,cb) \
67 pj_pool_create_imp(__FILE__, __LINE__, fc, nm, init, inc, cb)
68
69#define pj_pool_release(pool) pj_pool_release_imp(pool)
70#define pj_pool_getobjname(pool) pj_pool_getobjname_imp(pool)
71#define pj_pool_reset(pool) pj_pool_reset_imp(pool)
72#define pj_pool_get_capacity(pool) pj_pool_get_capacity_imp(pool)
73#define pj_pool_get_used_size(pool) pj_pool_get_used_size_imp(pool)
74#define pj_pool_alloc(pool,sz) \
75 pj_pool_alloc_imp(__FILE__, __LINE__, pool, sz)
76
77#define pj_pool_calloc(pool,cnt,elem) \
78 pj_pool_calloc_imp(__FILE__, __LINE__, pool, cnt, elem)
79
80#define pj_pool_zalloc(pool,sz) \
81 pj_pool_zalloc_imp(__FILE__, __LINE__, pool, sz)
82
83
84
85/*
86 * Declare prototypes for pool implementation API.
87 */
88
89/* Create pool */
90PJ_DECL(pj_pool_t*) pj_pool_create_imp(const char *file, int line,
91 void *factory,
92 const char *name,
93 pj_size_t initial_size,
94 pj_size_t increment_size,
95 pj_pool_callback *callback);
96
97/* Release pool */
98PJ_DECL(void) pj_pool_release_imp(pj_pool_t *pool);
99
100/* Get pool name */
101PJ_DECL(const char*) pj_pool_getobjname_imp(pj_pool_t *pool);
102
103/* Reset pool */
104PJ_DECL(void) pj_pool_reset_imp(pj_pool_t *pool);
105
106/* Get capacity */
107PJ_DECL(pj_size_t) pj_pool_get_capacity_imp(pj_pool_t *pool);
108
109/* Get total used size */
110PJ_DECL(pj_size_t) pj_pool_get_used_size_imp(pj_pool_t *pool);
111
112/* Allocate memory from the pool */
113PJ_DECL(void*) pj_pool_alloc_imp(const char *file, int line,
114 pj_pool_t *pool, pj_size_t sz);
115
116/* Allocate memory from the pool and zero the memory */
117PJ_DECL(void*) pj_pool_calloc_imp(const char *file, int line,
118 pj_pool_t *pool, unsigned cnt,
119 unsigned elemsz);
120
121/* Allocate memory from the pool and zero the memory */
122PJ_DECL(void*) pj_pool_zalloc_imp(const char *file, int line,
123 pj_pool_t *pool, pj_size_t sz);
124
125
Benny Prijonod7c6d052008-07-12 09:31:34 +0000126#define PJ_POOL_ZALLOC_T(pool,type) \
127 ((type*)pj_pool_zalloc(pool, sizeof(type)))
128#define PJ_POOL_ALLOC_T(pool,type) \
129 ((type*)pj_pool_alloc(pool, sizeof(type)))
130#ifndef PJ_POOL_ALIGNMENT
131# define PJ_POOL_ALIGNMENT 4
132#endif
133
134/**
135 * This structure declares pool factory interface.
136 */
137typedef struct pj_pool_factory_policy
138{
139 /**
140 * Allocate memory block (for use by pool). This function is called
141 * by memory pool to allocate memory block.
142 *
143 * @param factory Pool factory.
144 * @param size The size of memory block to allocate.
145 *
146 * @return Memory block.
147 */
148 void* (*block_alloc)(pj_pool_factory *factory, pj_size_t size);
149
150 /**
151 * Free memory block.
152 *
153 * @param factory Pool factory.
154 * @param mem Memory block previously allocated by block_alloc().
155 * @param size The size of memory block.
156 */
157 void (*block_free)(pj_pool_factory *factory, void *mem, pj_size_t size);
158
159 /**
160 * Default callback to be called when memory allocation fails.
161 */
162 pj_pool_callback *callback;
163
164 /**
165 * Option flags.
166 */
167 unsigned flags;
168
169} pj_pool_factory_policy;
170
Benny Prijono07895192008-10-02 22:13:01 +0000171struct pj_pool_factory
Benny Prijono8508aa02006-03-30 15:56:01 +0000172{
Benny Prijonod7c6d052008-07-12 09:31:34 +0000173 pj_pool_factory_policy policy;
Benny Prijono8508aa02006-03-30 15:56:01 +0000174 int dummy;
Benny Prijono07895192008-10-02 22:13:01 +0000175};
Benny Prijono8508aa02006-03-30 15:56:01 +0000176
Benny Prijono07895192008-10-02 22:13:01 +0000177struct pj_caching_pool
Benny Prijono8508aa02006-03-30 15:56:01 +0000178{
179 pj_pool_factory factory;
Benny Prijono8508aa02006-03-30 15:56:01 +0000180
Benny Prijono07895192008-10-02 22:13:01 +0000181 /* just to make it compilable */
182 unsigned used_count;
183 unsigned used_size;
184 unsigned peak_used_size;
185};
186
187/* just to make it compilable */
188typedef struct pj_pool_block
189{
190 int dummy;
191} pj_pool_block;
Benny Prijono8508aa02006-03-30 15:56:01 +0000192
193#define pj_caching_pool_init( cp, pol, mac)
194#define pj_caching_pool_destroy(cp)
195#define pj_pool_factory_dump(pf, detail)
196
Benny Prijono8508aa02006-03-30 15:56:01 +0000197#endif /* __PJ_POOL_ALT_H__ */
198