]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
added memory handlers
authorMoises Silva <moy@sangoma.com>
Sun, 15 Nov 2009 01:46:43 +0000 (01:46 +0000)
committerMoises Silva <moy@sangoma.com>
Sun, 15 Nov 2009 01:46:43 +0000 (01:46 +0000)
git-svn-id: http://svn.openzap.org/svn/openzap/branches/sangoma_boost@861 a93c3328-9c30-0410-af19-c9cd2b2d52af

libs/freetdm/src/hashtable_itr.c
libs/freetdm/src/include/openzap.h
libs/freetdm/src/zap_io.c

index 8973b08ad0a34d19e564bab8c4d6c972fd89c7af..cb8165dc919b3ec7ae053154ad823d29e6f93f23 100644 (file)
@@ -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;
index 8f135e173cee43539b6ae1e152405fa5155534ed..bdd97ca591e24fab011bc5523ae655626e4f6431 100644 (file)
@@ -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;
index 48ea8e0f05f4b973facb055248069261dc09c8f5..cc4ebb7f423cb83d80ba5a71d4c0a407b0d76a92 100644 (file)
@@ -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) {