From c3a1f65be9862566512ffef2396327388ce82b16 Mon Sep 17 00:00:00 2001 From: Moises Silva Date: Sun, 15 Nov 2009 01:46:43 +0000 Subject: [PATCH] added memory handlers git-svn-id: http://svn.openzap.org/svn/openzap/branches/sangoma_boost@861 a93c3328-9c30-0410-af19-c9cd2b2d52af --- libs/freetdm/src/hashtable_itr.c | 4 +-- libs/freetdm/src/include/openzap.h | 56 +++++++++++++++++++++++++----- libs/freetdm/src/zap_io.c | 45 ++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 11 deletions(-) diff --git a/libs/freetdm/src/hashtable_itr.c b/libs/freetdm/src/hashtable_itr.c index 8973b08ad0..cb8165dc91 100644 --- a/libs/freetdm/src/hashtable_itr.c +++ b/libs/freetdm/src/hashtable_itr.c @@ -31,6 +31,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "openzap.h" #include "hashtable.h" #include "hashtable_private.h" #include "hashtable_itr.h" @@ -43,8 +44,7 @@ struct hashtable_itr * hashtable_iterator(struct hashtable *h) { unsigned int i, tablelength; - struct hashtable_itr *itr = (struct hashtable_itr *) - malloc(sizeof(struct hashtable_itr)); + struct hashtable_itr *itr = zap_malloc(sizeof(struct hashtable_itr)); if (NULL == itr) return NULL; itr->h = h; itr->e = NULL; diff --git a/libs/freetdm/src/include/openzap.h b/libs/freetdm/src/include/openzap.h index 8f135e173c..bdd97ca591 100644 --- a/libs/freetdm/src/include/openzap.h +++ b/libs/freetdm/src/include/openzap.h @@ -318,15 +318,6 @@ typedef enum { */ #define zap_copy_flags(dest, src, flags) (dest)->flags &= ~(flags); (dest)->flags |= ((src)->flags & (flags)) -/*! - \brief Free a pointer and set it to NULL unless it already is NULL - \command it the pointer -*/ -#define zap_safe_free(it) if (it) {free(it);it=NULL;} - -#define zap_socket_close(it) if (it > -1) { close(it); it = -1;} - - struct zap_stream_handle { zap_stream_handle_write_function_t write_function; zap_stream_handle_raw_write_function_t raw_write_function; @@ -586,6 +577,18 @@ typedef enum { OZ_DECLARE_DATA extern zap_crash_policy_t g_zap_crash_policy; +typedef void *(*zap_malloc_func_t)(void *pool, zap_size_t len); +typedef void *(*zap_calloc_func_t)(void *pool, zap_size_t elements, zap_size_t len); +typedef void (*zap_free_func_t)(void *pool, void *ptr); +typedef struct zap_memory_handler { + void *pool; + zap_malloc_func_t malloc; + zap_calloc_func_t calloc; + zap_free_func_t free; +} zap_memory_handler_t; + +OZ_DECLARE_DATA extern zap_memory_handler_t g_zap_mem_handler; + struct zap_io_interface { const char *name; zio_configure_span_t configure_span; @@ -691,6 +694,7 @@ OZ_DECLARE(const char *) zap_channel_get_var(zap_channel_t *zchan, const char *v OZ_DECLARE(zap_status_t) zap_channel_clear_vars(zap_channel_t *zchan); OZ_DECLARE(zap_status_t) zap_global_init(void); OZ_DECLARE(zap_status_t) zap_global_destroy(void); +OZ_DECLARE(zap_status_t) zap_global_set_memory_handler(zap_memory_handler_t *handler); OZ_DECLARE(void) zap_global_set_crash_policy(zap_crash_policy_t policy); OZ_DECLARE(void) zap_global_set_logger(zap_logger_t logger); OZ_DECLARE(void) zap_global_set_default_logger(int level); @@ -731,6 +735,10 @@ ZIO_CODEC_FUNCTION(zio_alaw2ulaw); #define zap_mutex_unlock(_x) _zap_mutex_unlock(_x) #endif +/*! + \brief Allocate uninitialized memory + \command chunksize the chunk size +*/ #define zap_assert(assertion, retval, msg) \ if (!(assertion)) { \ zap_log(ZAP_LOG_CRIT, msg); \ @@ -741,6 +749,36 @@ ZIO_CODEC_FUNCTION(zio_alaw2ulaw); } \ } +/*! + \brief Allocate uninitialized memory + \command chunksize the chunk size +*/ +#define zap_malloc(chunksize) g_zap_mem_handler.malloc(g_zap_mem_handler.pool, chunksize); + +/*! + \brief Allocate initialized memory + \command chunksize the chunk size +*/ +#define zap_calloc(elements, chunksize) g_zap_mem_handler.calloc(g_zap_mem_handler.pool, elements, chunksize); + +/*! + \brief Free chunk of memory + \command chunksize the chunk size +*/ +#define zap_free(chunk) g_zap_mem_handler.free(g_zap_mem_handler.pool, chunk); + +/*! + \brief Free a pointer and set it to NULL unless it already is NULL + \command it the pointer +*/ +#define zap_safe_free(it) if (it) { zap_free(it); it = NULL; } + +/*! + \brief Socket the given socket + \command it the socket +*/ +#define zap_socket_close(it) if (it > -1) { close(it); it = -1;} + static __inline__ void zap_set_state_all(zap_span_t *span, zap_channel_state_t state) { uint32_t j; diff --git a/libs/freetdm/src/zap_io.c b/libs/freetdm/src/zap_io.c index 48ea8e0f05..cc4ebb7f42 100644 --- a/libs/freetdm/src/zap_io.c +++ b/libs/freetdm/src/zap_io.c @@ -176,6 +176,33 @@ static void default_logger(const char *file, const char *func, int line, int lev } +static inline void *zap_std_malloc(void *pool, zap_size_t size) +{ + void *ptr = malloc(size); + zap_assert(ptr != NULL, NULL, "Out of memory"); + return ptr; +} + +static inline void *zap_std_calloc(void *pool, zap_size_t elements, zap_size_t size) +{ + void *ptr = calloc(elements, size); + zap_assert(ptr != NULL, NULL, "Out of memory"); + return ptr; +} + +static inline void zap_std_free(void *pool, void *ptr) +{ + free(ptr); +} + +OZ_DECLARE_DATA zap_memory_handler_t g_zap_mem_handler = +{ + .pool = NULL, + .malloc = zap_std_malloc, + .calloc = zap_std_calloc, + .free = zap_std_free +}; + OZ_DECLARE_DATA zap_crash_policy_t g_zap_crash_policy = ZAP_CRASH_NEVER; OZ_DECLARE_DATA zap_logger_t zap_log = null_logger; @@ -185,6 +212,24 @@ OZ_DECLARE(void) zap_global_set_crash_policy(zap_crash_policy_t policy) g_zap_crash_policy = policy; } +OZ_DECLARE(zap_status_t) zap_global_set_memory_handler(zap_memory_handler_t *handler) +{ + if (!handler) { + return ZAP_FAIL; + } + if (!handler->malloc) { + return ZAP_FAIL; + } + if (!handler->calloc) { + return ZAP_FAIL; + } + if (!handler->free) { + return ZAP_FAIL; + } + memcpy(&g_zap_mem_handler, handler, sizeof(*handler)); + return ZAP_SUCCESS; +} + OZ_DECLARE(void) zap_global_set_logger(zap_logger_t logger) { if (logger) {