diff options
author | Benny Prijono <bennylp@teluu.com> | 2008-07-12 09:31:34 +0000 |
---|---|---|
committer | Benny Prijono <bennylp@teluu.com> | 2008-07-12 09:31:34 +0000 |
commit | 132cbda37abce5fd5703f696ee8fb42cfd6fd41f (patch) | |
tree | afa85b9470f660e5b0a18d9ec800602b7a58d754 | |
parent | c90da3b928fb3ee1b215abfb291a69bf2b78a3d8 (diff) |
Ticket 559 (minor): Update the pool alternative API (pool_alt.h) with the latest pool API
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@2123 74dad513-b988-da41-8d7b-12977e46ad98
-rw-r--r-- | pjlib/include/pj/pool_alt.h | 69 | ||||
-rw-r--r-- | pjlib/src/pj/pool_buf.c | 5 | ||||
-rw-r--r-- | pjlib/src/pj/pool_dbg.c | 34 |
3 files changed, 86 insertions, 22 deletions
diff --git a/pjlib/include/pj/pool_alt.h b/pjlib/include/pj/pool_alt.h index 9091b854..fb923155 100644 --- a/pjlib/include/pj/pool_alt.h +++ b/pjlib/include/pj/pool_alt.h @@ -22,9 +22,6 @@ #define __PJ_POOL_H__ -typedef struct pj_pool_t pj_pool_t; - - /** * The type for function to receive callback from the pool when it is unable * to allocate memory. The elegant way to handle this condition is to throw @@ -33,6 +30,25 @@ typedef struct pj_pool_t pj_pool_t; */ typedef void pj_pool_callback(pj_pool_t *pool, pj_size_t size); +struct pj_pool_mem +{ + struct pj_pool_mem *next; + + /* data follows immediately */ +}; + + +typedef struct pj_pool_t +{ + struct pj_pool_mem *first_mem; + pj_pool_factory *factory; + char obj_name[32]; + pj_size_t used_size; + pj_pool_callback *cb; +} pj_pool_t; + + + /** * This constant denotes the exception number that will be thrown by default * memory factory policy when memory allocation fails. @@ -105,8 +121,54 @@ PJ_DECL(void*) pj_pool_zalloc_imp(const char *file, int line, pj_pool_t *pool, pj_size_t sz); +#define PJ_POOL_ZALLOC_T(pool,type) \ + ((type*)pj_pool_zalloc(pool, sizeof(type))) +#define PJ_POOL_ALLOC_T(pool,type) \ + ((type*)pj_pool_alloc(pool, sizeof(type))) +#ifndef PJ_POOL_ALIGNMENT +# define PJ_POOL_ALIGNMENT 4 +#endif + +/** + * This structure declares pool factory interface. + */ +typedef struct pj_pool_factory_policy +{ + /** + * Allocate memory block (for use by pool). This function is called + * by memory pool to allocate memory block. + * + * @param factory Pool factory. + * @param size The size of memory block to allocate. + * + * @return Memory block. + */ + void* (*block_alloc)(pj_pool_factory *factory, pj_size_t size); + + /** + * Free memory block. + * + * @param factory Pool factory. + * @param mem Memory block previously allocated by block_alloc(). + * @param size The size of memory block. + */ + void (*block_free)(pj_pool_factory *factory, void *mem, pj_size_t size); + + /** + * Default callback to be called when memory allocation fails. + */ + pj_pool_callback *callback; + + /** + * Option flags. + */ + unsigned flags; + +} pj_pool_factory_policy; + typedef struct pj_pool_factory { + pj_pool_factory_policy policy; int dummy; } pj_pool_factory; @@ -120,6 +182,5 @@ typedef struct pj_caching_pool #define pj_caching_pool_destroy(cp) #define pj_pool_factory_dump(pf, detail) - #endif /* __PJ_POOL_ALT_H__ */ diff --git a/pjlib/src/pj/pool_buf.c b/pjlib/src/pj/pool_buf.c index f3c095d1..b123e061 100644 --- a/pjlib/src/pj/pool_buf.c +++ b/pjlib/src/pj/pool_buf.c @@ -80,6 +80,7 @@ PJ_DEF(pj_pool_t*) pj_pool_create_on_buf(const char *name, void *buf, pj_size_t size) { +#if PJ_HAS_POOL_ALT_API == 0 struct creation_param param; long align_diff; @@ -105,5 +106,9 @@ PJ_DEF(pj_pool_t*) pj_pool_create_on_buf(const char *name, return pj_pool_create_int(&stack_based_factory, name, size, 0, pj_pool_factory_default_policy.callback); +#else + PJ_UNUSED_ARG(buf); + return pj_pool_create(NULL, name, size, size, NULL); +#endif } diff --git a/pjlib/src/pj/pool_dbg.c b/pjlib/src/pj/pool_dbg.c index 71dd5647..1cc5bccd 100644 --- a/pjlib/src/pj/pool_dbg.c +++ b/pjlib/src/pj/pool_dbg.c @@ -19,7 +19,7 @@ #include <pj/pool.h> #include <pj/string.h> -#if PJ_POOL_DEBUG +#if PJ_HAS_POOL_ALT_API #if PJ_HAS_MALLOC_H # include <malloc.h> @@ -41,24 +41,14 @@ //#undef TRACE_ -struct pj_pool_mem -{ - struct pj_pool_mem *next; - /* data follows immediately */ -}; +int PJ_NO_MEMORY_EXCEPTION; -struct pj_pool_t +PJ_DEF(int) pj_NO_MEMORY_EXCEPTION() { - struct pj_pool_mem *first_mem; - pj_size_t used_size; - pj_pool_callback *cb; -}; - - -int PJ_NO_MEMORY_EXCEPTION; - + return PJ_NO_MEMORY_EXCEPTION; +} /* Create pool */ PJ_DEF(pj_pool_t*) pj_pool_create_imp( const char *file, int line, @@ -73,7 +63,6 @@ PJ_DEF(pj_pool_t*) pj_pool_create_imp( const char *file, int line, PJ_UNUSED_ARG(file); PJ_UNUSED_ARG(line); PJ_UNUSED_ARG(factory); - PJ_UNUSED_ARG(name); PJ_UNUSED_ARG(initial_size); PJ_UNUSED_ARG(increment_size); @@ -81,6 +70,14 @@ PJ_DEF(pj_pool_t*) pj_pool_create_imp( const char *file, int line, if (!pool) return NULL; + if (name) { + pj_ansi_strncpy(pool->obj_name, name, sizeof(pool->obj_name)); + pool->obj_name[sizeof(pool->obj_name)-1] = '\0'; + } else { + strcpy(pool->obj_name, "altpool"); + } + + pool->factory = NULL; pool->first_mem = NULL; pool->used_size = 0; pool->cb = callback; @@ -184,8 +181,9 @@ PJ_DEF(void*) pj_pool_calloc_imp( const char *file, int line, PJ_DEF(void*) pj_pool_zalloc_imp( const char *file, int line, pj_pool_t *pool, pj_size_t sz) { - return pj_pool_calloc_imp(file, line, pool, 1, sz); + return pj_pool_calloc_imp(file, line, pool, 1, sz); } -#endif /* PJ_POOL_DEBUG */ + +#endif /* PJ_HAS_POOL_ALT_API */ |