summaryrefslogtreecommitdiffstats
path: root/libdimension/dimension/array.h
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-10-26 00:25:21 -0400
committerTavian Barnes <tavianator@gmail.com>2009-10-26 00:25:21 -0400
commitc6612fb215d71ac2bea3b614786cf585cd1a6c74 (patch)
treefa8a4d6eb0bc83ba112b8c1b03b212641669c002 /libdimension/dimension/array.h
parente4e3ea4fd58cbbe462368abde728f9443f01de19 (diff)
downloaddimension-c6612fb215d71ac2bea3b614786cf585cd1a6c74.tar.xz
Clean up some dmnsn_new_*() functions.
Rather than special-case every failed memory allocation, just make dmnsn_delete_*() more robust and call it.
Diffstat (limited to 'libdimension/dimension/array.h')
-rw-r--r--libdimension/dimension/array.h21
1 files changed, 11 insertions, 10 deletions
diff --git a/libdimension/dimension/array.h b/libdimension/dimension/array.h
index d962d42..1b3771d 100644
--- a/libdimension/dimension/array.h
+++ b/libdimension/dimension/array.h
@@ -36,6 +36,16 @@ typedef struct {
size_t obj_size, length, capacity;
} dmnsn_array;
+/* Delete an array */
+DMNSN_INLINE void
+dmnsn_delete_array(dmnsn_array *array)
+{
+ if (array) {
+ free(array->ptr);
+ free(array);
+ }
+}
+
/* Array allocation never returns NULL - if dmnsn_new_array returns, it
succeeded */
DMNSN_INLINE dmnsn_array *
@@ -50,6 +60,7 @@ dmnsn_new_array(size_t obj_size)
/* Allocate the memory */
array->ptr = malloc(array->capacity*array->obj_size);
if (!array->ptr) {
+ dmnsn_delete_array(array);
dmnsn_error(DMNSN_SEVERITY_HIGH, "Array allocation failed.");
}
} else {
@@ -59,16 +70,6 @@ dmnsn_new_array(size_t obj_size)
return array;
}
-/* Delete the array */
-DMNSN_INLINE void
-dmnsn_delete_array(dmnsn_array *array)
-{
- if (array) {
- free(array->ptr);
- free(array);
- }
-}
-
/* Get the size of the array */
DMNSN_INLINE size_t
dmnsn_array_size(const dmnsn_array *array)