summaryrefslogtreecommitdiff
path: root/test/raytracer/memory.c
diff options
context:
space:
mode:
authorGravatar xleroy <xleroy@fca1b0fc-160b-0410-b1d3-a4f43f01ea2e>2008-08-09 08:06:33 +0000
committerGravatar xleroy <xleroy@fca1b0fc-160b-0410-b1d3-a4f43f01ea2e>2008-08-09 08:06:33 +0000
commit285f5bec5bb03d4e825e5d866e94008088dd6155 (patch)
tree9df69ded9ed4f4049e0b3887fdd99fcdf3b1746f /test/raytracer/memory.c
parenta83f0c1710cc5143dd885e84c94e14f7d3216f93 (diff)
Ajout nouveaux tests
git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@708 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e
Diffstat (limited to 'test/raytracer/memory.c')
-rw-r--r--test/raytracer/memory.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/test/raytracer/memory.c b/test/raytracer/memory.c
new file mode 100644
index 0000000..fe194cc
--- /dev/null
+++ b/test/raytracer/memory.c
@@ -0,0 +1,60 @@
+/* Arena-based memory management */
+
+#include "config.h"
+
+#define SIZE_AREA 1024000
+
+struct area {
+ struct area * next;
+ char data[SIZE_AREA];
+};
+
+struct area * arena_head;
+struct area * arena_cur;
+int arena_cur_ofs;
+
+void arena_init(void)
+{
+ arena_head = malloc(sizeof(struct area));
+ if (arena_head == NULL) {
+ fprintf(stderr, "Out of memory\n");
+ exit(2);
+ }
+ arena_head->next = NULL;
+ arena_cur = arena_head;
+ arena_cur_ofs = 0;
+}
+
+void arena_clear(void)
+{
+ arena_cur = arena_head;
+ arena_cur_ofs = 0;
+}
+
+void * arena_alloc(int size)
+{
+ char * res;
+ struct area * n;
+
+ if (size >= SIZE_AREA) {
+ fprintf(stderr, "Memory request too large (%d)\n", size);
+ exit(2);
+ }
+ if (arena_cur_ofs + size <= SIZE_AREA) {
+ res = arena_cur->data + arena_cur_ofs;
+ arena_cur_ofs += size;
+ return res;
+ }
+ if (arena_cur->next == NULL) {
+ n = malloc(sizeof(struct area));
+ if (n == NULL) {
+ fprintf(stderr, "Out of memory\n");
+ exit(2);
+ }
+ n->next = NULL;
+ arena_cur->next = n;
+ }
+ arena_cur = arena_cur->next;
+ arena_cur_ofs = size;
+ return arena_cur->data;
+}