lib/memalign: remove smalloc()/sfree() dependency
authorTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Sun, 5 Jan 2020 15:01:10 +0000 (00:01 +0900)
committerTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Sun, 5 Jan 2020 15:01:19 +0000 (00:01 +0900)
fio_memalign()/fio_memfree() implementation shouldn't depend on
smalloc()/sfree() which has dependency on fio code itself.
e.g. This forces unittest code to prepare stab functions for
smalloc()/sfree().

This smalloc()/sfree() dependency was added by 3114b675fd
("fio: enable cross-thread overlap checking with processes").

Rename fio_memalign()/fio_memfree() to __fio_memalign()/__fio_memfree()
and take a function pointer instead of a boolean flag.
Add fio_memalign()/fio_memfree() as an inlined wrapper for
__fio_memalign()/__fio_memfree() without API change.
The only real change here is lib/memalign functions got renamed.

Signed-off-by: Tomohiro Kusumi <kusumi.tomohiro@gmail.com>
fio.h
lib/memalign.c
lib/memalign.h
t/dedupe.c
unittests/lib/memalign.c
unittests/unittest.c
unittests/unittest.h

diff --git a/fio.h b/fio.h
index e943ad165ba01c2d4d8132009f33af5efe72da8f..6a5ead4d453dfa288dffdece1b5191afb8d9ac73 100644 (file)
--- a/fio.h
+++ b/fio.h
@@ -36,6 +36,8 @@
 #include "lib/rand.h"
 #include "lib/rbtree.h"
 #include "lib/num2str.h"
+#include "lib/memalign.h"
+#include "smalloc.h"
 #include "client.h"
 #include "server.h"
 #include "stat.h"
@@ -856,4 +858,14 @@ extern void check_trigger_file(void);
 extern bool in_flight_overlap(struct io_u_queue *q, struct io_u *io_u);
 extern pthread_mutex_t overlap_check;
 
+static inline void *fio_memalign(size_t alignment, size_t size, bool shared)
+{
+       return __fio_memalign(alignment, size, shared ? smalloc : malloc);
+}
+
+static inline void fio_memfree(void *ptr, size_t size, bool shared)
+{
+       return __fio_memfree(ptr, size, shared ? sfree : free);
+}
+
 #endif
index 537bb9fb1b92930e862426156f7b7b46c4cee8f9..214a66faca0b3359d8c8ce0050bd173d3de40ad0 100644 (file)
@@ -11,18 +11,14 @@ struct align_footer {
        unsigned int offset;
 };
 
-void *fio_memalign(size_t alignment, size_t size, bool shared)
+void *__fio_memalign(size_t alignment, size_t size, malloc_fn fn)
 {
        struct align_footer *f;
        void *ptr, *ret = NULL;
 
        assert(!(alignment & (alignment - 1)));
 
-       if (shared)
-               ptr = smalloc(size + alignment + sizeof(*f) - 1);
-       else
-               ptr = malloc(size + alignment + sizeof(*f) - 1);
-
+       ptr = fn(size + alignment + sizeof(*f) - 1);
        if (ptr) {
                ret = PTR_ALIGN(ptr, alignment - 1);
                f = ret + size;
@@ -32,12 +28,9 @@ void *fio_memalign(size_t alignment, size_t size, bool shared)
        return ret;
 }
 
-void fio_memfree(void *ptr, size_t size, bool shared)
+void __fio_memfree(void *ptr, size_t size, free_fn fn)
 {
        struct align_footer *f = ptr + size;
 
-       if (shared)
-               sfree(ptr - f->offset);
-       else
-               free(ptr - f->offset);
+       fn(ptr - f->offset);
 }
index d70308705b40872b4b9a920c369dbee09d472a7c..815e3aa28ffb743bdd31180ce65bd8589fd55220 100644 (file)
@@ -4,7 +4,10 @@
 #include <inttypes.h>
 #include <stdbool.h>
 
-extern void *fio_memalign(size_t alignment, size_t size, bool shared);
-extern void fio_memfree(void *ptr, size_t size, bool shared);
+typedef void* (*malloc_fn)(size_t);
+typedef void (*free_fn)(void*);
+
+extern void *__fio_memalign(size_t alignment, size_t size, malloc_fn fn);
+extern void __fio_memfree(void *ptr, size_t size, free_fn fn);
 
 #endif
index 2ef8dc539809884cc0372597ef1a260385ee9165..68d31f19bd7b6a028975ece96918e6f9a014c8c0 100644 (file)
 #include <unistd.h>
 #include <sys/stat.h>
 
+#include "../fio.h"
 #include "../flist.h"
 #include "../log.h"
 #include "../fio_sem.h"
 #include "../smalloc.h"
 #include "../minmax.h"
 #include "../crc/md5.h"
-#include "../lib/memalign.h"
 #include "../os/os.h"
 #include "../gettime.h"
 #include "../fio_time.h"
index 854c2744bf64d3d082f010a21ec264974e9ae5cc..42a2e31ab863cfd41a243147d0518ad2701e5222 100644 (file)
@@ -1,3 +1,4 @@
+#include <stdlib.h>
 #include "../unittest.h"
 
 #include "../../lib/memalign.h"
@@ -5,7 +6,7 @@
 static void test_memalign_1(void)
 {
        size_t align = 4096;
-       void *p = fio_memalign(align, 1234, false);
+       void *p = __fio_memalign(align, 1234, malloc);
 
        if (p)
                CU_ASSERT_EQUAL(((int)(uintptr_t)p) & (align - 1), 0);
index 66789e4fcc2f0c6f6c9cbc35f51a46e82f86353d..c37e1971a0518544c2cdc136b5e276f387b16d4e 100644 (file)
@@ -8,17 +8,6 @@
 
 #include "./unittest.h"
 
-/* XXX workaround lib/memalign.c's dependency on smalloc.c */
-void *smalloc(size_t size)
-{
-       return malloc(size);
-}
-
-void sfree(void *ptr)
-{
-       free(ptr);
-}
-
 CU_ErrorCode fio_unittest_add_suite(const char *name, CU_InitializeFunc initfn,
        CU_CleanupFunc cleanfn, struct fio_unittest_entry *tvec)
 {
index bbc49613591156c88e974a66ec605d1670c08631..786c1c97ec8f149b8b43c4cd7b6dbe017966230c 100644 (file)
@@ -11,10 +11,6 @@ struct fio_unittest_entry {
        CU_TestFunc fn;
 };
 
-/* XXX workaround lib/memalign.c's dependency on smalloc.c */
-void *smalloc(size_t);
-void sfree(void*);
-
 CU_ErrorCode fio_unittest_add_suite(const char*, CU_InitializeFunc,
        CU_CleanupFunc, struct fio_unittest_entry*);