Pārlūkot izejas kodu

Integrated allocator and heap information inside malloc() and free().

Ioannis Koutras 13 gadi atpakaļ
vecāks
revīzija
f0117fc98c
10 mainītis faili ar 86 papildinājumiem un 83 dzēšanām
  1. 16 3
      custom_free.c
  2. 0 4
      custom_free.h
  3. 22 3
      custom_malloc.c
  4. 0 4
      custom_malloc.h
  5. 19 25
      dmm_init.c
  6. 1 1
      dmm_init.h
  7. 12 0
      dmmlib.h
  8. 3 0
      heap.h
  9. 6 25
      larson.c
  10. 7 18
      test.c

+ 16 - 3
custom_free.c

@@ -1,15 +1,24 @@
-#include "custom_free.h"
+#include "dmmlib.h"
 #include "other.h"
 #ifdef HAVE_LOCKS
 #include "posix_lock.h"
 #endif /* HAVE_LOCKS */
 #include "block_header.h"
 
-void custom_free(heap_t* heap, void *ptr) {
+void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr) {
     size_t size;
-    int fixed_list_id, i;
+    int heap_id, fixed_list_id, i;
     maptable_node_t *current_maptable_node;
 
+    if(allocator == NULL) {
+        allocator = &systemallocator;
+    }
+    
+    if(heap == NULL) {
+        heap_id = map_thread_heap();
+        heap = &allocator->heaps[heap_id];
+    }
+
     size = get_size(ptr);
 
 #ifdef HAVE_LOCKS	
@@ -49,3 +58,7 @@ void custom_free(heap_t* heap, void *ptr) {
 #endif /* HAVE_LOCKS */
 }
 
+void custom_free(void *ptr) {
+    custom_ahfree(NULL, NULL, ptr);
+}
+

+ 0 - 4
custom_free.h

@@ -1,4 +0,0 @@
-#include "heap.h"
-
-void custom_free(heap_t* heap, void *ptr);
-

+ 22 - 3
custom_malloc.c

@@ -1,18 +1,33 @@
-#include "custom_malloc.h"
+#include "dmmlib.h"
+#include <stdbool.h>
 #ifdef HAVE_LOCKS
 #include "posix_lock.h"
 #endif /* HAVE_LOCKS */
+#include "dmm_init.h"
 #include "other.h"
 #include "sys_alloc.h"
 #include "block_header.h"
 #include "dmm_adaptor.h"
 
-void * custom_malloc(heap_t* heap, size_t size) {
+void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
     void *ptr;
-    int fixed_list_id, i;
+    int heap_id, fixed_list_id, i;
     maptable_node_t *current_maptable_node;
     void *current_block, *previous_block;
 
+    // Go to the system allocator if none was given
+    if(allocator == NULL) {
+        allocator = &systemallocator;
+        if(allocator->initialized != true) {
+            dmm_init(allocator);
+        }
+    }
+
+    if(heap == NULL) {
+        heap_id = map_thread_heap();
+        heap = &allocator->heaps[heap_id];
+    }
+
     ptr = NULL;
     previous_block = NULL;
 
@@ -84,3 +99,7 @@ void * custom_malloc(heap_t* heap, size_t size) {
     return ptr;
 }
 
+void * custom_malloc(size_t size) {
+    return custom_ahmalloc(NULL, NULL, size);
+}
+

+ 0 - 4
custom_malloc.h

@@ -1,4 +0,0 @@
-#include "heap.h"
-
-void * custom_malloc(heap_t* heap, size_t size);
-

+ 19 - 25
dmm_init.c

@@ -4,36 +4,32 @@
 #endif /* HAVE_LOCKS */
 #include "dmm_init.h"
 
-allocator_t * dmm_init(void) {
+void dmm_init(allocator_t *allocator) {
     int i;
-    allocator_t *main_allocator;
     heap_t *current_heap;
     maptable_node_t *maptablenode;
 
-    /* FIXME Replace sbrk with a library call */
-    main_allocator = (allocator_t *) sbrk((int) sizeof(allocator_t));
-
     for(i = 0; i < NUM_HEAPS; i++) {
-        main_allocator->heaps[i].maptable_head = NULL;
-        main_allocator->heaps[i].free_list_head = NULL;
-        main_allocator->heaps[i].used_blocks_head = NULL;
-        main_allocator->heaps[i].rov_ptr = NULL;
-        main_allocator->heaps[i].num_objects = 0;
-        main_allocator->heaps[i].dmm_stats.mem_allocated = 0;
-        main_allocator->heaps[i].dmm_stats.mem_requested = 0;
-        main_allocator->heaps[i].dmm_stats.live_objects = 0;
-        main_allocator->heaps[i].dmm_stats.num_malloc = 0;
-        main_allocator->heaps[i].dmm_stats.num_free = 0;
+        allocator->heaps[i].maptable_head = NULL;
+        allocator->heaps[i].free_list_head = NULL;
+        allocator->heaps[i].used_blocks_head = NULL;
+        allocator->heaps[i].rov_ptr = NULL;
+        allocator->heaps[i].num_objects = 0;
+        allocator->heaps[i].dmm_stats.mem_allocated = 0;
+        allocator->heaps[i].dmm_stats.mem_requested = 0;
+        allocator->heaps[i].dmm_stats.live_objects = 0;
+        allocator->heaps[i].dmm_stats.num_malloc = 0;
+        allocator->heaps[i].dmm_stats.num_free = 0;
 
         // Knobs initialization
-        main_allocator->heaps[i].dmm_knobs.max_coalesce_size = -1;
+        allocator->heaps[i].dmm_knobs.max_coalesce_size = -1;
         // FIXME Create a constant for the initial value of the next
         // variables
-        main_allocator->heaps[i].dmm_knobs.frag_threshold = 1.0;
-        main_allocator->heaps[i].dmm_knobs.mem_threshold = 17000;
+        allocator->heaps[i].dmm_knobs.frag_threshold = 1.0;
+        allocator->heaps[i].dmm_knobs.mem_threshold = 17000;
 
 #ifdef HAVE_LOCKS
-        pthread_mutex_init(&main_allocator->heaps[i].mutex, NULL);
+        pthread_mutex_init(&allocator->heaps[i].mutex, NULL);
 #endif /* HAVE_LOCKS */
     }
 
@@ -41,7 +37,7 @@ allocator_t * dmm_init(void) {
     // 2 first ones with 32, 64, 128 and 256 (4 fixed lists per heap)
     // 2 last ones with 64 and 256 (2 fixed lists per heap)
     // 2 * 4 + 2 * 2 = 12 maptable nodes
-    current_heap = &main_allocator->heaps[0];
+    current_heap = &allocator->heaps[0];
     maptablenode = (maptable_node_t *) sbrk((int)(12*(sizeof(maptable_node_t))));
 
     maptablenode->size = 32;
@@ -57,7 +53,7 @@ allocator_t * dmm_init(void) {
     (maptablenode+3)->size = 256;
     (maptablenode+3)->fixed_list_head = NULL;
     (maptablenode+3)->next = NULL;
-    current_heap = &main_allocator->heaps[1];
+    current_heap = &allocator->heaps[1];
     maptablenode += 4;
     maptablenode->size = 32;
     maptablenode->fixed_list_head = NULL;
@@ -72,7 +68,7 @@ allocator_t * dmm_init(void) {
     (maptablenode+3)->size = 256;
     (maptablenode+3)->fixed_list_head = NULL;
     (maptablenode+3)->next = NULL;
-    current_heap = &main_allocator->heaps[2];
+    current_heap = &allocator->heaps[2];
     maptablenode += 4;
     maptablenode->size = 64;
     maptablenode->fixed_list_head = NULL;
@@ -81,7 +77,7 @@ allocator_t * dmm_init(void) {
     (maptablenode+1)->size = 256;
     (maptablenode+1)->fixed_list_head = NULL;
     (maptablenode+1)->next = NULL;
-    current_heap = &main_allocator->heaps[3];
+    current_heap = &allocator->heaps[3];
     maptablenode += 2;
     maptablenode->size = 64;
     maptablenode->fixed_list_head = NULL;
@@ -90,7 +86,5 @@ allocator_t * dmm_init(void) {
     (maptablenode+1)->size = 256;
     (maptablenode+1)->fixed_list_head = NULL;
     (maptablenode+1)->next = NULL;
-
-    return main_allocator;
 }
 

+ 1 - 1
dmm_init.h

@@ -3,6 +3,6 @@
 
 #include "heap.h"
 
-allocator_t *dmm_init(void);
+void dmm_init(allocator_t *allocator);
 
 #endif /* DMM_INIT_H */

+ 12 - 0
dmmlib.h

@@ -0,0 +1,12 @@
+#ifndef DMMLIB_H
+#include "heap.h"
+
+static allocator_t systemallocator;
+
+void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size);
+void * custom_malloc(size_t size);
+
+void custom_ahfree(allocator_t *allocator, heap_t* heap, void *ptr);
+void custom_free(void *ptr);
+
+#endif /* DMMLIB_H */

+ 3 - 0
heap.h

@@ -9,6 +9,8 @@
 #define HEAP_H
 
 #include <stdint.h>
+#include <stdbool.h>
+
 #ifdef HAVE_LOCKS
 #include <pthread.h>
 #endif /* HAVE_LOCKS */
@@ -92,6 +94,7 @@ typedef struct heap_s {
  */
 typedef struct allocator_s {
 	heap_t heaps[NUM_HEAPS]; /**< \brief The heaps that the allocator manages. */
+	bool initialized; /**< \brief Initialization flag of the allocator. */
 } allocator_t;
 
 #endif /* HEAP_H */

+ 6 - 25
larson.c

@@ -4,11 +4,8 @@
 #include <string.h>
 #include <assert.h>
 #include <unistd.h>
-#include "custom_malloc.h"
-#include "custom_free.h"
-#include "other.h"
+#include "dmmlib.h"
 #include "lran2.h"
-#include "dmm_init.h"
 
 #define MAX_THREADS     100
 #define MAX_BLOCKS  1000000
@@ -46,8 +43,6 @@ typedef struct thr_data {
 
 } thread_data;
 
-allocator_t *myallocator;
-
 int volatile stopflag = FALSE;
 int min_size = 10, max_size = 500;
 struct lran2_st rgen;
@@ -85,15 +80,9 @@ static void warmup(char **blkp, int num_chunks) {
     int blk_size;
     LPVOID tmp;
 
-    heap_t *myheap;
-    int heap_id;
-
-    heap_id = map_thread_heap();
-    myheap = &myallocator->heaps[heap_id];	
-
     for(cblks = 0; cblks < num_chunks; cblks++) {
         blk_size = min_size + lran2(&rgen) % (max_size - min_size);
-        blkp[cblks] = (char *) custom_malloc(myheap, (size_t) blk_size);
+        blkp[cblks] = (char *) custom_malloc((size_t) blk_size);
         blksize[cblks] = blk_size;
         assert(blkp[cblks] != NULL);
     }
@@ -108,10 +97,10 @@ static void warmup(char **blkp, int num_chunks) {
 
     for(cblks=0; cblks < 4 * num_chunks; cblks++) {
         victim = lran2(&rgen) % num_chunks;
-        custom_free(myheap, blkp[victim]);
+        custom_free(blkp[victim]);
 
         blk_size = min_size + lran2(&rgen) % (max_size - min_size);
-        blkp[victim] = (char *) custom_malloc(myheap, (size_t) blk_size);
+        blkp[victim] = (char *) custom_malloc((size_t) blk_size);
         blksize[victim] = blk_size;
         assert(blkp[victim] != NULL);
     }
@@ -124,12 +113,6 @@ static void * exercise_heap( void *pinput) {
     long          blk_size;
     int           range;
 
-    heap_t *myheap;
-    int heap_id;
-
-    heap_id = map_thread_heap();
-    myheap = &myallocator->heaps[heap_id];
-
     if( stopflag ) return 0;
 
     pdea = (thread_data *) pinput;
@@ -140,11 +123,11 @@ static void * exercise_heap( void *pinput) {
     /* allocate NumBlocks chunks of random size */
     for(cblks=0; cblks < pdea->NumBlocks; cblks++) {
         victim = lran2(&pdea->rgen)%pdea->asize;
-        custom_free(myheap, pdea->array[victim]);
+        custom_free(pdea->array[victim]);
         pdea->cFrees++;
 
         blk_size = pdea->min_size+lran2(&pdea->rgen)%range;
-        pdea->array[victim] = (char *) custom_malloc(myheap, (size_t) blk_size);
+        pdea->array[victim] = (char *) custom_malloc((size_t) blk_size);
 
         pdea->blksize[victim] = blk_size;
         assert(pdea->array[victim] != NULL);
@@ -288,8 +271,6 @@ int main(void) {
     int num_rounds;
     int chperthread;
 
-    myallocator = dmm_init();
-
     printf("Larson benchmark\n");
 
     printf("runtime (sec): ") ;

+ 7 - 18
test.c

@@ -1,24 +1,13 @@
 #include <stdio.h>
-#include "heap.h"
-#include "other.h"
-#include "dmm_init.h"
-#include "custom_malloc.h"
-#include "custom_free.h"
+#include "dmmlib.h"
 
 int main(void) {
-    allocator_t *myallocator;
-    heap_t *myheap;
-    int heap_id;
     void *p1, *p2, *p3;
 
-    myallocator = dmm_init();
-    heap_id = map_thread_heap();
-    printf("This thread accesses heap %d\n", heap_id);
-    myheap = &myallocator->heaps[heap_id];
-    p1 = custom_malloc(myheap, (size_t) 1024);
-    custom_free(myheap, p1);
-    p2 = custom_malloc(myheap, (size_t) 2855);
-    custom_free(myheap, p2);
-    p3 = custom_malloc(myheap, (size_t) 3018);
-    custom_free(myheap, p3);
+    p1 = custom_malloc((size_t) 1024);
+    custom_free(p1);
+    p2 = custom_malloc((size_t) 2855);
+    custom_free(p2);
+    p3 = custom_malloc((size_t) 3018);
+    custom_free(p3);
 }