blob: 06cdd0377d6209a4ab77bc831d0f682abf875369 [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
230 // Create the pool factory, in this case, a caching pool.
231 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy,
232 1024*1024 );
233
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 Prijono9033e312005-11-21 02:08:39 +0000508
509
510/**
511 * @} // PJ_POOL
512 */
513
Benny Prijono92ac4472006-07-22 13:42:56 +0000514/* **************************************************************************/
Benny Prijono9033e312005-11-21 02:08:39 +0000515/**
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000516 * @defgroup PJ_POOL_FACTORY Pool Factory and Policy
Benny Prijono9033e312005-11-21 02:08:39 +0000517 * @ingroup PJ_POOL_GROUP
518 * @brief
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000519 * A pool object must be created through a factory. A factory not only provides
520 * generic interface functions to create and release pool, but also provides
521 * strategy to manage the life time of pools. One sample implementation,
522 * \a pj_caching_pool, can be set to keep the pools released by application for
523 * future use as long as the total memory is below the limit.
524 *
525 * The pool factory interface declared in PJLIB is designed to be extensible.
526 * Application can define its own strategy by creating it's own pool factory
527 * implementation, and this strategy can be used even by existing library
528 * without recompilation.
Benny Prijono9033e312005-11-21 02:08:39 +0000529 *
530 * \section PJ_POOL_FACTORY_ITF Pool Factory Interface
531 * The pool factory defines the following interface:
532 * - \a policy: the memory pool factory policy.
533 * - \a create_pool(): create a new memory pool.
534 * - \a release_pool(): release memory pool back to factory.
535 *
536 * \section PJ_POOL_FACTORY_POL Pool Factory Policy.
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000537 *
538 * A pool factory only defines functions to create and release pool and how
539 * to manage pools, but the rest of the functionalities are controlled by
540 * policy. A pool policy defines:
541 * - how memory block is allocated and deallocated (the default implementation
542 * allocates and deallocate memory by calling malloc() and free()).
543 * - callback to be called when memory allocation inside a pool fails (the
544 * default implementation will throw PJ_NO_MEMORY_EXCEPTION exception).
545 * - concurrency when creating and releasing pool from/to the factory.
546 *
547 * A pool factory can be given different policy during creation to make
548 * it behave differently. For example, caching pool factory can be configured
549 * to allocate and deallocate from a static/contiguous/preallocated memory
550 * instead of using malloc()/free().
551 *
552 * What strategy/factory and what policy to use is not defined by PJLIB, but
553 * instead is left to application to make use whichever is most efficient for
554 * itself.
555 *
Benny Prijono9033e312005-11-21 02:08:39 +0000556 * The pool factory policy controls the behaviour of memory factories, and
557 * defines the following interface:
558 * - \a block_alloc(): allocate memory block from backend memory mgmt/system.
559 * - \a block_free(): free memory block back to backend memory mgmt/system.
560 * @{
561 */
562
563/* We unfortunately don't have support for factory policy options as now,
564 so we keep this commented at the moment.
565enum PJ_POOL_FACTORY_OPTION
566{
567 PJ_POOL_FACTORY_SERIALIZE = 1
568};
569*/
570
571/**
572 * This structure declares pool factory interface.
573 */
574typedef struct pj_pool_factory_policy
575{
576 /**
577 * Allocate memory block (for use by pool). This function is called
578 * by memory pool to allocate memory block.
579 *
580 * @param factory Pool factory.
581 * @param size The size of memory block to allocate.
582 *
583 * @return Memory block.
584 */
585 void* (*block_alloc)(pj_pool_factory *factory, pj_size_t size);
586
587 /**
588 * Free memory block.
589 *
590 * @param factory Pool factory.
591 * @param mem Memory block previously allocated by block_alloc().
592 * @param size The size of memory block.
593 */
594 void (*block_free)(pj_pool_factory *factory, void *mem, pj_size_t size);
595
596 /**
597 * Default callback to be called when memory allocation fails.
598 */
599 pj_pool_callback *callback;
600
601 /**
602 * Option flags.
603 */
604 unsigned flags;
605
606} pj_pool_factory_policy;
607
608/**
609 * This constant denotes the exception number that will be thrown by default
610 * memory factory policy when memory allocation fails.
611 */
612extern int PJ_NO_MEMORY_EXCEPTION;
613
614/**
615 * This global variable points to default memory pool factory policy.
616 * The behaviour of the default policy is:
617 * - block allocation and deallocation use malloc() and free().
618 * - callback will raise PJ_NO_MEMORY_EXCEPTION exception.
619 * - access to pool factory is not serialized (i.e. not thread safe).
620 */
621extern pj_pool_factory_policy pj_pool_factory_default_policy;
622
623/**
624 * This structure contains the declaration for pool factory interface.
625 */
626struct pj_pool_factory
627{
628 /**
629 * Memory pool policy.
630 */
631 pj_pool_factory_policy policy;
632
633 /**
634 * Create a new pool from the pool factory.
635 *
636 * @param factory The pool factory.
637 * @param name the name to be assigned to the pool. The name should
638 * not be longer than PJ_MAX_OBJ_NAME (32 chars), or
639 * otherwise it will be truncated.
640 * @param initial_size the size of initial memory blocks taken by the pool.
641 * Note that the pool will take 68+20 bytes for
642 * administrative area from this block.
643 * @param increment_size the size of each additional blocks to be allocated
644 * when the pool is running out of memory. If user
645 * requests memory which is larger than this size, then
646 * an error occurs.
647 * Note that each time a pool allocates additional block,
648 * it needs 20 bytes (equal to sizeof(pj_pool_block)) to
649 * store some administrative info.
650 * @param callback Cllback to be called when error occurs in the pool.
651 * Note that when an error occurs during pool creation,
652 * the callback itself is not called. Instead, NULL
653 * will be returned.
654 *
655 * @return the memory pool, or NULL.
656 */
657 pj_pool_t* (*create_pool)( pj_pool_factory *factory,
658 const char *name,
659 pj_size_t initial_size,
660 pj_size_t increment_size,
661 pj_pool_callback *callback);
662
663 /**
664 * Release the pool to the pool factory.
665 *
666 * @param factory The pool factory.
667 * @param pool The pool to be released.
668 */
669 void (*release_pool)( pj_pool_factory *factory, pj_pool_t *pool );
670
671 /**
672 * Dump pool status to log.
673 *
674 * @param factory The pool factory.
675 */
676 void (*dump_status)( pj_pool_factory *factory, pj_bool_t detail );
Benny Prijonoc6e165f2006-07-09 10:05:46 +0000677
678 /**
679 * This is optional callback to be called by allocation policy when
680 * it allocates a new memory block. The factory may use this callback
681 * for example to keep track of the total number of memory blocks
682 * currently allocated by applications.
683 *
684 * @param factory The pool factory.
685 * @param size Size requested by application.
686 *
687 * @return MUST return PJ_TRUE, otherwise the block
688 * allocation is cancelled.
689 */
690 pj_bool_t (*on_block_alloc)(pj_pool_factory *factory, pj_size_t size);
691
692 /**
693 * This is optional callback to be called by allocation policy when
694 * it frees memory block. The factory may use this callback
695 * for example to keep track of the total number of memory blocks
696 * currently allocated by applications.
697 *
698 * @param factory The pool factory.
699 * @param size Size freed.
700 */
701 void (*on_block_free)(pj_pool_factory *factory, pj_size_t size);
702
Benny Prijono9033e312005-11-21 02:08:39 +0000703};
704
705/**
706 * This function is intended to be used by pool factory implementors.
707 * @param factory Pool factory.
708 * @param name Pool name.
709 * @param initial_size Initial size.
710 * @param increment_size Increment size.
711 * @param callback Callback.
712 * @return The pool object, or NULL.
713 */
714PJ_DECL(pj_pool_t*) pj_pool_create_int( pj_pool_factory *factory,
715 const char *name,
716 pj_size_t initial_size,
717 pj_size_t increment_size,
718 pj_pool_callback *callback);
719
720/**
721 * This function is intended to be used by pool factory implementors.
722 * @param pool The pool.
723 * @param name Pool name.
724 * @param increment_size Increment size.
725 * @param callback Callback function.
726 */
727PJ_DECL(void) pj_pool_init_int( pj_pool_t *pool,
728 const char *name,
729 pj_size_t increment_size,
730 pj_pool_callback *callback);
731
732/**
733 * This function is intended to be used by pool factory implementors.
734 * @param pool The memory pool.
735 */
736PJ_DECL(void) pj_pool_destroy_int( pj_pool_t *pool );
737
738
739/**
Benny Prijono8508aa02006-03-30 15:56:01 +0000740 * Dump pool factory state.
741 * @param pf The pool factory.
742 * @param detail Detail state required.
743 */
744PJ_INLINE(void) pj_pool_factory_dump( pj_pool_factory *pf,
745 pj_bool_t detail )
746{
747 (*pf->dump_status)(pf, detail);
748}
749
750/**
Benny Prijono9033e312005-11-21 02:08:39 +0000751 * @} // PJ_POOL_FACTORY
752 */
753
Benny Prijono92ac4472006-07-22 13:42:56 +0000754/* **************************************************************************/
Benny Prijono9033e312005-11-21 02:08:39 +0000755
756/**
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +0000757 * @defgroup PJ_CACHING_POOL Caching Pool Factory
Benny Prijono9033e312005-11-21 02:08:39 +0000758 * @ingroup PJ_POOL_GROUP
759 * @brief
760 * Caching pool is one sample implementation of pool factory where the
761 * factory can reuse memory to create a pool. Application defines what the
762 * maximum memory the factory can hold, and when a pool is released the
763 * factory decides whether to destroy the pool or to keep it for future use.
764 * If the total amount of memory in the internal cache is still within the
765 * limit, the factory will keep the pool in the internal cache, otherwise the
766 * pool will be destroyed, thus releasing the memory back to the system.
767 *
768 * @{
769 */
770
771/**
772 * Number of unique sizes, to be used as index to the free list.
773 * Each pool in the free list is organized by it's size.
774 */
775#define PJ_CACHING_POOL_ARRAY_SIZE 16
776
777/**
778 * Declaration for caching pool. Application doesn't normally need to
779 * care about the contents of this struct, it is only provided here because
780 * application need to define an instance of this struct (we can not allocate
781 * the struct from a pool since there is no pool factory yet!).
782 */
783struct pj_caching_pool
784{
785 /** Pool factory interface, must be declared first. */
786 pj_pool_factory factory;
787
788 /** Current factory's capacity, i.e. number of bytes that are allocated
789 * and available for application in this factory. The factory's
790 * capacity represents the size of all pools kept by this factory
791 * in it's free list, which will be returned to application when it
792 * requests to create a new pool.
793 */
794 pj_size_t capacity;
795
796 /** Maximum size that can be held by this factory. Once the capacity
797 * has exceeded @a max_capacity, further #pj_pool_release() will
798 * flush the pool. If the capacity is still below the @a max_capacity,
799 * #pj_pool_release() will save the pool to the factory's free list.
800 */
801 pj_size_t max_capacity;
802
803 /**
804 * Number of pools currently held by applications. This number gets
805 * incremented everytime #pj_pool_create() is called, and gets
806 * decremented when #pj_pool_release() is called.
807 */
808 pj_size_t used_count;
809
810 /**
Benny Prijonoc6e165f2006-07-09 10:05:46 +0000811 * Total size of memory currently used by application.
812 */
813 pj_size_t used_size;
814
815 /**
816 * The maximum size of memory used by application throughout the life
817 * of the caching pool.
818 */
819 pj_size_t peak_used_size;
820
821 /**
Benny Prijono9033e312005-11-21 02:08:39 +0000822 * Lists of pools in the cache, indexed by pool size.
823 */
824 pj_list free_list[PJ_CACHING_POOL_ARRAY_SIZE];
825
826 /**
827 * List of pools currently allocated by applications.
828 */
829 pj_list used_list;
Benny Prijonoc8322f32006-02-26 21:18:42 +0000830
831 /**
832 * Internal pool.
833 */
Benny Prijono472f2f52007-01-09 15:02:00 +0000834 char pool_buf[256 * (sizeof(long) / 4)];
Benny Prijonoc8322f32006-02-26 21:18:42 +0000835
836 /**
837 * Mutex.
838 */
Benny Prijono31333b62006-11-21 08:36:12 +0000839 pj_lock_t *lock;
Benny Prijono9033e312005-11-21 02:08:39 +0000840};
841
842
843
844/**
845 * Initialize caching pool.
846 *
847 * @param ch_pool The caching pool factory to be initialized.
848 * @param policy Pool factory policy.
849 * @param max_capacity The total capacity to be retained in the cache. When
850 * the pool is returned to the cache, it will be kept in
851 * recycling list if the total capacity of pools in this
852 * list plus the capacity of the pool is still below this
853 * value.
854 */
855PJ_DECL(void) pj_caching_pool_init( pj_caching_pool *ch_pool,
856 const pj_pool_factory_policy *policy,
857 pj_size_t max_capacity);
858
859
860/**
861 * Destroy caching pool, and release all the pools in the recycling list.
862 *
863 * @param ch_pool The caching pool.
864 */
865PJ_DECL(void) pj_caching_pool_destroy( pj_caching_pool *ch_pool );
866
867/**
868 * @} // PJ_CACHING_POOL
869 */
870
871# if PJ_FUNCTIONS_ARE_INLINED
872# include "pool_i.h"
873# endif
874
875PJ_END_DECL
876
877#endif /* __PJ_POOL_H__ */
878