apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Knight <Christopher.D.Kni...@nasa.gov>
Subject Re: allocator and using malloc alternatives
Date Tue, 18 May 2010 22:14:44 GMT
Sorry 'bout those diffs, to be more clear, here's a SVN diff of my changes against apr's trunk:

Index: memory/unix/apr_pools.c
===================================================================
--- memory/unix/apr_pools.c	(revision 945924)
+++ memory/unix/apr_pools.c	(working copy)
@@ -96,6 +96,12 @@
      * slot 19: size 81920
      */
     apr_memnode_t      *free[MAX_INDEX];
+
+
+    // define these three if you'd like to use something other than malloc
+    apr_allocator_alloc_fn_t *alloc_fn;
+    apr_allocator_free_fn_t *free_fn;
+    void *alloc_opaque;
 };
 
 #define SIZEOF_ALLOCATOR_T  APR_ALIGN_DEFAULT(sizeof(apr_allocator_t))
@@ -323,6 +329,10 @@
     /* If we haven't got a suitable node, malloc a new one
      * and initialize it.
      */
+    // if we have an alternate to malloc, call it
+    if (allocator->alloc_fn && (node = allocator->alloc_fn(size, allocator->alloc_opaque))
== NULL) return NULL;
+
+    // otherwise, just call malloc
     if ((node = malloc(size)) == NULL)
         return NULL;
 
@@ -400,7 +410,9 @@
     while (freelist != NULL) {
         node = freelist;
         freelist = node->next;
-        free(node);
+
+	if (allocator->free_fn) allocator->free_fn(node, allocator->alloc_opaque);
+        else free(node);
     }
 }
 
Index: include/apr_allocator.h
===================================================================
--- include/apr_allocator.h	(revision 945924)
+++ include/apr_allocator.h	(working copy)
@@ -100,6 +100,17 @@
 APR_DECLARE(void) apr_allocator_free(apr_allocator_t *allocator,
                                      apr_memnode_t *memnode);
 
+/**
+ * Redefines the function used by this apr_allocator to allocate a block of memory.
+ * @param allocator The allocator to set the memory allocation functions on
+ * @param alloc_fn The function for me to call when apr_allocator_alloc is called
+ * @param free_fn The function for me to call when apr_allocator_free is called
+ * @param opaque The opaque information to pass to these back-end functions
+ */
+typedef apr_memnode_t *(apr_allocator_alloc_fn_t)(apr_size_t size, void *opaque);
+typedef void(apr_allocator_free_fn_t)(apr_memnode_t *memnode, void *opaque);
+APR_DECLARE(apr_status_t) apr_allocator_set_fns(apr_allocator_t *allocator, apr_allocator_alloc_fn_t
*alloc_fn, apr_allocator_free_fn_t *free_fn, void *opaque);
+
 #include "apr_pools.h"
 
 /**


Mime
View raw message