]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Add basic storage management.
authorJulian Seward <jseward@acm.org>
Sat, 26 Jun 2004 20:10:35 +0000 (20:10 +0000)
committerJulian Seward <jseward@acm.org>
Sat, 26 Jun 2004 20:10:35 +0000 (20:10 +0000)
git-svn-id: svn://svn.valgrind.org/vex/trunk@20

VEX/Makefile
VEX/arena.c [new file with mode: 0644]
VEX/basictypes.c [new file with mode: 0644]
VEX/include/arena.h [new file with mode: 0644]
VEX/include/basictypes.h [moved from VEX/backend/basictypes.h with 58% similarity]
VEX/linker.c

index cb1af4a080c481c79f3affb3118e9460e4fa11a5..f8dde35caf47e8e981718a42e11c7c81f4e714b5 100644 (file)
@@ -1,5 +1,23 @@
 
-# Preps the FFI stuff (sigh)
-all:
-       gcc -Wall -g -o vex linker.c
+INCLUDES = include/basictypes.h
+
+OBJS = basictypes.o arena.o linker.o dispatch.o
+
+CC_OPTS = -g -Wall -Iinclude
+
+all: $(OBJS)
+       gcc $(CC_OPTS) -o vex $(OBJS)
+
+clean:
+       rm -f *.o vex
+
+basictypes.o: basictypes.c $(INCLUDES)
+       gcc $(CC_OPTS) -c basictypes.c
+arena.o: arena.c $(INCLUDES)
+       gcc $(CC_OPTS) -c arena.c
+linker.o: linker.c $(INCLUDES)
+       gcc $(CC_OPTS) -c linker.c
+dispatch.o: dispatch.c $(INCLUDES)
+       gcc $(CC_OPTS) -c dispatch.c
+
 
diff --git a/VEX/arena.c b/VEX/arena.c
new file mode 100644 (file)
index 0000000..8ee7c9a
--- /dev/null
@@ -0,0 +1,144 @@
+
+/* This is a modified version of the file "arena.c" from 
+   "C Interfaces and Implementations", by David R. Hanson.
+   The license is below.
+*/
+/* 
+
+The author of this software is David R. Hanson.
+
+Copyright (c) 1994,1995,1996,1997 by David R. Hanson. All Rights Reserved.
+
+Permission to use, copy, modify, and distribute this software for any
+purpose, subject to the provisions described below, without fee is
+hereby granted, provided that this entire notice is included in all
+copies of any software that is or includes a copy or modification of
+this software and in all copies of the supporting documentation for
+such software.
+
+THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
+WARRANTY. IN PARTICULAR, THE AUTHOR DOES MAKE ANY REPRESENTATION OR
+WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS SOFTWARE OR
+ITS FITNESS FOR ANY PARTICULAR PURPOSE.
+
+David Hanson / drh@microsoft.com / http://www.research.microsoft.com/~drh/
+$Id: CPYRIGHT,v 1.2 1997/11/04 22:31:40 drh Exp $
+*/
+
+
+//static char rcsid[] = "$Id: H:/drh/idioms/book/RCS/arena.doc,v 1.10 1997/02/21 19:45:19 drh Exp $";
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "basictypes.h"
+
+//#include "assert.h"
+//#include "except.h"
+#include "arena.h"
+#define T Arena_T
+//const Except_T Arena_NewFailed =
+//     { "Arena Creation Failed" };
+//const Except_T Arena_Failed    =
+//     { "Arena Allocation Failed" };
+#define THRESHOLD 10
+struct T {
+       T prev;
+       char *avail;
+       char *limit;
+};
+union align {
+#ifdef MAXALIGN
+       char pad[MAXALIGN];
+#else
+       int i;
+       long l;
+       long *lp;
+       void *p;
+       void (*fp)(void);
+       float f;
+       double d;
+       long double ld;
+#endif
+};
+union header {
+       struct T b;
+       union align a;
+};
+static T freechunks;
+static int nfree;
+T Arena_new(void) {
+       T arena = malloc(sizeof (*arena));
+       if (arena == NULL)
+               panic("Arena_NewFailed");
+       arena->prev = NULL;
+       arena->limit = arena->avail = NULL;
+       return arena;
+}
+void Arena_dispose(T *ap) {
+       assert(ap && *ap);
+       Arena_free(*ap);
+       free(*ap);
+       *ap = NULL;
+}
+void *Arena_alloc(T arena, long nbytes,
+       const char *file, int line) {
+       assert(arena);
+       assert(nbytes > 0);
+       nbytes = ((nbytes + sizeof (union align) - 1)/
+               (sizeof (union align)))*(sizeof (union align));
+       while (nbytes > arena->limit - arena->avail) {
+               T ptr;
+               char *limit;
+               if ((ptr = freechunks) != NULL) {
+                       freechunks = freechunks->prev;
+                       nfree--;
+                       limit = ptr->limit;
+               } else {
+                       long m = sizeof (union header) + nbytes + 10*1024;
+                       ptr = malloc(m);
+                       if (ptr == NULL)
+                               {
+                                       if (file == NULL)
+                                               panic("Arena_Failed");
+                                       else
+                                         {
+                                           fprintf(stderr, "\nArena_alloc: allocation failed at %s:%d\n", file, line);
+                                           panic("Arena_alloc: allocation failed");
+                                           //Except_raise(&Arena_Failed, file, line);
+                                         }
+                               }
+                       limit = (char *)ptr + m;
+               }
+               *ptr = *arena;
+               arena->avail = (char *)((union header *)ptr + 1);
+               arena->limit = limit;
+               arena->prev  = ptr;
+       }
+       arena->avail += nbytes;
+       return arena->avail - nbytes;
+}
+void *Arena_calloc(T arena, long count, long nbytes,
+       const char *file, int line) {
+       void *ptr;
+       assert(count > 0);
+       ptr = Arena_alloc(arena, count*nbytes, file, line);
+       memset(ptr, '\0', count*nbytes);
+       return ptr;
+}
+void Arena_free(T arena) {
+       assert(arena);
+       while (arena->prev) {
+               struct T tmp = *arena->prev;
+               if (nfree < THRESHOLD) {
+                       arena->prev->prev = freechunks;
+                       freechunks = arena->prev;
+                       nfree++;
+                       freechunks->limit = arena->limit;
+               } else
+                       free(arena->prev);
+               *arena = tmp;
+       }
+       assert(arena->limit == NULL);
+       assert(arena->avail == NULL);
+}
diff --git a/VEX/basictypes.c b/VEX/basictypes.c
new file mode 100644 (file)
index 0000000..43a3948
--- /dev/null
@@ -0,0 +1,20 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "basictypes.h"
+
+__attribute__ ((noreturn))
+void vex_assert_fail ( const Char* expr,
+                       const Char* file, Int line, const Char* fn )
+{
+   fprintf(stderr, "\nvex: %s:%d (%s): Assertion `%s' failed.\n",
+                   file, line, fn, expr );
+   exit(1);
+}
+
+__attribute__ ((noreturn))
+void panic ( Char* str )
+{
+   fprintf(stderr, "\nvex: the `impossible' happened:\n   %s\n", str);
+   exit(1);
+}
diff --git a/VEX/include/arena.h b/VEX/include/arena.h
new file mode 100644 (file)
index 0000000..a74936c
--- /dev/null
@@ -0,0 +1,47 @@
+
+/* This is a modified version of the file "arena.h" from 
+   "C Interfaces and Implementations", by David R. Hanson.
+   The license is below.
+*/
+/* 
+
+The author of this software is David R. Hanson.
+
+Copyright (c) 1994,1995,1996,1997 by David R. Hanson. All Rights Reserved.
+
+Permission to use, copy, modify, and distribute this software for any
+purpose, subject to the provisions described below, without fee is
+hereby granted, provided that this entire notice is included in all
+copies of any software that is or includes a copy or modification of
+this software and in all copies of the supporting documentation for
+such software.
+
+THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
+WARRANTY. IN PARTICULAR, THE AUTHOR DOES MAKE ANY REPRESENTATION OR
+WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS SOFTWARE OR
+ITS FITNESS FOR ANY PARTICULAR PURPOSE.
+
+David Hanson / drh@microsoft.com / http://www.research.microsoft.com/~drh/
+$Id: CPYRIGHT,v 1.2 1997/11/04 22:31:40 drh Exp $
+*/
+
+/* $Id: H:/drh/idioms/book/RCS/arena.doc,v 1.10 1997/02/21 19:45:19 drh Exp $ */
+
+#ifndef _CII_ARENA_H
+#define _CII_ARENA_H
+
+//#include "except.h"
+#define T Arena_T
+typedef struct T *T;
+//extern const Except_T Arena_NewFailed;
+//extern const Except_T Arena_Failed;
+extern T    Arena_new    (void);
+extern void Arena_dispose(T *ap);
+extern void *Arena_alloc (T arena, long nbytes,
+       const char *file, int line);
+extern void *Arena_calloc(T arena, long count,
+       long nbytes, const char *file, int line);
+extern void  Arena_free  (T arena);
+#undef T
+
+#endif /* ndef _CII_ARENA_H */
similarity index 58%
rename from VEX/backend/basictypes.h
rename to VEX/include/basictypes.h
index 241cfa9d786905a683a13752df92c4228654be96..61d2e187491e101f47d03223c92d0c5cd9d997e3 100644 (file)
@@ -25,4 +25,22 @@ typedef  unsigned char  Bool;
 #define  True   ((Bool)1)
 #define  False  ((Bool)0)
 
+
+/* Stuff for panicking and assertion. */
+
+#define VG__STRING(__str)  #__str
+
+#define assert(expr)                                            \
+  ((void) ((expr) ? 0 :                                         \
+           (vex_assert_fail (VG__STRING(expr),                  \
+                             __FILE__, __LINE__,                \
+                             __PRETTY_FUNCTION__), 0)))
+
+__attribute__ ((__noreturn__))
+extern void vex_assert_fail ( const Char* expr, const Char* file,
+                              Int line, const Char* fn );
+__attribute__ ((__noreturn__))
+extern void panic ( Char* str );
+
+
 #endif /* ndef __BASICTYPES_H */
index 10a732d1db80deb5e5640784331800e009bfe54c..9fe91b92c00069dbce264f3561ca4fd384dce749 100644 (file)
@@ -1360,7 +1360,7 @@ int n_transtab_used = 0;
 TTEntry transtab[N_TT_ENTRIES];
 
 
-/* Called by Haskell to add a translation to the trans cache.
+/* Call here to add a translation to the trans cache.
    Supplied translation is in mallocville.  add_translation should
    copy it out as the caller will free it on return.  */
 
@@ -1378,7 +1378,9 @@ void add_translation ( char* orig, int orig_size, char* trans, int trans_size )
    for (i = 0; i < trans_size; i++)
       transtab[n_transtab_used].trans[i] = trans[i];
 
+#ifdef arm_TARGET_ARCH
    arm_notify_new_code(transtab[n_transtab_used].trans, trans_size);
+#endif
 
    n_transtab_used++;
 }