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