GNU header update.
[cacao.git] / src / mm / memory.c
index 0e2ac4583eec1e4d81d9fd3e500d4cce59357271..134ecdb5fd64bfa920f4f8861c2bdacd2f8e0ce2 100644 (file)
@@ -1,9 +1,9 @@
 /* toolbox/memory.c - 
 
-   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
-   R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
-   M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
-   P. Tomsich, J. Wenninger
+   Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
+   R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
+   C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
+   Institut f. Computersprachen - TU Wien
 
    This file is part of CACAO.
 
 
    Authors: Reinhard Grafl
 
-   $Id: memory.c 1437 2004-11-05 09:51:07Z twisti $
+   $Id: memory.c 1735 2004-12-07 14:33:27Z twisti $
 
 */
 
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <assert.h>
 #include <string.h>
+
+#if defined(__DARWIN__)
+/* If we compile with -ansi on darwin, <sys/types.h> is not included. So      */
+/* let's do it here.                                                          */
+# include <sys/types.h>
+#endif
+
 #include <sys/mman.h>
 #include <unistd.h>
 
-#include "exceptions.h"
-#include "global.h"
-#include "native.h"
-#include "options.h"
-#include "statistics.h"
-#include "threads/nativethread.h"
+#include "config.h"
+#include "mm/memory.h"
+#include "vm/exceptions.h"
+#include "vm/global.h"
+#include "vm/options.h"
+#include "vm/statistics.h"
+#include "native/native.h"
+
+#if defined(USE_THREADS)
+# if defined(NATIVE_THREADS)
+#  include "threads/native/threads.h"
+# else
+#  include "threads/green/threads.h"
+# endif
+#endif
+
 #include "toolbox/logging.h"
-#include "toolbox/memory.h"
 
 
 /********* general types, variables and auxiliary functions *********/
@@ -61,12 +76,12 @@ static void *mmapcodeptr = NULL;
 
 *******************************************************************************/
 
-#if !defined(USE_THREADS)
+#if !defined(USE_THREADS) || (defined(USE_THREADS) && !defined(NATIVE_THREADS))
 static dumpinfo nothreads_dumpinfo;
 #endif
 
 
-void *mem_mmap(int size)
+void *mem_mmap(s4 size)
 {
        void *m;
 
@@ -82,7 +97,14 @@ void *mem_mmap(int size)
                mmapcodeptr = mmap(NULL,
                                                   (size_t) mmapcodesize,
                                                   PROT_READ | PROT_WRITE | PROT_EXEC,
-                                                  MAP_PRIVATE | MAP_ANONYMOUS,
+                                                  MAP_PRIVATE |
+#if defined(HAVE_MAP_ANONYMOUS)
+                                                  MAP_ANONYMOUS,
+#elif defined(HAVE_MAP_ANON)
+                                                  MAP_ANON,
+#else
+                                                  0,
+#endif
                                                   -1,
                                                   (off_t) 0);
 
@@ -99,7 +121,7 @@ void *mem_mmap(int size)
 }
 
 
-static void *checked_alloc(int size)
+static void *checked_alloc(s4 size)
 {
        /* always allocate memory zeroed out */
        void *m = calloc(size, 1);
@@ -112,7 +134,7 @@ static void *checked_alloc(int size)
 }
 
 
-void *mem_alloc(int size)
+void *mem_alloc(s4 size)
 {
        if (size == 0)
                return NULL;
@@ -128,7 +150,7 @@ void *mem_alloc(int size)
 }
 
 
-void *mem_realloc(void *src, int len1, int len2)
+void *mem_realloc(void *src, s4 len1, s4 len2)
 {
        void *dst;
 
@@ -150,7 +172,7 @@ void *mem_realloc(void *src, int len1, int len2)
 }
 
 
-void mem_free(void *m, int size)
+void mem_free(void *m, s4 size)
 {
        if (!m) {
                if (size == 0)
@@ -165,14 +187,14 @@ void mem_free(void *m, int size)
 }
 
 
-void *dump_alloc(int size)
+void *dump_alloc(s4 size)
 {
        void *m;
        dumpinfo *di;
 
        /* If no threads are used, the dumpinfo structure is a static structure   */
        /* defined at the top of this file.                                       */
-#if defined(USE_THREADS)
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
        di = &((threadobject *) THREADOBJECT)->dumpinfo;
 #else
        di = &nothreads_dumpinfo;
@@ -200,7 +222,7 @@ void *dump_alloc(int size)
                }
 
                /* allocate dumpblock memory */
-               //printf("new dumpblock: %d\n", newdumpblocksize);
+               /*printf("new dumpblock: %d\n", newdumpblocksize);*/
                newdumpblock->dumpmem = checked_alloc(newdumpblocksize);
 
                newdumpblock->prev = di->currentdumpblock;
@@ -209,7 +231,7 @@ void *dump_alloc(int size)
 
                /* Used dump size is previously allocated dump size, because the      */
                /* remaining free memory of the previous dump block cannot be used.   */
-               //printf("unused memory: %d\n", allocateddumpsize - useddumpsize);
+               /*printf("unused memory: %d\n", allocateddumpsize - useddumpsize);*/
                di->useddumpsize = di->allocateddumpsize;
 
                /* increase the allocated dump size by the size of the new dump block */
@@ -238,7 +260,7 @@ void *dump_alloc(int size)
 }   
 
 
-void *dump_realloc(void *src, int len1, int len2)
+void *dump_realloc(void *src, s4 len1, s4 len2)
 {
        void *dst = dump_alloc(len2);
 
@@ -248,13 +270,13 @@ void *dump_realloc(void *src, int len1, int len2)
 }
 
 
-void dump_release(int size)
+void dump_release(s4 size)
 {
        dumpinfo *di;
 
        /* If no threads are used, the dumpinfo structure is a static structure   */
        /* defined at the top of this file.                                       */
-#if defined(USE_THREADS)
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
        di = &((threadobject *) THREADOBJECT)->dumpinfo;
 #else
        di = &nothreads_dumpinfo;
@@ -295,18 +317,21 @@ void dump_release(int size)
 }
 
 
-long dump_size()
+s4 dump_size()
 {
        dumpinfo *di;
 
        /* If no threads are used, the dumpinfo structure is a static structure   */
        /* defined at the top of this file.                                       */
-#if defined(USE_THREADS)
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
        di = &((threadobject *) THREADOBJECT)->dumpinfo;
 #else
        di = &nothreads_dumpinfo;
 #endif
 
+       if (!di)
+               return 0;
+
        return di->useddumpsize;
 }