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
--- a/test/buffer_tests.c	Fri Aug 16 13:40:10 2013 +0200
+++ b/test/buffer_tests.c	Fri Aug 16 14:48:58 2013 +0200
@@ -129,7 +129,7 @@
     
     UCX_TEST_END
     
-    
+    ucx_buffer_free(b);
 }
 
 UCX_TEST(test_ucx_buffer_getc) {
@@ -262,7 +262,7 @@
     
     UCX_TEST_END
     
-    
+    ucx_buffer_free(b);
 }
 
 UCX_TEST(test_ucx_buffer_extract) {
--- a/test/prop_tests.c	Fri Aug 16 13:40:10 2013 +0200
+++ b/test/prop_tests.c	Fri Aug 16 14:48:58 2013 +0200
@@ -27,6 +27,7 @@
  */
 
 #include "prop_tests.h"
+#include "ucx/mempool.h"
 
 UCX_TEST(test_ucx_properties_new) {
     UcxProperties *parser = ucx_properties_new();
@@ -363,7 +364,9 @@
 }
 
 UCX_TEST(test_ucx_properties2map) {
-    UcxMap *map = ucx_map_new(16);
+    UcxMempool *mp = ucx_mempool_new(64);
+    UcxAllocator *a = ucx_mempool_allocator(mp);
+    UcxMap *map = ucx_map_new_a(a, 16);
     UcxProperties *parser = ucx_properties_new();
     
     UCX_TEST_BEGIN
@@ -390,10 +393,7 @@
     
     // second test
     ucx_map_free(map);
-    free(v1);
-    free(v2);
-    free(v3);
-    map = ucx_map_new(16);
+    map = ucx_map_new_a(a, 16);
     
     str = "\n#comment\n";
     ucx_properties_fill(parser, (char*)str, strlen(str));
@@ -414,6 +414,7 @@
     
     UCX_TEST_END
     
+    ucx_mempool_destroy(mp);
     ucx_properties_free(parser);
 }
 
@@ -477,6 +478,9 @@
     ucx_map_free(map);
     fclose(f);
     
+    free(long_name);
+    free(long_value);
+    
     UCX_TEST_END
 }
 
--- a/test/string_tests.c	Fri Aug 16 13:40:10 2013 +0200
+++ b/test/string_tests.c	Fri Aug 16 14:48:58 2013 +0200
@@ -91,6 +91,9 @@
             "original string shall be returned as single list element");
     UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
                 "ndo, original has been modified");
+    for(int i=0;i<n;i++) {
+        free(list[i].ptr);
+    }
     free(list);
 
     /* partially matching delimiter (pmd) */
@@ -101,6 +104,9 @@
             "original string shall be returned as single list element");
     UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
                 "pmd, original has been modified");
+    for(int i=0;i<n;i++) {
+        free(list[i].ptr);
+    }
     free(list);
 
     /* matching single-char delimiter (mscd) */
@@ -114,6 +120,9 @@
     UCX_TEST_ASSERT(strcmp(list[4].ptr, "string")==0, "mscd, item 4 mismatch");
     UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
             "mscd, original has been modified");
+    for(int i=0;i<n;i++) {
+        free(list[i].ptr);
+    }
     free(list);
 
     /* matching multi-char delimiter (mmcd) */
@@ -126,6 +135,9 @@
             "mmcd, item 2 mismatch");
     UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
             "mmcd, original has been modified");
+    for(int i=0;i<n;i++) {
+        free(list[i].ptr);
+    }
     free(list);
 
     /* bounded list using single-char delimiter (blsc) */
@@ -138,6 +150,9 @@
             "blsc, item 2 mismatch");
     UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
             "blsc, original has been modified");
+    for(int i=0;i<n;i++) {
+        free(list[i].ptr);
+    }
     free(list);
 
     /* bounded list using multi-char delimiter (blmc) */
@@ -149,6 +164,9 @@
             "blmc, item 1 mismatch");
     UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
             "blmc, original has been modified");
+    for(int i=0;i<n;i++) {
+        free(list[i].ptr);
+    }
     free(list);
 
     /* start with delimiter (swd) */
@@ -160,6 +178,9 @@
             "swd, second item corrupt");
     UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
             "swd, original has been modified");
+    for(int i=0;i<n;i++) {
+        free(list[i].ptr);
+    }
     free(list);
 
     /* end with delimiter (ewd) */
@@ -171,6 +192,9 @@
     UCX_TEST_ASSERT(list[1].length == 0, "ewd, second item must be empty");
     UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
             "ewd, original has been modified");
+    for(int i=0;i<n;i++) {
+        free(list[i].ptr);
+    }
     free(list);
 
     /* exact match (exm) */
@@ -178,6 +202,9 @@
     list = sstrsplit(test, S("this,is,a,csv,string"), &n);
     UCX_TEST_ASSERT(n == 0, "exm, list length must be 0");
     UCX_TEST_ASSERT(list == NULL, "exm, list must be NULL");
+    for(int i=0;i<n;i++) {
+        free(list[i].ptr);
+    }
     free(list);
 
     /* substring (subs) */
@@ -188,6 +215,9 @@
             "subs, single item must be the original string");
     UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0,
             "subs, original has been modified");
+    for(int i=0;i<n;i++) {
+        free(list[i].ptr);
+    }
     free(list);
 
     UCX_TEST_END
--- a/test/utils_tests.c	Fri Aug 16 13:40:10 2013 +0200
+++ b/test/utils_tests.c	Fri Aug 16 14:48:58 2013 +0200
@@ -46,7 +46,7 @@
     UCX_TEST_ASSERT(!strcmp(b1->space, "Hello World\nend.\n"),
             "wrong content in b1 after second fprintf");
     
-    ucx_fprintf(b2, (write_func)ucx_buffer_write, "%s%s", teststr1, teststr2);
+    ucx_bprintf(b2, "%s%s", teststr1, teststr2);
     UCX_TEST_ASSERT(b2->pos == 2046, "wrong length");
     UCX_TEST_ASSERT(!memcmp(b2->space, teststr1, 1023),
             "wrong first half in b2");
@@ -57,6 +57,8 @@
     
     ucx_buffer_free(b1);
     ucx_buffer_free(b2);
+    free(teststr1);
+    free(teststr2);
 }
 
 UCX_TEST(test_ucx_asprintf) {
@@ -84,4 +86,7 @@
     free(s2.ptr);
     
     UCX_TEST_END
+            
+    free(teststr1);
+    free(teststr2);
 }
--- a/ucx/map.c	Fri Aug 16 13:40:10 2013 +0200
+++ b/ucx/map.c	Fri Aug 16 14:48:58 2013 +0200
@@ -200,6 +200,7 @@
                     } else {
                         map->map[slot] = elm->next;
                     }
+                    map->allocator->free(map->allocator->pool, elm->key.data);
                     map->allocator->free(map->allocator->pool, elm);
                     map->count--;
                 }
--- a/ucx/utils.h	Fri Aug 16 13:40:10 2013 +0200
+++ b/ucx/utils.h	Fri Aug 16 14:48:58 2013 +0200
@@ -29,7 +29,7 @@
 /**
  * @file utils.h
  * 
- * Common utilities like compare and copy functions.
+ * Compare, copy and printf functions.
  * 
  * @author Mike Becker
  * @author Olaf Wintermann
@@ -230,6 +230,18 @@
  */
 sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap);
 
+/**
+ * A <code>printf()</code> like function which writes the output to an
+ * UcxBuffer.
+ * 
+ * @param buffer the buffer the data is written to
+ * @param ... format string and additional arguments
+ * @return the total number of bytes written
+ * @see ucx_fprintf()
+ */
+#define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \
+        (write_func)ucx_buffer_write, __VA_ARGS__)
+
 #ifdef	__cplusplus
 }
 #endif

mercurial