Wrote array_header.h. Now for array_impl.h.

This commit is contained in:
Pietro Gagliardi 2019-05-30 23:47:07 -04:00
parent 2dd68fc47f
commit dca2e5f038
4 changed files with 50 additions and 47 deletions

View File

@ -30,31 +30,11 @@ extern bool uiprivSysCheckThread(void);
// alloc.c
#define sharedbitsPrefix uipriv
#include "../sharedbits/alloc_header.h"
typedef struct uiprivArray uiprivArray;
struct uiprivArray {
void *buf;
size_t len;
size_t cap;
size_t elemsize;
size_t nGrow;
const char *what;
};
#include "../sharedbits/array_header.h"
#define uiprivArrayStaticInit(T, grow, whatstr) { NULL, 0, 0, sizeof (T), grow, whatstr }
#define uiprivArrayInit(arr, T, grow, whatstr) \
memset(&(arr), 0, sizeof (uiprivArray)); \
arr.elemsize = sizeof (T); \
arr.nGrow = grow; \
arr.what = whatstr;
#define uiprivArrayFree(arr) \
uiprivFree(arr.buf); \
memset(&arr, 0, sizeof (uiprivArray));
#define uiprivArrayInit(arr, T, nGrow, what) uiprivArrayInitFull(&(arr), sizeof (T), nGrow, what)
#define uiprivArrayFree(arr) uiprivArrayFreeFull(&(arr))
#define uiprivArrayAt(arr, T, n) (((T *) (arr.buf)) + (n))
extern void *uiprivArrayAppend(uiprivArray *arr, size_t n);
extern void *uiprivArrayInsertAt(uiprivArray *arr, size_t pos, size_t n);
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

View File

@ -3,4 +3,33 @@
#include "start.h"
typedef struct sharedbitsPrefixName(Array) sharedbitsPrefixName(Array);
struct sharedbitsPrefixName(Array) {
void *buf;
size_t len;
size_t cap;
size_t elemsize;
size_t nGrow;
const char *what;
};
extern void sharedbitsPrefixName(ArrayInitFull)(sharedbitsPrefixName(Array) *arr, size_t elemsize, size_t nGrow, const char *what);
extern void sharedbitsPrefixName(ArrayFreeFull)(sharedbitsPrefixName(Array) *arr);
extern void *sharedbitsPrefixName(ArrayAppend)(sharedbitsPrefixName(Array) *arr, size_t n);
extern void *sharedbitsPrefixName(ArrayInsertAt)(sharedbitsPrefixName(Array) *arr, size_t pos, size_t n);
extern void sharedbitsPrefixName(ArrayDelete)(sharedbitsPrefixName(Array) *arr, size_t pos, size_t n);
extern void sharedbitsPrefixName(ArrayDeleteItem)(sharedbitsPrefixName(Array) *arr, void *p, size_t n);
extern void *sharedbitsPrefixName(ArrayBsearch)(const sharedbitsPrefixName(Array) *arr, const void *key, int (*compare)(const void *, const void *));
extern void sharedbitsPrefixName(ArrayQsort)(sharedbitsPrefixName(Array) *arr, int (*compare)(const void *, const void *));
/*
you may also want to define the following:
#define sharedbitsPrefixName(StaticInit)(T, nGrow, what) { NULL, 0, 0, sizeof (T), nGrow, what }
#define sharedbitsPrefixName(Init)(arr, T, nGrow, what) sharedbitsPrefixName(InitFull)(&(arr), sizeof (T), nGrow, what)
#define sharedbitsPrefixName(ArrayFree)(arr) sharedbitsPrefixName(ArrayFreeFull)(&(arr))
#define sharedbitsPrefixName(ArrayAt)(arr, T, n) (((T *) (arr.buf)) + (n))
*/
#include "end.h"

View File

@ -3,4 +3,18 @@
#include "start.h"
void sharedbitsPrefixName(ArrayInitFull)(sharedbitsPrefixName(Array) *arr, size_t elemsize, size_t nGrow, const char *what)
{
memset(arr, 0, sizeof (sharedbitsPrefixName(Array)));
arr->elemsize = elemsize;
arr->nGrow = nGrow;
arr->what = what;
}
void sharedbitsPrefixName(ArrayFreeFull)(sharedbitsPrefixName(Array) *arr)
{
sharedbitsPrefixName(Free)(arr->buf);
memset(arr, 0, sizeof (sharedbitsPrefixName(Array)));
}
#include "end.h"

View File

@ -9,31 +9,11 @@ extern void testingprivInternalError(const char *fmt, ...);
#define testingprivNewArray(T, n) ((T *) testingprivAlloc(n * sizeof (T), #T "[]"))
#define testingprivResizeArray(x, T, old, new) ((T *) testingprivRealloc(x, old * sizeof (T), new * sizeof (T), #T "[]"))
typedef struct testingprivArray testingprivArray;
struct testingprivArray {
void *buf;
size_t len;
size_t cap;
size_t elemsize;
size_t nGrow;
const char *what;
};
#define testingprivArrayStaticInit(T, grow, whatstr) { NULL, 0, 0, sizeof (T), grow, whatstr }
#define testingprivArrayInit(arr, T, grow, whatstr) \
memset(&(arr), 0, sizeof (testingprivArray)); \
arr.elemsize = sizeof (T); \
arr.nGrow = grow; \
arr.what = whatstr;
#define testingprivArrayFree(arr) \
testingprivFree(arr.buf); \
memset(&arr, 0, sizeof (testingprivArray));
#include "../../sharedbits/array_header.h"
#define testingprivArrayStaticInit(T, nGrow, what) { NULL, 0, 0, sizeof (T), nGrow, what }
#define testingprivArrayInit(arr, T, nGrow, what) testingprivArrayInitFull(&(arr), sizeof (T), nGrow, what)
#define testingprivArrayFree(arr) testingprivArrayFreeFull(&(arr))
#define testingprivArrayAt(arr, T, n) (((T *) (arr.buf)) + (n))
extern void *testingprivArrayAppend(testingprivArray *arr, size_t n);
extern void *testingprivArrayInsertAt(testingprivArray *arr, size_t pos, size_t n);
extern void testingprivArrayDelete(testingprivArray *arr, size_t pos, size_t n);
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