* Removed all Id tags.
[cacao.git] / src / mm / memory.c
index 188f57ce4c9530e40509c3bb8188094bdbcff199..9c3034491c051994ef2c9aeaf20061b81fb5308d 100644 (file)
@@ -1,6 +1,6 @@
-/* src/mm/memory.c - 
+/* src/mm/memory.c - memory management
 
-   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   Copyright (C) 1996-2005, 2006, 2007 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
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Contact: cacao@cacaojvm.org
-
-   Authors: Reinhard Grafl
-            Christian Thalinger
-            Edwin Steiner
-
-   $Id: memory.c 6276 2007-01-04 21:48:51Z twisti $
-
 */
 
 
 #include "arch.h"
 
 #include "mm/memory.h"
+
 #include "native/native.h"
 
-#if defined(ENABLE_THREADS)
-# include "threads/native/lock.h"
-# include "threads/native/threads.h"
-#else
-# include "threads/none/lock.h"
-#endif
+#include "threads/lock-common.h"
+#include "threads/threads-common.h"
 
 #include "toolbox/logging.h"
+
 #include "vm/exceptions.h"
 #include "vm/global.h"
-#include "vm/options.h"
-#include "vm/statistics.h"
 #include "vm/stringlocal.h"
 #include "vm/vm.h"
 
+#include "vmcore/options.h"
+
+#if defined(ENABLE_STATISTICS)
+# include "vmcore/statistics.h"
+#endif
+
 
 /* constants for ENABLE_MEMCHECK **********************************************/
 
@@ -104,11 +98,11 @@ static dumpinfo_t _no_threads_dumpinfo;
 #define DEFAULT_CODE_MEMORY_SIZE    128 * 1024 /* defaulting to 128kB         */
 
 #if defined(ENABLE_THREADS)
-static java_objectheader *lock_code_memory = NULL;
+static java_object_t *lock_code_memory = NULL;
 #endif
-static void              *code_memory      = NULL;
-static int                code_memory_size = 0;
-static int                pagesize         = 0;
+static void          *code_memory      = NULL;
+static int            code_memory_size = 0;
+static int            pagesize         = 0;
 
 
 /* memory_init *****************************************************************
@@ -120,7 +114,9 @@ static int                pagesize         = 0;
 bool memory_init(void)
 {
 #if defined(ENABLE_THREADS)
-       lock_code_memory = NEW(java_objectheader);
+       /* create lock for code memory */
+
+       lock_code_memory = NEW(java_object_t);
 
        lock_init_object_lock(lock_code_memory);
 #endif
@@ -192,7 +188,7 @@ static void *memory_checked_alloc(s4 size)
        void *p = calloc(size, 1);
 
        if (p == NULL)
-               exceptions_throw_outofmemory_exit();
+               vm_abort("memory_checked_alloc: calloc failed: out of memory");
 
        return p;
 }
@@ -309,6 +305,10 @@ void *mem_realloc(void *src, s4 len1, s4 len2)
 {
        void *dst;
 
+       /* prevent compiler warnings */
+
+       dst = NULL;
+
        if (src == NULL)
                if (len1 != 0)
                        vm_abort("mem_realloc: reallocating memoryblock with address NULL, length != 0");
@@ -326,7 +326,7 @@ void *mem_realloc(void *src, s4 len1, s4 len2)
        dst = realloc(src, len2);
 
        if (dst == NULL)
-               vm_abort("realloc failed: out of memory");
+               vm_abort("mem_realloc: realloc failed: out of memory");
 
 #if defined(ENABLE_MEMCHECK)
        if (len2 > len1)
@@ -361,6 +361,78 @@ void mem_free(void *m, s4 size)
 }
 
 
+/* memory_thread ***************************************************************
+
+   Prints regularly memory statistics.
+
+*******************************************************************************/
+
+#if defined(ENABLE_THREADS) && !defined(NDEBUG)
+static void memory_thread(void)
+{
+       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 */
+
+               threads_sleep(seconds * 1000, 0);
+
+# if defined(ENABLE_STATISTICS)
+               /* Print current date and time (only when we print to the
+                  stdout). */
+
+               if (!opt_ProfileMemoryUsageGNUPlot)
+                       statistics_print_date();
+
+               /* print memory usage */
+
+               if (opt_ProfileMemoryUsage)
+                       statistics_print_memory_usage();
+
+               /* print GC memory usage */
+
+               if (opt_ProfileGCMemoryUsage)
+                       statistics_print_gc_memory_usage();
+# endif
+       }
+}
+#endif
+
+
+/* memory_start_thread *********************************************************
+
+   Starts the memory profiling thread.
+
+*******************************************************************************/
+
+#if defined(ENABLE_THREADS) && !defined(NDEBUG)
+bool memory_start_thread(void)
+{
+       utf *name;
+
+       name = utf_new_char("Memory Profiler");
+
+       /* start the memory profiling thread */
+
+       if (!threads_thread_start_internal(name, memory_thread))
+               return false;
+
+       /* everything's ok */
+
+       return true;
+}
+#endif
+
+
 /* dump_check_canaries *********************************************************
 
    Check canaries in dump memory.