xref: /haiku/headers/private/kernel/heap.h (revision 746cac055adc6ac3308c7bc2d29040fb95689cc9)
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 // allocate 16MB initial heap for the kernel
14 #define INITIAL_HEAP_SIZE			16 * 1024 * 1024
15 // grow by another 4MB each time the heap runs out of memory
16 #define HEAP_GROW_SIZE				4 * 1024 * 1024
17 // allocate a dedicated 1MB area for dynamic growing
18 #define HEAP_DEDICATED_GROW_SIZE	1 * 1024 * 1024
19 // use areas for allocations bigger than 1MB
20 #define HEAP_AREA_USE_THRESHOLD		1 * 1024 * 1024
21 
22 // store size, thread and team info at the end of each allocation block
23 #define KERNEL_HEAP_LEAK_CHECK 0
24 
25 
26 typedef struct heap_class_s {
27 	const char *name;
28 	uint32		initial_percentage;
29 	size_t		max_allocation_size;
30 	size_t		page_size;
31 	size_t		min_bin_size;
32 	size_t		bin_alignment;
33 	uint32		min_count_per_page;
34 	size_t		max_waste_per_page;
35 } heap_class;
36 
37 typedef struct heap_allocator_s heap_allocator;
38 
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 // malloc_nogrow disallows waiting for a grow to happen - only to be used by
45 // vm functions that may deadlock on a triggered area creation
46 void *malloc_nogrow(size_t size);
47 
48 void *memalign(size_t alignment, size_t size);
49 
50 void deferred_free(void* block);
51 
52 void* malloc_referenced(size_t size);
53 void* malloc_referenced_acquire(void* data);
54 void malloc_referenced_release(void* data);
55 
56 heap_allocator*	heap_create_allocator(const char* name, addr_t base,
57 	size_t size, const heap_class* heapClass);
58 void* heap_memalign(heap_allocator* heap, size_t alignment, size_t size);
59 status_t heap_free(heap_allocator* heap, void* address);
60 
61 #if KERNEL_HEAP_LEAK_CHECK
62 void heap_set_get_caller(heap_allocator* heap, addr_t (*getCaller)());
63 #endif
64 
65 status_t heap_init(addr_t heapBase, size_t heapSize);
66 status_t heap_init_post_sem();
67 status_t heap_init_post_thread();
68 
69 #ifdef __cplusplus
70 }
71 #endif
72 
73 
74 #ifdef __cplusplus
75 
76 #include <new>
77 
78 static const struct nogrow_t {
79 } nogrow = {};
80 
81 inline void*
82 operator new(size_t size, const nogrow_t& nogrow)
83 {
84 	return malloc_nogrow(size);
85 }
86 
87 #endif	/* __cplusplus */
88 
89 
90 #endif	/* _KERNEL_MEMHEAP_H */
91