Skip to content

Commit fc1cbed

Browse files
authored
Merge pull request #1116 from ldorau/Fix_node_list_rm_first_and_node_list_rm_with_alignment
Fix `node_list_rm_first()` and `node_list_rm_with_alignment()` in the coarse library
2 parents a9ff7a8 + d6c5327 commit fc1cbed

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/coarse/coarse.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 Intel Corporation
2+
* Copyright (C) 2024-2025 Intel Corporation
33
*
44
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -278,7 +278,7 @@ static block_t *node_list_rm_first(ravl_free_blocks_head_t *head_node,
278278
assert(node->prev == NULL);
279279
struct block_t *block = node->block;
280280

281-
if (IS_NOT_ALIGNED(block->size, alignment)) {
281+
if (IS_NOT_ALIGNED(((uintptr_t)block->data), alignment)) {
282282
return NULL;
283283
}
284284

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

304304
ravl_free_blocks_elem_t *node;
305305
for (node = head_node->head; node != NULL; node = node->next) {
306-
if (IS_ALIGNED(node->block->size, alignment)) {
306+
if (IS_ALIGNED(((uintptr_t)node->block->data), alignment)) {
307307
return node_list_rm(head_node, node);
308308
}
309309
}

test/coarse_lib.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 Intel Corporation
2+
* Copyright (C) 2024-2025 Intel Corporation
33
*
44
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -1349,3 +1349,48 @@ TEST_P(CoarseWithMemoryStrategyTest, coarseTest_alignment_fixed_memory) {
13491349

13501350
coarse_delete(ch);
13511351
}
1352+
1353+
TEST_P(CoarseWithMemoryStrategyTest,
1354+
coarseTest_basic_non_aligned_fixed_memory) {
1355+
// preallocate some memory and initialize the vector with zeros
1356+
const size_t buff_size = 20 * MB + coarse_params.page_size;
1357+
std::vector<char> buffer(buff_size, 0);
1358+
1359+
void *buf_aligned = (void *)ALIGN_UP_SAFE((uintptr_t)buffer.data(),
1360+
coarse_params.page_size);
1361+
ASSERT_NE(buf_aligned, nullptr);
1362+
1363+
void *buf_non_aligned = (void *)((uintptr_t)buf_aligned + 64);
1364+
size_t buf_non_aligned_size =
1365+
buff_size - ((uintptr_t)buf_non_aligned - (uintptr_t)buffer.data());
1366+
buf_non_aligned_size =
1367+
ALIGN_DOWN(buf_non_aligned_size, coarse_params.page_size);
1368+
1369+
coarse_params.cb.alloc = NULL;
1370+
coarse_params.cb.free = NULL;
1371+
1372+
umf_result = coarse_new(&coarse_params, &coarse_handle);
1373+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
1374+
ASSERT_NE(coarse_handle, nullptr);
1375+
1376+
coarse_t *ch = coarse_handle;
1377+
char *ptr = nullptr;
1378+
1379+
umf_result =
1380+
coarse_add_memory_fixed(ch, buf_non_aligned, buf_non_aligned_size);
1381+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
1382+
1383+
ASSERT_EQ(coarse_get_stats(ch).used_size, 0 * MB);
1384+
ASSERT_EQ(coarse_get_stats(ch).alloc_size, buf_non_aligned_size);
1385+
ASSERT_EQ(coarse_get_stats(ch).num_all_blocks, 1);
1386+
1387+
umf_result = coarse_alloc(ch, buf_non_aligned_size, 0, (void **)&ptr);
1388+
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY);
1389+
ASSERT_EQ(ptr, nullptr);
1390+
1391+
ASSERT_EQ(coarse_get_stats(ch).used_size, 0 * MB);
1392+
ASSERT_EQ(coarse_get_stats(ch).alloc_size, buf_non_aligned_size);
1393+
ASSERT_EQ(coarse_get_stats(ch).num_all_blocks, 1);
1394+
1395+
coarse_delete(ch);
1396+
}

0 commit comments

Comments
 (0)