libui/ptrarray.c

50 lines
1.1 KiB
C
Raw Normal View History

// 5 may 2015
#include <string.h>
2015-08-27 11:05:11 -05:00
#include "ui.h"
#include "uipriv.h"
struct ptrArray *newPtrArray(void)
{
return uiNew(struct ptrArray);
}
void ptrArrayDestroy(struct ptrArray *p)
{
2015-05-06 09:14:37 -05:00
if (p->len != 0)
complain("attempt to destroy ptrarray %p while it still has pointers inside", p);
2015-05-08 09:25:58 -05:00
if (p->ptrs != NULL) // array was created but nothing was ever put inside
uiFree(p->ptrs);
uiFree(p);
}
#define grow 32
void ptrArrayAppend(struct ptrArray *p, void *d)
{
2015-05-18 09:32:08 -05:00
ptrArrayInsertAt(p, p->len, d);
}
2015-05-18 09:32:08 -05:00
void ptrArrayInsertAt(struct ptrArray *p, uintmax_t i, void *d)
{
if (i > p->len)
2015-05-18 09:32:08 -05:00
complain("index out of range in ptrArrayInsertAt()");
if (p->len >= p->cap) {
p->cap += grow;
p->ptrs = (void **) uiRealloc(p->ptrs, p->cap * sizeof (void *), "void *[]");
}
// thanks to ValleyBell
memmove(&(p->ptrs[i + 1]), &(p->ptrs[i]), (p->len - i) * sizeof (void *));
p->ptrs[i] = d;
p->len++;
}
void ptrArrayDelete(struct ptrArray *p, uintmax_t i)
{
if (i >= p->len)
complain("index out of range in ptrArrayRemove()");
// thanks to ValleyBell
memmove(&(p->ptrs[i]), &(p->ptrs[i + 1]), (p->len - i - 1) * sizeof (void *));
p->ptrs[p->len - 1] = NULL;
p->len--;
}