Browse Source

Few functional changes on border_ptr, now it points to the memory block which was allocated last.

Ioannis Koutras 13 years ago
parent
commit
1848841db0
3 changed files with 17 additions and 13 deletions
  1. 3 3
      private-include/block_header.h
  2. 2 2
      src/block_header.c
  3. 12 8
      src/sys_alloc.c

+ 3 - 3
private-include/block_header.h

@@ -80,11 +80,11 @@ void set_requested_size(void *ptr, size_t size);
  * Set the availability and the size of the previous memory block's data
  *
  * \param ptr 		The pointer to the data part of the current memory block.
- * \param previous_size The size for the data part of the previous memory 
- * block on data layout level.
+ * \param previous_size_availability The size for the data part of the previous
+ * memory block on data layout level.
  *
  */
-void set_previous_size(void *ptr, size_t previous_size);
+void set_previous_size_availability(void *ptr, size_t previous_size_availability);
 
 /**
  * Set the next memory block of a block

+ 2 - 2
src/block_header.c

@@ -39,8 +39,8 @@ bool is_previous_free(void *ptr) {
     return (bool) (get_header(ptr)->previous_size & 1);
 }
 
-void set_previous_size(void *ptr, size_t previous_size) {
-    get_header(ptr)->previous_size = previous_size;
+void set_previous_size_availability(void *ptr, size_t previous_size_availability) {
+    get_header(ptr)->previous_size = previous_size_availability;
 }
 
 size_t get_previous_size(void *ptr) {

+ 12 - 8
src/sys_alloc.c

@@ -13,7 +13,7 @@
 
 void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
 
-    size_t allocation_size, previous_size;
+    size_t allocation_size, previous_size, previous_size_availability;
     void *ptr;
 
 #ifdef HAVE_LOCKS
@@ -23,9 +23,11 @@ void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
     allocation_size = req_padding(size) + HEADER_SIZE;
 
     if(allocator->border_ptr != NULL) {
-        previous_size = get_size_availability(allocator->border_ptr);
+        previous_size = get_size(allocator->border_ptr);
+        previous_size_availability = get_size_availability(allocator->border_ptr);
     } else {
         previous_size = 0;
+        previous_size_availability = 1; /* Occupied and of 0 size */
     }
 
 #ifndef WITH_MEMORY_SPACE_AWARENESS
@@ -34,26 +36,28 @@ void *sys_alloc(allocator_t *allocator, heap_t *heap, size_t size) {
         printf("sbrk problem for size of: %zu\n", allocation_size);
         printf( "Error on sbrk: %s\n", strerror( errno ) );
     }   
-    if(allocator->border_ptr != NULL &&
-            ptr != (void *) ((char *) allocator->border_ptr + previous_size)) {
+    if(allocator->border_ptr != NULL && ptr != (void *) 
+            ((char *) allocator->border_ptr + previous_size)) {
         printf("sbrk() does not return sequential space.\n");
     }
 #else
     if(allocator->remaining_size >= allocation_size) {
-        ptr = allocator->border_ptr;
+        ptr = (void *) ((char *) allocator->border_ptr + previous_size);
         allocator->remaining_size -= allocation_size;
     } else {
         printf("No more free space.\n");
     }
 #endif /* WITH_MEMORY_SPACE_AWARENESS */
 
-    allocator->border_ptr = (char *) allocator->border_ptr + allocation_size;
-
+    /* Go to the data part of the block */
     ptr = (void *) ((char *) ptr + HEADER_SIZE);
+    
+    /* Set the new border pointer */
+    allocator->border_ptr = ptr;
 
     /* Set some values for block header */
     set_size(ptr, req_padding(size));
-    set_previous_size(ptr, previous_size);
+    set_previous_size_availability(ptr, previous_size_availability);
 
     /* Update stats */
     heap->dmm_stats.mem_allocated += req_padding(size);