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