xref: /haiku/headers/private/kernel/heap.h (revision c9ad965c81b08802fed0827fd1dd16f45297928a)
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 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- and memalign_nogrow disallow waiting for a grow to happen - only to
45 // be used by vm functions that may deadlock on a triggered area creation.
46 void* memalign_nogrow(size_t alignment, size_t size);
47 void* malloc_nogrow(size_t size);
48 
49 void* memalign(size_t alignment, size_t size);
50 
51 void deferred_free(void* block);
52 
53 void* malloc_referenced(size_t size);
54 void* malloc_referenced_acquire(void* data);
55 void malloc_referenced_release(void* data);
56 
57 void heap_add_area(heap_allocator* heap, area_id areaID, addr_t base,
58 	size_t size);
59 heap_allocator*	heap_create_allocator(const char* name, addr_t base,
60 	size_t size, const heap_class* heapClass, bool allocateOnHeap);
61 void* heap_memalign(heap_allocator* heap, size_t alignment, size_t size);
62 status_t heap_free(heap_allocator* heap, void* address);
63 
64 #if KERNEL_HEAP_LEAK_CHECK
65 void heap_set_get_caller(heap_allocator* heap, addr_t (*getCaller)());
66 #endif
67 
68 status_t heap_init(addr_t heapBase, size_t heapSize);
69 status_t heap_init_post_sem();
70 status_t heap_init_post_thread();
71 
72 #ifdef __cplusplus
73 }
74 #endif
75 
76 
77 #ifdef __cplusplus
78 
79 #include <new>
80 
81 #include <util/SinglyLinkedList.h>
82 
83 
84 static const struct nogrow_t {
85 } nogrow = {};
86 
87 
88 inline void*
89 operator new(size_t size, const nogrow_t& nogrow) throw()
90 {
91 	return malloc_nogrow(size);
92 }
93 
94 
95 inline void*
96 operator new[](size_t size, const nogrow_t& nogrow) throw()
97 {
98 	return malloc_nogrow(size);
99 }
100 
101 
102 class DeferredDeletable : public SinglyLinkedListLinkImpl<DeferredDeletable> {
103 public:
104 	virtual						~DeferredDeletable();
105 };
106 
107 
108 void deferred_delete(DeferredDeletable* deletable);
109 
110 
111 #endif	/* __cplusplus */
112 
113 
114 #endif	/* _KERNEL_MEMHEAP_H */
115