From 66554a4c9860d045da46b97197706e230d5c6e77 Mon Sep 17 00:00:00 2001 From: notion Date: Thu, 25 Jul 2002 14:49:29 +0000 Subject: [PATCH] Fixed the overflow bug in heap.c that would occur on allocation of all the memory of the heap. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@442 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/kernel/memheap.h | 6 ++++-- src/kernel/core/heap.c | 12 ++++-------- src/kernel/core/vm/vm.c | 4 +--- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/headers/private/kernel/memheap.h b/headers/private/kernel/memheap.h index 4e77e7871b..d405d63431 100755 --- a/headers/private/kernel/memheap.h +++ b/headers/private/kernel/memheap.h @@ -1,4 +1,4 @@ -/* +/* ** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved. ** Distributed under the terms of the NewOS License. */ @@ -8,7 +8,9 @@ #include #include -int heap_init(addr new_heap_base, unsigned int new_heap_size); +#define HEAP_SIZE 0x00400000 + +int heap_init(addr new_heap_base); int heap_init_postsem(kernel_args *ka); void *kmalloc(unsigned int size); void kfree(void *address); diff --git a/src/kernel/core/heap.c b/src/kernel/core/heap.c index 6fc80f5843..e179b28653 100644 --- a/src/kernel/core/heap.c +++ b/src/kernel/core/heap.c @@ -1,5 +1,3 @@ -/* Heap + other assorted stuff. Needs cleanup */ - /* ** Copyright 2001, Travis Geiselbrecht. All rights reserved. ** Distributed under the terms of the NewOS License. @@ -108,15 +106,13 @@ dump_bin_list(int argc, char **argv) */ int -heap_init(addr new_heap_base, unsigned int new_heap_size) +heap_init(addr new_heap_base) { - // ToDo: the heap size may overflow in certain circumstances, but I didn't like - // the NewOS fix for this... -- axeld. - + const unsigned int page_entries = PAGE_SIZE / sizeof(struct heap_page); // set some global pointers heap_alloc_table = (struct heap_page *)new_heap_base; - heap_size = new_heap_size; - heap_base = PAGE_ALIGN((unsigned int)heap_alloc_table + (heap_size / PAGE_SIZE) * sizeof(struct heap_page)); + heap_size = ((uint64)HEAP_SIZE * page_entries / (page_entries + 1)) & ~(PAGE_SIZE-1); + heap_base = (unsigned int)heap_alloc_table + PAGE_ALIGN(heap_size / page_entries); heap_base_ptr = heap_base; dprintf("heap_alloc_table = %p, heap_base = 0x%lx, heap_size = 0x%lx\n", heap_alloc_table, heap_base, heap_size); diff --git a/src/kernel/core/vm/vm.c b/src/kernel/core/vm/vm.c index 4b21251e03..5ea9a0a58d 100755 --- a/src/kernel/core/vm/vm.c +++ b/src/kernel/core/vm/vm.c @@ -35,8 +35,6 @@ #include #include -#define HEAP_SIZE 0x00400000 - #define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1)) #define ROUNDOWN(a, b) (((a) / (b)) * (b)) @@ -1667,7 +1665,7 @@ int vm_init(kernel_args *ka) // map in the new heap and initialize it heap_base = vm_alloc_from_ka_struct(ka, HEAP_SIZE, LOCK_KERNEL|LOCK_RW); dprintf("heap at 0x%lx\n", heap_base); - heap_init(heap_base, HEAP_SIZE); + heap_init(heap_base); // initialize the free page list and physical page mapper vm_page_init(ka);