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