From 0f4602ee0f4eab6456b2c252a0ec27bd804db7bd Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Thu, 30 May 2019 23:11:08 -0400 Subject: [PATCH] Moved the allocation functions to sharedbits/. Not fully working yet... --- common/alloc.c | 28 +++------------------------- common/uipriv.h | 6 +++--- sharedbits/alloc_header.h | 17 +++++++++++++++++ sharedbits/alloc_impl.h | 33 +++++++++++++++++++++++++++++++++ sharedbits/array_header.h | 5 +++++ sharedbits/array_impl.h | 5 +++++ test/lib/testingpriv.c | 28 +++------------------------- test/lib/testingpriv.h | 8 +++++--- 8 files changed, 74 insertions(+), 56 deletions(-) diff --git a/common/alloc.c b/common/alloc.c index 5fca95f5..39e3aade 100644 --- a/common/alloc.c +++ b/common/alloc.c @@ -4,31 +4,9 @@ #include "ui.h" #include "uipriv.h" -void *uiprivAlloc(size_t n, const char *what) -{ - void *p; - - p = malloc(n); - if (p == NULL) - uiprivInternalError("memory exhausted allocating %s", what); - memset(p, 0, n); - return p; -} - -void *uiprivRealloc(void *p, size_t nOld, size_t nNew, const char *what) -{ - p = realloc(p, nNew); - if (p == NULL) - uiprivInternalError("memory exhausted reallocating %s", what); - if (nNew > nOld) - memset(((uint8_t *) p) + nOld, 0, nNew - nOld); - return p; -} - -void uiprivFree(void *p) -{ - free(p); -} +#define sharedbitsPrefix uipriv +#include "../sharedbits/alloc_impl.h" +#undef sharedbitsPrefix void *uiprivArrayAppend(uiprivArray *arr, size_t n) { diff --git a/common/uipriv.h b/common/uipriv.h index 0b84a74a..629366e5 100644 --- a/common/uipriv.h +++ b/common/uipriv.h @@ -28,9 +28,8 @@ extern bool uiprivCheckInitializedAndThreadImpl(const char *func); extern bool uiprivSysCheckThread(void); // alloc.c -extern void *uiprivAlloc(size_t n, const char *what); -extern void *uiprivRealloc(void *p, size_t nOld, size_t nNew, const char *what); -extern void uiprivFree(void *p); +#define sharedbitsPrefix uipriv +#include "../sharedbits/alloc_header.h" typedef struct uiprivArray uiprivArray; struct uiprivArray { void *buf; @@ -56,6 +55,7 @@ extern void uiprivArrayDelete(uiprivArray *arr, size_t pos, size_t n); extern void uiprivArrayDeleteItem(uiprivArray *arr, void *p, size_t n); extern void *uiprivArrayBsearch(const uiprivArray *arr, const void *key, int (*compare)(const void *, const void *)); extern void uiprivArrayQsort(uiprivArray *arr, int (*compare)(const void *, const void *)); +#undef sharedbitsPrefix // errors.c extern void uiprivInternalError(const char *fmt, ...); diff --git a/sharedbits/alloc_header.h b/sharedbits/alloc_header.h index 83b24946..2a6a414a 100644 --- a/sharedbits/alloc_header.h +++ b/sharedbits/alloc_header.h @@ -3,3 +3,20 @@ #ifndef sharedbitsPrefix #error you must define sharedbitsPrefix before including this #endif +#define sharedbitsPrefixMakeName(x, y) x ## y +#define sharedbitsPrefixName(Name) sharedbitsPrefixMakeName(sharedbitsPrefix, Name) + +extern void *sharedbitsPrefixName(Alloc)(size_t n, const char *what); +extern void *sharedbitsPrefixName(Realloc)(void *p, size_t nOld, size_t nNew, const char *what); +extern void sharedbitsPrefixName(Free)(void *p); + +/* +you may also want to define the following: + +#define sharedbitsPrefixName(New)(T) ((T *) sharedbitsPrefix ## Alloc(sizeof (T), #T)) +#define sharedbitsPrefixName(NewArray)(T, n) ((T *) sharedbitsPrefix ## Alloc(n * sizeof (T), #T "[]")) +#define sharedbitsPrefixName(ResizeArray)(x, T, old, new) ((T *) sharedbitsPrefix ## Realloc(x, old * sizeof (T), new * sizeof (T), #T "[]")) +*/ + +#undef sharedbitsPrefixName +#undef sharedbitsPrefixMakeName diff --git a/sharedbits/alloc_impl.h b/sharedbits/alloc_impl.h index bd7318d9..e6c194e6 100644 --- a/sharedbits/alloc_impl.h +++ b/sharedbits/alloc_impl.h @@ -4,3 +4,36 @@ #ifndef sharedbitsPrefix #error you must define sharedbitsPrefix before including this #endif +#define sharedbitsPrefixMakeName(x, y) x ## y +#define sharedbitsPrefixName(Name) sharedbitsPrefixMakeName(sharedbitsPrefix, Name) + +extern void sharedbitsPrefixName(InternalError)(const char *fmt, ...); + +void *sharedbitsPrefixName(Alloc)(size_t n, const char *what) +{ + void *p; + + p = malloc(n); + if (p == NULL) + sharedbitsPrefixName(InternalError)("memory exhausted allocating %s", what); + memset(p, 0, n); + return p; +} + +void *sharedbitsPrefixName(Realloc)(void *p, size_t nOld, size_t nNew, const char *what) +{ + p = realloc(p, nNew); + if (p == NULL) + sharedbitsPrefixName(InternalError)("memory exhausted reallocating %s", what); + if (nNew > nOld) + memset(((uint8_t *) p) + nOld, 0, nNew - nOld); + return p; +} + +void sharedbitsPrefixName(Free)(void *p) +{ + free(p); +} + +#undef sharedbitsPrefixName +#undef sharedbitsPrefixMakeName diff --git a/sharedbits/array_header.h b/sharedbits/array_header.h index bd7318d9..db2ad9f4 100644 --- a/sharedbits/array_header.h +++ b/sharedbits/array_header.h @@ -4,3 +4,8 @@ #ifndef sharedbitsPrefix #error you must define sharedbitsPrefix before including this #endif +#define sharedbitsPrefixMakeName(x, y) x ## y +#define sharedbitsPrefixName(Name) sharedbitsPrefixMakeName(sharedbitsPrefix, Name) + +#undef sharedbitsPrefixName +#undef sharedbitsPrefixMakeName diff --git a/sharedbits/array_impl.h b/sharedbits/array_impl.h index e485d5a4..1eda47af 100644 --- a/sharedbits/array_impl.h +++ b/sharedbits/array_impl.h @@ -4,3 +4,8 @@ #ifndef sharedbitsPrefix #error you must define sharedbitsPrefix before including this #endif +#define sharedbitsPrefixMakeName(x, y) x ## y +#define sharedbitsPrefixName(Name) sharedbitsPrefixMakeName(sharedbitsPrefix, Name) + +#undef sharedbitsPrefixName +#undef sharedbitsPrefixMakeName diff --git a/test/lib/testingpriv.c b/test/lib/testingpriv.c index 70797e68..770a6e6b 100644 --- a/test/lib/testingpriv.c +++ b/test/lib/testingpriv.c @@ -21,31 +21,9 @@ void testingprivInternalError(const char *fmt, ...) abort(); } -void *testingprivAlloc(size_t n, const char *what) -{ - void *p; - - p = malloc(n); - if (p == NULL) - testingprivInternalError("memory exhausted allocating %s", what); - memset(p, 0, n); - return p; -} - -void *testingprivRealloc(void *p, size_t nOld, size_t nNew, const char *what) -{ - p = realloc(p, nNew); - if (p == NULL) - testingprivInternalError("memory exhausted reallocating %s", what); - if (nNew > nOld) - memset(((uint8_t *) p) + nOld, 0, nNew - nOld); - return p; -} - -void testingprivFree(void *p) -{ - free(p); -} +#define sharedbitsPrefix testingpriv +#include "../../sharedbits/alloc_impl.h" +#undef sharedbitsPrefix void *testingprivArrayAppend(testingprivArray *arr, size_t n) { diff --git a/test/lib/testingpriv.h b/test/lib/testingpriv.h index b3a78d76..c8f655d0 100644 --- a/test/lib/testingpriv.h +++ b/test/lib/testingpriv.h @@ -2,12 +2,12 @@ extern void testingprivInternalError(const char *fmt, ...); -extern void *testingprivAlloc(size_t n, const char *what); +#define sharedbitsPrefix testingpriv + +#include "../../sharedbits/alloc_header.h" #define testingprivNew(T) ((T *) testingprivAlloc(sizeof (T), #T)) #define testingprivNewArray(T, n) ((T *) testingprivAlloc(n * sizeof (T), #T "[]")) -extern void *testingprivRealloc(void *p, size_t nOld, size_t nNew, const char *what); #define testingprivResizeArray(x, T, old, new) ((T *) testingprivRealloc(x, old * sizeof (T), new * sizeof (T), #T "[]")) -extern void testingprivFree(void *p); typedef struct testingprivArray testingprivArray; struct testingprivArray { @@ -35,6 +35,8 @@ extern void testingprivArrayDeleteItem(testingprivArray *arr, void *p, size_t n) extern void *testingprivArrayBsearch(const testingprivArray *arr, const void *key, int (*compare)(const void *, const void *)); extern void testingprivArrayQsort(testingprivArray *arr, int (*compare)(const void *, const void *)); +#undef sharedbitsPrefix + extern int testingprivVsnprintf(char *s, size_t n, const char *format, va_list ap); extern int testingprivSnprintf(char *s, size_t n, const char *format, ...); extern char *testingprivStrdup(const char *s);