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
     1.1 --- a/ucx/list.c	Mon Jul 22 14:51:52 2013 +0200
     1.2 +++ b/ucx/list.c	Tue Jul 23 12:06:28 2013 +0200
     1.3 @@ -29,12 +29,17 @@
     1.4  #include "list.h"
     1.5  
     1.6  UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) {
     1.7 +    return ucx_list_clone_a(ucx_default_allocator(), l, fnc, data);
     1.8 +}
     1.9 +
    1.10 +UcxList *ucx_list_clone_a(UcxAllocator *alloc, UcxList *l,
    1.11 +        copy_func fnc, void *data) {
    1.12      UcxList *ret = NULL;
    1.13 -    while (l != NULL) {
    1.14 -        if (fnc != NULL) {
    1.15 -            ret = ucx_list_append(ret, fnc(l->data, data));
    1.16 +    while (l) {
    1.17 +        if (fnc) {
    1.18 +            ret = ucx_list_append_a(alloc, ret, fnc(l->data, data));
    1.19          } else {
    1.20 -            ret = ucx_list_append(ret, l->data);
    1.21 +            ret = ucx_list_append_a(alloc, ret, l->data);
    1.22          }
    1.23          l = l->next;
    1.24      }
    1.25 @@ -59,36 +64,53 @@
    1.26  }
    1.27  
    1.28  void ucx_list_free(UcxList *l) {
    1.29 +    ucx_list_free_a(ucx_default_allocator(), l);
    1.30 +}
    1.31 +
    1.32 +void ucx_list_free_a(UcxAllocator *alloc, UcxList *l) {
    1.33      UcxList *e = l, *f;
    1.34      while (e != NULL) {
    1.35          f = e;
    1.36          e = e->next;
    1.37 -        free(f);
    1.38 +        alloc->free(alloc->pool, f);
    1.39      }
    1.40  }
    1.41  
    1.42  UcxList *ucx_list_append(UcxList *l, void *data)  {
    1.43 -    UcxList *nl = (UcxList*) malloc(sizeof(UcxList));
    1.44 -    if (nl == NULL) return NULL;
    1.45 +    return ucx_list_append_a(ucx_default_allocator(), l, data);
    1.46 +}
    1.47 +
    1.48 +UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data)  {
    1.49 +    UcxList *nl = (UcxList*) alloc->malloc(alloc->pool, sizeof(UcxList));
    1.50 +    if (!nl) {
    1.51 +        return NULL;
    1.52 +    }
    1.53      
    1.54      nl->data = data;
    1.55      nl->next = NULL;
    1.56 -    if (l == NULL) {
    1.57 -        nl->prev = NULL;
    1.58 -        return nl;
    1.59 -    } else {
    1.60 +    if (l) {
    1.61          UcxList *t = ucx_list_last(l);
    1.62          t->next = nl;
    1.63          nl->prev = t;
    1.64          return l;
    1.65 +    } else {
    1.66 +        nl->prev = NULL;
    1.67 +        return nl;
    1.68      }
    1.69  }
    1.70  
    1.71  UcxList *ucx_list_prepend(UcxList *l, void *data) {
    1.72 -    UcxList *nl = ucx_list_append(NULL, data);
    1.73 -    if (nl == NULL) return NULL;
    1.74 +    return ucx_list_prepend_a(ucx_default_allocator(), l, data);
    1.75 +}
    1.76 +
    1.77 +UcxList *ucx_list_prepend_a(UcxAllocator *alloc, UcxList *l, void *data) {
    1.78 +    UcxList *nl = ucx_list_append_a(alloc, NULL, data);
    1.79 +    if (!nl) {
    1.80 +        return NULL;
    1.81 +    }
    1.82 +    l = ucx_list_first(l);
    1.83      
    1.84 -    if (l != NULL) {
    1.85 +    if (l) {
    1.86          nl->next = l;
    1.87          l->prev = nl;
    1.88      }
    1.89 @@ -260,16 +282,22 @@
    1.90  }
    1.91  
    1.92  UcxList *ucx_list_first(const UcxList *l) {
    1.93 -    if (l == NULL) return NULL;
    1.94 +    if (!l) {
    1.95 +        return NULL;
    1.96 +    }
    1.97      
    1.98      const UcxList *e = l;
    1.99 -    while (e->prev != NULL) {
   1.100 +    while (e->prev) {
   1.101          e = e->prev;
   1.102      }
   1.103      return (UcxList *)e;
   1.104  }
   1.105  
   1.106  UcxList *ucx_list_remove(UcxList *l, UcxList *e) {
   1.107 +    return ucx_list_remove_a(ucx_default_allocator(), l, e);
   1.108 +}
   1.109 +    
   1.110 +UcxList *ucx_list_remove_a(UcxAllocator *alloc, UcxList *l, UcxList *e) {
   1.111      if (e->prev == NULL) {
   1.112          if(e->next != NULL) {
   1.113              e->next->prev = NULL;
   1.114 @@ -282,6 +310,6 @@
   1.115          e->prev->next = e->next;
   1.116          e->next->prev = e->prev;
   1.117      }
   1.118 -    free(e);
   1.119 +    alloc->free(alloc->pool, e);
   1.120      return l;
   1.121  }
     2.1 --- a/ucx/list.h	Mon Jul 22 14:51:52 2013 +0200
     2.2 +++ b/ucx/list.h	Tue Jul 23 12:06:28 2013 +0200
     2.3 @@ -37,6 +37,7 @@
     2.4  #define	UCX_LIST_H
     2.5  
     2.6  #include "ucx.h"
     2.7 +#include "allocator.h"
     2.8  #include <stddef.h>
     2.9  
    2.10  #ifdef	__cplusplus
    2.11 @@ -81,7 +82,9 @@
    2.12      UcxList *prev;
    2.13  };
    2.14  
    2.15 -UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void* data);
    2.16 +UcxList *ucx_list_clone(UcxList *list, copy_func cpyfnc, void* data);
    2.17 +UcxList *ucx_list_clone_a(UcxAllocator *allocator, UcxList *list,
    2.18 +        copy_func cpyfnc, void* data);
    2.19  
    2.20  /**
    2.21   * Compares two UCX lists element-wise by using a compare function.
    2.22 @@ -116,8 +119,11 @@
    2.23   * @param list The list to free.
    2.24   */
    2.25  void ucx_list_free(UcxList *list);
    2.26 +void ucx_list_free_a(UcxAllocator *allocator, UcxList *list);
    2.27  UcxList *ucx_list_append(UcxList *list, void *data);
    2.28 +UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data);
    2.29  UcxList *ucx_list_prepend(UcxList *list, void *data);
    2.30 +UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data);
    2.31  UcxList *ucx_list_concat(UcxList *list1, UcxList *list2);
    2.32  /**
    2.33   * Returns the first element of a list.
    2.34 @@ -162,6 +168,8 @@
    2.35   * is now empty
    2.36   */
    2.37  UcxList *ucx_list_remove(UcxList *list, UcxList *element);
    2.38 +UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list,
    2.39 +        UcxList *element);
    2.40  
    2.41  #ifdef	__cplusplus
    2.42  }
     3.1 --- a/ucx/properties.c	Mon Jul 22 14:51:52 2013 +0200
     3.2 +++ b/ucx/properties.c	Tue Jul 23 12:06:28 2013 +0200
     3.3 @@ -201,7 +201,7 @@
     3.4      sstr_t name;
     3.5      sstr_t value;
     3.6      while(ucx_properties_next(parser, &name, &value)) {
     3.7 -        value = sstrdupa(map->allocator, value);
     3.8 +        value = sstrdup_a(map->allocator, value);
     3.9          if(!value.ptr) {
    3.10              return 1;
    3.11          }
     4.1 --- a/ucx/string.c	Mon Jul 22 14:51:52 2013 +0200
     4.2 +++ b/ucx/string.c	Tue Jul 23 12:06:28 2013 +0200
     4.3 @@ -120,10 +120,10 @@
     4.4  }
     4.5  
     4.6  sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) {
     4.7 -    return sstrsplita(s, d, n, ucx_default_allocator());
     4.8 +    return sstrsplit_a(ucx_default_allocator(), s, d, n);
     4.9  }
    4.10  
    4.11 -sstr_t* sstrsplita(sstr_t s, sstr_t d, size_t *n, UcxAllocator *allocator) {
    4.12 +sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, size_t *n) {
    4.13      if (s.length == 0 || d.length == 0) {
    4.14          *n = -1;
    4.15          return NULL;
    4.16 @@ -198,10 +198,10 @@
    4.17  }
    4.18  
    4.19  sstr_t sstrdup(sstr_t s) {
    4.20 -    return sstrdupa(ucx_default_allocator(), s);
    4.21 +    return sstrdup_a(ucx_default_allocator(), s);
    4.22  }
    4.23  
    4.24 -sstr_t sstrdupa(UcxAllocator *allocator, sstr_t s) {
    4.25 +sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) {
    4.26      sstr_t newstring;
    4.27      newstring.ptr = (char*)allocator->malloc(allocator->pool, s.length + 1);
    4.28      if (newstring.ptr) {
     5.1 --- a/ucx/string.h	Mon Jul 22 14:51:52 2013 +0200
     5.2 +++ b/ucx/string.h	Tue Jul 23 12:06:28 2013 +0200
     5.3 @@ -232,7 +232,7 @@
     5.4   * exceeded, the last list item will be an empty string.
     5.5   * 
     5.6   * <b>Attention:</b> All list items <b>AND</b> all sstr_t.ptr of the list
     5.7 - * items must be manually passed to <code>free()</code>. Use sstrsplita() with
     5.8 + * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
     5.9   * an allocator to managed memory, to avoid this.
    5.10   *
    5.11   * @param string the string to split
    5.12 @@ -242,7 +242,7 @@
    5.13   * @return a list of the split strings as sstr_t array or
    5.14   *         <code>NULL</code> on error
    5.15   * 
    5.16 - * @see sstrsplita()
    5.17 + * @see sstrsplit_a()
    5.18   */
    5.19  sstr_t* sstrsplit(sstr_t string, sstr_t delim, size_t *count);
    5.20  
    5.21 @@ -258,18 +258,18 @@
    5.22   * <b>Note:</b> the allocator is not used for memory that is freed within the
    5.23   * same call of this function (locally scoped variables).
    5.24   * 
    5.25 + * @param allocator the UcxAllocator used for allocating memory
    5.26   * @param string the string to split
    5.27   * @param delim  the delimiter string
    5.28   * @param count  IN: the maximum size of the resulting list (0 for an
    5.29   *               unbounded list), OUT: the actual size of the list
    5.30 - * @param allocator the UcxAllocator used for allocating memory
    5.31   * @return a list of the split strings as sstr_t array or
    5.32   *         <code>NULL</code> on error
    5.33   * 
    5.34   * @see sstrsplit()
    5.35   */
    5.36 -sstr_t* sstrsplita(sstr_t string, sstr_t delim, size_t *count,
    5.37 -        UcxAllocator *allocator);
    5.38 +sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim,
    5.39 +        size_t *count);
    5.40  
    5.41  /**
    5.42   * Compares two UCX strings with standard <code>memcmp()</code>.
    5.43 @@ -297,7 +297,7 @@
    5.44   * 
    5.45   * @param string the string to duplicate
    5.46   * @return a duplicate of the string
    5.47 - * @see sstrdupa()
    5.48 + * @see sstrdup_a()
    5.49   */
    5.50  sstr_t sstrdup(sstr_t string);
    5.51  
    5.52 @@ -317,7 +317,7 @@
    5.53   * @return a duplicate of the string
    5.54   * @see sstrdup()
    5.55   */
    5.56 -sstr_t sstrdupa(UcxAllocator *allocator, sstr_t string);
    5.57 +sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string);
    5.58  
    5.59  /**
    5.60   * Omits leading and trailing spaces.

mercurial