Skip to content
Merged
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
6 changes: 3 additions & 3 deletions src/coarse/coarse.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Intel Corporation
* Copyright (C) 2024-2025 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Expand Down Expand Up @@ -278,7 +278,7 @@ static block_t *node_list_rm_first(ravl_free_blocks_head_t *head_node,
assert(node->prev == NULL);
struct block_t *block = node->block;

if (IS_NOT_ALIGNED(block->size, alignment)) {
if (IS_NOT_ALIGNED(((uintptr_t)block->data), alignment)) {
return NULL;
}

Expand All @@ -303,7 +303,7 @@ static block_t *node_list_rm_with_alignment(ravl_free_blocks_head_t *head_node,

ravl_free_blocks_elem_t *node;
for (node = head_node->head; node != NULL; node = node->next) {
if (IS_ALIGNED(node->block->size, alignment)) {
if (IS_ALIGNED(((uintptr_t)node->block->data), alignment)) {
return node_list_rm(head_node, node);
}
}
Expand Down
47 changes: 46 additions & 1 deletion test/coarse_lib.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Intel Corporation
* Copyright (C) 2024-2025 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Expand Down Expand Up @@ -1349,3 +1349,48 @@ TEST_P(CoarseWithMemoryStrategyTest, coarseTest_alignment_fixed_memory) {

coarse_delete(ch);
}

TEST_P(CoarseWithMemoryStrategyTest,
coarseTest_basic_non_aligned_fixed_memory) {
// preallocate some memory and initialize the vector with zeros
const size_t buff_size = 20 * MB + coarse_params.page_size;
std::vector<char> buffer(buff_size, 0);

void *buf_aligned = (void *)ALIGN_UP_SAFE((uintptr_t)buffer.data(),
coarse_params.page_size);
ASSERT_NE(buf_aligned, nullptr);

void *buf_non_aligned = (void *)((uintptr_t)buf_aligned + 64);
size_t buf_non_aligned_size =
buff_size - ((uintptr_t)buf_non_aligned - (uintptr_t)buffer.data());
buf_non_aligned_size =
ALIGN_DOWN(buf_non_aligned_size, coarse_params.page_size);

coarse_params.cb.alloc = NULL;
coarse_params.cb.free = NULL;

umf_result = coarse_new(&coarse_params, &coarse_handle);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
ASSERT_NE(coarse_handle, nullptr);

coarse_t *ch = coarse_handle;
char *ptr = nullptr;

umf_result =
coarse_add_memory_fixed(ch, buf_non_aligned, buf_non_aligned_size);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);

ASSERT_EQ(coarse_get_stats(ch).used_size, 0 * MB);
ASSERT_EQ(coarse_get_stats(ch).alloc_size, buf_non_aligned_size);
ASSERT_EQ(coarse_get_stats(ch).num_all_blocks, 1);

umf_result = coarse_alloc(ch, buf_non_aligned_size, 0, (void **)&ptr);
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY);
ASSERT_EQ(ptr, nullptr);

ASSERT_EQ(coarse_get_stats(ch).used_size, 0 * MB);
ASSERT_EQ(coarse_get_stats(ch).alloc_size, buf_non_aligned_size);
ASSERT_EQ(coarse_get_stats(ch).num_all_blocks, 1);

coarse_delete(ch);
}
Loading