Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/runtime/internal/region_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ int RegionAllocator::initialize(void *user_context, BlockResource *mb, const Mem
block_request.dedicated = block->memory.dedicated;
block->allocator = this;
block->regions = create_block_region(user_context, block_request);
if (block->regions != nullptr) {
// conform() only ever rounds a request up, and the block size is not
// required to satisfy the region constraints, so conforming the whole
// block can describe more memory than the block owns. Every other
// region is derived from this one by splitting or coalescing, so it
// has to span exactly the block.
block->regions->memory.allocation.size = block->memory.size;
}
return 0;
}

Expand Down
28 changes: 28 additions & 0 deletions test/runtime/block_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,34 @@ int main(int argc, char **argv) {
HALIDE_CHECK(user_context, get_allocated_system_memory() == 0);
}

// Regression: the initial region has to span exactly the block, so that no
// region derived from it reaches past the end of the block.
{
BlockResource block_resource = {};
block_resource.memory.size = 500; // not a multiple of nearest_multiple
block_resource.memory.properties.nearest_multiple = 8;
allocate_block(user_context, &block_resource.memory);
RegionAllocator::MemoryAllocators allocators = {
system_allocator, {allocate_region, deallocate_region, nullptr}};
RegionAllocator *instance = RegionAllocator::create(user_context, &block_resource, allocators);

HALIDE_CHECK(user_context, block_resource.regions != nullptr);
HALIDE_CHECK(user_context, block_resource.regions->memory.allocation.offset == 0);
HALIDE_CHECK(user_context, block_resource.regions->memory.allocation.size == block_resource.memory.size);

MemoryRequest request = {};
request.size = 100;
MemoryRegion *region = instance->reserve(user_context, request);
check_region(user_context, region);
HALIDE_CHECK(user_context,
(region->allocation.offset + region->allocation.size) <= block_resource.memory.size);

RegionAllocator::destroy(user_context, instance);
deallocate_block(user_context, &block_resource.memory);
HALIDE_CHECK(user_context, allocated_region_memory == 0);
HALIDE_CHECK(user_context, get_allocated_system_memory() == 0);
}

// test region allocator class interface
{
// Use custom conform allocation request callbacks
Expand Down
Loading