Merge branch 'release/1.3'
[reservoir_sample] / buf.c
diff --git a/buf.c b/buf.c
index 7a94079d0e965ebcd3b174e032c18064d7c51eca..4465ff236e5563f6825034f0690cc4373041efbc 100644 (file)
--- a/buf.c
+++ b/buf.c
@@ -7,9 +7,14 @@
 #include "notify.h"
 #include "test_suite.h"
 
+/* If these ever need to be overridden... */
+void *(*buf_malloc_)(size_t) = malloc;
+void *(*buf_realloc_)(void *, size_t) = realloc;
+void (*buf_free_)(void *) = free;
+
 inline
 buf_t buf_new(size_t sz) {
-       buf_t buf = malloc(sz + sizeof *buf);
+       buf_t buf = buf_malloc_(sz + sizeof *buf);
        if (buf) {
                buf->buf_sz = sz;
                buf->buf_start = buf->buf_used = 0;
@@ -18,6 +23,16 @@ buf_t buf_new(size_t sz) {
        return buf;
 }
 
+inline
+void buf_del(buf_t *pbuf) {
+       if (pbuf) {
+               if (*pbuf) {
+                       buf_free_(*pbuf);
+                       *pbuf = NULL;
+               }
+       }
+}
+
 inline
 void buf_rebase(buf_t buf) {
        if (buf->buf_start == 0)
@@ -46,7 +61,7 @@ int buf_makeroom(buf_t *pbuf, size_t roomfor) {
                return 0;
 
        new_sz = (*pbuf)->buf_used + roomfor;
-       tmp_ptr = realloc(*pbuf, new_sz + sizeof **pbuf);
+       tmp_ptr = buf_realloc_(*pbuf, new_sz + sizeof **pbuf);
        if (tmp_ptr == NULL) {
                NOTIFY_ERROR("%s:%s", "realloc", strerror(errno));
                return -1;
@@ -198,8 +213,7 @@ int test_buf_flense_(void *test_arg, void *suite_arg) {
                TEST_INFO("flensed: '%.*s'", (int)dst->buf_used, dst->buf + dst->buf_start);
 
                memset(dst, 0, dst->buf_sz + sizeof *dst);
-               free(dst);
-               dst = NULL;
+               buf_del(&dst);
 
                next_result++;
        }
@@ -216,8 +230,7 @@ int test_buf_flense_(void *test_arg, void *suite_arg) {
        TEST_INFO("remains: '%.*s'", (int)src->buf_used, src->buf + src->buf_start);
 
        memset(src, 0, src->buf_sz + sizeof *src);
-       free(src);
-       src = NULL;
+       buf_del(&src);
 
        return retval;
 }