# HG changeset patch # User Mike Becker # Date 1402407793 -7200 # Node ID 31a8682fffb761510f5812712fb31b34808640c2 # Parent 7084e8e8433ca8e2424c266bd14418c266074d10 fixed some sstring issues + added allocator macros diff -r 7084e8e8433c -r 31a8682fffb7 test/string_tests.c --- a/test/string_tests.c Mon Jun 02 16:04:11 2014 +0200 +++ b/test/string_tests.c Tue Jun 10 15:43:13 2014 +0200 @@ -119,7 +119,7 @@ const char *original = "this,is,a,csv,string"; sstr_t test = ST("this,is,a,csv,string"); /* use copy of original here */ - size_t n; + ssize_t n; sstr_t *list; UCX_TEST_BEGIN diff -r 7084e8e8433c -r 31a8682fffb7 ucx/allocator.h --- a/ucx/allocator.h Mon Jun 02 16:04:11 2014 +0200 +++ b/ucx/allocator.h Tue Jun 10 15:43:13 2014 +0200 @@ -157,6 +157,41 @@ void ucx_default_free(void *ignore, void *data); /** + * Shorthand for calling an allocators malloc function. + * @param allocator the allocator to use + * @param n size of space to allocate + * @return a pointer to the allocated memory area + */ +#define almalloc(allocator, n) ((allocator)->malloc(allocator->pool, n)) + +/** + * Shorthand for calling an allocators calloc function. + * @param allocator the allocator to use + * @param n the count of elements the space should be allocated for + * @param size the size of each element + * @return a pointer to the allocated memory area + */ +#define alcalloc(allocator, n, size) \ + ((allocator)->calloc(allocator->pool, n, size)) + +/** + * Shorthand for calling an allocators realloc function. + * @param allocator the allocator to use + * @param ptr the pointer to the memory area that shall be reallocated + * @param n the new size of the allocated memory area + * @return a pointer to the reallocated memory area + */ +#define alrealloc(allocator, ptr, n) \ + ((allocator)->realloc(allocator->pool, ptr, n)) + +/** + * Shorthand for calling an allocators free function. + * @param allocator the allocator to use + * @param ptr the pointer to the memory area that shall be freed + */ +#define alfree(allocator, ptr) ((allocator)->free(allocator->pool, ptr)) + +/** * Convenient macro for a default allocator struct definition. */ #define UCX_ALLOCATOR_DEFAULT {NULL, \ diff -r 7084e8e8433c -r 31a8682fffb7 ucx/list.c --- a/ucx/list.c Mon Jun 02 16:04:11 2014 +0200 +++ b/ucx/list.c Tue Jun 10 15:43:13 2014 +0200 @@ -72,7 +72,7 @@ while (e != NULL) { f = e; e = e->next; - alloc->free(alloc->pool, f); + alfree(alloc, f); } } @@ -81,7 +81,7 @@ } UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data) { - UcxList *nl = (UcxList*) alloc->malloc(alloc->pool, sizeof(UcxList)); + UcxList *nl = (UcxList*) almalloc(alloc, sizeof(UcxList)); if (!nl) { return NULL; } @@ -323,6 +323,6 @@ e->prev->next = e->next; } - alloc->free(alloc->pool, e); + alfree(alloc, e); return l; } diff -r 7084e8e8433c -r 31a8682fffb7 ucx/map.c --- a/ucx/map.c Mon Jun 02 16:04:11 2014 +0200 +++ b/ucx/map.c Tue Jun 10 15:43:13 2014 +0200 @@ -44,18 +44,16 @@ allocator = ucx_default_allocator(); } - UcxMap *map = (UcxMap*)allocator->malloc(allocator->pool, sizeof(UcxMap)); + UcxMap *map = (UcxMap*)almalloc(allocator, sizeof(UcxMap)); if (!map) { return NULL; } map->allocator = allocator; - map->map = (UcxMapElement**)allocator->calloc( - allocator->pool, - size, - sizeof(UcxMapElement*)); + map->map = (UcxMapElement**)alcalloc( + allocator, size, sizeof(UcxMapElement*)); if(map->map == NULL) { - allocator->free(allocator->pool, map); + alfree(allocator, map); return NULL; } map->size = size; @@ -70,18 +68,18 @@ if (elem != NULL) { do { UcxMapElement *next = elem->next; - map->allocator->free(map->allocator->pool, elem->key.data); - map->allocator->free(map->allocator->pool, elem); + alfree(map->allocator, elem->key.data); + alfree(map->allocator, elem); elem = next; } while (elem != NULL); } } - map->allocator->free(map->allocator->pool, map->map); + alfree(map->allocator, map->map); } void ucx_map_free(UcxMap *map) { ucx_map_free_elmlist(map); - map->allocator->free(map->allocator->pool, map); + alfree(map->allocator, map); } int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, @@ -116,10 +114,8 @@ oldmap.allocator = map->allocator; map->size = (map->count * 5) >> 1; - map->map = (UcxMapElement**)map->allocator->calloc( - map->allocator->pool, - map->size, - sizeof(UcxMapElement*)); + map->map = (UcxMapElement**)alcalloc( + map->allocator, map->size, sizeof(UcxMapElement*)); if (!map->map) { *map = oldmap; return 1; @@ -150,9 +146,8 @@ } if (!elm || elm->key.hash != key.hash) { - UcxMapElement *e = (UcxMapElement*)allocator->malloc( - allocator->pool, - sizeof(UcxMapElement)); + UcxMapElement *e = (UcxMapElement*)almalloc( + allocator, sizeof(UcxMapElement)); if (!e) { return -1; } @@ -167,7 +162,7 @@ } if (!elm->key.data) { - void *kd = allocator->malloc(allocator->pool, key.len); + void *kd = almalloc(allocator, key.len); if (!kd) { return -1; } @@ -200,8 +195,8 @@ } else { map->map[slot] = elm->next; } - map->allocator->free(map->allocator->pool, elm->key.data); - map->allocator->free(map->allocator->pool, elm); + alfree(map->allocator, elm->key.data); + alfree(map->allocator, elm); map->count--; } diff -r 7084e8e8433c -r 31a8682fffb7 ucx/properties.c --- a/ucx/properties.c Mon Jun 02 16:04:11 2014 +0200 +++ b/ucx/properties.c Tue Jun 10 15:43:13 2014 +0200 @@ -206,7 +206,7 @@ return 1; } if(ucx_map_sstr_put(map, name, value.ptr)) { - map->allocator->free(map->allocator->pool, value.ptr); + alfree(map->allocator, value.ptr); return 1; } } diff -r 7084e8e8433c -r 31a8682fffb7 ucx/string.c --- a/ucx/string.c Mon Jun 02 16:04:11 2014 +0200 +++ b/ucx/string.c Tue Jun 10 15:43:13 2014 +0200 @@ -97,13 +97,15 @@ sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) { sstr_t new_sstr; if (start >= s.length) { - return s; + new_sstr.ptr = NULL; + new_sstr.length = 0; + } else { + if (length > s.length-start) { + length = s.length-start; + } + new_sstr.ptr = &s.ptr[start]; + new_sstr.length = length; } - if (length > s.length-start) { - length = s.length-start; - } - new_sstr.ptr = &s.ptr[start]; - new_sstr.length = length; return new_sstr; } @@ -133,18 +135,18 @@ return n; } -sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) { +sstr_t* sstrsplit(sstr_t s, sstr_t d, ssize_t *n) { return sstrsplit_a(ucx_default_allocator(), s, d, n); } -sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, size_t *n) { +sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, ssize_t *n) { if (s.length == 0 || d.length == 0) { *n = -1; return NULL; } sstr_t* result; - size_t nmax = *n; + ssize_t nmax = *n; *n = 1; /* special case: exact match - no processing needed */ @@ -179,18 +181,27 @@ } if ((*n) == nmax) break; } - result = (sstr_t*) allocator->malloc(allocator->pool, sizeof(sstr_t)*(*n)); + result = (sstr_t*) almalloc(allocator, sizeof(sstr_t)*(*n)); if (result) { char *pptr = sv.ptr; - for (size_t i = 0 ; i < *n ; i++) { + for (ssize_t i = 0 ; i < *n ; i++) { size_t l = strlen(pptr); - char* ptr = (char*) allocator->malloc(allocator->pool, l + 1); - memcpy(ptr, pptr, l); - ptr[l] = 0; + char* ptr = (char*) almalloc(allocator, l + 1); + if (ptr) { + memcpy(ptr, pptr, l); + ptr[l] = 0; - result[i] = sstrn(ptr, l); - pptr += l + d.length; + result[i] = sstrn(ptr, l); + pptr += l + d.length; + } else { + for (ssize_t j = i-1 ; j >= 0 ; j--) { + alfree(allocator, result[j].ptr); + } + alfree(allocator, result); + *n = -2; + break; + } } } else { *n = -2; @@ -231,7 +242,7 @@ sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) { sstr_t newstring; - newstring.ptr = (char*)allocator->malloc(allocator->pool, s.length + 1); + newstring.ptr = (char*)almalloc(allocator, s.length + 1); if (newstring.ptr) { newstring.length = s.length; newstring.ptr[newstring.length] = 0; diff -r 7084e8e8433c -r 31a8682fffb7 ucx/string.h --- a/ucx/string.h Mon Jun 02 16:04:11 2014 +0200 +++ b/ucx/string.h Tue Jun 10 15:43:13 2014 +0200 @@ -259,7 +259,7 @@ * * @see sstrsplit_a() */ -sstr_t* sstrsplit(sstr_t string, sstr_t delim, size_t *count); +sstr_t* sstrsplit(sstr_t string, sstr_t delim, ssize_t *count); /** * Performing sstrsplit() using an UcxAllocator. @@ -284,7 +284,7 @@ * @see sstrsplit() */ sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim, - size_t *count); + ssize_t *count); /** * Compares two UCX strings with standard memcmp(). diff -r 7084e8e8433c -r 31a8682fffb7 ucx/utils.c --- a/ucx/utils.c Mon Jun 02 16:04:11 2014 +0200 +++ b/ucx/utils.c Tue Jun 10 15:43:13 2014 +0200 @@ -212,7 +212,7 @@ va_copy(ap2, ap); int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { - s.ptr = (char*)a->malloc(a->pool, ret + 1); + s.ptr = (char*)almalloc(a, ret + 1); s.length = (size_t)ret; memcpy(s.ptr, buf, ret); s.ptr[s.length] = '\0'; @@ -220,7 +220,7 @@ errno = ENOMEM; } else { int len = ret + 1; - s.ptr = (char*)a->malloc(a->pool, len); + s.ptr = (char*)almalloc(a, len); ret = vsnprintf(s.ptr, len, fmt, ap2); if (ret < 0) { free(s.ptr); @@ -232,7 +232,7 @@ #else int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { - s.ptr = (char*)a->malloc(a->pool, ret + 1); + s.ptr = (char*)almalloc(a, ret + 1); s.length = (size_t)ret; memcpy(s.ptr, buf, ret); s.ptr[s.length] = '\0';