Browse Source

Keep better track of mem_allocated and mem_requested.

Ioannis Koutras 13 years ago
parent
commit
b88cc334a6
3 changed files with 13 additions and 9 deletions
  1. 3 0
      src/custom_free.c
  2. 2 0
      src/custom_malloc.c
  3. 8 9
      src/sys_alloc.c

+ 3 - 0
src/custom_free.c

@@ -57,6 +57,9 @@ void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr) {
 
     remove_block(ptr, heap->used_blocks_head);
 
+    heap->dmm_stats.mem_allocated -= size;
+    heap->dmm_stats.mem_requested -= get_requested_size(ptr);
+
 #if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
     if(is_previous_free(ptr)) {
         if(get_owner(ptr) == get_owner(get_dlprevious(ptr))) {

+ 2 - 0
src/custom_malloc.c

@@ -95,6 +95,8 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
         push_block(ptr, heap->used_blocks_head);
 
         /* Update statistics */
+        heap->dmm_stats.mem_allocated += req_padding(size);
+        heap->dmm_stats.mem_requested += size;
         heap->dmm_stats.live_objects += 1;
         heap->dmm_stats.num_malloc += 1;
 

+ 8 - 9
src/sys_alloc.c

@@ -27,6 +27,7 @@
 #include "other.h"
 #include "sys_alloc.h"
 #include "block_header.h"
+#include "print_stats.h"
 
 void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
 
@@ -52,10 +53,13 @@ void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
     if(ptr == (void *) -1) {
         printf("sbrk problem for size of: %zu\n", allocation_size);
         printf("Error on sbrk: %s\n", strerror( errno ) );
+        return NULL;
     }   
     if(allocator->border_ptr != NULL && ptr != (void *) 
             ((char *) allocator->border_ptr + previous_size)) {
         printf("sbrk() does not return sequential space.\n");
+        print_stats(allocator);
+        return NULL;
     }
 #else
     if(allocator->remaining_size >= allocation_size) {
@@ -63,6 +67,7 @@ void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
         allocator->remaining_size -= allocation_size;
     } else {
         printf("No more free space.\n");
+        return NULL;
     }
 #endif /* WITH_MEMORY_SPACE_AWARENESS */
 
@@ -76,11 +81,6 @@ void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
     set_size(ptr, req_padding(size));
     set_previous_size_availability(ptr, previous_size_availability);
 
-    /* Update stats */
-    heap->dmm_stats.mem_allocated += req_padding(size);
-    heap->dmm_stats.mem_requested += size;
-
-
     /* FIXME To be refactored - START */
     set_requested_size(ptr, size);
     mark_used(ptr);
@@ -88,13 +88,12 @@ void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
     /* Update the used blocks list */
     push_block(ptr, heap->used_blocks_head);
 
-    // Begin of Stats
-
+    /* Update statistics */
+    heap->dmm_stats.mem_allocated += req_padding(size);
+    heap->dmm_stats.mem_requested += size;
     heap->dmm_stats.live_objects += 1;
     heap->dmm_stats.num_malloc += 1;
 
-    // End of Stats
-
     /* FIXME To be refactored - END */
 
 #ifdef HAVE_LOCKS