1#ifndef HALIDE_RUNTIME_BLOCK_ALLOCATOR_H
2#define HALIDE_RUNTIME_BLOCK_ALLOCATOR_H
4#include "../HalideRuntime.h"
62 bool collect(
void *user_context);
63 int release(
void *user_context);
64 int destroy(
void *user_context);
105 int alloc_memory_block(
void *user_context,
BlockResource *block);
108 int free_memory_block(
void *user_context,
BlockResource *block);
111 size_t constrain_requested_size(
size_t size)
const;
129 if (result ==
nullptr) {
130 error(user_context) <<
"BlockAllocator: Failed to create instance! Out of memory\n";
134 result->initialize(user_context,
cfg, allocators);
141 instance->
destroy(user_context);
146void BlockAllocator::initialize(
void *user_context,
const Config &
cfg,
const MemoryAllocators &
ma) {
156#ifdef DEBUG_RUNTIME_INTERNAL
157 debug(user_context) <<
"BlockAllocator: Reserve ("
158 <<
"user_context=" << (
void *)(user_context) <<
" "
161 <<
"dedicated=" << (
request.dedicated ?
"true" :
"false") <<
" "
169 error(user_context) <<
"BlockAllocator: Failed to allocate new empty block of requested size ("
180 if (result ==
nullptr) {
185 error(user_context) <<
"BlockAllocator: Out of memory! Failed to allocate empty block of size ("
192 block->
allocator = create_region_allocator(user_context, block);
205 if (allocator ==
nullptr) {
216 if (allocator ==
nullptr) {
227 if (allocator ==
nullptr) {
244#ifdef DEBUG_RUNTIME_INTERNAL
250#ifdef DEBUG_RUNTIME_INTERNAL
251 debug(user_context) <<
"Collected block ("
252 <<
"block=" << (
void *)block <<
" "
285 block_list.
destroy(user_context);
291 if (result ==
nullptr) {
292#ifdef DEBUG_RUNTIME_INTERNAL
293 debug(user_context) <<
"BlockAllocator: Failed to allocate region of size ("
298 if (allocator->
collect(user_context)) {
305bool BlockAllocator::is_block_suitable_for_request(
void *user_context,
const BlockResource *block,
const MemoryRequest &
request)
const {
306 if (!is_compatible_block(block,
request.properties)) {
307#ifdef DEBUG_RUNTIME_INTERNAL
308 debug(user_context) <<
"BlockAllocator: skipping block ... incompatible properties! ("
309 <<
"block_resource=" << (
void *)block <<
" "
311 <<
"block_reserved=" << (
uint32_t)block->reserved <<
" "
324 if (
request.dedicated && (block->reserved > 0)) {
325#ifdef DEBUG_RUNTIME_INTERNAL
326 debug(user_context) <<
"BlockAllocator: skipping block ... can be used for dedicated allocation! ("
327 <<
"block_resource=" << (
void *)block <<
" "
328 <<
"block_size=" << (
uint32_t)block->memory.size <<
" "
329 <<
"block_reserved=" << (
uint32_t)block->reserved <<
")";
334 }
else if (block->memory.dedicated && (block->reserved > 0)) {
335#ifdef DEBUG_RUNTIME_INTERNAL
336 debug(user_context) <<
"BlockAllocator: skipping block ... already dedicated to an allocation! ("
337 <<
"block_resource=" << (
void *)block <<
" "
338 <<
"block_size=" << (
uint32_t)block->memory.size <<
" "
339 <<
"block_reserved=" << (
uint32_t)block->reserved <<
")";
345 size_t available = (block->memory.size - block->reserved);
346 if (available >=
request.size) {
353BlockAllocator::BlockEntry *
354BlockAllocator::find_block_entry(
void *user_context,
const MemoryRequest &
request) {
358 const BlockResource *block =
static_cast<BlockResource *
>(
block_entry->value);
359 if (is_block_suitable_for_request(user_context, block,
request)) {
360#ifdef DEBUG_RUNTIME_INTERNAL
361 debug(user_context) <<
"BlockAllocator: found suitable block ("
362 <<
"user_context=" << (
void *)(user_context) <<
" "
363 <<
"block_resource=" << (
void *)block <<
" "
364 <<
"block_size=" << (
uint32_t)block->memory.size <<
" "
365 <<
"block_reserved=" << (
uint32_t)block->reserved <<
" "
367 <<
"request_dedicated=" << (
request.dedicated ?
"true" :
"false") <<
" "
378#ifdef DEBUG_RUNTIME_INTERNAL
379 debug(user_context) <<
"BlockAllocator: couldn't find suitable block! ("
380 <<
"user_context=" << (
void *)(user_context) <<
" "
382 <<
"request_dedicated=" << (
request.dedicated ?
"true" :
"false") <<
" "
391BlockAllocator::BlockEntry *
392BlockAllocator::reserve_block_entry(
void *user_context,
const MemoryRequest &
request) {
393#ifdef DEBUG_RUNTIME_INTERNAL
394 debug(user_context) <<
"BlockAllocator: reserving block ... ! ("
396 <<
"requested_is_dedicated=" << (
request.dedicated ?
"true" :
"false") <<
" "
403#ifdef DEBUG_RUNTIME_INTERNAL
404 debug(user_context) <<
"BlockAllocator: creating block ... ! ("
406 <<
"requested_is_dedicated=" << (
request.dedicated ?
"true" :
"false") <<
" "
415 BlockResource *block =
static_cast<BlockResource *
>(
block_entry->value);
416 if (block->allocator ==
nullptr) {
417 block->allocator = create_region_allocator(user_context, block);
424BlockAllocator::create_region_allocator(
void *user_context, BlockResource *block) {
425#ifdef DEBUG_RUNTIME_INTERNAL
426 debug(user_context) <<
"BlockAllocator: Creating region allocator ("
427 <<
"user_context=" << (
void *)(user_context) <<
" "
428 <<
"block_resource=" << (
void *)(block) <<
")...";
432 user_context, block, {allocators.
system, allocators.
region});
435 error(user_context) <<
"BlockAllocator: Failed to create new region allocator\n";
442int BlockAllocator::destroy_region_allocator(
void *user_context, RegionAllocator *
region_allocator) {
443#ifdef DEBUG_RUNTIME_INTERNAL
444 debug(user_context) <<
"BlockAllocator: Destroying region allocator ("
445 <<
"user_context=" << (
void *)(user_context) <<
" "
454BlockAllocator::BlockEntry *
455BlockAllocator::create_block_entry(
void *user_context,
const MemoryRequest &
request) {
457 error(user_context) <<
"BlockAllocator: No free blocks found! Maximum pool size reached ("
464 error(user_context) <<
"BlockAllocator: No free blocks found! Maximum block count reached ("
471 debug(user_context) <<
"BlockAllocator: Failed to allocate new block entry\n";
475#ifdef DEBUG_RUNTIME_INTERNAL
476 debug(user_context) <<
"BlockAllocator: Creating block entry ("
479 <<
"allocator=" << (
void *)(allocators.
block.
allocate) <<
")...";
487 BlockResource *block =
static_cast<BlockResource *
>(
block_entry->value);
489 block->memory.handle =
nullptr;
493 block->allocator = create_region_allocator(user_context, block);
494 alloc_memory_block(user_context, block);
498int BlockAllocator::release_block_entry(
void *user_context, BlockAllocator::BlockEntry *
block_entry) {
499#ifdef DEBUG_RUNTIME_INTERNAL
500 debug(user_context) <<
"BlockAllocator: Releasing block entry ("
502 <<
"block=" << (
void *)(
block_entry->value) <<
")...";
504 BlockResource *block =
static_cast<BlockResource *
>(
block_entry->value);
505 if (block->allocator) {
506 return block->allocator->release(user_context);
511int BlockAllocator::destroy_block_entry(
void *user_context, BlockAllocator::BlockEntry *
block_entry) {
512#ifdef DEBUG_RUNTIME_INTERNAL
513 debug(user_context) <<
"BlockAllocator: Destroying block entry ("
518 BlockResource *block =
static_cast<BlockResource *
>(
block_entry->value);
519 if (block->allocator) {
520 destroy_region_allocator(user_context, block->allocator);
521 block->allocator =
nullptr;
523 free_memory_block(user_context, block);
528int BlockAllocator::alloc_memory_block(
void *user_context, BlockResource *block) {
529#ifdef DEBUG_RUNTIME_INTERNAL
530 debug(user_context) <<
"BlockAllocator: Allocating block (ptr=" << (
void *)block <<
" allocator=" << (
void *)allocators.
block.
allocate <<
")...";
539int BlockAllocator::free_memory_block(
void *user_context, BlockResource *block) {
540#ifdef DEBUG_RUNTIME_INTERNAL
541 debug(user_context) <<
"BlockAllocator: Deallocating block (ptr=" << (
void *)block <<
" allocator=" << (
void *)allocators.
block.
deallocate <<
")...";
548 block->memory.size = 0;
552size_t BlockAllocator::constrain_requested_size(
size_t size)
const {
575 if (
request->properties.nearest_multiple) {
576 size_t nm =
request->properties.nearest_multiple;
634 return block_list.
size();
638 size_t total_size = 0;
642 if (block !=
nullptr) {
@ halide_error_code_internal_error
There is a bug in the Halide compiler.
Allocator class interface for managing large contiguous blocks of memory, which are then sub-allocate...
BlockAllocator(const BlockAllocator &)=delete
const Config & default_config() const
bool collect(void *user_context)
size_t block_count() const
MemoryRegion * reserve(void *user_context, const MemoryRequest &request)
int release(void *user_context, MemoryRegion *region)
const Config & current_config() const
int retain(void *user_context, MemoryRegion *region)
static void destroy(void *user_context, BlockAllocator *block_allocator)
static BlockAllocator * create(void *user_context, const Config &config, const MemoryAllocators &allocators)
int reclaim(void *user_context, MemoryRegion *region)
int conform(void *user_context, MemoryRequest *request) const
BlockAllocator & operator=(const BlockAllocator &)=delete
const MemoryAllocators & current_allocators() const
void initialize(void *user_context, uint32_t entry_size, uint32_t capacity=default_capacity, const SystemMemoryAllocatorFns &allocator=default_allocator())
EntryType * append(void *user_context)
void remove(void *user_context, EntryType *entry_ptr)
void destroy(void *user_context)
Allocator class interface for sub-allocating a contiguous memory block into smaller regions of memory...
MemoryRegion * reserve(void *user_context, const MemoryRequest &request)
static int destroy(void *user_context, RegionAllocator *region_allocator)
static RegionAllocator * find_allocator(void *user_context, MemoryRegion *memory_region)
int retain(void *user_context, MemoryRegion *memory_region)
bool collect(void *user_context)
int reclaim(void *user_context, MemoryRegion *memory_region)
int release(void *user_context, MemoryRegion *memory_region)
static RegionAllocator * create(void *user_context, BlockResource *block, const MemoryAllocators &ma)
WEAK const char * halide_memory_caching_name(MemoryCaching value)
WEAK const char * halide_memory_usage_name(MemoryUsage value)
WEAK const char * halide_memory_visibility_name(MemoryVisibility value)
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
Internal::ConstantInterval cast(Type t, const Internal::ConstantInterval &a)
Cast operators for ConstantIntervals.
unsigned __INT64_TYPE__ uint64_t
signed __INT32_TYPE__ int32_t
unsigned __INT32_TYPE__ uint32_t
#define halide_abort_if_false(user_context, cond)
size_t maximum_block_count
size_t minimum_block_size
size_t maximum_block_size
MemoryRegionAllocatorFns region
MemoryBlockAllocatorFns block
SystemMemoryAllocatorFns system
RegionAllocator * allocator
DeallocateBlockFn deallocate
ConformBlockRequestFn conform
MemoryProperties properties
MemoryVisibility visibility
DeallocateSystemFn deallocate
AllocateSystemFn allocate