changeset 2:9c1cecba517c

Make arrays crash on malloc failure.
author David A. Holland
date Sun, 19 Dec 2010 16:51:21 -0500
parents 411b28d78483
children bfa97d43197e
files array.c array.h
diffstat 2 files changed, 24 insertions(+), 37 deletions(-) [+]
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
--- a/array.h	Sun Dec 19 16:47:59 2010 -0500
+++ b/array.h	Sun Dec 19 16:51:21 2010 -0500
@@ -30,6 +30,8 @@
 #ifndef ARRAY_H
 #define ARRAY_H
 
+#include "utils.h"
+
 #define ARRAYS_CHECKED
 
 #ifdef ARRAYS_CHECKED
@@ -54,9 +56,9 @@
 unsigned array_num(const struct array *);
 void *array_get(const struct array *, unsigned index_);
 void array_set(const struct array *, unsigned index_, void *val);
-int array_setsize(struct array *, unsigned num);
-int array_add(struct array *, void *val, unsigned *index_ret);
-int array_insert(struct array *a, unsigned index_);
+void array_setsize(struct array *, unsigned num);
+void array_add(struct array *, void *val, unsigned *index_ret);
+void array_insert(struct array *a, unsigned index_);
 void array_remove(struct array *a, unsigned index_);
 
 ////////////////////////////////////////////////////////////
@@ -86,18 +88,15 @@
 	a->v[index_] = val;
 }
 
-ARRAYINLINE int
+ARRAYINLINE void
 array_add(struct array *a, void *val, unsigned *index_ret)
 {
 	unsigned index_ = a->num;
-	if (array_setsize(a, index_+1)) {
-		return -1;
-	}
+	array_setsize(a, index_+1);
 	a->v[index_] = val;
 	if (index_ret != NULL) {
 		*index_ret = index_;
 	}
-	return 0;
 }
 
 ////////////////////////////////////////////////////////////
@@ -146,9 +145,9 @@
 	unsigned ARRAY##_num(const struct ARRAY *a);		\
 	T *ARRAY##_get(const struct ARRAY *a, unsigned index_);	\
 	void ARRAY##_set(struct ARRAY *a, unsigned index_, T *val); \
-	int ARRAY##_setsize(struct ARRAY *a, unsigned num);	\
-	int ARRAY##_add(struct ARRAY *a, T *val, unsigned *index_ret); \
-	int ARRAY##_insert(struct ARRAY *a, unsigned index_);	\
+	void ARRAY##_setsize(struct ARRAY *a, unsigned num);	\
+	void ARRAY##_add(struct ARRAY *a, T *val, unsigned *index_ret); \
+	void ARRAY##_insert(struct ARRAY *a, unsigned index_);	\
 	void ARRAY##_remove(struct ARRAY *a, unsigned index_)
 
 
@@ -170,10 +169,7 @@
 	{							\
 		struct ARRAY *a;				\
 								\
-		a  = malloc(sizeof(*a));			\
-		if (a == NULL) {				\
-			return NULL;				\
-		}						\
+		a = domalloc(sizeof(*a));			\
 		ARRAY##_init(a);				\
 		return a;					\
 	}							\
@@ -203,28 +199,28 @@
 		array_set(&a->arr, index_, (void *)val);	\
 	}							\
 								\
-	INLINE int						\
+	INLINE void						\
 	ARRAY##_setsize(struct ARRAY *a, unsigned num)		\
 	{				 			\
-		return array_setsize(&a->arr, num);		\
+		array_setsize(&a->arr, num);			\
 	}							\
 								\
-	INLINE int						\
+	INLINE void						\
 	ARRAY##_add(struct ARRAY *a, T *val, unsigned *ret)	\
 	{				 			\
-		return array_add(&a->arr, (void *)val, ret);	\
+		array_add(&a->arr, (void *)val, ret);		\
 	}							\
 								\
-	INLINE int						\
+	INLINE void						\
 	ARRAY##_insert(struct ARRAY *a, unsigned index_)	\
 	{				 			\
-		return array_insert(&a->arr, index_);		\
+		array_insert(&a->arr, index_);			\
 	}							\
 								\
 	INLINE void						\
 	ARRAY##_remove(struct ARRAY *a, unsigned index_)	\
 	{				 			\
-		return array_remove(&a->arr, index_);		\
+		array_remove(&a->arr, index_);			\
 	}
 
 #define DECLARRAY(T) DECLARRAY_BYTYPE(T##array, struct T)