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 #include "kernel_debug_config.h" 14 15 16 #if USE_GUARDED_HEAP_FOR_MALLOC && USE_GUARDED_HEAP_FOR_OBJECT_CACHE 17 18 // This requires a lot of up-front memory to boot at all... 19 #define INITIAL_HEAP_SIZE 128 * 1024 * 1024 20 // ... and a lot of reserves to keep running. 21 #define HEAP_GROW_SIZE 128 * 1024 * 1024 22 23 #else // USE_GUARDED_HEAP_FOR_MALLOC && USE_GUARDED_HEAP_FOR_OBJECT_CACHE 24 25 // allocate 16MB initial heap for the kernel 26 #define INITIAL_HEAP_SIZE 16 * 1024 * 1024 27 // grow by another 4MB each time the heap runs out of memory 28 #define HEAP_GROW_SIZE 4 * 1024 * 1024 29 // allocate a dedicated 1MB area for dynamic growing 30 #define HEAP_DEDICATED_GROW_SIZE 1 * 1024 * 1024 31 // use areas for allocations bigger than 1MB 32 #define HEAP_AREA_USE_THRESHOLD 1 * 1024 * 1024 33 34 #endif // !(USE_GUARDED_HEAP_FOR_MALLOC && USE_GUARDED_HEAP_FOR_OBJECT_CACHE) 35 36 37 // allocation/deallocation flags for {malloc,free}_etc() 38 #define HEAP_DONT_WAIT_FOR_MEMORY 0x01 39 #define HEAP_DONT_LOCK_KERNEL_SPACE 0x02 40 #define HEAP_PRIORITY_VIP 0x04 41 42 43 typedef struct heap_class_s { 44 const char *name; 45 uint32 initial_percentage; 46 size_t max_allocation_size; 47 size_t page_size; 48 size_t min_bin_size; 49 size_t bin_alignment; 50 uint32 min_count_per_page; 51 size_t max_waste_per_page; 52 } heap_class; 53 54 typedef struct heap_allocator_s heap_allocator; 55 56 57 #ifdef __cplusplus 58 extern "C" { 59 #endif 60 61 62 void* memalign_etc(size_t alignment, size_t size, uint32 flags) _ALIGNED_BY_ARG(1); 63 void* realloc_etc(void* address, size_t newSize, uint32 flags); 64 void free_etc(void* address, uint32 flags); 65 66 void* memalign(size_t alignment, size_t size) _ALIGNED_BY_ARG(1); 67 68 void deferred_free(void* block); 69 70 void heap_add_area(heap_allocator* heap, area_id areaID, addr_t base, 71 size_t size); 72 heap_allocator* heap_create_allocator(const char* name, addr_t base, 73 size_t size, const heap_class* heapClass, bool allocateOnHeap); 74 void* heap_memalign(heap_allocator* heap, size_t alignment, size_t size) _ALIGNED_BY_ARG(2); 75 status_t heap_free(heap_allocator* heap, void* address); 76 77 #if KERNEL_HEAP_LEAK_CHECK 78 void heap_set_get_caller(heap_allocator* heap, addr_t (*getCaller)()); 79 #endif 80 81 status_t heap_init(addr_t heapBase, size_t heapSize); 82 status_t heap_init_post_area(); 83 status_t heap_init_post_sem(); 84 status_t heap_init_post_thread(); 85 86 #ifdef __cplusplus 87 } 88 #endif 89 90 91 static inline void* 92 malloc_etc(size_t size, uint32 flags) 93 { 94 return memalign_etc(0, size, flags); 95 } 96 97 98 #ifdef __cplusplus 99 100 #include <new> 101 102 #include <util/SinglyLinkedList.h> 103 104 105 struct malloc_flags { 106 uint32 flags; 107 108 malloc_flags(uint32 flags) 109 : 110 flags(flags) 111 { 112 } 113 114 malloc_flags(const malloc_flags& other) 115 : 116 flags(other.flags) 117 { 118 } 119 }; 120 121 122 inline void* 123 operator new(size_t size, const malloc_flags& flags) throw() 124 { 125 return malloc_etc(size, flags.flags); 126 } 127 128 129 inline void* 130 operator new[](size_t size, const malloc_flags& flags) throw() 131 { 132 return malloc_etc(size, flags.flags); 133 } 134 135 136 class DeferredDeletable : public SinglyLinkedListLinkImpl<DeferredDeletable> { 137 public: 138 virtual ~DeferredDeletable(); 139 }; 140 141 142 void deferred_delete(DeferredDeletable* deletable); 143 144 145 #endif /* __cplusplus */ 146 147 148 #endif /* _KERNEL_MEMHEAP_H */ 149