xref: /haiku/headers/private/kernel/heap.h (revision 9e25244c5e9051f6cd333820d6332397361abd6c)
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 free_etc(void* address, uint32 flags);
64 
65 void* memalign(size_t alignment, size_t size) _ALIGNED_BY_ARG(1);
66 
67 void deferred_free(void* block);
68 
69 void heap_add_area(heap_allocator* heap, area_id areaID, addr_t base,
70 	size_t size);
71 heap_allocator*	heap_create_allocator(const char* name, addr_t base,
72 	size_t size, const heap_class* heapClass, bool allocateOnHeap);
73 void* heap_memalign(heap_allocator* heap, size_t alignment, size_t size) _ALIGNED_BY_ARG(2);
74 status_t heap_free(heap_allocator* heap, void* address);
75 
76 #if KERNEL_HEAP_LEAK_CHECK
77 void heap_set_get_caller(heap_allocator* heap, addr_t (*getCaller)());
78 #endif
79 
80 status_t heap_init(addr_t heapBase, size_t heapSize);
81 status_t heap_init_post_area();
82 status_t heap_init_post_sem();
83 status_t heap_init_post_thread();
84 
85 #ifdef __cplusplus
86 }
87 #endif
88 
89 
90 static inline void*
91 malloc_etc(size_t size, uint32 flags)
92 {
93 	return memalign_etc(0, size, flags);
94 }
95 
96 
97 #ifdef __cplusplus
98 
99 #include <new>
100 
101 #include <util/SinglyLinkedList.h>
102 
103 
104 struct malloc_flags {
105 	uint32	flags;
106 
107 	malloc_flags(uint32 flags)
108 		:
109 		flags(flags)
110 	{
111 	}
112 
113 	malloc_flags(const malloc_flags& other)
114 		:
115 		flags(other.flags)
116 	{
117 	}
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 inline void*
129 operator new[](size_t size, const malloc_flags& flags) throw()
130 {
131 	return malloc_etc(size, flags.flags);
132 }
133 
134 
135 class DeferredDeletable : public SinglyLinkedListLinkImpl<DeferredDeletable> {
136 public:
137 	virtual						~DeferredDeletable();
138 };
139 
140 
141 void deferred_delete(DeferredDeletable* deletable);
142 
143 
144 #endif	/* __cplusplus */
145 
146 
147 #endif	/* _KERNEL_MEMHEAP_H */
148