* Updated header: Added 2006. Changed address of FSF. Changed email
[cacao.git] / src / mm / memory.c
index 23bdf5aa9956fd1756d4d1dba8e04f022ce17234..8c685f3d2dbdb41ceb0423e93aaac136efc16c83 100644 (file)
@@ -1,9 +1,9 @@
 /* src/mm/memory.c - 
 
-   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
+   Copyright (C) 1996-2005, 2006 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.
 
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
-   Contact: cacao@complang.tuwien.ac.at
+   Contact: cacao@cacaojvm.org
 
    Authors: Reinhard Grafl
 
    Changes: Christian Thalinger
 
-   $Id: memory.c 3205 2005-09-19 09:02:55Z twisti $
+   $Id: memory.c 4357 2006-01-22 23:33:38Z twisti $
 
 */
 
 
 #include <assert.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
+#include <sys/mman.h>
 
 #if defined(__DARWIN__)
 /* If we compile with -ansi on darwin, <sys/types.h> is not included. So      */
 #endif
 
 #include "config.h"
-#include "arch.h"
+#include "vm/types.h"
 
-#if USE_CODEMMAP
-# include <sys/mman.h>
-# include <unistd.h>
-#endif
+#include "arch.h"
 
 #include "mm/memory.h"
 #include "native/native.h"
 #include "vm/stringlocal.h"
 
 
-/********* general types, variables and auxiliary functions *********/
-
-#if USE_CODEMMAP
-static int mmapcodesize = 0;
-static void *mmapcodeptr = NULL;
-#endif
-
-
 /*******************************************************************************
 
-    This structure is used for dump memory allocation if cacao runs
-    without threads.
+  This structure is used for dump memory allocation if cacao
+  runswithout threads.
 
 *******************************************************************************/
 
@@ -97,58 +89,133 @@ static dumpinfo _no_threads_dumpinfo;
 #endif
 
 
-#if USE_CODEMMAP
-void *mem_mmap(s4 size)
-{
-       void *m;
+/* global code memory variables ***********************************************/
 
-       size = ALIGN(size, ALIGNSIZE);
+#define DEFAULT_CODEMEM_SIZE    128 * 1024  /* defaulting to 128kB            */
 
-       if (size > mmapcodesize) {
-               mmapcodesize = 0x10000;
-
-               if (size > mmapcodesize)
-                       mmapcodesize = size;
-
-               mmapcodesize = ALIGN(mmapcodesize, getpagesize());
-               mmapcodeptr = mmap(NULL,
-                                                  (size_t) mmapcodesize,
-                                                  PROT_READ | PROT_WRITE | PROT_EXEC,
-                                                  MAP_PRIVATE |
-#if defined(HAVE_MAP_ANONYMOUS)
-                                                  MAP_ANONYMOUS,
-#elif defined(HAVE_MAP_ANON)
-                                                  MAP_ANON,
-#else
-                                                  0,
+#if defined(USE_THREADS)
+static java_objectheader *codememlock = NULL;
 #endif
-                                                  -1,
-                                                  (off_t) 0);
+static int                codememsize = 0;
+static void              *codememptr  = NULL;
 
-               if (mmapcodeptr == MAP_FAILED)
-                       throw_cacao_exception_exit(string_java_lang_InternalError,
-                                                                          "Out of memory");
-       }
 
-       m = mmapcodeptr;
-       mmapcodeptr = (void *) ((char *) mmapcodeptr + size);
-       mmapcodesize -= size;
+/* memory_init *****************************************************************
 
-       return m;
-}
+   Initialize the memory subsystem.
+
+*******************************************************************************/
+
+bool memory_init(void)
+{
+#if defined(USE_THREADS)
+       codememlock = NEW(java_objectheader);
+
+# if defined(NATIVE_THREADS)
+       initObjectLock(codememlock);
+# endif
 #endif
 
+       /* everything's ok */
+
+       return true;
+}
+
+
+/* memory_checked_alloc ********************************************************
+
+   Allocated zeroed-out memory and does an OOM check.
 
-static void *checked_alloc(s4 size)
+*******************************************************************************/
+
+static void *memory_checked_alloc(s4 size)
 {
        /* always allocate memory zeroed out */
-       void *m = calloc(size, 1);
 
-       if (!m)
-               throw_cacao_exception_exit(string_java_lang_InternalError,
-                                                                  "Out of memory");
+       void *p = calloc(size, 1);
 
-       return m;
+       if (!p)
+               exceptions_throw_outofmemory_exit();
+
+       return p;
+}
+
+
+/* memory_cnew *****************************************************************
+
+   Allocates memory from the heap, aligns it to architecutres PAGESIZE
+   and make the memory read-, write-, and executeable.
+
+*******************************************************************************/
+
+void *memory_cnew(s4 size)
+{
+       void *p;
+       int   pagesize;
+
+#if defined(USE_THREADS)
+       builtin_monitorenter(codememlock);
+#endif
+
+       size = ALIGN(size, ALIGNSIZE);
+
+       /* check if enough memory is available */
+
+       if (size > codememsize) {
+               /* set default code size */
+
+               codememsize = DEFAULT_CODEMEM_SIZE;
+
+               /* do we need more? */
+
+               if (size > codememsize)
+                       codememsize = size;
+
+               /* get the pagesize of this architecture */
+
+               pagesize = getpagesize();
+
+               /* allocate normal heap memory */
+
+               if ((p = memory_checked_alloc(codememsize + pagesize - 1)) == NULL)
+                       return NULL;
+
+#if defined(ENABLE_STATISTICS)
+               if (opt_stat) {
+                       codememusage += codememsize + pagesize - 1;
+
+                       if (codememusage > maxcodememusage)
+                               maxcodememusage = codememusage;
+               }
+#endif
+
+               /* align the memory allocated to a multiple of PAGESIZE,
+                  mprotect requires this */
+
+               p = (void *) (((ptrint) p + pagesize - 1) & ~(pagesize - 1));
+
+               /* make the memory read-, write-, and executeable */
+
+               if (mprotect(p, codememsize, PROT_READ | PROT_WRITE | PROT_EXEC) == -1)
+                       throw_cacao_exception_exit(string_java_lang_InternalError,
+                                                                          strerror(errno));
+
+               /* set global code memory pointer */
+
+               codememptr = p;
+       }
+
+       /* get a memory chunk of the allocated memory */
+
+       p = codememptr;
+       codememptr = (void *) ((ptrint) codememptr + size);
+       codememsize -= size;
+
+#if defined(USE_THREADS)
+       builtin_monitorexit(codememlock);
+#endif
+
+       return p;
 }
 
 
@@ -157,7 +224,7 @@ void *mem_alloc(s4 size)
        if (size == 0)
                return NULL;
 
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
        if (opt_stat) {
                memoryusage += size;
 
@@ -166,7 +233,7 @@ void *mem_alloc(s4 size)
        }
 #endif
 
-       return checked_alloc(size);
+       return memory_checked_alloc(size);
 }
 
 
@@ -181,7 +248,7 @@ void *mem_realloc(void *src, s4 len1, s4 len2)
                }
        }
 
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
        if (opt_stat)
                memoryusage = (memoryusage - len1) + len2;
 #endif
@@ -189,8 +256,7 @@ void *mem_realloc(void *src, s4 len1, s4 len2)
        dst = realloc(src, len2);
 
        if (!dst)
-               throw_cacao_exception_exit(string_java_lang_InternalError,
-                                                                  "Out of memory");
+               exceptions_throw_outofmemory_exit();
 
        return dst;
 }
@@ -206,7 +272,7 @@ void mem_free(void *m, s4 size)
                assert(0);
        }
 
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
        if (opt_stat)
                memoryusage -= size;
 #endif
@@ -247,7 +313,7 @@ void *dump_alloc(s4 size)
 
                /* allocate a new dumplist structure */
 
-               newdumpblock = checked_alloc(sizeof(dumpblock));
+               newdumpblock = memory_checked_alloc(sizeof(dumpblock));
 
                /* If requested size is greater than the default, make the new dump   */
                /* block as big as the size requested. Else use the default size.     */
@@ -261,7 +327,7 @@ void *dump_alloc(s4 size)
 
                /* allocate dumpblock memory */
 
-               newdumpblock->dumpmem = checked_alloc(newdumpblocksize);
+               newdumpblock->dumpmem = memory_checked_alloc(newdumpblocksize);
 
                newdumpblock->prev = di->currentdumpblock;
                newdumpblock->size = newdumpblocksize;
@@ -276,7 +342,7 @@ void *dump_alloc(s4 size)
 
                di->allocateddumpsize += newdumpblocksize;
 
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
                /* the amount of globally allocated dump memory (thread save) */
 
                if (opt_stat)
@@ -294,7 +360,7 @@ void *dump_alloc(s4 size)
 
        di->useddumpsize += size;
 
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
        if (opt_stat)
                if (di->useddumpsize > maxdumpsize)
                        maxdumpsize = di->useddumpsize;
@@ -372,7 +438,7 @@ void dump_release(s4 size)
                di->allocateddumpsize -= tmp->size;
                di->currentdumpblock = tmp->prev;
 
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
                /* the amount of globally allocated dump memory (thread save) */
 
                if (opt_stat)