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_sem(); 75 status_t heap_init_post_thread(); 76 77 #ifdef __cplusplus 78 } 79 #endif 80 81 82 static inline void* 83 malloc_etc(size_t size, uint32 flags) 84 { 85 return memalign_etc(0, size, flags); 86 } 87 88 89 #ifdef __cplusplus 90 91 #include <new> 92 93 #include <util/SinglyLinkedList.h> 94 95 96 struct malloc_flags { 97 uint32 flags; 98 99 malloc_flags(uint32 flags) 100 : 101 flags(flags) 102 { 103 } 104 105 malloc_flags(const malloc_flags& other) 106 : 107 flags(other.flags) 108 { 109 } 110 }; 111 112 113 inline void* 114 operator new(size_t size, const malloc_flags& flags) throw() 115 { 116 return malloc_etc(size, flags.flags); 117 } 118 119 120 inline void* 121 operator new[](size_t size, const malloc_flags& flags) throw() 122 { 123 return malloc_etc(size, flags.flags); 124 } 125 126 127 class DeferredDeletable : public SinglyLinkedListLinkImpl<DeferredDeletable> { 128 public: 129 virtual ~DeferredDeletable(); 130 }; 131 132 133 void deferred_delete(DeferredDeletable* deletable); 134 135 136 #endif /* __cplusplus */ 137 138 139 #endif /* _KERNEL_MEMHEAP_H */ 140