* m4/assertion.m4: Fixed copyright header.
[cacao.git] / src / mm / memory.c
index c15c4011aa31c82086442211bae95b4a398dd105..4071d6bb430149a4d0736161ef8bda2edb4a664f 100644 (file)
-/************************* toolbox/memory.c ************************************
+/* src/mm/memory.c - memory management
 
-       Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
+   Copyright (C) 1996-2005, 2006, 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
-       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.
 
-*******************************************************************************/
+*/
+
+
+#include "config.h"
 
+#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 "callargs.h"
-#include "loging.h"
-#include "memory.h"
-
-
-       /********* allgemeine Typen, Variablen und Hilfsfunktionen *********/
-
-#define DUMPBLOCKSIZE  (2<<18)
-#define ALIGNSIZE           8
-
-typedef struct dumplist {
-       struct dumplist *prev;
-       char *dumpmem;
-} dumplist;
+#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 "vm/types.h"
 
+#include "arch.h"
 
-long int memoryusage = 0;
+#include "mm/memory.h"
 
-long int dumpsize = 0;
-long int dumpspace = 0;
-dumplist *topdumpblock = NULL;
+#include "native/native.h"
 
-long int maxmemusage = 0;
-long int maxdumpsize = 0;
+#include "threads/lock-common.h"
+#include "threads/threads-common.h"
 
-/* #define TRACECALLARGS */
+#include "toolbox/logging.h"
 
-#ifdef TRACECALLARGS
-static char  nomallocmem[16777216];
-static char *nomalloctop = nomallocmem + 16777216;
-static char *nomallocptr = nomallocmem;
+#include "vm/exceptions.h"
+#include "vm/global.h"
+#include "vm/stringlocal.h"
+#include "vm/vm.h"
 
-static void *lit_checked_alloc (int length)
-{
-       void *m;
+#include "vmcore/options.h"
 
-       nomallocptr = (void*) ALIGN ((long) nomallocptr, ALIGNSIZE);
-       
-       m = nomallocptr;
-       nomallocptr += length;
-       if (nomallocptr > nomalloctop) panic ("Out of memory");
-       return m;
-}
+#if defined(ENABLE_STATISTICS)
+# include "vmcore/statistics.h"
+#endif
 
-#else
+#include "vmcore/system.h"
 
-static void *lit_checked_alloc (int length)
-{
-       void *m = malloc(length);
-       if (!m) panic ("Out of memory");
-       return m;
-}
-
-#endif
 
+/* memory_mprotect *************************************************************
 
-static void *checked_alloc (int length)
-{
-       void *m = malloc(length);
-       if (!m) panic ("Out of memory");
-       return m;
-}
+   Convenience function for mprotect.  This function also does error
+   checking.
 
-static int mmapcodesize = 0;
-static void *mmapcodeptr = NULL;
+*******************************************************************************/
 
-void *mem_mmap(int length)
+void memory_mprotect(void *addr, size_t len, int prot)
 {
-       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;
+       if (system_mprotect(addr, len, prot) != 0)
+               vm_abort("memory_mprotect: system_mprotect failed: %s",
+                                strerror(errno));
 }
 
 
-#ifdef DEBUG
-
-       /************ Sichere Version des Speichermanages **************/
-
+/* memory_checked_alloc ********************************************************
 
-typedef struct memblock {
-       struct memblock *prev,*next;
-       int length;
-} memblock;
-
-#define BLOCKOFFSET    (ALIGN(sizeof(memblock),ALIGNSIZE))
-
-struct memblock *firstmemblock;
+   Allocated zeroed-out memory and does an OOM check.
 
+   ERROR HANDLING:
+      XXX If no memory could be allocated, this function justs *exists*.
 
+*******************************************************************************/
 
-void *mem_alloc(int length)
+void *memory_checked_alloc(size_t size)
 {
-       memblock *mb;
+       /* always allocate memory zeroed out */
 
-       if (length==0) return NULL;
-       mb = checked_alloc (length + BLOCKOFFSET);
+       void *p = calloc(size, 1);
 
-       mb -> prev = NULL;
-       mb -> next = firstmemblock;     
-       mb -> length = length;
+       if (p == NULL)
+               vm_abort("memory_checked_alloc: calloc failed: out of memory");
 
-       if (firstmemblock) firstmemblock -> prev = mb;
-       firstmemblock = mb;
-
-       memoryusage += length;
-       if (memoryusage > maxmemusage) maxmemusage = memoryusage;
-
-       return ((char*) mb) + BLOCKOFFSET;
+       return p;
 }
 
 
-void *lit_mem_alloc(int length)
+void *mem_alloc(s4 size)
 {
-       memblock *mb;
-
-       if (length==0) return NULL;
-       mb = lit_checked_alloc (length + BLOCKOFFSET);
-
-       mb -> prev = NULL;
-       mb -> next = firstmemblock;     
-       mb -> length = length;
-
-       if (firstmemblock) firstmemblock -> prev = mb;
-       firstmemblock = mb;
-
-       memoryusage += length;
-       if (memoryusage > maxmemusage) maxmemusage = memoryusage;
+       void *m;
 
-       return ((char*) mb) + BLOCKOFFSET;
-}
+       if (size == 0)
+               return NULL;
 
+#if defined(ENABLE_STATISTICS)
+       if (opt_stat) {
+               memoryusage += size;
 
-void mem_free(void *m, int length)
-{
-       memblock *mb;
-       if (!m) {
-               if (length==0) return;
-               panic ("returned memoryblock with address NULL, length != 0");
-               }
-
-       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;
-
-       free (mb);
-
-       memoryusage -= length;
-}
-
-
-void lit_mem_free(void *m, int length)
-{
-       memblock *mb;
-       if (!m) {
-               if (length==0) return;
-               panic ("returned memoryblock with address NULL, length != 0");
-               }
-
-       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;
-
-#ifdef TRACECALLARGS
-#else
-       free (mb);
+               if (memoryusage > maxmemusage)
+                       maxmemusage = memoryusage;
+       }
 #endif
 
-       memoryusage -= length;
-}
+       m = memory_checked_alloc(size);
 
+#if defined(ENABLE_MEMCHECK)
+       /* XXX we would like to poison the memory, but callers rely on */
+       /* the zeroing. This should change sooner or later.            */
+       /* memset(m, MEMORY_CLEAR_BYTE, size); */
+#endif
 
-void *mem_realloc (void *m1, int len1, int len2)
-{
-       void *m2;
-       
-       m2 = mem_alloc (len2);
-       memcpy (m2, m1, len1);
-       mem_free (m1, len1);
-
-       return m2;
+       return m;
 }
 
 
-
-
-static void mem_characterlog (unsigned char *m, int len)
+void *mem_realloc(void *src, s4 len1, s4 len2)
 {
-#      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();
-               }
-}
-
-#else
-               /******* Schnelle Version des Speichermanagers ******/
+       void *dst;
 
+       /* prevent compiler warnings */
 
-void *mem_alloc(int length)
-{
-       if (length==0) return NULL;
-
-       memoryusage += length;
-       if (memoryusage > maxmemusage) maxmemusage = memoryusage;
-       
-       return checked_alloc (length);
-}
+       dst = NULL;
 
+       if (src == NULL)
+               if (len1 != 0)
+                       vm_abort("mem_realloc: reallocating memoryblock with address NULL, length != 0");
 
-void *lit_mem_alloc(int length)
-{
-       if (length==0) return NULL;
+#if defined(ENABLE_STATISTICS)
+       if (opt_stat)
+               memoryusage = (memoryusage - len1) + len2;
+#endif
 
-       memoryusage += length;
-       if (memoryusage > maxmemusage) maxmemusage = memoryusage;
-       
-       return lit_checked_alloc (length);
-}
+#if defined(ENABLE_MEMCHECK)
+       if (len2 < len1)
+               memset((u1*)dst + len2, MEMORY_CLEAR_BYTE, len1 - len2);
+#endif
 
+       dst = realloc(src, len2);
 
-void mem_free(void *m, int length)
-{
-       if (!m) {
-               if (length==0) return;
-               panic ("returned memoryblock with address NULL, length != 0");
-               }
+       if (dst == NULL)
+               vm_abort("mem_realloc: realloc failed: out of memory");
 
-       memoryusage -= length;
+#if defined(ENABLE_MEMCHECK)
+       if (len2 > len1)
+               memset((u1*)dst + len1, MEMORY_CLEAR_BYTE, len2 - len1);
+#endif
 
-       free (m);
+       return dst;
 }
 
 
-void lit_mem_free(void *m, int length)
+void mem_free(void *m, s4 size)
 {
        if (!m) {
-               if (length==0) return;
-               panic ("returned memoryblock with address NULL, length != 0");
-               }
+               if (size == 0)
+                       return;
 
-       memoryusage -= length;
+               log_text("returned memoryblock with address NULL, length != 0");
+               assert(0);
+       }
 
-#ifdef TRACECALLARGS
-#else
-       free (m);
+#if defined(ENABLE_STATISTICS)
+       if (opt_stat)
+               memoryusage -= size;
 #endif
-}
 
+#if defined(ENABLE_MEMCHECK)
+       /* destroy the contents */
+       memset(m, MEMORY_CLEAR_BYTE, size);
+#endif
 
-void *mem_realloc (void *m1, int len1, int len2)
-{
-       void *m2;
-
-       if (!m1) {
-               if (len1!=0) 
-                 panic ("reallocating memoryblock with address NULL, length != 0");
-               }
-               
-       memoryusage = (memoryusage - len1) + len2;
-
-       m2 = realloc (m1, len2);
-       if (!m2) panic ("Out of memory");
-       return m2;
+       free(m);
 }
 
 
-#endif
-
-               /******* allgemeine Teile des Speichermanagers ******/
+/* memory_thread ***************************************************************
 
+   Prints regularly memory statistics.
 
+*******************************************************************************/
 
-long int mem_usage()
+#if defined(ENABLE_THREADS) && !defined(NDEBUG)
+static void memory_thread(void)
 {
-       return memoryusage;
-}
+       int32_t seconds;
 
+       /* If both arguments are specified, use the value of
+          ProfileMemoryUsage. */
 
+       if (opt_ProfileGCMemoryUsage)
+               seconds = opt_ProfileGCMemoryUsage;
 
+       if (opt_ProfileMemoryUsage)
+               seconds = opt_ProfileMemoryUsage;
 
+       while (true) {
+               /* sleep thread */
 
-void *dump_alloc(int length)
-{
-       void *m;
+               threads_sleep(seconds * 1000, 0);
 
-        if (length==0) return NULL;
-       
-       length = ALIGN (length, ALIGNSIZE);
+# if defined(ENABLE_STATISTICS)
+               /* Print current date and time (only when we print to the
+                  stdout). */
 
-       assert (length <= DUMPBLOCKSIZE);
-       assert (length > 0);
+               if (!opt_ProfileMemoryUsageGNUPlot)
+                       statistics_print_date();
 
-       if (dumpsize + length > dumpspace) {
-               dumplist *newdumpblock = checked_alloc (sizeof(dumplist));
+               /* print memory usage */
 
-               newdumpblock -> prev = topdumpblock;
-               topdumpblock = newdumpblock;
+               if (opt_ProfileMemoryUsage)
+                       statistics_print_memory_usage();
 
-               newdumpblock -> dumpmem = checked_alloc (DUMPBLOCKSIZE);
+               /* print GC memory usage */
 
-               dumpsize = dumpspace;
-               dumpspace += DUMPBLOCKSIZE;             
-               }
-       
-       m = topdumpblock -> dumpmem + DUMPBLOCKSIZE - (dumpspace - dumpsize);
-       dumpsize += length;
-       
-       if (dumpsize > maxdumpsize) {
-               maxdumpsize = dumpsize;
-               }
-               
-       return m;
-}   
-
-
-void *dump_realloc(void *ptr, int len1, int len2)
-{
-       void *p2 = dump_alloc (len2);
-       memcpy (p2, ptr, len1); 
-       return p2;
+               if (opt_ProfileGCMemoryUsage)
+                       statistics_print_gc_memory_usage();
+# endif
+       }
 }
+#endif
 
 
-long int dump_size()
-{
-       return dumpsize;
-}
-
+/* memory_start_thread *********************************************************
 
-void dump_release(long int size)
-{
-       assert (size >= 0 && size <= dumpsize);
-
-       dumpsize = size;
-       
-       while (dumpspace  >  dumpsize + DUMPBLOCKSIZE) {
-               dumplist *oldtop = topdumpblock;
-               
-               topdumpblock = oldtop -> prev;
-               dumpspace -= DUMPBLOCKSIZE;
-               
-#ifdef TRACECALLARGS
-#else
-               free (oldtop -> dumpmem);
-               free (oldtop);
-#endif
-               }               
-}
+   Starts the memory profiling thread.
 
+*******************************************************************************/
 
+#if defined(ENABLE_THREADS) && !defined(NDEBUG)
+bool memory_start_thread(void)
+{
+       utf *name;
 
+       name = utf_new_char("Memory Profiler");
 
-void mem_usagelog (int givewarnings)
-{
-       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
-                       
-               }
+       /* start the memory profiling thread */
 
-       if ((dumpsize!=0) && givewarnings) {
-               sprintf (logtext, "Dump memory not returned: %d",(int)dumpsize);
-               dolog();
-               }
+       if (!threads_thread_start_internal(name, memory_thread))
+               return false;
 
+       /* everything's ok */
 
-       sprintf (logtext, "Random/Dump - memory usage: %dK/%dK", 
-             (int)((maxmemusage+1023)/1024), 
-             (int)((maxdumpsize+1023)/1024) );
-       dolog();
-       
+       return true;
 }
+#endif
+
 
+/*
+ * 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:
+ * vim:noexpandtab:sw=4:ts=4:
+ */