blob: 270ae71ee471b9ca5249681b85a08b5e2bdc9a1d [file] [log] [blame]
Benny Prijonodd859a62005-11-01 16:42:51 +00001/* $Header: /pjproject-0.3/pjlib/include/pj/pool.h 10 10/14/05 12:26a Bennylp $ */
2
3#ifndef __PJ_POOL_H__
4#define __PJ_POOL_H__
5
6/**
7 * @file pool.h
8 * @brief Memory Pool.
9 */
10
11#include <pj/list.h>
12
13PJ_BEGIN_DECL
14
15/**
16 * @defgroup PJ_POOL_GROUP Memory Pool Management
17 * @ingroup PJ
18 * @brief
19 * Memory pool management provides API to allocate and deallocate memory from
20 * memory pool and to manage and establish policy for pool creation and
21 * destruction in pool factory.
22 *
23 * \section PJ_POOL_FACTORY_SEC Pool Factory
24 * See: \ref PJ_POOL_FACTORY "Pool Factory"
25 *
26 * A memory pool must be created through a factory. A factory not only provides
27 * generic interface functions to create and release pool, but also provides
28 * strategy to manage the life time of pools. One sample implementation,
29 * \a pj_caching_pool, can be set to keep the pools released by application for
30 * future use as long as the total memory is below the limit.
31 *
32 * The pool factory interface declared in PJLIB is designed to be extensible.
33 * Application can define its own strategy by creating it's own pool factory
34 * implementation, and this strategy can be used even by existing library
35 * without recompilation.
36 *
37 *
38 * \section PJ_POOL_POLICY_SEC Pool Factory Policy
39 * See: \ref PJ_POOL_FACTORY "Pool Factory Policy"
40 *
41 * A pool factory only defines functions to create and release pool and how
42 * to manage pools, but the rest of the functionalities are controlled by
43 * policy. A pool policy defines:
44 * - how memory block is allocated and deallocated (the default implementation
45 * allocates and deallocate memory by calling malloc() and free()).
46 * - callback to be called when memory allocation inside a pool fails (the
47 * default implementation will throw PJ_NO_MEMORY_EXCEPTION exception).
48 * - concurrency when creating and releasing pool from/to the factory.
49 *
50 * A pool factory can be given different policy during creation to make
51 * it behave differently. For example, caching pool factory can be configured
52 * to allocate and deallocate from a static/contiguous/preallocated memory
53 * instead of using malloc()/free().
54 *
55 * What strategy/factory and what policy to use is not defined by PJLIB, but
56 * instead is left to application to make use whichever is most efficient for
57 * itself.
58 *
59 *
60 * \section PJ_POOL_POOL_SEC The Pool
61 * See: \ref PJ_POOL "Pool"
62 *
63 * The memory pool is an opaque object created by pool factory.
64 * Application uses this object to request a memory chunk, by calling
65 * #pj_pool_alloc or #pj_pool_calloc. When the application has finished using
66 * the pool, it must call #pj_pool_release to free all the chunks previously
67 * allocated and release the pool back to the factory.
68 *
69 * \section PJ_POOL_THREADING_SEC More on Threading Policies:
70 * - By design, memory allocation from a pool is not thread safe. We assumed
71 * that a pool will be owned by an object, and thread safety should be
72 * handled by that object. Thus these functions are not thread safe:
73 * - #pj_pool_alloc,
74 * - #pj_pool_calloc,
75 * - and other pool statistic functions.
76 * - Threading in the pool factory is decided by the policy set for the
77 * factory when it was created.
78 *
79 * \section PJ_POOL_EXAMPLES_SEC Examples
80 *
81 * For some sample codes on how to use the pool, please see:
82 * - @ref page_pjlib_pool_test
83 */
84
85/**
86 * @defgroup PJ_POOL Memory Pool.
87 * @ingroup PJ_POOL_GROUP
88 * @brief
89 * A memory pool is initialized with an initial amount of memory, which is
90 * called a block. Pool can be configured to dynamically allocate more memory
91 * blocks when it runs out of memory. Subsequent memory allocations by user
92 * will use up portions of these block.
93 * The pool doesn't keep track of individual memory allocations
94 * by user, and the user doesn't have to free these indidual allocations. This
95 * makes memory allocation simple and very fast. All the memory allocated from
96 * the pool will be destroyed when the pool itself is destroyed.
97 * @{
98 */
99
100/**
101 * The type for function to receive callback from the pool when it is unable
102 * to allocate memory. The elegant way to handle this condition is to throw
103 * exception, and this is what is expected by most of this library
104 * components.
105 */
106typedef void pj_pool_callback(pj_pool_t *pool, pj_size_t size);
107
108/**
109 * This class, which is used internally by the pool, describes a single
110 * block of memory from which user memory allocations will be allocated from.
111 */
112typedef struct pj_pool_block
113{
114 PJ_DECL_LIST_MEMBER(struct pj_pool_block) /**< List's prev and next. */
115 unsigned char *buf; /**< Start of buffer. */
116 unsigned char *cur; /**< Current alloc ptr. */
117 unsigned char *end; /**< End of buffer. */
118} pj_pool_block;
119
120
121/**
122 * This structure describes the memory pool. Only implementors of pool factory
123 * need to care about the contents of this structure.
124 */
125struct pj_pool_t
126{
127 PJ_DECL_LIST_MEMBER(struct pj_pool_t)
128
129 /** Pool name */
130 char obj_name[PJ_MAX_OBJ_NAME];
131
132 /** Pool factory. */
133 pj_pool_factory *factory;
134
135 /** Current capacity allocated by the pool. */
136 pj_size_t capacity;
137
138 /** Number of memory used/allocated. */
139 pj_size_t used_size;
140
141 /** Size of memory block to be allocated when the pool runs out of memory */
142 pj_size_t increment_size;
143
144 /** List of memory blocks allcoated by the pool. */
145 pj_pool_block block_list;
146
147 /** The callback to be called when the pool is unable to allocate memory. */
148 pj_pool_callback *callback;
149
150};
151
152
153/**
154 * Guidance on how much memory required for initial pool administrative data.
155 */
156#define PJ_POOL_SIZE (sizeof(struct pj_pool_t))
157
158/**
159 * Pool memory alignment (must be power of 2).
160 */
161#ifndef PJ_POOL_ALIGNMENT
162# define PJ_POOL_ALIGNMENT 4
163#endif
164
165/**
166 * Create a new pool from the pool factory. This wrapper will call create_pool
167 * member of the pool factory.
168 *
169 * @param factory The pool factory.
170 * @param name The name to be assigned to the pool. The name should
171 * not be longer than PJ_MAX_OBJ_NAME (32 chars), or
172 * otherwise it will be truncated.
173 * @param initial_size The size of initial memory blocks taken by the pool.
174 * Note that the pool will take 68+20 bytes for
175 * administrative area from this block.
176 * @param increment_size the size of each additional blocks to be allocated
177 * when the pool is running out of memory. If user
178 * requests memory which is larger than this size, then
179 * an error occurs.
180 * Note that each time a pool allocates additional block,
181 * it needs PJ_POOL_SIZE more to store some
182 * administrative info.
183 * @param callback Callback to be called when error occurs in the pool.
184 * If this value is NULL, then the callback from pool
185 * factory policy will be used.
186 * Note that when an error occurs during pool creation,
187 * the callback itself is not called. Instead, NULL
188 * will be returned.
189 *
190 * @return The memory pool, or NULL.
191 */
192PJ_IDECL(pj_pool_t*) pj_pool_create(pj_pool_factory *factory,
193 const char *name,
194 pj_size_t initial_size,
195 pj_size_t increment_size,
196 pj_pool_callback *callback);
197
198/**
199 * Release the pool back to pool factory.
200 *
201 * @param pool Memory pool.
202 */
203PJ_IDECL(void) pj_pool_release( pj_pool_t *pool );
204
205/**
206 * Get pool object name.
207 *
208 * @param pool the pool.
209 *
210 * @return pool name as NULL terminated string.
211 */
212PJ_IDECL(const char *) pj_pool_getobjname( const pj_pool_t *pool );
213
214/**
215 * Reset the pool to its state when it was initialized.
216 * This means that if additional blocks have been allocated during runtime,
217 * then they will be freed. Only the original block allocated during
218 * initialization is retained. This function will also reset the internal
219 * counters, such as pool capacity and used size.
220 *
221 * @param pool the pool.
222 */
223PJ_DECL(void) pj_pool_reset( pj_pool_t *pool );
224
225
226/**
227 * Get the pool capacity, that is, the system storage that have been allocated
228 * by the pool, and have been used/will be used to allocate user requests.
229 * There's no guarantee that the returned value represent a single
230 * contiguous block, because the capacity may be spread in several blocks.
231 *
232 * @param pool the pool.
233 *
234 * @return the capacity.
235 */
236PJ_IDECL(pj_size_t) pj_pool_get_capacity( pj_pool_t *pool );
237
238/**
239 * Get the total size of user allocation request.
240 *
241 * @param pool the pool.
242 *
243 * @return the total size.
244 */
245PJ_IDECL(pj_size_t) pj_pool_get_used_size( pj_pool_t *pool );
246
247/**
248 * Allocate storage with the specified size from the pool.
249 * If there's no storage available in the pool, then the pool can allocate more
250 * blocks if the increment size is larger than the requested size.
251 *
252 * @param pool the pool.
253 * @param size the requested size.
254 *
255 * @return pointer to the allocated memory.
256 */
257PJ_IDECL(void*) pj_pool_alloc( pj_pool_t *pool, pj_size_t size);
258
259/**
260 * Allocate storage from the pool, and initialize it to zero.
261 * This function behaves like pj_pool_alloc(), except that the storage will
262 * be initialized to zero.
263 *
264 * @param pool the pool.
265 * @param count the number of elements in the array.
266 * @param elem the size of individual element.
267 *
268 * @return pointer to the allocated memory.
269 */
270PJ_IDECL(void*) pj_pool_calloc( pj_pool_t *pool, pj_size_t count,
271 pj_size_t elem);
272
273
274/**
275 * @def pj_pool_zalloc(pj_pool_t *pool, pj_size_t size)
276 * Allocate storage from the pool and initialize it to zero.
277 *
278 * @param pool The pool.
279 * @param size The size to be allocated.
280 *
281 * @return Pointer to the allocated memory.
282 */
283#define pj_pool_zalloc(pool, size) pj_pool_calloc(pool, 1, size)
284
285
286/**
287 * @} // PJ_POOL
288 */
289
290///////////////////////////////////////////////////////////////////////////////
291/**
292 * @defgroup PJ_POOL_FACTORY Pool Factory and Policy.
293 * @ingroup PJ_POOL_GROUP
294 * @brief
295 * Pool factory declares an interface to create and destroy pool. There may
296 * be several strategies for pool creation, and these strategies should
297 * implement the interface defined by pool factory.
298 *
299 * \section PJ_POOL_FACTORY_ITF Pool Factory Interface
300 * The pool factory defines the following interface:
301 * - \a policy: the memory pool factory policy.
302 * - \a create_pool(): create a new memory pool.
303 * - \a release_pool(): release memory pool back to factory.
304 *
305 * \section PJ_POOL_FACTORY_POL Pool Factory Policy.
306 * The pool factory policy controls the behaviour of memory factories, and
307 * defines the following interface:
308 * - \a block_alloc(): allocate memory block from backend memory mgmt/system.
309 * - \a block_free(): free memory block back to backend memory mgmt/system.
310 * @{
311 */
312
313/* We unfortunately don't have support for factory policy options as now,
314 so we keep this commented at the moment.
315enum PJ_POOL_FACTORY_OPTION
316{
317 PJ_POOL_FACTORY_SERIALIZE = 1
318};
319*/
320
321/**
322 * This structure declares pool factory interface.
323 */
324typedef struct pj_pool_factory_policy
325{
326 /**
327 * Allocate memory block (for use by pool). This function is called
328 * by memory pool to allocate memory block.
329 *
330 * @param factory Pool factory.
331 * @param size The size of memory block to allocate.
332 *
333 * @return Memory block.
334 */
335 void* (*block_alloc)(pj_pool_factory *factory, pj_size_t size);
336
337 /**
338 * Free memory block.
339 *
340 * @param factory Pool factory.
341 * @param mem Memory block previously allocated by block_alloc().
342 * @param size The size of memory block.
343 */
344 void (*block_free)(pj_pool_factory *factory, void *mem, pj_size_t size);
345
346 /**
347 * Default callback to be called when memory allocation fails.
348 */
349 pj_pool_callback *callback;
350
351 /**
352 * Option flags.
353 */
354 unsigned flags;
355
356} pj_pool_factory_policy;
357
358/**
359 * This constant denotes the exception number that will be thrown by default
360 * memory factory policy when memory allocation fails.
361 */
362extern int PJ_NO_MEMORY_EXCEPTION;
363
364/**
365 * This global variable points to default memory pool factory policy.
366 * The behaviour of the default policy is:
367 * - block allocation and deallocation use malloc() and free().
368 * - callback will raise PJ_NO_MEMORY_EXCEPTION exception.
369 * - access to pool factory is not serialized (i.e. not thread safe).
370 */
371extern pj_pool_factory_policy pj_pool_factory_default_policy;
372
373/**
374 * This structure contains the declaration for pool factory interface.
375 */
376struct pj_pool_factory
377{
378 /**
379 * Memory pool policy.
380 */
381 pj_pool_factory_policy policy;
382
383 /**
384 * Create a new pool from the pool factory.
385 *
386 * @param factory The pool factory.
387 * @param name the name to be assigned to the pool. The name should
388 * not be longer than PJ_MAX_OBJ_NAME (32 chars), or
389 * otherwise it will be truncated.
390 * @param initial_size the size of initial memory blocks taken by the pool.
391 * Note that the pool will take 68+20 bytes for
392 * administrative area from this block.
393 * @param increment_size the size of each additional blocks to be allocated
394 * when the pool is running out of memory. If user
395 * requests memory which is larger than this size, then
396 * an error occurs.
397 * Note that each time a pool allocates additional block,
398 * it needs 20 bytes (equal to sizeof(pj_pool_block)) to
399 * store some administrative info.
400 * @param callback Cllback to be called when error occurs in the pool.
401 * Note that when an error occurs during pool creation,
402 * the callback itself is not called. Instead, NULL
403 * will be returned.
404 *
405 * @return the memory pool, or NULL.
406 */
407 pj_pool_t* (*create_pool)( pj_pool_factory *factory,
408 const char *name,
409 pj_size_t initial_size,
410 pj_size_t increment_size,
411 pj_pool_callback *callback);
412
413 /**
414 * Release the pool to the pool factory.
415 *
416 * @param factory The pool factory.
417 * @param pool The pool to be released.
418 */
419 void (*release_pool)( pj_pool_factory *factory, pj_pool_t *pool );
420
421 /**
422 * Dump pool status to log.
423 *
424 * @param factory The pool factory.
425 */
426 void (*dump_status)( pj_pool_factory *factory, pj_bool_t detail );
427};
428
429/**
430 * This function is intended to be used by pool factory implementors.
431 * @param factory Pool factory.
432 * @param name Pool name.
433 * @param initial_size Initial size.
434 * @param increment_size Increment size.
435 * @param callback Callback.
436 * @return The pool object, or NULL.
437 */
438PJ_DECL(pj_pool_t*) pj_pool_create_int( pj_pool_factory *factory,
439 const char *name,
440 pj_size_t initial_size,
441 pj_size_t increment_size,
442 pj_pool_callback *callback);
443
444/**
445 * This function is intended to be used by pool factory implementors.
446 * @param pool The pool.
447 * @param name Pool name.
448 * @param increment_size Increment size.
449 * @param callback Callback function.
450 */
451PJ_DECL(void) pj_pool_init_int( pj_pool_t *pool,
452 const char *name,
453 pj_size_t increment_size,
454 pj_pool_callback *callback);
455
456/**
457 * This function is intended to be used by pool factory implementors.
458 * @param pool The memory pool.
459 */
460PJ_DECL(void) pj_pool_destroy_int( pj_pool_t *pool );
461
462
463/**
464 * @} // PJ_POOL_FACTORY
465 */
466
467///////////////////////////////////////////////////////////////////////////////
468
469/**
470 * @defgroup PJ_CACHING_POOL Caching Pool Factory.
471 * @ingroup PJ_POOL_GROUP
472 * @brief
473 * Caching pool is one sample implementation of pool factory where the
474 * factory can reuse memory to create a pool. Application defines what the
475 * maximum memory the factory can hold, and when a pool is released the
476 * factory decides whether to destroy the pool or to keep it for future use.
477 * If the total amount of memory in the internal cache is still within the
478 * limit, the factory will keep the pool in the internal cache, otherwise the
479 * pool will be destroyed, thus releasing the memory back to the system.
480 *
481 * @{
482 */
483
484/**
485 * Number of unique sizes, to be used as index to the free list.
486 * Each pool in the free list is organized by it's size.
487 */
488#define PJ_CACHING_POOL_ARRAY_SIZE 16
489
490/**
491 * Declaration for caching pool. Application doesn't normally need to
492 * care about the contents of this struct, it is only provided here because
493 * application need to define an instance of this struct (we can not allocate
494 * the struct from a pool since there is no pool factory yet!).
495 */
496struct pj_caching_pool
497{
498 /** Pool factory interface, must be declared first. */
499 pj_pool_factory factory;
500
501 /** Current factory's capacity, i.e. number of bytes that are allocated
502 * and available for application in this factory. The factory's
503 * capacity represents the size of all pools kept by this factory
504 * in it's free list, which will be returned to application when it
505 * requests to create a new pool.
506 */
507 pj_size_t capacity;
508
509 /** Maximum size that can be held by this factory. Once the capacity
510 * has exceeded @a max_capacity, further #pj_pool_release() will
511 * flush the pool. If the capacity is still below the @a max_capacity,
512 * #pj_pool_release() will save the pool to the factory's free list.
513 */
514 pj_size_t max_capacity;
515
516 /**
517 * Number of pools currently held by applications. This number gets
518 * incremented everytime #pj_pool_create() is called, and gets
519 * decremented when #pj_pool_release() is called.
520 */
521 pj_size_t used_count;
522
523 /**
524 * Lists of pools in the cache, indexed by pool size.
525 */
526 pj_list free_list[PJ_CACHING_POOL_ARRAY_SIZE];
527
528 /**
529 * List of pools currently allocated by applications.
530 */
531 pj_list used_list;
532};
533
534
535
536/**
537 * Initialize caching pool.
538 *
539 * @param ch_pool The caching pool factory to be initialized.
540 * @param policy Pool factory policy.
541 * @param max_capacity The total capacity to be retained in the cache. When
542 * the pool is returned to the cache, it will be kept in
543 * recycling list if the total capacity of pools in this
544 * list plus the capacity of the pool is still below this
545 * value.
546 */
547PJ_DECL(void) pj_caching_pool_init( pj_caching_pool *ch_pool,
548 const pj_pool_factory_policy *policy,
549 pj_size_t max_capacity);
550
551
552/**
553 * Destroy caching pool, and release all the pools in the recycling list.
554 *
555 * @param ch_pool The caching pool.
556 */
557PJ_DECL(void) pj_caching_pool_destroy( pj_caching_pool *ch_pool );
558
559/**
560 * @} // PJ_CACHING_POOL
561 */
562
563# if PJ_FUNCTIONS_ARE_INLINED
564# include "pool_i.h"
565# endif
566
567PJ_END_DECL
568
569#endif /* __PJ_POOL_H__ */
570