1 #include "HalideRuntime.h"
2 #include "runtime_internal.h"
3
4 extern "C" {
5
pseudostack_alloc(void * user_context,halide_pseudostack_slot_t * slot,size_t sz)6 WEAK_INLINE __attribute__((used)) void *pseudostack_alloc(void *user_context, halide_pseudostack_slot_t *slot, size_t sz) {
7 if (__builtin_expect(sz > slot->size, 0)) {
8 if (slot->ptr) halide_free(user_context, slot->ptr);
9 slot->ptr = halide_malloc(user_context, sz);
10 slot->size = sz;
11 }
12 return slot->ptr;
13 }
14
15 // Only called as a destructor at function exit
pseudostack_free(void * user_context,void * ptr)16 WEAK_INLINE __attribute__((used)) void pseudostack_free(void *user_context, void *ptr) {
17 halide_pseudostack_slot_t *slot = (halide_pseudostack_slot_t *)ptr;
18 slot->size = 0;
19 if (slot->ptr) halide_free(user_context, slot->ptr);
20 slot->ptr = NULL;
21 }
22 }
23