Skip to content

Commit

Permalink
Add support for setting the default allocator and deallocator functio…
Browse files Browse the repository at this point in the history
…ns in Halide::Runtime::Buffer.
  • Loading branch information
mcourteaux committed Mar 1, 2024
1 parent 7636c44 commit 027b6a2
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/runtime/HalideBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ struct AllInts<float, Args...> : std::false_type {};
template<typename... Args>
struct AllInts<double, Args...> : std::false_type {};

// A helper to detect if there are any zeros in a container
namespace Internal {
// A helper to detect if there are any zeros in a container
template<typename Container>
bool any_zero(const Container &c) {
for (int i : c) {
Expand All @@ -153,6 +153,15 @@ bool any_zero(const Container &c) {
}
return false;
}

template<typename U>
struct DefaultAllocatorFns {};

template<>
struct DefaultAllocatorFns<void> {
void *(*default_allocate_fn)(size_t) = malloc;
void (*default_deallocate_fn)(void *) = free;
};
} // namespace Internal

/** A struct acting as a header for allocations owned by the Buffer
Expand Down Expand Up @@ -710,7 +719,16 @@ class Buffer {
"Can't convert from a Buffer with static dimensionality to a Buffer with different static dimensionality");
}

static inline Internal::DefaultAllocatorFns<T> default_allocator_fns = {};

public:
static void set_default_allocate_fn(void *(*allocate_fn)(size_t)) {
Buffer<>::default_allocator_fns.default_allocate_fn = allocate_fn;
}
static void set_default_deallocate_fn(void (*deallocate_fn)(void *)) {
Buffer<>::default_allocator_fns.default_deallocate_fn = deallocate_fn;
}

/** Determine if a Buffer<T, Dims, InClassDimStorage> can be constructed from some other Buffer type.
* If this can be determined at compile time, fail with a static assert; otherwise
* return a boolean based on runtime typing. */
Expand Down Expand Up @@ -893,7 +911,7 @@ class Buffer {

#if HALIDE_RUNTIME_BUFFER_USE_ALIGNED_ALLOC
// Only use aligned_alloc() if no custom allocators are specified.
if (!allocate_fn && !deallocate_fn) {
if (!allocate_fn && !deallocate_fn && Buffer<>::default_allocator_fns.default_allocate_fn == malloc && Buffer<>::default_allocator_fns.default_deallocate_fn == free) {
// As a practical matter, sizeof(AllocationHeader) is going to be no more than 16 bytes
// on any supported platform, so we will just overallocate by 'alignment'
// so that the user storage also starts at an aligned point. This is a bit
Expand All @@ -908,10 +926,10 @@ class Buffer {
// else fall thru
#endif
if (!allocate_fn) {
allocate_fn = malloc;
allocate_fn = Buffer<>::default_allocator_fns.default_allocate_fn;
}
if (!deallocate_fn) {
deallocate_fn = free;
deallocate_fn = Buffer<>::default_allocator_fns.default_deallocate_fn;
}

static_assert(sizeof(AllocationHeader) <= alignment);
Expand Down

0 comments on commit 027b6a2

Please sign in to comment.