* src/mm/dumpmemory.c (dump_alloc): Renamed to dumpmemory_get.
authorChristian Thalinger <twisti@complang.tuwien.ac.at>
Fri, 11 Jan 2008 13:42:25 +0000 (14:42 +0100)
committerChristian Thalinger <twisti@complang.tuwien.ac.at>
Fri, 11 Jan 2008 13:42:25 +0000 (14:42 +0100)
(dumpmemory_alloc): New function.
(dump_realloc): Renamed to dumpmemory_realloc.

* src/mm/dumpmemory.h (dump_allocation_t): Renamed useddumpsize to
used.
(dumpinfo_t): Renamed currentdumpblock to block, allocateddumpsize to
allocated, useddumpsize to used.
(DNEW, DMNEW, DMREALLOC): Defined to new function names.

* src/cacaoh/dummy.c (dump_alloc): Renamed to dumpmemory_get.

* src/vm/jit/verify/typeinfo.h (DNEW_TYPEVECTOR): Use DMNEW.
(DMNEW_TYPEVECTOR): Likewise.
(TYPEINFO_ALLOCMERGED): Likewise.

src/cacaoh/dummy.c
src/mm/dumpmemory.c
src/mm/dumpmemory.h
src/vm/jit/verify/typeinfo.h

index eb0b68d9277ac51c9a570e0cf312a694e4f51d38..905237ab69cf50a89342f25be71b711f08378b92 100644 (file)
@@ -557,7 +557,7 @@ void mem_free(void *m, int32_t size)
        free(m);
 }
 
-void *dump_alloc(int32_t size)
+void *dumpmemory_get(size_t size)
 {
        return malloc(size);
 }
index 52d4c3a3227dae8aa516120a9184598582ff70cc..db650c887f14db561051224504265404ed9ec084 100644 (file)
@@ -87,7 +87,7 @@ static void dump_check_canaries(dumpinfo_t *di, s4 bottomsize)
 
        da = di->allocations;
 
-       while (da && da->useddumpsize >= bottomsize) {
+       while (da && da->used >= bottomsize) {
                /* check canaries */
 
                pm = ((uint8_t *) da->mem) - MEMORY_CANARY_SIZE;
@@ -130,7 +130,67 @@ static void dump_check_canaries(dumpinfo_t *di, s4 bottomsize)
 #endif /* defined(ENABLE_MEMCHECK) */
 
 
-/* dump_alloc ******************************************************************
+/* dumpmemory_alloc ************************************************************
+
+   ATTENTION: This function must only be called from dumpmemory_get!
+
+   Allocate a new dump memory block.
+
+   IN:
+      di ..... dumpinfo_t of the current thread
+      size ... required memory size
+
+*******************************************************************************/
+
+void dumpmemory_alloc(dumpinfo_t *di, size_t size)
+{
+       dumpblock_t *db;
+       size_t       newblocksize;
+
+       /* Allocate a new dumpblock_t structure. */
+
+       db = memory_checked_alloc(sizeof(dumpblock_t));
+
+       /* If requested size is greater than the default, make the new
+          dump block as big as the size requested. Else use the default
+          size. */
+
+       if (size > DUMPBLOCKSIZE) {
+               newblocksize = size;
+       }
+       else {
+               newblocksize = DUMPBLOCKSIZE;
+       }
+
+       /* allocate dumpblock memory */
+
+       db->dumpmem = memory_checked_alloc(newblocksize);
+
+       db->size  = newblocksize;
+       db->prev  = di->block;
+       di->block = db;
+
+       /* Used dump size is previously allocated dump size, because the
+          remaining free memory of the previous dump block cannot be
+          used. */
+
+       di->used = di->allocated;
+
+       /* Increase the allocated dump size by the size of the new dump
+          block. */
+
+       di->allocated += newblocksize;
+
+#if defined(ENABLE_STATISTICS)
+       /* The amount of globally allocated dump memory (thread save). */
+
+       if (opt_stat)
+               globalallocateddumpsize += newblocksize;
+#endif
+}
+
+
+/* dumpmemory_get **************************************************************
 
    Allocate memory in the dump area.
 
@@ -149,17 +209,19 @@ static void dump_check_canaries(dumpinfo_t *di, s4 bottomsize)
    THREADS:
       dump_alloc is thread safe. Each thread has its own dump memory area.
 
-   dump_alloc is a fast allocator suitable for scratch memory that can be
-   collectively freed when the current activity (eg. compiling) is done.
+   This function is a fast allocator suitable for scratch memory that
+   can be collectively freed when the current activity (eg. compiling)
+   is done.
 
-   You cannot selectively free dump memory. Before you start allocating it, 
-   you remember the current size returned by `dump_size`. Later, when you no 
-   longer need the memory, call `dump_release` with the remembered size and
-   all dump memory allocated since the call to `dump_size` will be freed.
+   You cannot selectively free dump memory. Before you start
+   allocating it, you remember the current size returned by
+   `dump_size`. Later, when you no longer need the memory, call
+   `dump_release` with the remembered size and all dump memory
+   allocated since the call to `dump_size` will be freed.
 
 *******************************************************************************/
 
-void *dump_alloc(s4 size)
+void *dumpmemory_get(size_t size)
 {
 #if defined(DISABLE_DUMP)
 
@@ -175,69 +237,30 @@ void *dump_alloc(s4 size)
        s4          origsize = size; /* needed for the canary system */
 #endif
 
-       /* If no threads are used, the dumpinfo structure is a static structure   */
-       /* defined at the top of this file.                                       */
-
        di = DUMPINFO;
 
        if (size == 0)
                return NULL;
 
 #if defined(ENABLE_MEMCHECK)
-       size += 2*MEMORY_CANARY_SIZE;
+       size += 2 * MEMORY_CANARY_SIZE;
 #endif
 
        size = MEMORY_ALIGN(size, ALIGNSIZE);
 
-       if (di->useddumpsize + size > di->allocateddumpsize) {
-               dumpblock_t *newdumpblock;
-               s4         newdumpblocksize;
-
-               /* allocate a new dumplist structure */
-
-               newdumpblock = memory_checked_alloc(sizeof(dumpblock_t));
-
-               /* If requested size is greater than the default, make the new dump   */
-               /* block as big as the size requested. Else use the default size.     */
-
-               if (size > DUMPBLOCKSIZE) {
-                       newdumpblocksize = size;
-
-               } else {
-                       newdumpblocksize = DUMPBLOCKSIZE;
-               }
-
-               /* allocate dumpblock memory */
-
-               newdumpblock->dumpmem = memory_checked_alloc(newdumpblocksize);
-
-               newdumpblock->prev = di->currentdumpblock;
-               newdumpblock->size = newdumpblocksize;
-               di->currentdumpblock = newdumpblock;
+       /* Check if we have enough memory in the current memory block. */
 
-               /* Used dump size is previously allocated dump size, because the      */
-               /* remaining free memory of the previous dump block cannot be used.   */
+       if (di->used + size > di->allocated) {
+               /* If not, allocate a new one. */
 
-               di->useddumpsize = di->allocateddumpsize;
-
-               /* 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
+               dumpmemory_alloc(di, size);
        }
 
        /* current dump block base address + the size of the current dump
           block - the size of the unused memory = new start address  */
 
-       p = ((uint8_t *) di->currentdumpblock->dumpmem) +
-               di->currentdumpblock->size -
-               (di->allocateddumpsize - di->useddumpsize);
+       p = ((uint8_t *) di->block->dumpmem) + di->block->size -
+               (di->allocated - di->used);
 
 #if defined(ENABLE_MEMCHECK)
        {
@@ -247,10 +270,10 @@ void *dump_alloc(s4 size)
 
                /* add the allocation to our linked list of allocations */
 
-               da->next         = di->allocations;
-               da->mem          = ((uint8_t *) p) + MEMORY_CANARY_SIZE;
-               da->size         = origsize;
-               da->useddumpsize = di->useddumpsize;
+               da->next = di->allocations;
+               da->mem  = ((uint8_t *) p) + MEMORY_CANARY_SIZE;
+               da->size = origsize;
+               da->used = di->used;
 
                di->allocations = da;
 
@@ -276,14 +299,14 @@ void *dump_alloc(s4 size)
        }
 #endif /* defined(ENABLE_MEMCHECK) */
 
-       /* increase used dump size by the allocated memory size */
+       /* Increase used dump size by the allocated memory size. */
 
-       di->useddumpsize += size;
+       di->used += size;
 
 #if defined(ENABLE_STATISTICS)
        if (opt_stat)
-               if (di->useddumpsize > maxdumpsize)
-                       maxdumpsize = di->useddumpsize;
+               if (di->used > maxdumpsize)
+                       maxdumpsize = di->used;
 #endif
 
        return p;
@@ -292,25 +315,28 @@ void *dump_alloc(s4 size)
 }
 
 
-/* dump_realloc ****************************************************************
+/* dumpmemory_realloc **********************************************************
 
    Stupid realloc implementation for dump memory. Avoid, if possible.
 
 *******************************************************************************/
 
-void *dump_realloc(void *src, s4 len1, s4 len2)
+void *dumpmemory_realloc(void *src, s4 len1, s4 len2)
 {
 #if defined(DISABLE_DUMP)
        /* use malloc memory for dump memory (for debugging only!) */
 
        return mem_realloc(src, len1, len2);
 #else
-       void *dst = dump_alloc(len2);
+       void *dst;
+
+       dst = dumpmemory_get(len2);
 
        (void) system_memcpy(dst, src, len1);
 
 #if defined(ENABLE_MEMCHECK)
        /* destroy the source */
+
        (void) system_memset(src, MEMORY_CLEAR_BYTE, len1);
 #endif
 
@@ -348,13 +374,10 @@ void dump_release(s4 size)
 
        dumpinfo_t *di;
 
-       /* If no threads are used, the dumpinfo structure is a static structure   */
-       /* defined at the top of this file.                                       */
-
        di = DUMPINFO;
 
-       if ((size < 0) || (size > di->useddumpsize))
-               vm_abort("Illegal dump release size: %d", size);
+       if ((size < 0) || (size > di->used))
+               vm_abort("dump_release: Illegal dump release size: %d", size);
 
 #if defined(ENABLE_MEMCHECK)
        {
@@ -367,7 +390,8 @@ void dump_release(s4 size)
                /* iterate over all dump memory allocations about to be released */
 
                da = di->allocations;
-               while (da && da->useddumpsize >= size) {
+
+               while ((da != NULL) && (da->used >= size)) {
                        next = da->next;
 
                        /* invalidate the freed memory */
@@ -382,15 +406,15 @@ void dump_release(s4 size)
        }
 #endif /* defined(ENABLE_MEMCHECK) */
 
-       /* reset the used dump size to the size specified */
+       /* Reset the used dump size to the size specified. */
 
-       di->useddumpsize = size;
+       di->used = size;
 
-       while (di->currentdumpblock && di->allocateddumpsize - di->currentdumpblock->size >= di->useddumpsize) {
-               dumpblock_t *tmp = di->currentdumpblock;
+       while ((di->block != NULL) && di->allocated - di->block->size >= di->used) {
+               dumpblock_t *tmp = di->block;
 
-               di->allocateddumpsize -= tmp->size;
-               di->currentdumpblock = tmp->prev;
+               di->allocated -= tmp->size;
+               di->block      = tmp->prev;
 
 #if defined(ENABLE_STATISTICS)
                /* the amount of globally allocated dump memory (thread save) */
@@ -399,7 +423,7 @@ void dump_release(s4 size)
                        globalallocateddumpsize -= tmp->size;
 #endif
 
-               /* release the dump memory and the dumpinfo structure */
+               /* Release the dump memory and the dumpinfo structure. */
 
                system_free(tmp->dumpmem);
                system_free(tmp);
@@ -426,15 +450,12 @@ s4 dump_size(void)
 
        dumpinfo_t *di;
 
-       /* If no threads are used, the dumpinfo structure is a static
-          structure defined at the top of this file. */
-
        di = DUMPINFO;
 
        if (di == NULL)
                return 0;
 
-       return di->useddumpsize;
+       return di->used;
 
 #endif /* defined(DISABLE_DUMP) */
 }
index def812624a47ea7cdf1aeefe390709fa73588b9b..f08bdf9d08cc43f3cef00edb74a60af8b2f088f3 100644 (file)
@@ -65,7 +65,7 @@ typedef struct dump_allocation_t dump_allocation_t;
 struct dump_allocation_t {
        dump_allocation_t *next;
        void              *mem;
-       int32_t            useddumpsize;
+       int32_t            used;
        int32_t            size;
 };
 #endif
@@ -74,9 +74,9 @@ struct dump_allocation_t {
 /* dumpinfo *******************************************************************/
 
 struct dumpinfo_t {
-       dumpblock_t       *currentdumpblock;        /* the current top-most block */
-       int32_t            allocateddumpsize;     /* allocated bytes in this area */
-       int32_t            useddumpsize;          /* used bytes in this dump area */
+       dumpblock_t       *block;                   /* the current top-most block */
+       int32_t            allocated;             /* allocated bytes in this area */
+       int32_t            used;                  /* used bytes in this dump area */
 #if defined(ENABLE_MEMCHECK)
        dump_allocation_t *allocations;       /* list of allocations in this area */
 #endif
@@ -85,15 +85,15 @@ struct dumpinfo_t {
 
 /* convenience macros *********************************************************/
 
-#define DNEW(type)            ((type *) dump_alloc(sizeof(type)))
-#define DMNEW(type,num)       ((type *) dump_alloc(sizeof(type) * (num)))
-#define DMREALLOC(ptr,type,num1,num2) dump_realloc((ptr), sizeof(type) * (num1), \
+#define DNEW(type)            ((type *) dumpmemory_get(sizeof(type)))
+#define DMNEW(type,num)       ((type *) dumpmemory_get(sizeof(type) * (num)))
+#define DMREALLOC(ptr,type,num1,num2) dumpmemory_realloc((ptr), sizeof(type) * (num1), \
                                                           sizeof(type) * (num2))
 
 /* function prototypes ********************************************************/
 
-void    *dump_alloc(int32_t size);
-void    *dump_realloc(void *src, int32_t len1, int32_t len2);
+void    *dumpmemory_get(size_t size);
+void    *dumpmemory_realloc(void *src, int32_t len1, int32_t len2);
 int32_t  dump_size(void);
 void     dump_release(int32_t size);
 
index 10125f12cd620f368e1fa803f98910c21427d49c..38e8086bf01d8bfeeb799aa3f885dc1723a0b649 100644 (file)
@@ -267,10 +267,10 @@ struct typedescriptor {
     ((size) * sizeof(varinfo)) 
 
 #define DNEW_TYPEVECTOR(size)                                          \
-    ((varinfo*)dump_alloc(TYPEVECTOR_SIZE(size)))
+    ((varinfo *) DMNEW(uint8_t, TYPEVECTOR_SIZE(size)))
 
 #define DMNEW_TYPEVECTOR(num,size)                                             \
-    ((void*)dump_alloc((num) * TYPEVECTOR_SIZE(size)))
+    ((void *) DMNEW(uint8_t, (num) * TYPEVECTOR_SIZE(size)))
 
 #define MGET_TYPEVECTOR(array,index,size) \
     ((varinfo*) (((u1*)(array)) + TYPEVECTOR_SIZE(size) * (index)))
@@ -278,8 +278,8 @@ struct typedescriptor {
 /* internally used macros ***************************************************/
 
 /* internal, don't use this explicitly! */
-#define TYPEINFO_ALLOCMERGED(mergedlist,count)                  \
-    do {(mergedlist) = (typeinfo_mergedlist*)dump_alloc(        \
+#define TYPEINFO_ALLOCMERGED(mergedlist,count)                                 \
+    do {(mergedlist) = (typeinfo_mergedlist *) DMNEW(uint8_t,  \
             sizeof(typeinfo_mergedlist)                         \
             + ((count)-1)*sizeof(classinfo*));} while(0)