/* 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.
*******************************************************************************/
#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;
}
if (size == 0)
return NULL;
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
if (opt_stat) {
memoryusage += size;
}
#endif
- return checked_alloc(size);
+ return memory_checked_alloc(size);
}
}
}
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
if (opt_stat)
memoryusage = (memoryusage - len1) + len2;
#endif
dst = realloc(src, len2);
if (!dst)
- throw_cacao_exception_exit(string_java_lang_InternalError,
- "Out of memory");
+ exceptions_throw_outofmemory_exit();
return dst;
}
assert(0);
}
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
if (opt_stat)
memoryusage -= size;
#endif
/* 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. */
/* allocate dumpblock memory */
- newdumpblock->dumpmem = checked_alloc(newdumpblocksize);
+ newdumpblock->dumpmem = memory_checked_alloc(newdumpblocksize);
newdumpblock->prev = di->currentdumpblock;
newdumpblock->size = newdumpblocksize;
di->allocateddumpsize += newdumpblocksize;
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
/* the amount of globally allocated dump memory (thread save) */
if (opt_stat)
di->useddumpsize += size;
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
if (opt_stat)
if (di->useddumpsize > maxdumpsize)
maxdumpsize = di->useddumpsize;
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)