fixed some sstring issues + added allocator macros

Tue, 10 Jun 2014 15:43:13 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 10 Jun 2014 15:43:13 +0200
changeset 173
31a8682fffb7
parent 172
7084e8e8433c
child 174
bbfe511cfddb

fixed some sstring issues + added allocator macros

test/string_tests.c file | annotate | diff | comparison | revisions
ucx/allocator.h file | annotate | diff | comparison | revisions
ucx/list.c file | annotate | diff | comparison | revisions
ucx/map.c 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
ucx/utils.c file | annotate | diff | comparison | revisions
     1.1 --- a/test/string_tests.c	Mon Jun 02 16:04:11 2014 +0200
     1.2 +++ b/test/string_tests.c	Tue Jun 10 15:43:13 2014 +0200
     1.3 @@ -119,7 +119,7 @@
     1.4  
     1.5      const char *original = "this,is,a,csv,string";
     1.6      sstr_t test = ST("this,is,a,csv,string"); /* use copy of original here */
     1.7 -    size_t n;
     1.8 +    ssize_t n;
     1.9      sstr_t *list;
    1.10  
    1.11      UCX_TEST_BEGIN
     2.1 --- a/ucx/allocator.h	Mon Jun 02 16:04:11 2014 +0200
     2.2 +++ b/ucx/allocator.h	Tue Jun 10 15:43:13 2014 +0200
     2.3 @@ -157,6 +157,41 @@
     2.4  void ucx_default_free(void *ignore, void *data);
     2.5  
     2.6  /**
     2.7 + * Shorthand for calling an allocators malloc function.
     2.8 + * @param allocator the allocator to use
     2.9 + * @param n size of space to allocate
    2.10 + * @return a pointer to the allocated memory area
    2.11 + */
    2.12 +#define almalloc(allocator, n) ((allocator)->malloc(allocator->pool, n))
    2.13 +
    2.14 +/**
    2.15 + * Shorthand for calling an allocators calloc function.
    2.16 + * @param allocator the allocator to use
    2.17 + * @param n the count of elements the space should be allocated for
    2.18 + * @param size the size of each element
    2.19 + * @return a pointer to the allocated memory area
    2.20 + */
    2.21 +#define alcalloc(allocator, n, size) \
    2.22 +        ((allocator)->calloc(allocator->pool, n, size))
    2.23 +
    2.24 +/**
    2.25 + * Shorthand for calling an allocators realloc function.
    2.26 + * @param allocator the allocator to use
    2.27 + * @param ptr the pointer to the memory area that shall be reallocated
    2.28 + * @param n the new size of the allocated memory area
    2.29 + * @return a pointer to the reallocated memory area
    2.30 + */
    2.31 +#define alrealloc(allocator, ptr, n) \
    2.32 +        ((allocator)->realloc(allocator->pool, ptr, n))
    2.33 +
    2.34 +/**
    2.35 + * Shorthand for calling an allocators free function.
    2.36 + * @param allocator the allocator to use
    2.37 + * @param ptr the pointer to the memory area that shall be freed
    2.38 + */
    2.39 +#define alfree(allocator, ptr) ((allocator)->free(allocator->pool, ptr))
    2.40 +
    2.41 +/**
    2.42   * Convenient macro for a default allocator <code>struct</code> definition.
    2.43   */
    2.44  #define UCX_ALLOCATOR_DEFAULT {NULL, \
     3.1 --- a/ucx/list.c	Mon Jun 02 16:04:11 2014 +0200
     3.2 +++ b/ucx/list.c	Tue Jun 10 15:43:13 2014 +0200
     3.3 @@ -72,7 +72,7 @@
     3.4      while (e != NULL) {
     3.5          f = e;
     3.6          e = e->next;
     3.7 -        alloc->free(alloc->pool, f);
     3.8 +        alfree(alloc, f);
     3.9      }
    3.10  }
    3.11  
    3.12 @@ -81,7 +81,7 @@
    3.13  }
    3.14  
    3.15  UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data)  {
    3.16 -    UcxList *nl = (UcxList*) alloc->malloc(alloc->pool, sizeof(UcxList));
    3.17 +    UcxList *nl = (UcxList*) almalloc(alloc, sizeof(UcxList));
    3.18      if (!nl) {
    3.19          return NULL;
    3.20      }
    3.21 @@ -323,6 +323,6 @@
    3.22          e->prev->next = e->next;
    3.23      }
    3.24      
    3.25 -    alloc->free(alloc->pool, e);
    3.26 +    alfree(alloc, e);
    3.27      return l;
    3.28  }
     4.1 --- a/ucx/map.c	Mon Jun 02 16:04:11 2014 +0200
     4.2 +++ b/ucx/map.c	Tue Jun 10 15:43:13 2014 +0200
     4.3 @@ -44,18 +44,16 @@
     4.4          allocator = ucx_default_allocator();
     4.5      }
     4.6      
     4.7 -    UcxMap *map = (UcxMap*)allocator->malloc(allocator->pool, sizeof(UcxMap));
     4.8 +    UcxMap *map = (UcxMap*)almalloc(allocator, sizeof(UcxMap));
     4.9      if (!map) {
    4.10          return NULL;
    4.11      }
    4.12      
    4.13      map->allocator = allocator;
    4.14 -    map->map = (UcxMapElement**)allocator->calloc(
    4.15 -            allocator->pool,
    4.16 -            size,
    4.17 -            sizeof(UcxMapElement*));
    4.18 +    map->map = (UcxMapElement**)alcalloc(
    4.19 +            allocator, size, sizeof(UcxMapElement*));
    4.20      if(map->map == NULL) {
    4.21 -        allocator->free(allocator->pool, map);
    4.22 +        alfree(allocator, map);
    4.23          return NULL;
    4.24      }
    4.25      map->size = size;
    4.26 @@ -70,18 +68,18 @@
    4.27          if (elem != NULL) {
    4.28              do {
    4.29                  UcxMapElement *next = elem->next;
    4.30 -                map->allocator->free(map->allocator->pool, elem->key.data);
    4.31 -                map->allocator->free(map->allocator->pool, elem);
    4.32 +                alfree(map->allocator, elem->key.data);
    4.33 +                alfree(map->allocator, elem);
    4.34                  elem = next;
    4.35              } while (elem != NULL);
    4.36          }
    4.37      }
    4.38 -    map->allocator->free(map->allocator->pool, map->map);
    4.39 +    alfree(map->allocator, map->map);
    4.40  }
    4.41  
    4.42  void ucx_map_free(UcxMap *map) {
    4.43      ucx_map_free_elmlist(map);
    4.44 -    map->allocator->free(map->allocator->pool, map);
    4.45 +    alfree(map->allocator, map);
    4.46  }
    4.47  
    4.48  int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
    4.49 @@ -116,10 +114,8 @@
    4.50          oldmap.allocator = map->allocator;
    4.51          
    4.52          map->size = (map->count * 5) >> 1;
    4.53 -        map->map = (UcxMapElement**)map->allocator->calloc(
    4.54 -                map->allocator->pool,
    4.55 -                map->size,
    4.56 -                sizeof(UcxMapElement*));
    4.57 +        map->map = (UcxMapElement**)alcalloc(
    4.58 +                map->allocator, map->size, sizeof(UcxMapElement*));
    4.59          if (!map->map) {
    4.60              *map = oldmap;
    4.61              return 1;
    4.62 @@ -150,9 +146,8 @@
    4.63      }
    4.64      
    4.65      if (!elm || elm->key.hash != key.hash) {
    4.66 -        UcxMapElement *e = (UcxMapElement*)allocator->malloc(
    4.67 -                allocator->pool,
    4.68 -                sizeof(UcxMapElement));
    4.69 +        UcxMapElement *e = (UcxMapElement*)almalloc(
    4.70 +                allocator, sizeof(UcxMapElement));
    4.71          if (!e) {
    4.72              return -1;
    4.73          }
    4.74 @@ -167,7 +162,7 @@
    4.75      }
    4.76      
    4.77      if (!elm->key.data) {
    4.78 -        void *kd = allocator->malloc(allocator->pool, key.len);
    4.79 +        void *kd = almalloc(allocator, key.len);
    4.80          if (!kd) {
    4.81              return -1;
    4.82          }
    4.83 @@ -200,8 +195,8 @@
    4.84                      } else {
    4.85                          map->map[slot] = elm->next;
    4.86                      }
    4.87 -                    map->allocator->free(map->allocator->pool, elm->key.data);
    4.88 -                    map->allocator->free(map->allocator->pool, elm);
    4.89 +                    alfree(map->allocator, elm->key.data);
    4.90 +                    alfree(map->allocator, elm);
    4.91                      map->count--;
    4.92                  }
    4.93  
     5.1 --- a/ucx/properties.c	Mon Jun 02 16:04:11 2014 +0200
     5.2 +++ b/ucx/properties.c	Tue Jun 10 15:43:13 2014 +0200
     5.3 @@ -206,7 +206,7 @@
     5.4              return 1;
     5.5          }
     5.6          if(ucx_map_sstr_put(map, name, value.ptr)) {
     5.7 -            map->allocator->free(map->allocator->pool, value.ptr);
     5.8 +            alfree(map->allocator, value.ptr);
     5.9              return 1;
    5.10          }
    5.11      }
     6.1 --- a/ucx/string.c	Mon Jun 02 16:04:11 2014 +0200
     6.2 +++ b/ucx/string.c	Tue Jun 10 15:43:13 2014 +0200
     6.3 @@ -97,13 +97,15 @@
     6.4  sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) {
     6.5      sstr_t new_sstr;
     6.6      if (start >= s.length) {
     6.7 -        return s;
     6.8 +        new_sstr.ptr = NULL;
     6.9 +        new_sstr.length = 0;
    6.10 +    } else {
    6.11 +        if (length > s.length-start) {
    6.12 +            length = s.length-start;
    6.13 +        }
    6.14 +        new_sstr.ptr = &s.ptr[start];
    6.15 +        new_sstr.length = length;
    6.16      }
    6.17 -    if (length > s.length-start) {
    6.18 -        length = s.length-start;
    6.19 -    }
    6.20 -    new_sstr.ptr = &s.ptr[start];
    6.21 -    new_sstr.length = length;
    6.22      return new_sstr;
    6.23  }
    6.24  
    6.25 @@ -133,18 +135,18 @@
    6.26      return n;
    6.27  }
    6.28  
    6.29 -sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) {
    6.30 +sstr_t* sstrsplit(sstr_t s, sstr_t d, ssize_t *n) {
    6.31      return sstrsplit_a(ucx_default_allocator(), s, d, n);
    6.32  }
    6.33  
    6.34 -sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, size_t *n) {
    6.35 +sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, ssize_t *n) {
    6.36      if (s.length == 0 || d.length == 0) {
    6.37          *n = -1;
    6.38          return NULL;
    6.39      }
    6.40  
    6.41      sstr_t* result;
    6.42 -    size_t nmax = *n;
    6.43 +    ssize_t nmax = *n;
    6.44      *n = 1;
    6.45  
    6.46      /* special case: exact match - no processing needed */
    6.47 @@ -179,18 +181,27 @@
    6.48          }
    6.49          if ((*n) == nmax) break;
    6.50      }
    6.51 -    result = (sstr_t*) allocator->malloc(allocator->pool, sizeof(sstr_t)*(*n));
    6.52 +    result = (sstr_t*) almalloc(allocator, sizeof(sstr_t)*(*n));
    6.53  
    6.54      if (result) {
    6.55          char *pptr = sv.ptr;
    6.56 -        for (size_t i = 0 ; i < *n ; i++) {
    6.57 +        for (ssize_t i = 0 ; i < *n ; i++) {
    6.58              size_t l = strlen(pptr);
    6.59 -            char* ptr = (char*) allocator->malloc(allocator->pool, l + 1);
    6.60 -            memcpy(ptr, pptr, l);
    6.61 -            ptr[l] = 0;
    6.62 +            char* ptr = (char*) almalloc(allocator, l + 1);
    6.63 +            if (ptr) {
    6.64 +                memcpy(ptr, pptr, l);
    6.65 +                ptr[l] = 0;
    6.66  
    6.67 -            result[i] = sstrn(ptr, l);
    6.68 -            pptr += l + d.length;
    6.69 +                result[i] = sstrn(ptr, l);
    6.70 +                pptr += l + d.length;
    6.71 +            } else {
    6.72 +                for (ssize_t j = i-1 ; j >= 0 ; j--) {
    6.73 +                    alfree(allocator, result[j].ptr);
    6.74 +                }
    6.75 +                alfree(allocator, result);
    6.76 +                *n = -2;
    6.77 +                break;
    6.78 +            }
    6.79          }
    6.80      } else {
    6.81          *n = -2;
    6.82 @@ -231,7 +242,7 @@
    6.83  
    6.84  sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) {
    6.85      sstr_t newstring;
    6.86 -    newstring.ptr = (char*)allocator->malloc(allocator->pool, s.length + 1);
    6.87 +    newstring.ptr = (char*)almalloc(allocator, s.length + 1);
    6.88      if (newstring.ptr) {
    6.89          newstring.length = s.length;
    6.90          newstring.ptr[newstring.length] = 0;
     7.1 --- a/ucx/string.h	Mon Jun 02 16:04:11 2014 +0200
     7.2 +++ b/ucx/string.h	Tue Jun 10 15:43:13 2014 +0200
     7.3 @@ -259,7 +259,7 @@
     7.4   * 
     7.5   * @see sstrsplit_a()
     7.6   */
     7.7 -sstr_t* sstrsplit(sstr_t string, sstr_t delim, size_t *count);
     7.8 +sstr_t* sstrsplit(sstr_t string, sstr_t delim, ssize_t *count);
     7.9  
    7.10  /**
    7.11   * Performing sstrsplit() using an UcxAllocator.
    7.12 @@ -284,7 +284,7 @@
    7.13   * @see sstrsplit()
    7.14   */
    7.15  sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim,
    7.16 -        size_t *count);
    7.17 +        ssize_t *count);
    7.18  
    7.19  /**
    7.20   * Compares two UCX strings with standard <code>memcmp()</code>.
     8.1 --- a/ucx/utils.c	Mon Jun 02 16:04:11 2014 +0200
     8.2 +++ b/ucx/utils.c	Tue Jun 10 15:43:13 2014 +0200
     8.3 @@ -212,7 +212,7 @@
     8.4      va_copy(ap2, ap);
     8.5      int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
     8.6      if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
     8.7 -        s.ptr = (char*)a->malloc(a->pool, ret + 1);
     8.8 +        s.ptr = (char*)almalloc(a, ret + 1);
     8.9          s.length = (size_t)ret;
    8.10          memcpy(s.ptr, buf, ret);
    8.11          s.ptr[s.length] = '\0';
    8.12 @@ -220,7 +220,7 @@
    8.13          errno = ENOMEM;
    8.14      } else  {
    8.15          int len = ret + 1;
    8.16 -        s.ptr = (char*)a->malloc(a->pool, len);
    8.17 +        s.ptr = (char*)almalloc(a, len);
    8.18          ret = vsnprintf(s.ptr, len, fmt, ap2);
    8.19          if (ret < 0) {
    8.20              free(s.ptr);
    8.21 @@ -232,7 +232,7 @@
    8.22  #else
    8.23      int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
    8.24      if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
    8.25 -        s.ptr = (char*)a->malloc(a->pool, ret + 1);
    8.26 +        s.ptr = (char*)almalloc(a, ret + 1);
    8.27          s.length = (size_t)ret;
    8.28          memcpy(s.ptr, buf, ret);
    8.29          s.ptr[s.length] = '\0';

mercurial