summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Weiner <hannes@cmpxchg.org>2009-08-12 02:15:56 +0200
committerJohannes Weiner <hannes@cmpxchg.org>2009-08-12 02:15:56 +0200
commitb137bcf1216d9e2b0bd3bb19f964f891a5d2d057 (patch)
treed0a8bf468b7bc19d03226a66bd672447659a970a
parent016ae2fa28d1e2bfc4b1f569e44759cf28f3e637 (diff)
util: introduce sheep_bug_on()
Instead of spreading assert(), use our own primitive to check for unexpected conditions. They are meant to stay always enabled, also in production releases. And having our own bug spotting infrastructure allows for easy extension later on to dump more information such as the sheep version number etc.
-rw-r--r--include/sheep/util.h9
-rw-r--r--sheep/compile.c5
-rw-r--r--sheep/eval.c13
-rw-r--r--sheep/object.c3
-rw-r--r--sheep/util.c13
5 files changed, 31 insertions, 12 deletions
diff --git a/include/sheep/util.h b/include/sheep/util.h
index 93ca3b1..c195cda 100644
--- a/include/sheep/util.h
+++ b/include/sheep/util.h
@@ -1,6 +1,7 @@
#ifndef _SHEEP_UTIL_H
#define _SHEEP_UTIL_H
+#include <stdarg.h>
#include <stddef.h>
void *sheep_malloc(size_t);
@@ -9,4 +10,12 @@ void *sheep_realloc(void *, size_t);
char *sheep_strdup(const char *);
void sheep_free(const void *);
+void sheep_bug(const char *, ...);
+
+#define sheep_bug_on(cond) \
+ do if (cond) \
+ sheep_bug("Unexpected condition `%s' in %s:%d", \
+ #cond, __FILE__, __LINE__); \
+ while (0)
+
#endif /* _SHEEP_UTIL_H */
diff --git a/sheep/compile.c b/sheep/compile.c
index 6a84dbe..7a83d6c 100644
--- a/sheep/compile.c
+++ b/sheep/compile.c
@@ -6,7 +6,6 @@
#include <sheep/util.h>
#include <sheep/map.h>
#include <sheep/vm.h>
-#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
@@ -174,7 +173,7 @@ static struct unpack_map *map_control(char control)
for (map = unpack_table; map->control; map++)
if (map->control == control)
break;
- assert(map->control);
+ sheep_bug_on(!map->control);
return map;
}
@@ -185,7 +184,7 @@ static struct unpack_map *map_type(const struct sheep_type *type)
for (map = unpack_table; map->control; map++)
if (map->type == type)
break;
- assert(map->control);
+ sheep_bug_on(!map->control);
return map;
}
diff --git a/sheep/eval.c b/sheep/eval.c
index 0022574..36b1887 100644
--- a/sheep/eval.c
+++ b/sheep/eval.c
@@ -3,7 +3,6 @@
#include <sheep/code.h>
#include <sheep/util.h>
#include <sheep/vm.h>
-#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
@@ -15,7 +14,7 @@ static sheep_t closure(struct sheep_vm *vm, sheep_t sheep)
struct sheep_vector *foreigns;
unsigned int i;
- assert(sheep->type == &sheep_function_type);
+ sheep_bug_on(sheep->type != &sheep_function_type);
function = sheep_data(sheep);
if (!function->foreigns)
@@ -24,7 +23,7 @@ static sheep_t closure(struct sheep_vm *vm, sheep_t sheep)
foreigns = sheep_malloc(sizeof(struct sheep_vector));
sheep_vector_init(foreigns, 4);
- assert(!(function->foreigns->nr_items % 2));
+ sheep_bug_on(function->foreigns->nr_items % 2);
for (i = 0; i < function->foreigns->nr_items; i += 2) {
unsigned long fbasep, offset;
unsigned int dist, slot;
@@ -115,7 +114,7 @@ sheep_t sheep_eval(struct sheep_vm *vm, struct sheep_code *code)
/* Get the callee */
tmp = sheep_vector_pop(&vm->stack);
- assert(tmp->type == &sheep_function_type);
+ sheep_bug_on(tmp->type != &sheep_function_type);
function = sheep_data(tmp);
/* Prepare the stack */
@@ -133,8 +132,8 @@ sheep_t sheep_eval(struct sheep_vm *vm, struct sheep_code *code)
goto out;
/* Sanity-check: exactly one return value */
- assert(vm->stack.nr_items -
- basep - function->nr_locals == 1);
+ sheep_bug_on(vm->stack.nr_items -
+ basep - function->nr_locals != 1);
/* Nip the locals */
if (function->nr_locals) {
@@ -159,6 +158,6 @@ sheep_t sheep_eval(struct sheep_vm *vm, struct sheep_code *code)
pc++;
}
out:
- assert(vm->stack.nr_items == 1);
+ sheep_bug_on(vm->stack.nr_items != 1);
return sheep_vector_pop(&vm->stack);
}
diff --git a/sheep/object.c b/sheep/object.c
index 7538b5d..54dacdd 100644
--- a/sheep/object.c
+++ b/sheep/object.c
@@ -2,7 +2,6 @@
#include <sheep/util.h>
#include <sheep/vm.h>
#include <sys/mman.h>
-#include <assert.h>
#include <unistd.h>
#include <stdio.h>
@@ -176,7 +175,7 @@ void sheep_unprotect(struct sheep_vm *vm, sheep_t sheep)
sheep_t prot;
prot = sheep_vector_pop(&vm->protected);
- assert(prot == sheep);
+ sheep_bug_on(prot != sheep);
}
void sheep_gc_disable(struct sheep_vm *vm)
diff --git a/sheep/util.c b/sheep/util.c
index 3fa7b62..c581605 100644
--- a/sheep/util.c
+++ b/sheep/util.c
@@ -1,3 +1,4 @@
+#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
@@ -36,3 +37,15 @@ void sheep_free(const void *mem)
{
free((void *)mem);
}
+
+void sheep_bug(const char *fmt, ...)
+{
+ va_list ap;
+
+ fprintf(stderr, "*** sheep BUG: ");
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ fprintf(stderr, "\n");
+ abort();
+}