commit 2e72151adae06ba0412c172ec86d4fe4056d2353
parent 2741d8f7776d78de9fe9ffc08d26bba855e09886
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Wed, 18 Aug 2021 22:04:21 +0000
array: set errstr
Diffstat:
2 files changed, 12 insertions(+), 7 deletions(-)
diff --git a/array.c b/array.c
@@ -39,7 +39,10 @@ void *
arraygrow(Array *ap, long n)
{
void *v;
- if (n < 0) return nil;
+ if (n < 0) {
+ werrstr("arraygrow: negative growth size");
+ return nil;
+ }
v = arrayend(ap);
qlock(ap->l);
ap->count += n;
@@ -52,26 +55,28 @@ arraygrow(Array *ap, long n)
return (void *)(ap->p + ap->size * (ap->count - n));
}
-void
+int
arraydel(Array *ap, long n)
{
char *v;
- if ((n < 0) || (n >= ap->count)) return;
+ if ((n < 0) || (n >= ap->count)) {
+ werrstr("arraydel: out of bounds");
+ return -1;
+ }
qlock(ap->l);
v = ap->p + 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);
+ return 0;
}
void *
arrayget(Array *ap, long n)
{
- assert(n >= 0);
- assert(n < ap->count);
if ((n < 0) || (n >= ap->count)) {
- // fprint(2, "arrayget: n out of bonds\n");
+ werrstr("arrayget: out of bounds");
return nil;
}
return (void *)(ap->p + ap->size * n);
diff --git a/array.h b/array.h
@@ -12,6 +12,6 @@ struct Array {
Array * arraycreate(long, long, void (*free)(void *));
void arrayfree(Array *);
void * arraygrow(Array *, long);
-void arraydel(Array *, long);
+int arraydel(Array *, long);
void * arrayget(Array *, long);
void * arrayend(Array *);
\ No newline at end of file