1 /* 2 * Copyright 2002-2006, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 * 5 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved. 6 * Distributed under the terms of the NewOS License. 7 */ 8 #ifndef _KERNEL_HEAP_H 9 #define _KERNEL_HEAP_H 10 11 #include <OS.h> 12 13 // allocate 16MB initial heap for the kernel 14 #define INITIAL_HEAP_SIZE 16 * 1024 * 1024 15 // grow by another 4MB each time the heap runs out of memory 16 #define HEAP_GROW_SIZE 4 * 1024 * 1024 17 // allocate a dedicated 1MB area for dynamic growing 18 #define HEAP_DEDICATED_GROW_SIZE 1 * 1024 * 1024 19 // use areas for allocations bigger than 1MB 20 #define HEAP_AREA_USE_THRESHOLD 1 * 1024 * 1024 21 22 23 typedef struct heap_class_s { 24 const char *name; 25 uint32 initial_percentage; 26 size_t max_allocation_size; 27 size_t page_size; 28 size_t min_bin_size; 29 size_t bin_alignment; 30 uint32 min_count_per_page; 31 size_t max_waste_per_page; 32 } heap_class; 33 34 typedef struct heap_allocator_s heap_allocator; 35 36 37 #ifdef __cplusplus 38 extern "C" { 39 #endif 40 41 // malloc_nogrow disallows waiting for a grow to happen - only to be used by 42 // vm functions that may deadlock on a triggered area creation 43 void *malloc_nogrow(size_t size); 44 45 void *memalign(size_t alignment, size_t size); 46 47 void deferred_free(void* block); 48 49 void* malloc_referenced(size_t size); 50 void* malloc_referenced_acquire(void* data); 51 void malloc_referenced_release(void* data); 52 53 heap_allocator* heap_create_allocator(const char* name, addr_t base, 54 size_t size, const heap_class* heapClass); 55 void* heap_memalign(heap_allocator* heap, size_t alignment, size_t size); 56 status_t heap_free(heap_allocator* heap, void* address); 57 58 status_t heap_init(addr_t heapBase, size_t heapSize); 59 status_t heap_init_post_sem(); 60 status_t heap_init_post_thread(); 61 62 #ifdef __cplusplus 63 } 64 #endif 65 66 67 #ifdef __cplusplus 68 69 #include <new> 70 71 static const struct nogrow_t { 72 } nogrow = {}; 73 74 inline void* 75 operator new(size_t size, const nogrow_t& nogrow) 76 { 77 return malloc_nogrow(size); 78 } 79 80 #endif /* __cplusplus */ 81 82 83 #endif /* _KERNEL_MEMHEAP_H */ 84