diff array.c @ 2:9c1cecba517c

Make arrays crash on malloc failure.
author David A. Holland
date Sun, 19 Dec 2010 16:51:21 -0500
parents 411b28d78483
children b156910b59b2
line wrap: on
line diff
--- a/array.c	Sun Dec 19 16:47:59 2010 -0500
+++ b/array.c	Sun Dec 19 16:51:21 2010 -0500
@@ -38,10 +38,8 @@
 {
 	struct array *a;
 
-	a = malloc(sizeof(*a));
-	if (a != NULL) {
-		array_init(a);
-	}
+	a = domalloc(sizeof(*a));
+	array_init(a);
 	return a;
 }
 
@@ -69,7 +67,7 @@
 #endif
 }
 
-int
+void
 array_setsize(struct array *a, unsigned num)
 {
 	unsigned newmax;
@@ -80,18 +78,14 @@
 		while (num > newmax) {
 			newmax = newmax ? newmax*2 : 4;
 		}
-		newptr = realloc(a->v, newmax*sizeof(*a->v));
-		if (newptr == NULL) {
-			return -1;
-		}
+		newptr = dorealloc(a->v, newmax*sizeof(*a->v));
 		a->v = newptr;
 		a->max = newmax;
 	}
 	a->num = num;
-	return 0;
 }
 
-int
+void
 array_insert(struct array *a, unsigned index_)
 {
 	unsigned movers;
@@ -101,12 +95,9 @@
 
 	movers = a->num - index_;
 
-	if (array_setsize(a, a->num + 1)) {
-		return -1;
-	}
+	array_setsize(a, a->num + 1);
 
 	memmove(a->v + index_+1, a->v + index_, movers*sizeof(*a->v));
-	return 0;
 }
 
 void