added printf for UcxBuffer + fixed memory leaks

Fri, 16 Aug 2013 14:48:58 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 16 Aug 2013 14:48:58 +0200
changeset 147
1aa598f36872
parent 146
aa376dba1ba8
child 148
c27c2425c0b1

added printf for UcxBuffer + fixed memory leaks

test/buffer_tests.c file | annotate | diff | comparison | revisions
test/prop_tests.c file | annotate | diff | comparison | revisions
test/string_tests.c file | annotate | diff | comparison | revisions
test/utils_tests.c file | annotate | diff | comparison | revisions
ucx/map.c file | annotate | diff | comparison | revisions
ucx/utils.h file | annotate | diff | comparison | revisions
     1.1 --- a/test/buffer_tests.c	Fri Aug 16 13:40:10 2013 +0200
     1.2 +++ b/test/buffer_tests.c	Fri Aug 16 14:48:58 2013 +0200
     1.3 @@ -129,7 +129,7 @@
     1.4      
     1.5      UCX_TEST_END
     1.6      
     1.7 -    
     1.8 +    ucx_buffer_free(b);
     1.9  }
    1.10  
    1.11  UCX_TEST(test_ucx_buffer_getc) {
    1.12 @@ -262,7 +262,7 @@
    1.13      
    1.14      UCX_TEST_END
    1.15      
    1.16 -    
    1.17 +    ucx_buffer_free(b);
    1.18  }
    1.19  
    1.20  UCX_TEST(test_ucx_buffer_extract) {
     2.1 --- a/test/prop_tests.c	Fri Aug 16 13:40:10 2013 +0200
     2.2 +++ b/test/prop_tests.c	Fri Aug 16 14:48:58 2013 +0200
     2.3 @@ -27,6 +27,7 @@
     2.4   */
     2.5  
     2.6  #include "prop_tests.h"
     2.7 +#include "ucx/mempool.h"
     2.8  
     2.9  UCX_TEST(test_ucx_properties_new) {
    2.10      UcxProperties *parser = ucx_properties_new();
    2.11 @@ -363,7 +364,9 @@
    2.12  }
    2.13  
    2.14  UCX_TEST(test_ucx_properties2map) {
    2.15 -    UcxMap *map = ucx_map_new(16);
    2.16 +    UcxMempool *mp = ucx_mempool_new(64);
    2.17 +    UcxAllocator *a = ucx_mempool_allocator(mp);
    2.18 +    UcxMap *map = ucx_map_new_a(a, 16);
    2.19      UcxProperties *parser = ucx_properties_new();
    2.20      
    2.21      UCX_TEST_BEGIN
    2.22 @@ -390,10 +393,7 @@
    2.23      
    2.24      // second test
    2.25      ucx_map_free(map);
    2.26 -    free(v1);
    2.27 -    free(v2);
    2.28 -    free(v3);
    2.29 -    map = ucx_map_new(16);
    2.30 +    map = ucx_map_new_a(a, 16);
    2.31      
    2.32      str = "\n#comment\n";
    2.33      ucx_properties_fill(parser, (char*)str, strlen(str));
    2.34 @@ -414,6 +414,7 @@
    2.35      
    2.36      UCX_TEST_END
    2.37      
    2.38 +    ucx_mempool_destroy(mp);
    2.39      ucx_properties_free(parser);
    2.40  }
    2.41  
    2.42 @@ -477,6 +478,9 @@
    2.43      ucx_map_free(map);
    2.44      fclose(f);
    2.45      
    2.46 +    free(long_name);
    2.47 +    free(long_value);
    2.48 +    
    2.49      UCX_TEST_END
    2.50  }
    2.51  
     3.1 --- a/test/string_tests.c	Fri Aug 16 13:40:10 2013 +0200
     3.2 +++ b/test/string_tests.c	Fri Aug 16 14:48:58 2013 +0200
     3.3 @@ -91,6 +91,9 @@
     3.4              "original string shall be returned as single list element");
     3.5      UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
     3.6                  "ndo, original has been modified");
     3.7 +    for(int i=0;i<n;i++) {
     3.8 +        free(list[i].ptr);
     3.9 +    }
    3.10      free(list);
    3.11  
    3.12      /* partially matching delimiter (pmd) */
    3.13 @@ -101,6 +104,9 @@
    3.14              "original string shall be returned as single list element");
    3.15      UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
    3.16                  "pmd, original has been modified");
    3.17 +    for(int i=0;i<n;i++) {
    3.18 +        free(list[i].ptr);
    3.19 +    }
    3.20      free(list);
    3.21  
    3.22      /* matching single-char delimiter (mscd) */
    3.23 @@ -114,6 +120,9 @@
    3.24      UCX_TEST_ASSERT(strcmp(list[4].ptr, "string")==0, "mscd, item 4 mismatch");
    3.25      UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
    3.26              "mscd, original has been modified");
    3.27 +    for(int i=0;i<n;i++) {
    3.28 +        free(list[i].ptr);
    3.29 +    }
    3.30      free(list);
    3.31  
    3.32      /* matching multi-char delimiter (mmcd) */
    3.33 @@ -126,6 +135,9 @@
    3.34              "mmcd, item 2 mismatch");
    3.35      UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
    3.36              "mmcd, original has been modified");
    3.37 +    for(int i=0;i<n;i++) {
    3.38 +        free(list[i].ptr);
    3.39 +    }
    3.40      free(list);
    3.41  
    3.42      /* bounded list using single-char delimiter (blsc) */
    3.43 @@ -138,6 +150,9 @@
    3.44              "blsc, item 2 mismatch");
    3.45      UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
    3.46              "blsc, original has been modified");
    3.47 +    for(int i=0;i<n;i++) {
    3.48 +        free(list[i].ptr);
    3.49 +    }
    3.50      free(list);
    3.51  
    3.52      /* bounded list using multi-char delimiter (blmc) */
    3.53 @@ -149,6 +164,9 @@
    3.54              "blmc, item 1 mismatch");
    3.55      UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
    3.56              "blmc, original has been modified");
    3.57 +    for(int i=0;i<n;i++) {
    3.58 +        free(list[i].ptr);
    3.59 +    }
    3.60      free(list);
    3.61  
    3.62      /* start with delimiter (swd) */
    3.63 @@ -160,6 +178,9 @@
    3.64              "swd, second item corrupt");
    3.65      UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
    3.66              "swd, original has been modified");
    3.67 +    for(int i=0;i<n;i++) {
    3.68 +        free(list[i].ptr);
    3.69 +    }
    3.70      free(list);
    3.71  
    3.72      /* end with delimiter (ewd) */
    3.73 @@ -171,6 +192,9 @@
    3.74      UCX_TEST_ASSERT(list[1].length == 0, "ewd, second item must be empty");
    3.75      UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
    3.76              "ewd, original has been modified");
    3.77 +    for(int i=0;i<n;i++) {
    3.78 +        free(list[i].ptr);
    3.79 +    }
    3.80      free(list);
    3.81  
    3.82      /* exact match (exm) */
    3.83 @@ -178,6 +202,9 @@
    3.84      list = sstrsplit(test, S("this,is,a,csv,string"), &n);
    3.85      UCX_TEST_ASSERT(n == 0, "exm, list length must be 0");
    3.86      UCX_TEST_ASSERT(list == NULL, "exm, list must be NULL");
    3.87 +    for(int i=0;i<n;i++) {
    3.88 +        free(list[i].ptr);
    3.89 +    }
    3.90      free(list);
    3.91  
    3.92      /* substring (subs) */
    3.93 @@ -188,6 +215,9 @@
    3.94              "subs, single item must be the original string");
    3.95      UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
    3.96              "subs, original has been modified");
    3.97 +    for(int i=0;i<n;i++) {
    3.98 +        free(list[i].ptr);
    3.99 +    }
   3.100      free(list);
   3.101  
   3.102      UCX_TEST_END
     4.1 --- a/test/utils_tests.c	Fri Aug 16 13:40:10 2013 +0200
     4.2 +++ b/test/utils_tests.c	Fri Aug 16 14:48:58 2013 +0200
     4.3 @@ -46,7 +46,7 @@
     4.4      UCX_TEST_ASSERT(!strcmp(b1->space, "Hello World\nend.\n"),
     4.5              "wrong content in b1 after second fprintf");
     4.6      
     4.7 -    ucx_fprintf(b2, (write_func)ucx_buffer_write, "%s%s", teststr1, teststr2);
     4.8 +    ucx_bprintf(b2, "%s%s", teststr1, teststr2);
     4.9      UCX_TEST_ASSERT(b2->pos == 2046, "wrong length");
    4.10      UCX_TEST_ASSERT(!memcmp(b2->space, teststr1, 1023),
    4.11              "wrong first half in b2");
    4.12 @@ -57,6 +57,8 @@
    4.13      
    4.14      ucx_buffer_free(b1);
    4.15      ucx_buffer_free(b2);
    4.16 +    free(teststr1);
    4.17 +    free(teststr2);
    4.18  }
    4.19  
    4.20  UCX_TEST(test_ucx_asprintf) {
    4.21 @@ -84,4 +86,7 @@
    4.22      free(s2.ptr);
    4.23      
    4.24      UCX_TEST_END
    4.25 +            
    4.26 +    free(teststr1);
    4.27 +    free(teststr2);
    4.28  }
     5.1 --- a/ucx/map.c	Fri Aug 16 13:40:10 2013 +0200
     5.2 +++ b/ucx/map.c	Fri Aug 16 14:48:58 2013 +0200
     5.3 @@ -200,6 +200,7 @@
     5.4                      } else {
     5.5                          map->map[slot] = elm->next;
     5.6                      }
     5.7 +                    map->allocator->free(map->allocator->pool, elm->key.data);
     5.8                      map->allocator->free(map->allocator->pool, elm);
     5.9                      map->count--;
    5.10                  }
     6.1 --- a/ucx/utils.h	Fri Aug 16 13:40:10 2013 +0200
     6.2 +++ b/ucx/utils.h	Fri Aug 16 14:48:58 2013 +0200
     6.3 @@ -29,7 +29,7 @@
     6.4  /**
     6.5   * @file utils.h
     6.6   * 
     6.7 - * Common utilities like compare and copy functions.
     6.8 + * Compare, copy and printf functions.
     6.9   * 
    6.10   * @author Mike Becker
    6.11   * @author Olaf Wintermann
    6.12 @@ -230,6 +230,18 @@
    6.13   */
    6.14  sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap);
    6.15  
    6.16 +/**
    6.17 + * A <code>printf()</code> like function which writes the output to an
    6.18 + * UcxBuffer.
    6.19 + * 
    6.20 + * @param buffer the buffer the data is written to
    6.21 + * @param ... format string and additional arguments
    6.22 + * @return the total number of bytes written
    6.23 + * @see ucx_fprintf()
    6.24 + */
    6.25 +#define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \
    6.26 +        (write_func)ucx_buffer_write, __VA_ARGS__)
    6.27 +
    6.28  #ifdef	__cplusplus
    6.29  }
    6.30  #endif

mercurial