Moved the allocation functions to sharedbits/. Not fully working yet...

This commit is contained in:
Pietro Gagliardi 2019-05-30 23:11:08 -04:00
parent dc8620f9a8
commit 0f4602ee0f
8 changed files with 74 additions and 56 deletions

View File

@ -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)
{

View File

@ -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, ...);

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)
{

View File

@ -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);