-/************************* toolbox/memory.c ************************************
+/* src/mm/memory.c -
- Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
+ 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
- See file COPYRIGHT for information on usage and disclaimer of warranties
+ This file is part of CACAO.
- Not documented, see memory.h.
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
- Authors: Reinhard Grafl EMAIL: cacao@complang.tuwien.ac.at
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
- Last Change: 1996/10/03
+ 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., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA.
-*******************************************************************************/
+ Contact: cacao@cacaojvm.org
+
+ Authors: Reinhard Grafl
+
+ Changes: Christian Thalinger
+
+ $Id: memory.c 4357 2006-01-22 23:33:38Z twisti $
+
+*/
+
+#include <assert.h>
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
-#include <assert.h>
#include <string.h>
-#include <sys/mman.h>
#include <unistd.h>
+#include <sys/mman.h>
-#include "../callargs.h"
-#include "loging.h"
-#include "memory.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 "config.h"
+#include "vm/types.h"
- /********* allgemeine Typen, Variablen und Hilfsfunktionen *********/
+#include "arch.h"
-#define DUMPBLOCKSIZE (2<<18)
-#define ALIGNSIZE 8
+#include "mm/memory.h"
+#include "native/native.h"
-typedef struct dumplist {
- struct dumplist *prev;
- char *dumpmem;
-} dumplist;
+#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 "vm/exceptions.h"
+#include "vm/global.h"
+#include "vm/options.h"
+#include "vm/statistics.h"
+#include "vm/stringlocal.h"
-long int memoryusage = 0;
+/*******************************************************************************
-long int dumpsize = 0;
-long int dumpspace = 0;
-dumplist *topdumpblock = NULL;
+ This structure is used for dump memory allocation if cacao
+ runswithout threads.
-long int maxmemusage = 0;
-long int maxdumpsize = 0;
+*******************************************************************************/
-/* #define TRACECALLARGS */
+#if !defined(USE_THREADS) || (defined(USE_THREADS) && !defined(NATIVE_THREADS))
+static dumpinfo _no_threads_dumpinfo;
+#endif
-#ifdef TRACECALLARGS
-static char nomallocmem[16777216];
-static char *nomalloctop = nomallocmem + 16777216;
-static char *nomallocptr = nomallocmem;
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+#define DUMPINFO &((threadobject *) THREADOBJECT)->dumpinfo
+#else
+#define DUMPINFO &_no_threads_dumpinfo
+#endif
-static void *lit_checked_alloc (int length)
-{
- void *m;
- nomallocptr = (void*) ALIGN ((long) nomallocptr, ALIGNSIZE);
-
- m = nomallocptr;
- nomallocptr += length;
- if (nomallocptr > nomalloctop) panic ("Out of memory");
- return m;
-}
+/* global code memory variables ***********************************************/
-#else
+#define DEFAULT_CODEMEM_SIZE 128 * 1024 /* defaulting to 128kB */
+
+#if defined(USE_THREADS)
+static java_objectheader *codememlock = NULL;
+#endif
+static int codememsize = 0;
+static void *codememptr = NULL;
-static void *lit_checked_alloc (int length)
+
+/* memory_init *****************************************************************
+
+ Initialize the memory subsystem.
+
+*******************************************************************************/
+
+bool memory_init(void)
{
- void *m = malloc(length);
- if (!m) panic ("Out of memory");
- return m;
-}
+#if defined(USE_THREADS)
+ codememlock = NEW(java_objectheader);
+# if defined(NATIVE_THREADS)
+ initObjectLock(codememlock);
+# endif
#endif
+ /* everything's ok */
-static void *checked_alloc (int length)
-{
- void *m = malloc(length);
- if (!m) panic ("Out of memory");
- return m;
+ return true;
}
-static int mmapcodesize = 0;
-static void *mmapcodeptr = NULL;
-void *mem_mmap(int length)
+/* memory_checked_alloc ********************************************************
+
+ Allocated zeroed-out memory and does an OOM check.
+
+*******************************************************************************/
+
+static void *memory_checked_alloc(s4 size)
{
- void *retptr;
-
- length = (ALIGN(length,ALIGNSIZE));
- if (length > mmapcodesize) {
- mmapcodesize = 0x10000;
- if (length > mmapcodesize)
- mmapcodesize = length;
- mmapcodesize = (ALIGN(mmapcodesize, getpagesize()));
- mmapcodeptr = mmap (NULL, (size_t) mmapcodesize,
- PROT_READ | PROT_WRITE | PROT_EXEC,
- MAP_PRIVATE | MAP_ANONYMOUS, -1, (off_t) 0);
- if (mmapcodeptr == (void*) -1)
- panic ("Out of memory");
- }
- retptr = mmapcodeptr;
- mmapcodeptr = (void*) ((char*) mmapcodeptr + length);
- mmapcodesize -= length;
- return retptr;
+ /* always allocate memory zeroed out */
+
+ void *p = calloc(size, 1);
+
+ if (!p)
+ exceptions_throw_outofmemory_exit();
+
+ return p;
}
-#ifdef DEBUG
+/* memory_cnew *****************************************************************
- /************ Sichere Version des Speichermanages **************/
+ Allocates memory from the heap, aligns it to architecutres PAGESIZE
+ and make the memory read-, write-, and executeable.
+*******************************************************************************/
-typedef struct memblock {
- struct memblock *prev,*next;
- int length;
-} memblock;
+void *memory_cnew(s4 size)
+{
+ void *p;
+ int pagesize;
-#define BLOCKOFFSET (ALIGN(sizeof(memblock),ALIGNSIZE))
+#if defined(USE_THREADS)
+ builtin_monitorenter(codememlock);
+#endif
-struct memblock *firstmemblock;
+ size = ALIGN(size, ALIGNSIZE);
+ /* check if enough memory is available */
+ if (size > codememsize) {
+ /* set default code size */
-void *mem_alloc(int length)
-{
- memblock *mb;
+ codememsize = DEFAULT_CODEMEM_SIZE;
- if (length==0) return NULL;
- mb = checked_alloc (length + BLOCKOFFSET);
+ /* do we need more? */
- mb -> prev = NULL;
- mb -> next = firstmemblock;
- mb -> length = length;
+ if (size > codememsize)
+ codememsize = size;
- if (firstmemblock) firstmemblock -> prev = mb;
- firstmemblock = mb;
+ /* get the pagesize of this architecture */
- memoryusage += length;
- if (memoryusage > maxmemusage) maxmemusage = memoryusage;
+ pagesize = getpagesize();
- return ((char*) mb) + BLOCKOFFSET;
-}
+ /* allocate normal heap memory */
+ if ((p = memory_checked_alloc(codememsize + pagesize - 1)) == NULL)
+ return NULL;
-void *lit_mem_alloc(int length)
-{
- memblock *mb;
+#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 (length==0) return NULL;
- mb = lit_checked_alloc (length + BLOCKOFFSET);
+ if (mprotect(p, codememsize, PROT_READ | PROT_WRITE | PROT_EXEC) == -1)
+ throw_cacao_exception_exit(string_java_lang_InternalError,
+ strerror(errno));
- mb -> prev = NULL;
- mb -> next = firstmemblock;
- mb -> length = length;
+ /* set global code memory pointer */
- if (firstmemblock) firstmemblock -> prev = mb;
- firstmemblock = mb;
+ codememptr = p;
+ }
- memoryusage += length;
- if (memoryusage > maxmemusage) maxmemusage = memoryusage;
+ /* get a memory chunk of the allocated memory */
- return ((char*) mb) + BLOCKOFFSET;
+ p = codememptr;
+ codememptr = (void *) ((ptrint) codememptr + size);
+ codememsize -= size;
+
+#if defined(USE_THREADS)
+ builtin_monitorexit(codememlock);
+#endif
+
+ return p;
}
-void mem_free(void *m, int length)
+void *mem_alloc(s4 size)
{
- memblock *mb;
- if (!m) {
- if (length==0) return;
- panic ("returned memoryblock with address NULL, length != 0");
- }
+ if (size == 0)
+ return NULL;
- mb = (memblock*) (((char*) m) - BLOCKOFFSET);
-
- if (mb->length != length) {
- sprintf (logtext,
- "Memory block of size %d has been return as size %d",
- mb->length, length);
- error();
- }
-
- if (mb->prev) mb->prev->next = mb->next;
- else firstmemblock = mb->next;
- if (mb->next) mb->next->prev = mb->prev;
+#if defined(ENABLE_STATISTICS)
+ if (opt_stat) {
+ memoryusage += size;
- free (mb);
+ if (memoryusage > maxmemusage)
+ maxmemusage = memoryusage;
+ }
+#endif
- memoryusage -= length;
+ return memory_checked_alloc(size);
}
-void lit_mem_free(void *m, int length)
+void *mem_realloc(void *src, s4 len1, s4 len2)
{
- memblock *mb;
- if (!m) {
- if (length==0) return;
- panic ("returned memoryblock with address NULL, length != 0");
- }
+ void *dst;
- mb = (memblock*) (((char*) m) - BLOCKOFFSET);
-
- if (mb->length != length) {
- sprintf (logtext,
- "Memory block of size %d has been return as size %d",
- mb->length, length);
- error();
+ if (!src) {
+ if (len1 != 0) {
+ log_text("reallocating memoryblock with address NULL, length != 0");
+ assert(0);
}
-
- if (mb->prev) mb->prev->next = mb->next;
- else firstmemblock = mb->next;
- if (mb->next) mb->next->prev = mb->prev;
+ }
-#ifdef TRACECALLARGS
-#else
- free (mb);
+#if defined(ENABLE_STATISTICS)
+ if (opt_stat)
+ memoryusage = (memoryusage - len1) + len2;
#endif
- memoryusage -= length;
+ dst = realloc(src, len2);
+
+ if (!dst)
+ exceptions_throw_outofmemory_exit();
+
+ return dst;
}
-void *mem_realloc (void *m1, int len1, int len2)
+void mem_free(void *m, s4 size)
{
- void *m2;
-
- m2 = mem_alloc (len2);
- memcpy (m2, m1, len1);
- mem_free (m1, len1);
+ if (!m) {
+ if (size == 0)
+ return;
- return m2;
-}
+ log_text("returned memoryblock with address NULL, length != 0");
+ assert(0);
+ }
+#if defined(ENABLE_STATISTICS)
+ if (opt_stat)
+ memoryusage -= size;
+#endif
+ free(m);
+}
-static void mem_characterlog (unsigned char *m, int len)
-{
-# define LINESIZE 16
- int z,i;
-
- for (z=0; z<len; z+=LINESIZE) {
- sprintf (logtext, " ");
-
- for (i=z; i<(z+LINESIZE) && i<len; i++) {
- sprintf (logtext+strlen(logtext), "%2x ", m[i]);
- }
- for (; i<(z+LINESIZE); i++) {
- sprintf (logtext+strlen(logtext), " ");
- }
-
- sprintf (logtext+strlen(logtext)," ");
- for (i=z; i<(z+LINESIZE) && i<len; i++) {
- sprintf (logtext+strlen(logtext),
- "%c", (m[i]>=' ' && m[i]<=127) ? m[i] : '.');
- }
-
- dolog();
- }
-}
+/* dump_alloc ******************************************************************
-#else
- /******* Schnelle Version des Speichermanagers ******/
+ XXX
+*******************************************************************************/
-void *mem_alloc(int length)
+void *dump_alloc(s4 size)
{
- if (length==0) return NULL;
+#if defined(DISABLE_DUMP)
+ /* use malloc memory for dump memory (for debugging only!) */
- memoryusage += length;
- if (memoryusage > maxmemusage) maxmemusage = memoryusage;
-
- return checked_alloc (length);
-}
+ return mem_alloc(size);
+#else
+ void *m;
+ dumpinfo *di;
+ /* If no threads are used, the dumpinfo structure is a static structure */
+ /* defined at the top of this file. */
-void *lit_mem_alloc(int length)
-{
- if (length==0) return NULL;
+ di = DUMPINFO;
- memoryusage += length;
- if (memoryusage > maxmemusage) maxmemusage = memoryusage;
-
- return lit_checked_alloc (length);
-}
+ if (size == 0)
+ return NULL;
+ size = ALIGN(size, ALIGNSIZE);
-void mem_free(void *m, int length)
-{
- if (!m) {
- if (length==0) return;
- panic ("returned memoryblock with address NULL, length != 0");
- }
+ if (di->useddumpsize + size > di->allocateddumpsize) {
+ dumpblock *newdumpblock;
+ s4 newdumpblocksize;
- memoryusage -= length;
+ /* allocate a new dumplist structure */
- free (m);
-}
+ 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. */
-void lit_mem_free(void *m, int length)
-{
- if (!m) {
- if (length==0) return;
- panic ("returned memoryblock with address NULL, length != 0");
+ if (size > DUMPBLOCKSIZE) {
+ newdumpblocksize = size;
+
+ } else {
+ newdumpblocksize = DUMPBLOCKSIZE;
}
- memoryusage -= length;
+ /* allocate dumpblock memory */
-#ifdef TRACECALLARGS
-#else
- free (m);
-#endif
-}
+ newdumpblock->dumpmem = memory_checked_alloc(newdumpblocksize);
+ newdumpblock->prev = di->currentdumpblock;
+ newdumpblock->size = newdumpblocksize;
+ di->currentdumpblock = newdumpblock;
-void *mem_realloc (void *m1, int len1, int len2)
-{
- void *m2;
+ /* Used dump size is previously allocated dump size, because the */
+ /* remaining free memory of the previous dump block cannot be used. */
- if (!m1) {
- if (len1!=0)
- panic ("reallocating memoryblock with address NULL, length != 0");
- }
-
- memoryusage = (memoryusage - len1) + len2;
+ di->useddumpsize = di->allocateddumpsize;
- m2 = realloc (m1, len2);
- if (!m2) panic ("Out of memory");
- return m2;
-}
+ /* increase the allocated dump size by the size of the new dump block */
+
+ di->allocateddumpsize += newdumpblocksize;
+#if defined(ENABLE_STATISTICS)
+ /* the amount of globally allocated dump memory (thread save) */
+ if (opt_stat)
+ globalallocateddumpsize += newdumpblocksize;
#endif
+ }
- /******* allgemeine Teile des Speichermanagers ******/
+ /* current dump block base address + the size of the current dump block - */
+ /* the size of the unused memory = new start address */
+ m = di->currentdumpblock->dumpmem + di->currentdumpblock->size -
+ (di->allocateddumpsize - di->useddumpsize);
+ /* increase used dump size by the allocated memory size */
-long int mem_usage()
-{
- return memoryusage;
+ di->useddumpsize += size;
+
+#if defined(ENABLE_STATISTICS)
+ if (opt_stat)
+ if (di->useddumpsize > maxdumpsize)
+ maxdumpsize = di->useddumpsize;
+#endif
+
+ return m;
+#endif /* defined(DISABLE_DUMP) */
}
+/* dump_realloc ****************************************************************
+ XXX
+*******************************************************************************/
-void *dump_alloc(int length)
+void *dump_realloc(void *src, s4 len1, s4 len2)
{
- void *m;
+#if defined(DISABLE_DUMP)
+ /* use malloc memory for dump memory (for debugging only!) */
- if (length==0) return NULL;
-
- length = ALIGN (length, ALIGNSIZE);
+ return mem_realloc(src, len1, len2);
+#else
+ void *dst = dump_alloc(len2);
- assert (length <= DUMPBLOCKSIZE);
- assert (length > 0);
+ memcpy(dst, src, len1);
- if (dumpsize + length > dumpspace) {
- dumplist *newdumpblock = checked_alloc (sizeof(dumplist));
+ return dst;
+#endif
+}
- newdumpblock -> prev = topdumpblock;
- topdumpblock = newdumpblock;
- newdumpblock -> dumpmem = checked_alloc (DUMPBLOCKSIZE);
+/* dump_release ****************************************************************
- dumpsize = dumpspace;
- dumpspace += DUMPBLOCKSIZE;
- }
-
- m = topdumpblock -> dumpmem + DUMPBLOCKSIZE - (dumpspace - dumpsize);
- dumpsize += length;
-
- if (dumpsize > maxdumpsize) {
- maxdumpsize = dumpsize;
- }
-
- return m;
-}
+ XXX
+*******************************************************************************/
-void *dump_realloc(void *ptr, int len1, int len2)
+void dump_release(s4 size)
{
- void *p2 = dump_alloc (len2);
- memcpy (p2, ptr, len1);
- return p2;
-}
+#if defined(DISABLE_DUMP)
+ /* use malloc memory for dump memory (for debugging only!) */
+ /* do nothing */
+#else
+ dumpinfo *di;
-long int dump_size()
-{
- return dumpsize;
-}
+ /* If no threads are used, the dumpinfo structure is a static structure */
+ /* defined at the top of this file. */
+ di = DUMPINFO;
-void dump_release(long int size)
-{
- assert (size >= 0 && size <= dumpsize);
+ if (size < 0 || size > di->useddumpsize)
+ throw_cacao_exception_exit(string_java_lang_InternalError,
+ "Illegal dump release size %d", size);
- dumpsize = size;
-
- while (dumpspace > dumpsize + DUMPBLOCKSIZE) {
- dumplist *oldtop = topdumpblock;
-
- topdumpblock = oldtop -> prev;
- dumpspace -= DUMPBLOCKSIZE;
-
+ /* reset the used dump size to the size specified */
+
+ di->useddumpsize = size;
+
+ while (di->currentdumpblock && di->allocateddumpsize - di->currentdumpblock->size >= di->useddumpsize) {
+ dumpblock *tmp = di->currentdumpblock;
+
+#if 0
+ /* XXX TWISTI: can someone explain this to me? */
#ifdef TRACECALLARGS
-#else
- free (oldtop -> dumpmem);
- free (oldtop);
+ /* Keep the first dumpblock if we don't free memory. Otherwise
+ * a new dumpblock is allocated each time and we run out of
+ * memory.
+ */
+ if (!oldtop->prev) break;
#endif
- }
+#endif
+
+ di->allocateddumpsize -= tmp->size;
+ di->currentdumpblock = tmp->prev;
+
+#if defined(ENABLE_STATISTICS)
+ /* the amount of globally allocated dump memory (thread save) */
+
+ if (opt_stat)
+ globalallocateddumpsize -= tmp->size;
+#endif
+
+ /* release the dump memory and the dumpinfo structure */
+
+ free(tmp->dumpmem);
+ free(tmp);
+ }
+#endif /* defined(DISABLE_DUMP) */
}
+/* dump_size *******************************************************************
+ XXX
+
+*******************************************************************************/
-void mem_usagelog (int givewarnings)
+s4 dump_size(void)
{
- if ((memoryusage!=0) && givewarnings) {
- sprintf (logtext, "Allocated memory not returned: %d",
- (int)memoryusage);
- dolog();
-
-#ifdef DEBUG
- {
- memblock *mb = firstmemblock;
- while (mb) {
- sprintf (logtext, " Memory block size: %d",
- (int)(mb->length) );
- dolog();
- mem_characterlog ( ((unsigned char*)mb) + BLOCKOFFSET, mb->length);
- mb = mb->next;
- }
- }
-#endif
-
- }
+#if defined(DISABLE_DUMP)
+ /* use malloc memory for dump memory (for debugging only!) */
- if ((dumpsize!=0) && givewarnings) {
- sprintf (logtext, "Dump memory not returned: %d",(int)dumpsize);
- dolog();
- }
+ return 0;
+#else
+ dumpinfo *di;
+
+ /* If no threads are used, the dumpinfo structure is a static structure */
+ /* defined at the top of this file. */
+ di = DUMPINFO;
- sprintf (logtext, "Random/Dump - memory usage: %dK/%dK",
- (int)((maxmemusage+1023)/1024),
- (int)((maxdumpsize+1023)/1024) );
- dolog();
-
+ if (!di)
+ return 0;
+
+ return di->useddumpsize;
+#endif /* defined(DISABLE_DUMP) */
}
+
+/*
+ * These are local overrides for various environment variables in Emacs.
+ * Please do not remove this and leave it at the end of the file, where
+ * Emacs will automagically detect them.
+ * ---------------------------------------------------------------------
+ * Local variables:
+ * mode: c
+ * indent-tabs-mode: t
+ * c-basic-offset: 4
+ * tab-width: 4
+ * End:
+ */