summaryrefslogtreecommitdiffstats
path: root/libdimension
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2010-04-29 16:53:52 -0600
committerTavian Barnes <tavianator@gmail.com>2010-04-30 09:52:34 -0600
commit5fa471d6842b46ca7c20ebc454065cdaf7488bdb (patch)
tree7a56e30232165fbffd8bbf993373548263205f27 /libdimension
parentaeaa9a4e80244a5c66b1afe7e6ee1bcf48e70766 (diff)
downloaddimension-5fa471d6842b46ca7c20ebc454065cdaf7488bdb.tar.xz
Use dmnsn_assert() for array and list range checks.
Diffstat (limited to 'libdimension')
-rw-r--r--libdimension/dimension/array.h24
-rw-r--r--libdimension/dimension/error.h10
-rw-r--r--libdimension/dimension/list.h22
-rw-r--r--libdimension/inlines.c1
4 files changed, 19 insertions, 38 deletions
diff --git a/libdimension/dimension/array.h b/libdimension/dimension/array.h
index e383ece..e3dc7b4 100644
--- a/libdimension/dimension/array.h
+++ b/libdimension/dimension/array.h
@@ -84,10 +84,7 @@ dmnsn_array_resize(dmnsn_array *array, size_t length)
DMNSN_INLINE void
dmnsn_array_get(const dmnsn_array *array, size_t i, void *obj)
{
- if (i >= dmnsn_array_size(array)) {
- /* Range check failed */
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Array index out of bounds.");
- }
+ dmnsn_assert(i < dmnsn_array_size(array), "Array index out of bounds.");
memcpy(obj, (char *)array->ptr + array->obj_size*i, array->obj_size);
}
@@ -107,10 +104,7 @@ dmnsn_array_set(dmnsn_array *array, size_t i, const void *obj)
DMNSN_INLINE void *
dmnsn_array_at(const dmnsn_array *array, size_t i)
{
- if (i >= dmnsn_array_size(array)) {
- /* Resize if i is out of range */
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Array index out of bounds.");
- }
+ dmnsn_assert(i < dmnsn_array_size(array), "Array index out of bounds.");
return (char *)array->ptr + array->obj_size*i;
}
@@ -126,11 +120,7 @@ DMNSN_INLINE void
dmnsn_array_pop(dmnsn_array *array, void *obj)
{
size_t size = dmnsn_array_size(array);
- if (size <= 0) {
- /* Range check failed */
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Array is empty.");
- }
-
+ dmnsn_assert(size > 0, "Array is empty.");
dmnsn_array_get(array, size - 1, obj); /* Copy the object */
dmnsn_array_resize(array, size - 1); /* Shrink the array */
}
@@ -157,15 +147,11 @@ DMNSN_INLINE void
dmnsn_array_remove(dmnsn_array *array, size_t i)
{
size_t size = dmnsn_array_size(array);
- if (i >= size) {
- /* Range check failed */
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Array index out of bounds.");
- }
-
+ dmnsn_assert(i < size, "Array index out of bounds.");
/* Move the array elements after `i' 1 to the left */
memmove((char *)array->ptr + array->obj_size*i,
(char *)array->ptr + array->obj_size*(i + 1),
- array->obj_size*(size - i));
+ array->obj_size*(size - i - 1));
/* Decrease the size by 1 */
dmnsn_array_resize(array, size - 1);
}
diff --git a/libdimension/dimension/error.h b/libdimension/dimension/error.h
index 37b8d66..7abfb90 100644
--- a/libdimension/dimension/error.h
+++ b/libdimension/dimension/error.h
@@ -52,10 +52,12 @@ typedef enum {
#ifdef NDEBUG
#define dmnsn_assert(expr, str) ((void)0)
#else
- #define dmnsn_assert(expr, str) \
- if (!(expr)) { \
- dmnsn_error(DMNSN_SEVERITY_HIGH, (str)); \
- }
+ #define dmnsn_assert(expr, str) \
+ do { \
+ if (!(expr)) { \
+ dmnsn_error(DMNSN_SEVERITY_HIGH, (str)); \
+ } \
+ } while (0)
#endif
/* Called by dmnsn_error() - don't call directly */
diff --git a/libdimension/dimension/list.h b/libdimension/dimension/list.h
index deb7622..5c7d700 100644
--- a/libdimension/dimension/list.h
+++ b/libdimension/dimension/list.h
@@ -95,16 +95,14 @@ dmnsn_list_last(dmnsn_list *list)
DMNSN_INLINE dmnsn_list_iterator *
dmnsn_list_prev(dmnsn_list_iterator *i)
{
- if (!i)
- dmnsn_error(DMNSN_SEVERITY_HIGH, "NULL list iterator.");
+ dmnsn_assert(i, "NULL list iterator.");
return i->prev;
}
DMNSN_INLINE dmnsn_list_iterator *
dmnsn_list_next(dmnsn_list_iterator *i)
{
- if (!i)
- dmnsn_error(DMNSN_SEVERITY_HIGH, "NULL list iterator.");
+ dmnsn_assert(i, "NULL list iterator.");
return i->next;
}
@@ -118,8 +116,7 @@ dmnsn_list_size(dmnsn_list *list)
DMNSN_INLINE void
dmnsn_list_get(const dmnsn_list_iterator *i, void *obj)
{
- if (!i)
- dmnsn_error(DMNSN_SEVERITY_HIGH, "NULL list iterator.");
+ dmnsn_assert(i, "NULL list iterator.");
memcpy(obj, i->ptr, i->obj_size);
}
@@ -127,8 +124,7 @@ dmnsn_list_get(const dmnsn_list_iterator *i, void *obj)
DMNSN_INLINE void
dmnsn_list_set(dmnsn_list_iterator *i, const void *obj)
{
- if (!i)
- dmnsn_error(DMNSN_SEVERITY_HIGH, "NULL list iterator.");
+ dmnsn_assert(i, "NULL list iterator.");
memcpy(i->ptr, obj, i->obj_size);
}
@@ -136,9 +132,8 @@ dmnsn_list_set(dmnsn_list_iterator *i, const void *obj)
DMNSN_INLINE void
dmnsn_list_insert(dmnsn_list *list, dmnsn_list_iterator *i, const void *obj)
{
- if (!i)
- dmnsn_error(DMNSN_SEVERITY_HIGH, "NULL list iterator.");
dmnsn_list_iterator *j = dmnsn_new_list_iterator(obj, list->obj_size);
+ dmnsn_assert(i, "NULL list iterator.");
if (list->first == i)
list->first = j;
j->next = i;
@@ -151,8 +146,7 @@ dmnsn_list_insert(dmnsn_list *list, dmnsn_list_iterator *i, const void *obj)
DMNSN_INLINE void
dmnsn_list_remove(dmnsn_list *list, dmnsn_list_iterator *i)
{
- if (!i)
- dmnsn_error(DMNSN_SEVERITY_HIGH, "NULL list iterator.");
+ dmnsn_assert(i, "NULL list iterator.");
if (list->first == i)
list->first = i->next;
if (list->last == i)
@@ -184,9 +178,7 @@ dmnsn_list_push(dmnsn_list *list, const void *obj)
DMNSN_INLINE void
dmnsn_list_pop(dmnsn_list *list, void *obj)
{
- if (!list->last) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "List is empty.");
- }
+ dmnsn_assert(list->last, "List is empty.");
dmnsn_list_get(list->last, obj);
dmnsn_list_remove(list, list->last);
}
diff --git a/libdimension/inlines.c b/libdimension/inlines.c
index fd0ae66..6103655 100644
--- a/libdimension/inlines.c
+++ b/libdimension/inlines.c
@@ -35,4 +35,5 @@
#define DMNSN_INLINE static
#endif
+#undef NDEBUG
#include "dimension.h"