commit 8b1f720b6d74b526f80de75166593945a5c202cf
parent 11753aab5287d0541bd0f12b2df4355b037284fa
Author: glenda <glenda@9front.local>
Date: Fri, 13 Aug 2021 00:08:01 +0000
add qlocking to array
Diffstat:
2 files changed, 17 insertions(+), 6 deletions(-)
diff --git a/array.c b/array.c
@@ -8,11 +8,14 @@ arraycreate(usize size, long n, void (*free)(void *))
{
Array *ap;
ap = mallocz(sizeof(Array), 1);
- ap->size = size;
- ap->count = 0;
- ap->n = n;
- ap->mem = mallocz(size * n, 1);
- ap->free = free;
+ *ap = (Array) {
+ mallocz(sizeof(QLock), 1),
+ size,
+ n,
+ 0,
+ mallocz(size * n, 1),
+ free,
+ };
return ap;
}
@@ -20,6 +23,7 @@ void
arrayfree(Array *ap)
{
int i;
+ qlock(ap->l);
if (ap->free != nil) {
for (i = 0; i < ap->count; i ++) {
void **v;
@@ -27,28 +31,34 @@ arrayfree(Array *ap)
ap->free(*v);
}
}
+ qunlock(ap->l);
free(ap);
}
void *
arrayadd(Array *ap)
{
+ qlock(ap->l);
ap->count++;
if (ap->count > ap->n) {
ap->n += ap->n;
ap->mem = realloc(ap->mem, ap->size * ap->n);
}
- return memset(arrayget(ap, ap->count - 1), 0, ap->size);
+ memset(arrayget(ap, ap->count - 1), 0, ap->size);
+ qunlock(ap->l);
+ return (void *)(ap->mem + ap->size * (ap->count - 1));
}
void
arraydel(Array *ap, long n)
{
char *v;
+ qlock(ap->l);
v = ap->mem + ap->size * n;
if (ap->free != nil) ap->free(v);
memcpy(v, v + ap->size, (ap->count - n) * ap->size);
ap->count--;
+ qunlock(ap->l);
}
void *
diff --git a/array.h b/array.h
@@ -1,6 +1,7 @@
typedef struct Array Array;
struct Array {
+ QLock *l;
usize size;
usize n;
long count;