blob: 5cb6aaf8172e11b2d413002d8bec2fada465a633 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +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 */
Benny Prijono8508aa02006-03-30 15:56:01 +000019
20#include <pj/list.h>
21
22/* See if we use pool's alternate API.
23 * The alternate API is used e.g. to implement pool debugging.
24 */
25#if PJ_HAS_POOL_ALT_API
26# include <pj/pool_alt.h>
27#endif
28
29
Benny Prijono9033e312005-11-21 02:08:39 +000030#ifndef __PJ_POOL_H__
31#define __PJ_POOL_H__
32
33/**
34 * @file pool.h
35 * @brief Memory Pool.
36 */
37
Benny Prijono9033e312005-11-21 02:08:39 +000038PJ_BEGIN_DECL
39
40/**
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +000041 * @defgroup PJ_POOL_GROUP Fast Memory Pool
Benny Prijono9033e312005-11-21 02:08:39 +000042 * @ingroup PJ
43 * @brief
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +000044 * Memory pools allow dynamic memory allocation comparable to malloc or the
45 * new in operator C++. Those implementations are not desirable for very
46 * high performance applications or real-time systems, because of the
47 * performance bottlenecks and it suffers from fragmentation issue.
Benny Prijono9033e312005-11-21 02:08:39 +000048 *
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +000049 * \section PJ_POOL_INTRO_SEC PJLIB's Memory Pool
50 * \subsection PJ_POOL_ADVANTAGE_SUBSEC Advantages
Benny Prijono9033e312005-11-21 02:08:39 +000051 *
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +000052 * PJLIB's pool has many advantages over traditional malloc/new operator and
53 * over other memory pool implementations, because:
54 * - unlike other memory pool implementation, it allows allocation of
55 * memory chunks of different sizes,
56 * - it's very very fast.
57 * \n
58 * Memory chunk allocation is not only an O(1)
59 * operation, but it's also very simple (just
60 * few pointer arithmetic operations) and it doesn't require locking
61 * any mutex,
62 * - it's memory efficient.
63 * \n
64 * Pool doesn't keep track individual memory chunks allocated by
65 * applications, so there is no additional overhead needed for each
66 * memory allocation (other than possible additional of few bytes, up to
67 * PJ_POOL_ALIGNMENT-1, for aligning the memory).
68 * But see the @ref PJ_POOL_CAVEATS_SUBSEC below.
69 * - it prevents memory leaks.
70 * \n
71 * Memory pool inherently has garbage collection functionality. In fact,
72 * there is no need to free the chunks allocated from the memory pool.
73 * All chunks previously allocated from the pool will be freed once the
74 * pool itself is destroyed. This would prevent memory leaks that haunt
75 * programmers for decades, and it provides additional performance
76 * advantage over traditional malloc/new operator.
77 *
78 * Even more, PJLIB's memory pool provides some additional usability and
79 * flexibility for applications:
80 * - memory leaks are easily traceable, since memory pool is assigned name,
81 * and application can inspect what pools currently active in the system.
82 * - by design, memory allocation from a pool is not thread safe. We assumed
83 * that a pool will be owned by a higher level object, and thread safety
84 * should be handled by that object. This enables very fast pool operations
85 * and prevents unnecessary locking operations,
86 * - by default, the memory pool API behaves more like C++ new operator,
87 * in that it will throw PJ_NO_MEMORY_EXCEPTION exception (see
88 * @ref PJ_EXCEPT) when memory chunk allocation fails. This enables failure
89 * handling to be done on more high level function (instead of checking
90 * the result of pj_pool_alloc() everytime). If application doesn't like
91 * this, the default behavior can be changed on global basis by supplying
92 * different policy to the pool factory.
93 * - any memory allocation backend allocator/deallocator may be used. By
94 * default, the policy uses malloc() and free() to manage the pool's block,
95 * but application may use different strategy, for example to allocate
96 * memory blocks from a globally static memory location.
Benny Prijono9033e312005-11-21 02:08:39 +000097 *
98 *
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +000099 * \subsection PJ_POOL_PERFORMANCE_SUBSEC Performance
Benny Prijono9033e312005-11-21 02:08:39 +0000100 *
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000101 * The result of PJLIB's memory design and careful implementation is a
102 * memory allocation strategy that can speed-up the memory allocations
103 * and deallocations by up to <b>30 times</b> compared to standard
Benny Prijono5ee1f2e2006-09-22 20:43:00 +0000104 * malloc()/free() (more than 150 million allocations per second on a
105 * P4/3.0GHz Linux machine).
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000106 *
107 * (Note: your mileage may vary, of course. You can see how much PJLIB's
108 * pool improves the performance over malloc()/free() in your target
109 * system by running pjlib-test application).
Benny Prijono9033e312005-11-21 02:08:39 +0000110 *
111 *
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000112 * \subsection PJ_POOL_CAVEATS_SUBSEC Caveats
Benny Prijono9033e312005-11-21 02:08:39 +0000113 *
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000114 * There are some caveats though!
115 *
116 * When creating pool, PJLIB requires applications to specify the initial
117 * pool size, and as soon as the pool is created, PJLIB allocates memory
118 * from the system by that size. Application designers MUST choose the
119 * initial pool size carefully, since choosing too big value will result in
120 * wasting system's memory.
121 *
122 * But the pool can grow. Application designer can specify how the
123 * pool will grow in size, by specifying the size increment when creating
124 * the pool.
125 *
126 * The pool, however, <b>cannot</b> shrink! Since there is <b>no</b>
127 * function to deallocate memory chunks, there is no way for the pool to
128 * release back unused memory to the system.
129 * Application designers must be aware that constant memory allocations
130 * from pool that has infinite life-time may cause the memory usage of
131 * the application to grow over time.
132 *
133 *
134 * \section PJ_POOL_USING_SEC Using Memory Pool
135 *
136 * This section describes how to use PJLIB's memory pool framework.
137 * As we hope the readers will witness, PJLIB's memory pool API is quite
138 * straightforward.
139 *
140 * \subsection PJ_POOL_USING_F Create Pool Factory
141 * First, application needs to initialize a pool factory (this normally
142 * only needs to be done once in one application). PJLIB provides
143 * a pool factory implementation called caching pool (see @ref
144 * PJ_CACHING_POOL), and it is initialized by calling #pj_caching_pool_init().
145 *
146 * \subsection PJ_POOL_USING_P Create The Pool
147 * Then application creates the pool object itself with #pj_pool_create(),
148 * specifying among other thing the pool factory where the pool should
149 * be created from, the pool name, initial size, and increment/expansion
150 * size.
151 *
152 * \subsection PJ_POOL_USING_M Allocate Memory as Required
153 * Then whenever application needs to allocate dynamic memory, it would
154 * call #pj_pool_alloc(), #pj_pool_calloc(), or #pj_pool_zalloc() to
155 * allocate memory chunks from the pool.
156 *
157 * \subsection PJ_POOL_USING_DP Destroy the Pool
158 * When application has finished with the pool, it should call
159 * #pj_pool_release() to release the pool object back to the factory.
160 * Depending on the types of the factory, this may release the memory back
161 * to the operating system.
162 *
163 * \subsection PJ_POOL_USING_Dc Destroy the Pool Factory
164 * And finally, before application quites, it should deinitialize the
165 * pool factory, to make sure that all memory blocks allocated by the
166 * factory are released back to the operating system. After this, of
167 * course no more memory pool allocation can be requested.
168 *
169 * \subsection PJ_POOL_USING_EX Example
170 * Below is a sample complete program that utilizes PJLIB's memory pool.
171 *
172 * \code
173
174 #include <pjlib.h>
175
176 #define THIS_FILE "pool_sample.c"
177
178 static void my_perror(const char *title, pj_status_t status)
179 {
180 char errmsg[PJ_ERR_MSG_SIZE];
181
182 pj_strerror(status, errmsg, sizeof(errmsg));
183 PJ_LOG(1,(THIS_FILE, "%s: %s [status=%d]", title, errmsg, status));
184 }
185
186 static void pool_demo_1(pj_pool_factory *pfactory)
187 {
188 unsigned i;
189 pj_pool_t *pool;
190
191 // Must create pool before we can allocate anything
192 pool = pj_pool_create(pfactory, // the factory
193 "pool1", // pool's name
194 4000, // initial size
195 4000, // increment size
196 NULL); // use default callback.
197 if (pool == NULL) {
198 my_perror("Error creating pool", PJ_ENOMEM);
199 return;
200 }
201
202 // Demo: allocate some memory chunks
203 for (i=0; i<1000; ++i) {
204 void *p;
205
206 p = pj_pool_alloc(pool, (pj_rand()+1) % 512);
207
208 // Do something with p
209 ...
210
211 // Look! No need to free p!!
212 }
213
214 // Done with silly demo, must free pool to release all memory.
215 pj_pool_release(pool);
216 }
217
218 int main()
219 {
220 pj_caching_pool cp;
221 pj_status_t status;
222
223 // Must init PJLIB before anything else
224 status = pj_init();
225 if (status != PJ_SUCCESS) {
226 my_perror("Error initializing PJLIB", status);
227 return 1;
228 }
229
Benny Prijono8ab968f2007-07-20 08:08:30 +0000230 // Create the pool factory, in this case, a caching pool,
231 // using default pool policy.
232 pj_caching_pool_init(&cp, NULL, 1024*1024 );
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000233
234 // Do a demo
235 pool_demo_1(&cp.factory);
236
237 // Done with demos, destroy caching pool before exiting app.
238 pj_caching_pool_destroy(&cp);
239
240 return 0;
241 }
242
243 \endcode
244 *
245 * More information about pool factory, the pool object, and caching pool
246 * can be found on the Module Links below.
247 */
248
249
250/**
251 * @defgroup PJ_POOL Memory Pool Object
252 * @ingroup PJ_POOL_GROUP
253 * @brief
Benny Prijono9033e312005-11-21 02:08:39 +0000254 * The memory pool is an opaque object created by pool factory.
255 * Application uses this object to request a memory chunk, by calling
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000256 * #pj_pool_alloc(), #pj_pool_calloc(), or #pj_pool_zalloc().
257 * When the application has finished using
258 * the pool, it must call #pj_pool_release() to free all the chunks previously
Benny Prijono9033e312005-11-21 02:08:39 +0000259 * allocated and release the pool back to the factory.
260 *
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000261 * A memory pool is initialized with an initial amount of memory, which is
262 * called a block. Pool can be configured to dynamically allocate more memory
263 * blocks when it runs out of memory.
264 *
265 * The pool doesn't keep track of individual memory allocations
266 * by user, and the user doesn't have to free these indidual allocations. This
267 * makes memory allocation simple and very fast. All the memory allocated from
268 * the pool will be destroyed when the pool itself is destroyed.
269 *
270 * \section PJ_POOL_THREADING_SEC More on Threading Policies
Benny Prijono9033e312005-11-21 02:08:39 +0000271 * - By design, memory allocation from a pool is not thread safe. We assumed
272 * that a pool will be owned by an object, and thread safety should be
273 * handled by that object. Thus these functions are not thread safe:
274 * - #pj_pool_alloc,
275 * - #pj_pool_calloc,
276 * - and other pool statistic functions.
277 * - Threading in the pool factory is decided by the policy set for the
278 * factory when it was created.
279 *
280 * \section PJ_POOL_EXAMPLES_SEC Examples
281 *
282 * For some sample codes on how to use the pool, please see:
283 * - @ref page_pjlib_pool_test
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000284 *
Benny Prijono9033e312005-11-21 02:08:39 +0000285 * @{
286 */
287
288/**
289 * The type for function to receive callback from the pool when it is unable
290 * to allocate memory. The elegant way to handle this condition is to throw
291 * exception, and this is what is expected by most of this library
292 * components.
293 */
294typedef void pj_pool_callback(pj_pool_t *pool, pj_size_t size);
295
296/**
297 * This class, which is used internally by the pool, describes a single
298 * block of memory from which user memory allocations will be allocated from.
299 */
300typedef struct pj_pool_block
301{
302 PJ_DECL_LIST_MEMBER(struct pj_pool_block); /**< List's prev and next. */
303 unsigned char *buf; /**< Start of buffer. */
304 unsigned char *cur; /**< Current alloc ptr. */
305 unsigned char *end; /**< End of buffer. */
306} pj_pool_block;
307
308
309/**
310 * This structure describes the memory pool. Only implementors of pool factory
311 * need to care about the contents of this structure.
312 */
313struct pj_pool_t
314{
315 PJ_DECL_LIST_MEMBER(struct pj_pool_t); /**< Standard list elements. */
316
317 /** Pool name */
318 char obj_name[PJ_MAX_OBJ_NAME];
319
320 /** Pool factory. */
321 pj_pool_factory *factory;
322
Benny Prijono53e3ffd2006-07-06 14:27:31 +0000323 /** Data put by factory */
324 void *factory_data;
325
Benny Prijono9033e312005-11-21 02:08:39 +0000326 /** Current capacity allocated by the pool. */
327 pj_size_t capacity;
328
Benny Prijono9033e312005-11-21 02:08:39 +0000329 /** Size of memory block to be allocated when the pool runs out of memory */
330 pj_size_t increment_size;
331
332 /** List of memory blocks allcoated by the pool. */
333 pj_pool_block block_list;
334
335 /** The callback to be called when the pool is unable to allocate memory. */
336 pj_pool_callback *callback;
337
338};
339
340
341/**
342 * Guidance on how much memory required for initial pool administrative data.
343 */
344#define PJ_POOL_SIZE (sizeof(struct pj_pool_t))
345
346/**
347 * Pool memory alignment (must be power of 2).
348 */
349#ifndef PJ_POOL_ALIGNMENT
350# define PJ_POOL_ALIGNMENT 4
351#endif
352
353/**
354 * Create a new pool from the pool factory. This wrapper will call create_pool
355 * member of the pool factory.
356 *
357 * @param factory The pool factory.
358 * @param name The name to be assigned to the pool. The name should
359 * not be longer than PJ_MAX_OBJ_NAME (32 chars), or
360 * otherwise it will be truncated.
361 * @param initial_size The size of initial memory blocks taken by the pool.
362 * Note that the pool will take 68+20 bytes for
363 * administrative area from this block.
364 * @param increment_size the size of each additional blocks to be allocated
365 * when the pool is running out of memory. If user
366 * requests memory which is larger than this size, then
367 * an error occurs.
368 * Note that each time a pool allocates additional block,
369 * it needs PJ_POOL_SIZE more to store some
370 * administrative info.
371 * @param callback Callback to be called when error occurs in the pool.
372 * If this value is NULL, then the callback from pool
373 * factory policy will be used.
374 * Note that when an error occurs during pool creation,
375 * the callback itself is not called. Instead, NULL
376 * will be returned.
377 *
378 * @return The memory pool, or NULL.
379 */
380PJ_IDECL(pj_pool_t*) pj_pool_create(pj_pool_factory *factory,
381 const char *name,
382 pj_size_t initial_size,
383 pj_size_t increment_size,
384 pj_pool_callback *callback);
385
386/**
387 * Release the pool back to pool factory.
388 *
389 * @param pool Memory pool.
390 */
391PJ_IDECL(void) pj_pool_release( pj_pool_t *pool );
392
393/**
394 * Get pool object name.
395 *
396 * @param pool the pool.
397 *
398 * @return pool name as NULL terminated string.
399 */
400PJ_IDECL(const char *) pj_pool_getobjname( const pj_pool_t *pool );
401
402/**
403 * Reset the pool to its state when it was initialized.
404 * This means that if additional blocks have been allocated during runtime,
405 * then they will be freed. Only the original block allocated during
406 * initialization is retained. This function will also reset the internal
407 * counters, such as pool capacity and used size.
408 *
409 * @param pool the pool.
410 */
411PJ_DECL(void) pj_pool_reset( pj_pool_t *pool );
412
413
414/**
415 * Get the pool capacity, that is, the system storage that have been allocated
416 * by the pool, and have been used/will be used to allocate user requests.
417 * There's no guarantee that the returned value represent a single
418 * contiguous block, because the capacity may be spread in several blocks.
419 *
420 * @param pool the pool.
421 *
422 * @return the capacity.
423 */
424PJ_IDECL(pj_size_t) pj_pool_get_capacity( pj_pool_t *pool );
425
426/**
427 * Get the total size of user allocation request.
428 *
429 * @param pool the pool.
430 *
431 * @return the total size.
432 */
433PJ_IDECL(pj_size_t) pj_pool_get_used_size( pj_pool_t *pool );
434
435/**
436 * Allocate storage with the specified size from the pool.
437 * If there's no storage available in the pool, then the pool can allocate more
438 * blocks if the increment size is larger than the requested size.
439 *
440 * @param pool the pool.
441 * @param size the requested size.
442 *
443 * @return pointer to the allocated memory.
Benny Prijono14c2b862007-02-21 00:40:05 +0000444 *
Benny Prijono7cd16222007-03-05 00:58:24 +0000445 * @see PJ_POOL_ALLOC_T
Benny Prijono9033e312005-11-21 02:08:39 +0000446 */
447PJ_IDECL(void*) pj_pool_alloc( pj_pool_t *pool, pj_size_t size);
448
449/**
450 * Allocate storage from the pool, and initialize it to zero.
451 * This function behaves like pj_pool_alloc(), except that the storage will
452 * be initialized to zero.
453 *
454 * @param pool the pool.
455 * @param count the number of elements in the array.
456 * @param elem the size of individual element.
457 *
458 * @return pointer to the allocated memory.
459 */
460PJ_IDECL(void*) pj_pool_calloc( pj_pool_t *pool, pj_size_t count,
461 pj_size_t elem);
462
463
464/**
Benny Prijono9033e312005-11-21 02:08:39 +0000465 * Allocate storage from the pool and initialize it to zero.
466 *
467 * @param pool The pool.
468 * @param size The size to be allocated.
469 *
470 * @return Pointer to the allocated memory.
Benny Prijono14c2b862007-02-21 00:40:05 +0000471 *
Benny Prijono7cd16222007-03-05 00:58:24 +0000472 * @see PJ_POOL_ZALLOC_T
Benny Prijono9033e312005-11-21 02:08:39 +0000473 */
Benny Prijono14c2b862007-02-21 00:40:05 +0000474PJ_INLINE(void*) pj_pool_zalloc(pj_pool_t *pool, pj_size_t size)
475{
476 return pj_pool_calloc(pool, 1, size);
477}
478
479
480/**
481 * This macro allocates memory from the pool and returns the instance of
482 * the specified type. It provides a stricker type safety than pj_pool_alloc()
483 * since the return value of this macro will be type-casted to the specified
484 * type.
485 *
486 * @param pool The pool
487 * @param type The type of object to be allocated
488 *
489 * @return Memory buffer of the specified type.
490 */
Benny Prijono7cd16222007-03-05 00:58:24 +0000491#define PJ_POOL_ALLOC_T(pool,type) \
Benny Prijono14c2b862007-02-21 00:40:05 +0000492 ((type*)pj_pool_alloc(pool, sizeof(type)))
493
494/**
495 * This macro allocates memory from the pool, zeroes the buffer, and
496 * returns the instance of the specified type. It provides a stricker type
497 * safety than pj_pool_zalloc() since the return value of this macro will be
498 * type-casted to the specified type.
499 *
500 * @param pool The pool
501 * @param type The type of object to be allocated
502 *
503 * @return Memory buffer of the specified type.
504 */
Benny Prijono7cd16222007-03-05 00:58:24 +0000505#define PJ_POOL_ZALLOC_T(pool,type) \
Benny Prijono14c2b862007-02-21 00:40:05 +0000506 ((type*)pj_pool_zalloc(pool, sizeof(type)))
507
Benny Prijono8ab968f2007-07-20 08:08:30 +0000508/*
509 * Internal functions
510 */
511PJ_IDECL(void*) pj_pool_alloc_from_block(pj_pool_block *block, pj_size_t size);
512PJ_DECL(void*) pj_pool_allocate_find(pj_pool_t *pool, unsigned size);
Benny Prijono9033e312005-11-21 02:08:39 +0000513
514
Benny Prijono8ab968f2007-07-20 08:08:30 +0000515
Benny Prijono9033e312005-11-21 02:08:39 +0000516/**
517 * @} // PJ_POOL
518 */
519
Benny Prijono92ac4472006-07-22 13:42:56 +0000520/* **************************************************************************/
Benny Prijono9033e312005-11-21 02:08:39 +0000521/**
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000522 * @defgroup PJ_POOL_FACTORY Pool Factory and Policy
Benny Prijono9033e312005-11-21 02:08:39 +0000523 * @ingroup PJ_POOL_GROUP
524 * @brief
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000525 * A pool object must be created through a factory. A factory not only provides
526 * generic interface functions to create and release pool, but also provides
527 * strategy to manage the life time of pools. One sample implementation,
528 * \a pj_caching_pool, can be set to keep the pools released by application for
529 * future use as long as the total memory is below the limit.
530 *
531 * The pool factory interface declared in PJLIB is designed to be extensible.
532 * Application can define its own strategy by creating it's own pool factory
533 * implementation, and this strategy can be used even by existing library
534 * without recompilation.
Benny Prijono9033e312005-11-21 02:08:39 +0000535 *
536 * \section PJ_POOL_FACTORY_ITF Pool Factory Interface
537 * The pool factory defines the following interface:
538 * - \a policy: the memory pool factory policy.
539 * - \a create_pool(): create a new memory pool.
540 * - \a release_pool(): release memory pool back to factory.
541 *
542 * \section PJ_POOL_FACTORY_POL Pool Factory Policy.
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000543 *
544 * A pool factory only defines functions to create and release pool and how
545 * to manage pools, but the rest of the functionalities are controlled by
546 * policy. A pool policy defines:
547 * - how memory block is allocated and deallocated (the default implementation
548 * allocates and deallocate memory by calling malloc() and free()).
549 * - callback to be called when memory allocation inside a pool fails (the
550 * default implementation will throw PJ_NO_MEMORY_EXCEPTION exception).
551 * - concurrency when creating and releasing pool from/to the factory.
552 *
553 * A pool factory can be given different policy during creation to make
554 * it behave differently. For example, caching pool factory can be configured
555 * to allocate and deallocate from a static/contiguous/preallocated memory
556 * instead of using malloc()/free().
557 *
558 * What strategy/factory and what policy to use is not defined by PJLIB, but
559 * instead is left to application to make use whichever is most efficient for
560 * itself.
561 *
Benny Prijono9033e312005-11-21 02:08:39 +0000562 * The pool factory policy controls the behaviour of memory factories, and
563 * defines the following interface:
564 * - \a block_alloc(): allocate memory block from backend memory mgmt/system.
565 * - \a block_free(): free memory block back to backend memory mgmt/system.
566 * @{
567 */
568
569/* We unfortunately don't have support for factory policy options as now,
570 so we keep this commented at the moment.
571enum PJ_POOL_FACTORY_OPTION
572{
573 PJ_POOL_FACTORY_SERIALIZE = 1
574};
575*/
576
577/**
578 * This structure declares pool factory interface.
579 */
580typedef struct pj_pool_factory_policy
581{
582 /**
583 * Allocate memory block (for use by pool). This function is called
584 * by memory pool to allocate memory block.
585 *
586 * @param factory Pool factory.
587 * @param size The size of memory block to allocate.
588 *
589 * @return Memory block.
590 */
591 void* (*block_alloc)(pj_pool_factory *factory, pj_size_t size);
592
593 /**
594 * Free memory block.
595 *
596 * @param factory Pool factory.
597 * @param mem Memory block previously allocated by block_alloc().
598 * @param size The size of memory block.
599 */
600 void (*block_free)(pj_pool_factory *factory, void *mem, pj_size_t size);
601
602 /**
603 * Default callback to be called when memory allocation fails.
604 */
605 pj_pool_callback *callback;
606
607 /**
608 * Option flags.
609 */
610 unsigned flags;
611
612} pj_pool_factory_policy;
613
614/**
615 * This constant denotes the exception number that will be thrown by default
616 * memory factory policy when memory allocation fails.
617 */
618extern int PJ_NO_MEMORY_EXCEPTION;
619
620/**
621 * This global variable points to default memory pool factory policy.
622 * The behaviour of the default policy is:
623 * - block allocation and deallocation use malloc() and free().
624 * - callback will raise PJ_NO_MEMORY_EXCEPTION exception.
625 * - access to pool factory is not serialized (i.e. not thread safe).
Benny Prijono8ab968f2007-07-20 08:08:30 +0000626 *
627 * @see pj_pool_factory_get_default_policy
Benny Prijono9033e312005-11-21 02:08:39 +0000628 */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000629PJ_DECL_DATA(pj_pool_factory_policy) pj_pool_factory_default_policy;
630
631
632/**
633 * Get the default pool factory policy.
634 *
635 * @return the pool policy.
636 */
637PJ_DECL(const pj_pool_factory_policy*) pj_pool_factory_get_default_policy(void);
638
Benny Prijono9033e312005-11-21 02:08:39 +0000639
640/**
641 * This structure contains the declaration for pool factory interface.
642 */
643struct pj_pool_factory
644{
645 /**
646 * Memory pool policy.
647 */
648 pj_pool_factory_policy policy;
649
650 /**
651 * Create a new pool from the pool factory.
652 *
653 * @param factory The pool factory.
654 * @param name the name to be assigned to the pool. The name should
655 * not be longer than PJ_MAX_OBJ_NAME (32 chars), or
656 * otherwise it will be truncated.
657 * @param initial_size the size of initial memory blocks taken by the pool.
658 * Note that the pool will take 68+20 bytes for
659 * administrative area from this block.
660 * @param increment_size the size of each additional blocks to be allocated
661 * when the pool is running out of memory. If user
662 * requests memory which is larger than this size, then
663 * an error occurs.
664 * Note that each time a pool allocates additional block,
665 * it needs 20 bytes (equal to sizeof(pj_pool_block)) to
666 * store some administrative info.
667 * @param callback Cllback to be called when error occurs in the pool.
668 * Note that when an error occurs during pool creation,
669 * the callback itself is not called. Instead, NULL
670 * will be returned.
671 *
672 * @return the memory pool, or NULL.
673 */
674 pj_pool_t* (*create_pool)( pj_pool_factory *factory,
675 const char *name,
676 pj_size_t initial_size,
677 pj_size_t increment_size,
678 pj_pool_callback *callback);
679
680 /**
681 * Release the pool to the pool factory.
682 *
683 * @param factory The pool factory.
684 * @param pool The pool to be released.
685 */
686 void (*release_pool)( pj_pool_factory *factory, pj_pool_t *pool );
687
688 /**
689 * Dump pool status to log.
690 *
691 * @param factory The pool factory.
692 */
693 void (*dump_status)( pj_pool_factory *factory, pj_bool_t detail );
Benny Prijonoc6e165f2006-07-09 10:05:46 +0000694
695 /**
696 * This is optional callback to be called by allocation policy when
697 * it allocates a new memory block. The factory may use this callback
698 * for example to keep track of the total number of memory blocks
699 * currently allocated by applications.
700 *
701 * @param factory The pool factory.
702 * @param size Size requested by application.
703 *
704 * @return MUST return PJ_TRUE, otherwise the block
705 * allocation is cancelled.
706 */
707 pj_bool_t (*on_block_alloc)(pj_pool_factory *factory, pj_size_t size);
708
709 /**
710 * This is optional callback to be called by allocation policy when
711 * it frees memory block. The factory may use this callback
712 * for example to keep track of the total number of memory blocks
713 * currently allocated by applications.
714 *
715 * @param factory The pool factory.
716 * @param size Size freed.
717 */
718 void (*on_block_free)(pj_pool_factory *factory, pj_size_t size);
719
Benny Prijono9033e312005-11-21 02:08:39 +0000720};
721
722/**
723 * This function is intended to be used by pool factory implementors.
724 * @param factory Pool factory.
725 * @param name Pool name.
726 * @param initial_size Initial size.
727 * @param increment_size Increment size.
728 * @param callback Callback.
729 * @return The pool object, or NULL.
730 */
731PJ_DECL(pj_pool_t*) pj_pool_create_int( pj_pool_factory *factory,
732 const char *name,
733 pj_size_t initial_size,
734 pj_size_t increment_size,
735 pj_pool_callback *callback);
736
737/**
738 * This function is intended to be used by pool factory implementors.
739 * @param pool The pool.
740 * @param name Pool name.
741 * @param increment_size Increment size.
742 * @param callback Callback function.
743 */
744PJ_DECL(void) pj_pool_init_int( pj_pool_t *pool,
745 const char *name,
746 pj_size_t increment_size,
747 pj_pool_callback *callback);
748
749/**
750 * This function is intended to be used by pool factory implementors.
751 * @param pool The memory pool.
752 */
753PJ_DECL(void) pj_pool_destroy_int( pj_pool_t *pool );
754
755
756/**
Benny Prijono8508aa02006-03-30 15:56:01 +0000757 * Dump pool factory state.
758 * @param pf The pool factory.
759 * @param detail Detail state required.
760 */
761PJ_INLINE(void) pj_pool_factory_dump( pj_pool_factory *pf,
762 pj_bool_t detail )
763{
764 (*pf->dump_status)(pf, detail);
765}
766
767/**
Benny Prijono9033e312005-11-21 02:08:39 +0000768 * @} // PJ_POOL_FACTORY
769 */
770
Benny Prijono92ac4472006-07-22 13:42:56 +0000771/* **************************************************************************/
Benny Prijono9033e312005-11-21 02:08:39 +0000772
773/**
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000774 * @defgroup PJ_CACHING_POOL Caching Pool Factory
Benny Prijono9033e312005-11-21 02:08:39 +0000775 * @ingroup PJ_POOL_GROUP
776 * @brief
777 * Caching pool is one sample implementation of pool factory where the
778 * factory can reuse memory to create a pool. Application defines what the
779 * maximum memory the factory can hold, and when a pool is released the
780 * factory decides whether to destroy the pool or to keep it for future use.
781 * If the total amount of memory in the internal cache is still within the
782 * limit, the factory will keep the pool in the internal cache, otherwise the
783 * pool will be destroyed, thus releasing the memory back to the system.
784 *
785 * @{
786 */
787
788/**
789 * Number of unique sizes, to be used as index to the free list.
790 * Each pool in the free list is organized by it's size.
791 */
792#define PJ_CACHING_POOL_ARRAY_SIZE 16
793
794/**
795 * Declaration for caching pool. Application doesn't normally need to
796 * care about the contents of this struct, it is only provided here because
797 * application need to define an instance of this struct (we can not allocate
798 * the struct from a pool since there is no pool factory yet!).
799 */
800struct pj_caching_pool
801{
802 /** Pool factory interface, must be declared first. */
803 pj_pool_factory factory;
804
805 /** Current factory's capacity, i.e. number of bytes that are allocated
806 * and available for application in this factory. The factory's
807 * capacity represents the size of all pools kept by this factory
808 * in it's free list, which will be returned to application when it
809 * requests to create a new pool.
810 */
811 pj_size_t capacity;
812
813 /** Maximum size that can be held by this factory. Once the capacity
814 * has exceeded @a max_capacity, further #pj_pool_release() will
815 * flush the pool. If the capacity is still below the @a max_capacity,
816 * #pj_pool_release() will save the pool to the factory's free list.
817 */
818 pj_size_t max_capacity;
819
820 /**
821 * Number of pools currently held by applications. This number gets
822 * incremented everytime #pj_pool_create() is called, and gets
823 * decremented when #pj_pool_release() is called.
824 */
825 pj_size_t used_count;
826
827 /**
Benny Prijonoc6e165f2006-07-09 10:05:46 +0000828 * Total size of memory currently used by application.
829 */
830 pj_size_t used_size;
831
832 /**
833 * The maximum size of memory used by application throughout the life
834 * of the caching pool.
835 */
836 pj_size_t peak_used_size;
837
838 /**
Benny Prijono9033e312005-11-21 02:08:39 +0000839 * Lists of pools in the cache, indexed by pool size.
840 */
841 pj_list free_list[PJ_CACHING_POOL_ARRAY_SIZE];
842
843 /**
844 * List of pools currently allocated by applications.
845 */
846 pj_list used_list;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000847
848 /**
849 * Internal pool.
850 */
Benny Prijono472f2f52007-01-09 15:02:00 +0000851 char pool_buf[256 * (sizeof(long) / 4)];
Benny Prijonoc8322f32006-02-26 21:18:42 +0000852
853 /**
854 * Mutex.
855 */
Benny Prijono31333b62006-11-21 08:36:12 +0000856 pj_lock_t *lock;
Benny Prijono9033e312005-11-21 02:08:39 +0000857};
858
859
860
861/**
862 * Initialize caching pool.
863 *
864 * @param ch_pool The caching pool factory to be initialized.
865 * @param policy Pool factory policy.
866 * @param max_capacity The total capacity to be retained in the cache. When
867 * the pool is returned to the cache, it will be kept in
868 * recycling list if the total capacity of pools in this
869 * list plus the capacity of the pool is still below this
870 * value.
871 */
872PJ_DECL(void) pj_caching_pool_init( pj_caching_pool *ch_pool,
873 const pj_pool_factory_policy *policy,
874 pj_size_t max_capacity);
875
876
877/**
878 * Destroy caching pool, and release all the pools in the recycling list.
879 *
880 * @param ch_pool The caching pool.
881 */
882PJ_DECL(void) pj_caching_pool_destroy( pj_caching_pool *ch_pool );
883
884/**
885 * @} // PJ_CACHING_POOL
886 */
887
888# if PJ_FUNCTIONS_ARE_INLINED
889# include "pool_i.h"
890# endif
891
892PJ_END_DECL
893
894#endif /* __PJ_POOL_H__ */
895