1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup RBHeap |
---|
5 | * |
---|
6 | * @brief Red-Black Tree Heap API. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2012 embedded brains GmbH. All rights reserved. |
---|
11 | * |
---|
12 | * embedded brains GmbH |
---|
13 | * Obere Lagerstr. 30 |
---|
14 | * 82178 Puchheim |
---|
15 | * Germany |
---|
16 | * <rtems@embedded-brains.de> |
---|
17 | * |
---|
18 | * The license and distribution terms for this file may be |
---|
19 | * found in the file LICENSE in this distribution or at |
---|
20 | * http://www.rtems.com/license/LICENSE. |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef _RTEMS_RBHEAP_H |
---|
24 | #define _RTEMS_RBHEAP_H |
---|
25 | |
---|
26 | #include <rtems.h> |
---|
27 | #include <rtems/chain.h> |
---|
28 | #include <rtems/rbtree.h> |
---|
29 | |
---|
30 | #ifdef __cplusplus |
---|
31 | extern "C" { |
---|
32 | #endif |
---|
33 | |
---|
34 | /** |
---|
35 | * @defgroup RBHeap Red-Black Tree Heap |
---|
36 | * |
---|
37 | * @brief Red-Black Tree Heap API. |
---|
38 | * |
---|
39 | * In the Red-Black Tree Heap the administration data structures are not |
---|
40 | * contained in the managed memory area. This can be used for example in a |
---|
41 | * task stack allocator which protects the task stacks from access by other |
---|
42 | * tasks. |
---|
43 | * |
---|
44 | * @{ |
---|
45 | */ |
---|
46 | |
---|
47 | typedef struct { |
---|
48 | rtems_chain_node chain_node; |
---|
49 | rtems_rbtree_node tree_node; |
---|
50 | uintptr_t begin; |
---|
51 | uintptr_t size; |
---|
52 | } rtems_rbheap_page; |
---|
53 | |
---|
54 | typedef struct rtems_rbheap_control rtems_rbheap_control; |
---|
55 | |
---|
56 | typedef void (*rtems_rbheap_extend_page_pool)(rtems_rbheap_control *control); |
---|
57 | |
---|
58 | struct rtems_rbheap_control { |
---|
59 | rtems_chain_control free_chain; |
---|
60 | rtems_chain_control pool_chain; |
---|
61 | rtems_rbtree_control page_tree; |
---|
62 | uintptr_t page_alignment; |
---|
63 | rtems_rbheap_extend_page_pool extend_page_pool; |
---|
64 | void *handler_arg; |
---|
65 | }; |
---|
66 | |
---|
67 | rtems_status_code rtems_rbheap_initialize( |
---|
68 | rtems_rbheap_control *control, |
---|
69 | void *area_begin, |
---|
70 | uintptr_t area_size, |
---|
71 | uintptr_t page_alignment, |
---|
72 | rtems_rbheap_extend_page_pool extend_page_pool, |
---|
73 | void *handler_arg |
---|
74 | ); |
---|
75 | |
---|
76 | void *rtems_rbheap_allocate(rtems_rbheap_control *control, size_t size); |
---|
77 | |
---|
78 | rtems_status_code rtems_rbheap_free(rtems_rbheap_control *control, void *ptr); |
---|
79 | |
---|
80 | static inline rtems_chain_control *rtems_rbheap_get_pool_chain( |
---|
81 | rtems_rbheap_control *control |
---|
82 | ) |
---|
83 | { |
---|
84 | return &control->pool_chain; |
---|
85 | } |
---|
86 | |
---|
87 | static inline void rtems_rbheap_set_extend_page_pool( |
---|
88 | rtems_rbheap_control *control, |
---|
89 | rtems_rbheap_extend_page_pool extend_page_pool |
---|
90 | ) |
---|
91 | { |
---|
92 | control->extend_page_pool = extend_page_pool; |
---|
93 | } |
---|
94 | |
---|
95 | static inline void *rtems_rbheap_get_handler_arg( |
---|
96 | const rtems_rbheap_control *control |
---|
97 | ) |
---|
98 | { |
---|
99 | return control->handler_arg; |
---|
100 | } |
---|
101 | |
---|
102 | static inline void rtems_rbheap_set_handler_arg( |
---|
103 | rtems_rbheap_control *control, |
---|
104 | void *handler_arg |
---|
105 | ) |
---|
106 | { |
---|
107 | control->handler_arg = handler_arg; |
---|
108 | } |
---|
109 | |
---|
110 | void rtems_rbheap_extend_page_pool_never(rtems_rbheap_control *control); |
---|
111 | |
---|
112 | void rtems_rbheap_extend_page_pool_with_malloc(rtems_rbheap_control *control); |
---|
113 | |
---|
114 | /** @} */ |
---|
115 | |
---|
116 | /* Private API */ |
---|
117 | |
---|
118 | #define rtems_rbheap_page_of_node(node) \ |
---|
119 | rtems_rbtree_container_of(node, rtems_rbheap_page, tree_node) |
---|
120 | |
---|
121 | static inline bool rtems_rbheap_is_page_free(const rtems_rbheap_page *page) |
---|
122 | { |
---|
123 | return !rtems_chain_is_node_off_chain(&page->chain_node); |
---|
124 | } |
---|
125 | |
---|
126 | #ifdef __cplusplus |
---|
127 | } |
---|
128 | #endif |
---|
129 | |
---|
130 | #endif /* _RTEMS_RBHEAP_H */ |
---|