changed suffix for allocator aware functions + added allocator aware functions for UcxList

Tue, 23 Jul 2013 12:06:28 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Jul 2013 12:06:28 +0200
changeset 125
fca8efb122de
parent 124
8b44653541ef
child 126
dffb551c5f18

changed suffix for allocator aware functions + added allocator aware functions for UcxList

ucx/list.c file | annotate | diff | comparison | revisions
ucx/list.h file | annotate | diff | comparison | revisions
ucx/properties.c file | annotate | diff | comparison | revisions
ucx/string.c file | annotate | diff | comparison | revisions
ucx/string.h file | annotate | diff | comparison | revisions
--- a/ucx/list.c	Mon Jul 22 14:51:52 2013 +0200
+++ b/ucx/list.c	Tue Jul 23 12:06:28 2013 +0200
@@ -29,12 +29,17 @@
 #include "list.h"
 
 UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) {
+    return ucx_list_clone_a(ucx_default_allocator(), l, fnc, data);
+}
+
+UcxList *ucx_list_clone_a(UcxAllocator *alloc, UcxList *l,
+        copy_func fnc, void *data) {
     UcxList *ret = NULL;
-    while (l != NULL) {
-        if (fnc != NULL) {
-            ret = ucx_list_append(ret, fnc(l->data, data));
+    while (l) {
+        if (fnc) {
+            ret = ucx_list_append_a(alloc, ret, fnc(l->data, data));
         } else {
-            ret = ucx_list_append(ret, l->data);
+            ret = ucx_list_append_a(alloc, ret, l->data);
         }
         l = l->next;
     }
@@ -59,36 +64,53 @@
 }
 
 void ucx_list_free(UcxList *l) {
+    ucx_list_free_a(ucx_default_allocator(), l);
+}
+
+void ucx_list_free_a(UcxAllocator *alloc, UcxList *l) {
     UcxList *e = l, *f;
     while (e != NULL) {
         f = e;
         e = e->next;
-        free(f);
+        alloc->free(alloc->pool, f);
     }
 }
 
 UcxList *ucx_list_append(UcxList *l, void *data)  {
-    UcxList *nl = (UcxList*) malloc(sizeof(UcxList));
-    if (nl == NULL) return NULL;
+    return ucx_list_append_a(ucx_default_allocator(), l, data);
+}
+
+UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data)  {
+    UcxList *nl = (UcxList*) alloc->malloc(alloc->pool, sizeof(UcxList));
+    if (!nl) {
+        return NULL;
+    }
     
     nl->data = data;
     nl->next = NULL;
-    if (l == NULL) {
-        nl->prev = NULL;
-        return nl;
-    } else {
+    if (l) {
         UcxList *t = ucx_list_last(l);
         t->next = nl;
         nl->prev = t;
         return l;
+    } else {
+        nl->prev = NULL;
+        return nl;
     }
 }
 
 UcxList *ucx_list_prepend(UcxList *l, void *data) {
-    UcxList *nl = ucx_list_append(NULL, data);
-    if (nl == NULL) return NULL;
+    return ucx_list_prepend_a(ucx_default_allocator(), l, data);
+}
+
+UcxList *ucx_list_prepend_a(UcxAllocator *alloc, UcxList *l, void *data) {
+    UcxList *nl = ucx_list_append_a(alloc, NULL, data);
+    if (!nl) {
+        return NULL;
+    }
+    l = ucx_list_first(l);
     
-    if (l != NULL) {
+    if (l) {
         nl->next = l;
         l->prev = nl;
     }
@@ -260,16 +282,22 @@
 }
 
 UcxList *ucx_list_first(const UcxList *l) {
-    if (l == NULL) return NULL;
+    if (!l) {
+        return NULL;
+    }
     
     const UcxList *e = l;
-    while (e->prev != NULL) {
+    while (e->prev) {
         e = e->prev;
     }
     return (UcxList *)e;
 }
 
 UcxList *ucx_list_remove(UcxList *l, UcxList *e) {
+    return ucx_list_remove_a(ucx_default_allocator(), l, e);
+}
+    
+UcxList *ucx_list_remove_a(UcxAllocator *alloc, UcxList *l, UcxList *e) {
     if (e->prev == NULL) {
         if(e->next != NULL) {
             e->next->prev = NULL;
@@ -282,6 +310,6 @@
         e->prev->next = e->next;
         e->next->prev = e->prev;
     }
-    free(e);
+    alloc->free(alloc->pool, e);
     return l;
 }
--- a/ucx/list.h	Mon Jul 22 14:51:52 2013 +0200
+++ b/ucx/list.h	Tue Jul 23 12:06:28 2013 +0200
@@ -37,6 +37,7 @@
 #define	UCX_LIST_H
 
 #include "ucx.h"
+#include "allocator.h"
 #include <stddef.h>
 
 #ifdef	__cplusplus
@@ -81,7 +82,9 @@
     UcxList *prev;
 };
 
-UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void* data);
+UcxList *ucx_list_clone(UcxList *list, copy_func cpyfnc, void* data);
+UcxList *ucx_list_clone_a(UcxAllocator *allocator, UcxList *list,
+        copy_func cpyfnc, void* data);
 
 /**
  * Compares two UCX lists element-wise by using a compare function.
@@ -116,8 +119,11 @@
  * @param list The list to free.
  */
 void ucx_list_free(UcxList *list);
+void ucx_list_free_a(UcxAllocator *allocator, UcxList *list);
 UcxList *ucx_list_append(UcxList *list, void *data);
+UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data);
 UcxList *ucx_list_prepend(UcxList *list, void *data);
+UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data);
 UcxList *ucx_list_concat(UcxList *list1, UcxList *list2);
 /**
  * Returns the first element of a list.
@@ -162,6 +168,8 @@
  * is now empty
  */
 UcxList *ucx_list_remove(UcxList *list, UcxList *element);
+UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list,
+        UcxList *element);
 
 #ifdef	__cplusplus
 }
--- a/ucx/properties.c	Mon Jul 22 14:51:52 2013 +0200
+++ b/ucx/properties.c	Tue Jul 23 12:06:28 2013 +0200
@@ -201,7 +201,7 @@
     sstr_t name;
     sstr_t value;
     while(ucx_properties_next(parser, &name, &value)) {
-        value = sstrdupa(map->allocator, value);
+        value = sstrdup_a(map->allocator, value);
         if(!value.ptr) {
             return 1;
         }
--- a/ucx/string.c	Mon Jul 22 14:51:52 2013 +0200
+++ b/ucx/string.c	Tue Jul 23 12:06:28 2013 +0200
@@ -120,10 +120,10 @@
 }
 
 sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) {
-    return sstrsplita(s, d, n, ucx_default_allocator());
+    return sstrsplit_a(ucx_default_allocator(), s, d, n);
 }
 
-sstr_t* sstrsplita(sstr_t s, sstr_t d, size_t *n, UcxAllocator *allocator) {
+sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, size_t *n) {
     if (s.length == 0 || d.length == 0) {
         *n = -1;
         return NULL;
@@ -198,10 +198,10 @@
 }
 
 sstr_t sstrdup(sstr_t s) {
-    return sstrdupa(ucx_default_allocator(), s);
+    return sstrdup_a(ucx_default_allocator(), s);
 }
 
-sstr_t sstrdupa(UcxAllocator *allocator, sstr_t s) {
+sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) {
     sstr_t newstring;
     newstring.ptr = (char*)allocator->malloc(allocator->pool, s.length + 1);
     if (newstring.ptr) {
--- a/ucx/string.h	Mon Jul 22 14:51:52 2013 +0200
+++ b/ucx/string.h	Tue Jul 23 12:06:28 2013 +0200
@@ -232,7 +232,7 @@
  * exceeded, the last list item will be an empty string.
  * 
  * <b>Attention:</b> All list items <b>AND</b> all sstr_t.ptr of the list
- * items must be manually passed to <code>free()</code>. Use sstrsplita() with
+ * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
  * an allocator to managed memory, to avoid this.
  *
  * @param string the string to split
@@ -242,7 +242,7 @@
  * @return a list of the split strings as sstr_t array or
  *         <code>NULL</code> on error
  * 
- * @see sstrsplita()
+ * @see sstrsplit_a()
  */
 sstr_t* sstrsplit(sstr_t string, sstr_t delim, size_t *count);
 
@@ -258,18 +258,18 @@
  * <b>Note:</b> the allocator is not used for memory that is freed within the
  * same call of this function (locally scoped variables).
  * 
+ * @param allocator the UcxAllocator used for allocating memory
  * @param string the string to split
  * @param delim  the delimiter string
  * @param count  IN: the maximum size of the resulting list (0 for an
  *               unbounded list), OUT: the actual size of the list
- * @param allocator the UcxAllocator used for allocating memory
  * @return a list of the split strings as sstr_t array or
  *         <code>NULL</code> on error
  * 
  * @see sstrsplit()
  */
-sstr_t* sstrsplita(sstr_t string, sstr_t delim, size_t *count,
-        UcxAllocator *allocator);
+sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim,
+        size_t *count);
 
 /**
  * Compares two UCX strings with standard <code>memcmp()</code>.
@@ -297,7 +297,7 @@
  * 
  * @param string the string to duplicate
  * @return a duplicate of the string
- * @see sstrdupa()
+ * @see sstrdup_a()
  */
 sstr_t sstrdup(sstr_t string);
 
@@ -317,7 +317,7 @@
  * @return a duplicate of the string
  * @see sstrdup()
  */
-sstr_t sstrdupa(UcxAllocator *allocator, sstr_t string);
+sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string);
 
 /**
  * Omits leading and trailing spaces.

mercurial