diff --git a/ptrarray.c b/ptrarray.c new file mode 100644 index 00000000..d08cb295 --- /dev/null +++ b/ptrarray.c @@ -0,0 +1,58 @@ +// 5 may 2015 +#include +#include "ui.h" +#include "uipriv.h" + +// TODO see if we can use memmove() + +struct ptrArray newPtrArray(void) +{ + return uiNew(struct ptrArray); +} + +void ptrArrayDestroy(struct ptrArray *p) +{ + // TODO check iif len is nonzero + uiFree(p); +} + +#define grow 32 + +void ptrArrayAppend(struct ptrArray *p, void *d) +{ + p->len++; + if (p->len >= p->cap) { + p->cap += grow; + p->ptrs = (void **) uiRealloc(p->ptrs, p->cap * sizeof (void *)); + } + p->ptrs[p->len - 1] = d; +} + +void ptrArrayInsertBefore(struct ptrArray *p, uintmax_t i, void *d) +{ + uintmax_t j; + + if (i >= p->len) + complain("index out of range in ptrArrayInsertBefore()"); + // TODO does this need to be here + p->len++; + if (p->len >= p->cap) { + p->cap += grow; + p->ptrs = (void **) uiRealloc(p->ptrs, p->cap * sizeof (void *)); + } + for (j = p->len - 1; j >= i; j--) + p->ptrs[j + 1] = p->ptrs[j]; + p->ptrs[i] = d; +} + +void ptrArrayDelete(struct ptrArray *p, uintmax_t i) +{ + uintmax_t j; + + if (i >= p->len) + complain("index out of range in ptrArrayRemove()"); + for (j = i; j < p->len - 1; j++) + p->ptrs[j] = p->ptrs[j + 1]; + p->ptrs[j] = NULL; + p->len--; +} diff --git a/uipriv.h b/uipriv.h index 7d70d381..f37ca174 100644 --- a/uipriv.h +++ b/uipriv.h @@ -14,3 +14,15 @@ extern uiContainer *newBin(void); extern void binSetMainControl(uiContainer *, uiControl *); extern void binSetMargins(uiContainer *, intmax_t, intmax_t, intmax_t, intmax_t); extern void binSetParent(uiContainer *, uintptr_t); + +// array.c +struct ptrArray { + void **ptrs; + uintmax_t len; + uintmax_t cap; +}; +struct ptrArray *newPtrArray(void); +void ptrArrayDestroy(sstruct ptrArray *); +void ptrArrayAppend(struct ptrArray *, void *); +void ptrArrayInsertBefore(struct ptrArray *, uintmax_t, void *); +void ptrArrayDelete(struct ptrArray *, uintmax_t); diff --git a/windows/init.c b/windows/init.c index 3b500233..19e3340c 100644 --- a/windows/init.c +++ b/windows/init.c @@ -110,7 +110,6 @@ void uiFreeInitError(const char *err) uiFree((void *) err); } -// TODO consider DisableThreadLibraryCalls() (will require removing ALL C runtime calls); if we do so we will need to adjust the below signature BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { if (fdwReason == DLL_PROCESS_ATTACH)