|
23 | 23 | #include <new>
|
24 | 24 | #include <stdexcept>
|
25 | 25 |
|
| 26 | +#include "rcl/allocator.h" |
| 27 | +#include "rclcpp/allocator/allocator_common.hpp" |
26 | 28 | #include "tlsf/tlsf.h"
|
27 | 29 |
|
28 | 30 | template<typename T, size_t DefaultPoolSize = 1024 * 1024>
|
29 | 31 | struct tlsf_heap_allocator
|
30 | 32 | {
|
31 | 33 | // Needed for std::allocator_traits
|
32 | 34 | using value_type = T;
|
| 35 | + static const bool is_tlsf_heap_allocator = true; |
33 | 36 |
|
34 | 37 | explicit tlsf_heap_allocator(size_t size)
|
35 | 38 | : memory_pool(nullptr), pool_size(size)
|
@@ -137,4 +140,24 @@ constexpr bool operator!=(
|
137 | 140 | return a.memory_pool != b.memory_pool;
|
138 | 141 | }
|
139 | 142 |
|
| 143 | +template<typename T, size_t PoolSize> |
| 144 | +rcl_allocator_t get_rcl_allocator(tlsf_heap_allocator<T, PoolSize>& allocator) |
| 145 | +{ |
| 146 | + rcl_allocator_t rcl_allocator; |
| 147 | + rcl_allocator.allocate = [](size_t size, void*) { |
| 148 | + return tlsf_malloc(size); |
| 149 | + }; |
| 150 | + rcl_allocator.deallocate = [](void* pointer, void*) { |
| 151 | + return tlsf_free(pointer); |
| 152 | + }; |
| 153 | + rcl_allocator.reallocate = [](void* pointer, size_t size, void*) { |
| 154 | + return tlsf_realloc(pointer, size); |
| 155 | + }; |
| 156 | + rcl_allocator.zero_allocate = [](size_t nmemb, size_t size, void*) { |
| 157 | + return tlsf_calloc(nmemb, size); |
| 158 | + }; |
| 159 | + rcl_allocator.state = &allocator; |
| 160 | + return rcl_allocator; |
| 161 | +} |
| 162 | + |
140 | 163 | #endif // TLSF_CPP__TLSF_HPP_
|
0 commit comments