Browse Source

Make sizeof(long) the default heap alignment.

coderain 5 years ago
parent
commit
c25156ad46
2 changed files with 8 additions and 6 deletions
  1. 5 3
      crt/src/malloc.c
  2. 3 3
      kernel/src/heap.c

+ 5 - 3
crt/src/malloc.c

@@ -293,6 +293,8 @@ void *__crt_heap_realloc(__crt_heap_t *heap, void *ptr, size_t alignment, size_t
 
         heap_header_t *new_block = (heap_header_t*)(aligned_start - sizeof(heap_header_t));
         memmove(new_block, hole, sizeof(heap_header_t));
+        if (heap->next_offset == (uintptr_t)hole - (uintptr_t)heap->base) heap->next_offset += padding;
+
         hole = new_block;
         hole->size -= padding;
     }
@@ -332,17 +334,17 @@ void *aligned_alloc(size_t alignment, size_t size)
 
 void *realloc(void *ptr, size_t size)
 {
-    return __crt_heap_realloc(__crt_default_heap, ptr, 1, size);
+    return __crt_heap_realloc(__crt_default_heap, ptr, sizeof(long), size);
 }
 
 void *malloc(size_t size)
 {
-    return __crt_heap_realloc(__crt_default_heap, NULL, 1, size);
+    return __crt_heap_realloc(__crt_default_heap, NULL, sizeof(long), size);
 }
 
 void free(void *ptr)
 {
-    __crt_heap_realloc(__crt_default_heap, ptr, 1, 0);
+    __crt_heap_realloc(__crt_default_heap, ptr, sizeof(long), 0);
 }
 
 void *calloc(size_t nmemb, size_t size)

+ 3 - 3
kernel/src/heap.c

@@ -45,17 +45,17 @@ static void heap_problem(int problem)
 
 void *heap_realloc(heap_t *heap, void *ptr, uintptr_t size)
 {
-    return __crt_heap_realloc(&heap->crt, ptr, 1, size);
+    return __crt_heap_realloc(&heap->crt, ptr, sizeof(long), size);
 }
 
 void *heap_alloc(heap_t *heap, uintptr_t size)
 {
-    return __crt_heap_realloc(&heap->crt, NULL, 1, size);
+    return __crt_heap_realloc(&heap->crt, NULL, sizeof(long), size);
 }
 
 void heap_free(heap_t *heap, void *ptr)
 {
-    __crt_heap_realloc(&heap->crt, ptr, 1, 0);
+    __crt_heap_realloc(&heap->crt, ptr, sizeof(long), 0);
 }
 
 dword_t heap_create(heap_t *heap, uintptr_t start, uintptr_t end, dword_t flags, dword_t magic)