Skip to content

Improve coarse_alloc() #1249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
Merged
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
156 changes: 97 additions & 59 deletions src/coarse/coarse.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,24 @@ static umf_result_t coarse_add_used_block(coarse_t *coarse, void *addr,
return UMF_RESULT_SUCCESS;
}

static umf_result_t coarse_add_free_block(coarse_t *coarse, void *addr,
size_t size, block_t **free_block) {
*free_block = NULL;

block_t *new_block =
coarse_ravl_add_new(coarse->all_blocks, addr, size, NULL);
if (new_block == NULL) {
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}

new_block->used = false;
coarse->alloc_size += size;

*free_block = new_block;

return UMF_RESULT_SUCCESS;
}

static void coarse_ravl_cb_rm_all_blocks_node(void *data, void *arg) {
assert(data);
assert(arg);
Expand Down Expand Up @@ -1053,88 +1071,108 @@ umf_result_t coarse_alloc(coarse_t *coarse, size_t size, size_t alignment,

assert(debug_check(coarse));

*resultPtr = NULL;

// Find a block with greater or equal size using the given memory allocation strategy
block_t *curr = find_free_block(coarse->free_blocks, size, alignment,
coarse->allocation_strategy);

// If the block that we want to reuse has a greater size, split it.
// Try to merge the split part with the successor if it is not used.
enum { ACTION_NONE = 0, ACTION_USE, ACTION_SPLIT } action = ACTION_NONE;

if (curr && curr->size > size) {
action = ACTION_SPLIT;
} else if (curr && curr->size == size) {
action = ACTION_USE;
}

if (action) { // ACTION_SPLIT or ACTION_USE
assert(curr->used == false);

// In case of non-zero alignment create an aligned block what would be further used.
if (alignment > 0) {
umf_result = create_aligned_block(coarse, size, alignment, &curr);
if (umf_result != UMF_RESULT_SUCCESS) {
(void)free_blocks_re_add(coarse, curr);
goto err_unlock;
}
if (curr == NULL) {
// no suitable block found - try to get more memory from the upstream provider
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;

if (!coarse->cb.alloc) {
LOG_ERR("out of memory (the memory provider does not support "
"allocating more memory)");
goto err_unlock;
}

if (action == ACTION_SPLIT) {
// Split the current block and put the new block after the one that we use.
umf_result = split_current_block(coarse, curr, size);
if (umf_result != UMF_RESULT_SUCCESS) {
(void)free_blocks_re_add(coarse, curr);
goto err_unlock;
}
size_t size_aligned = ALIGN_UP_SAFE(size, alignment);
if (size_aligned == 0) {
// cannot align up (arithmetic overflow)
umf_result = UMF_RESULT_ERROR_INVALID_ARGUMENT;
LOG_ERR("size too huge (arithmetic overflow)");
goto err_unlock;
}

curr->size = size;
umf_result = coarse->cb.alloc(coarse->provider, size_aligned, alignment,
resultPtr);
if (umf_result != UMF_RESULT_SUCCESS) {
LOG_ERR("alloc() of memory provider failed: out of memory");
goto err_unlock;
}

LOG_DEBUG("coarse_ALLOC (split_block) %zu used %zu alloc %zu", size,
coarse->used_size, coarse->alloc_size);
ASSERT_IS_ALIGNED(((uintptr_t)(*resultPtr)), alignment);

} else { // action == ACTION_USE
LOG_DEBUG("coarse_ALLOC (same_block) %zu used %zu alloc %zu", size,
coarse->used_size, coarse->alloc_size);
block_t *new_free_block = NULL;
umf_result = coarse_add_free_block(coarse, *resultPtr, size_aligned,
&new_free_block);
if (umf_result != UMF_RESULT_SUCCESS) {
LOG_ERR("failed to add a newly allocated block from the memory "
"provider");
if (coarse->cb.free) {
coarse->cb.free(coarse->provider, *resultPtr, size_aligned);
} else {
LOG_WARN("the memory provider does not support the free() "
"operation, so the following memory block was leaked: "
"address %p, size %zu",
*resultPtr, size_aligned);
}
goto err_unlock;
}

curr->used = true;
*resultPtr = curr->data;
coarse->used_size += size;

assert(debug_check(coarse));
utils_mutex_unlock(&coarse->lock);
LOG_DEBUG("coarse_ALLOC (memory_provider) %zu used %zu alloc %zu",
size_aligned, coarse->used_size, coarse->alloc_size);

return UMF_RESULT_SUCCESS;
curr = new_free_block;
}

// no suitable block found - try to get more memory from the upstream provider
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;

*resultPtr = NULL;
// If the block that we want to reuse has a greater size, split it.
// Try to merge the split part with the successor if it is not used.
enum { ACTION_NONE = 0, ACTION_USE, ACTION_SPLIT } action = ACTION_NONE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACTION_NONE
AFAIK, starting enum entry is 0, so there is no need to set it up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but IMHO it is more readable as it is now


if (!coarse->cb.alloc) {
if (curr && curr->size > size) {
action = ACTION_SPLIT;
} else if (curr && curr->size == size) {
action = ACTION_USE;
} else {
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
LOG_ERR("out of memory");
goto err_unlock;
}

umf_result = coarse->cb.alloc(coarse->provider, size, alignment, resultPtr);
if (umf_result != UMF_RESULT_SUCCESS) {
LOG_ERR("coarse_alloc_cb() failed: out of memory");
goto err_unlock;
}
// ACTION_SPLIT or ACTION_USE
assert(curr->used == false);

ASSERT_IS_ALIGNED(((uintptr_t)(*resultPtr)), alignment);
// In case of non-zero alignment create an aligned block what would be further used.
if (alignment > 0) {
umf_result = create_aligned_block(coarse, size, alignment, &curr);
if (umf_result != UMF_RESULT_SUCCESS) {
(void)free_blocks_re_add(coarse, curr);
goto err_unlock;
}
}

umf_result = coarse_add_used_block(coarse, *resultPtr, size);
if (umf_result != UMF_RESULT_SUCCESS) {
if (coarse->cb.free) {
coarse->cb.free(coarse->provider, *resultPtr, size);
if (action == ACTION_SPLIT) {
// Split the current block and put the new block after the one that we use.
umf_result = split_current_block(coarse, curr, size);
if (umf_result != UMF_RESULT_SUCCESS) {
(void)free_blocks_re_add(coarse, curr);
goto err_unlock;
}
goto err_unlock;

curr->size = size;

LOG_DEBUG("coarse_ALLOC (split_block) %zu used %zu alloc %zu", size,
coarse->used_size, coarse->alloc_size);

} else { // action == ACTION_USE
LOG_DEBUG("coarse_ALLOC (same_block) %zu used %zu alloc %zu", size,
coarse->used_size, coarse->alloc_size);
}

LOG_DEBUG("coarse_ALLOC (memory_provider) %zu used %zu alloc %zu", size,
coarse->used_size, coarse->alloc_size);
curr->used = true;
*resultPtr = curr->data;
coarse->used_size += size;

umf_result = UMF_RESULT_SUCCESS;

Expand Down
Loading