1 #ifndef HALIDE_RUNTIME_POINTER_TABLE_H
2 #define HALIDE_RUNTIME_POINTER_TABLE_H
4 #include "../HalideRuntime.h"
28 void reserve(
void *user_context,
size_t capacity,
bool free_existing =
false);
29 void resize(
void *user_context,
size_t entry_count,
bool realloc =
true);
31 void assign(
void *user_context,
size_t index,
const void *entry_ptr);
32 void insert(
void *user_context,
size_t index,
const void *entry_ptr);
33 void prepend(
void *user_context,
const void *entry_ptr);
34 void append(
void *user_context,
const void *entry_ptr);
35 void remove(
void *user_context,
size_t index);
37 void fill(
void *user_context,
const void **array,
size_t array_size);
38 void insert(
void *user_context,
size_t index,
const void **array,
size_t array_size);
39 void replace(
void *user_context,
size_t index,
const void **array,
size_t array_size);
40 void prepend(
void *user_context,
const void **array,
size_t array_size);
41 void append(
void *user_context,
const void **array,
size_t array_size);
42 void remove(
void *user_context,
size_t index,
size_t entry_count);
47 void clear(
void *user_context);
48 void destroy(
void *user_context);
57 const void **
data()
const;
66 void allocate(
void *user_context,
size_t capacity);
78 if (initial_capacity) {
79 reserve(user_context, initial_capacity);
86 ptr =
static_cast<void **
>(allocator.
allocate(
nullptr, other.capacity *
sizeof(
void *)));
87 capacity = other.capacity;
89 if (ptr && other.count != 0) {
91 memcpy(this->ptr, other.ptr, count *
sizeof(
void *));
101 if (ptr !=
nullptr) {
104 capacity = count = 0;
110 capacity = count = 0;
112 if (initial_capacity) {
113 reserve(user_context, initial_capacity);
118 if (&other !=
this) {
119 resize(
nullptr, other.count);
120 if (count != 0 && other.ptr !=
nullptr) {
121 memcpy(ptr, other.ptr, count *
sizeof(
void *));
128 if (count != other.count) {
131 return memcmp(this->ptr, other.ptr, this->size() *
sizeof(
void *)) == 0;
135 return !(*
this == other);
139 if (array_size != 0) {
140 resize(user_context, array_size);
141 memcpy(this->ptr, array, array_size *
sizeof(
void *));
148 ptr[index] =
const_cast<void *
>(entry_ptr);
152 insert(user_context, 0, &entry_ptr, 1);
156 append(user_context, &entry_ptr, 1);
174 new_capacity =
max(new_capacity, count);
175 if ((new_capacity < capacity) && !free_existing) {
176 new_capacity = capacity;
178 allocate(user_context, new_capacity);
182 size_t current_size = capacity;
183 size_t requested_size = entry_count;
185 size_t actual_size = current_size;
186 count = requested_size;
188 #ifdef DEBUG_RUNTIME_INTERNAL
189 debug(user_context) <<
"PointerTable: Resize ("
190 <<
"requested_size=" << (
int32_t)requested_size <<
" "
191 <<
"current_size=" << (
int32_t)current_size <<
" "
192 <<
"minimum_size=" << (
int32_t)minimum_size <<
" "
193 <<
"sizeof(void*)=" << (
int32_t)
sizeof(
void *) <<
" "
194 <<
"realloc=" << (realloc ?
"true" :
"false") <<
")...\n";
198 if (requested_size > current_size) {
199 actual_size =
max(requested_size,
max(current_size * 3 / 2, minimum_size));
200 }
else if (!realloc) {
204 allocate(user_context, actual_size);
208 if (capacity > count) {
209 void *new_ptr =
nullptr;
211 size_t bytes = count *
sizeof(
void *);
212 new_ptr = allocator.
allocate(user_context, bytes);
213 memcpy(new_ptr, ptr, bytes);
217 ptr =
static_cast<void **
>(new_ptr);
222 const void *addr =
reinterpret_cast<const void *
>(entry_ptr);
223 insert(user_context, index, &addr, 1);
227 remove(user_context, index, 1);
232 const size_t last_index =
size();
233 if (index < (last_index - entry_count)) {
234 size_t dst_offset = index *
sizeof(
void *);
235 size_t src_offset = (index + entry_count) *
sizeof(
void *);
236 size_t bytes = (last_index - index - entry_count) *
sizeof(
void *);
238 #ifdef DEBUG_RUNTIME_INTERNAL
239 debug(user_context) <<
"PointerTable: Remove ("
240 <<
"index=" << (
int32_t)index <<
" "
241 <<
"entry_count=" << (
int32_t)entry_count <<
" "
242 <<
"last_index=" << (
int32_t)last_index <<
" "
243 <<
"src_offset=" << (
int32_t)src_offset <<
" "
244 <<
"dst_offset=" << (
int32_t)dst_offset <<
" "
245 <<
"bytes=" << (
int32_t)bytes <<
")...\n";
247 memmove(ptr + dst_offset, ptr + src_offset, bytes);
249 resize(user_context, last_index - entry_count);
254 size_t remaining = count - index;
255 size_t copy_count =
min(remaining, array_size);
257 #ifdef DEBUG_RUNTIME_INTERNAL
258 debug(user_context) <<
"PointerTable: Replace ("
259 <<
"index=" << (
int32_t)index <<
" "
260 <<
"array_size=" << (
int32_t)array_size <<
" "
261 <<
"remaining=" << (
int32_t)remaining <<
" "
262 <<
"copy_count=" << (
int32_t)copy_count <<
" "
263 <<
"capacity=" << (
int32_t)capacity <<
")...\n";
267 memcpy(ptr + index, array, copy_count *
sizeof(
void *));
268 count =
max(count, index + copy_count);
273 const size_t last_index =
size();
274 resize(user_context, last_index + array_size);
275 if (index < last_index) {
276 size_t src_offset = index *
sizeof(
void *);
277 size_t dst_offset = (index + array_size) *
sizeof(
void *);
278 size_t bytes = (last_index - index) *
sizeof(
void *);
279 memmove(ptr + dst_offset, ptr + src_offset, bytes);
281 replace(user_context, index, array, array_size);
285 insert(user_context, 0, array, array_size);
289 const size_t last_index =
size();
290 insert(user_context, last_index, array, array_size);
322 size_t index = count - 1;
327 return const_cast<const void **
>(ptr);
330 void PointerTable::allocate(
void *user_context,
size_t new_capacity) {
331 if (new_capacity != capacity) {
333 size_t bytes = new_capacity *
sizeof(
void *);
335 #ifdef DEBUG_RUNTIME_INTERNAL
336 debug(user_context) <<
"PointerTable: Allocating (bytes=" << (
int32_t)bytes <<
" allocator=" << (
void *)allocator.
allocate <<
")...\n";
339 void *new_ptr = bytes ? allocator.
allocate(user_context, bytes) :
nullptr;
340 if (count != 0 && ptr !=
nullptr && new_ptr !=
nullptr) {
341 memcpy(new_ptr, ptr, count *
sizeof(
void *));
343 if (ptr !=
nullptr) {
347 capacity = new_capacity;
348 ptr =
static_cast<void **
>(new_ptr);
352 const SystemMemoryAllocatorFns &
354 return this->allocator;
361 return native_allocator;
370 #endif // HALIDE_RUNTIME_POINTER_TABLE_H