Skip to content

Commit d1d10d9

Browse files
committed
Make coarse_alloc() return aligned address when alignment==0
Make coarse_alloc() always return address aligned to coarse->page_size when alignment==0. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 2fca364 commit d1d10d9

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

src/coarse/coarse.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,16 +1017,16 @@ umf_result_t coarse_alloc(coarse_t *coarse, size_t size, size_t alignment,
10171017
}
10181018

10191019
// alignment must be a power of two and a multiple or a divider of the page size
1020-
if (alignment &&
1021-
((alignment & (alignment - 1)) || ((alignment % coarse->page_size) &&
1022-
(coarse->page_size % alignment)))) {
1020+
if (alignment == 0) {
1021+
alignment = coarse->page_size;
1022+
} else if ((alignment & (alignment - 1)) ||
1023+
((alignment % coarse->page_size) &&
1024+
(coarse->page_size % alignment))) {
10231025
LOG_ERR("wrong alignment: %zu (not a power of 2 or a multiple or a "
10241026
"divider of the page size (%zu))",
10251027
alignment, coarse->page_size);
10261028
return UMF_RESULT_ERROR_INVALID_ALIGNMENT;
1027-
}
1028-
1029-
if (IS_NOT_ALIGNED(alignment, coarse->page_size)) {
1029+
} else if (IS_NOT_ALIGNED(alignment, coarse->page_size)) {
10301030
alignment = ALIGN_UP(alignment, coarse->page_size);
10311031
}
10321032

test/coarse_lib.cpp

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,10 @@ TEST_P(CoarseWithMemoryStrategyTest, coarseTest_basic_provider) {
166166

167167
TEST_P(CoarseWithMemoryStrategyTest, coarseTest_basic_fixed_memory) {
168168
// preallocate some memory and initialize the vector with zeros
169-
const size_t buff_size = 20 * MB;
169+
const size_t buff_size = 20 * MB + coarse_params.page_size;
170170
std::vector<char> buffer(buff_size, 0);
171-
void *buf = (void *)buffer.data();
171+
void *buf = (void *)ALIGN_UP_SAFE((uintptr_t)buffer.data(),
172+
coarse_params.page_size);
172173
ASSERT_NE(buf, nullptr);
173174

174175
coarse_params.cb.alloc = NULL;
@@ -206,9 +207,10 @@ TEST_P(CoarseWithMemoryStrategyTest, coarseTest_basic_fixed_memory) {
206207

207208
TEST_P(CoarseWithMemoryStrategyTest, coarseTest_fixed_memory_various) {
208209
// preallocate some memory and initialize the vector with zeros
209-
const size_t buff_size = 20 * MB;
210+
const size_t buff_size = 20 * MB + coarse_params.page_size;
210211
std::vector<char> buffer(buff_size, 0);
211-
void *buf = (void *)buffer.data();
212+
void *buf = (void *)ALIGN_UP_SAFE((uintptr_t)buffer.data(),
213+
coarse_params.page_size);
212214
ASSERT_NE(buf, nullptr);
213215

214216
coarse_params.cb.alloc = NULL;
@@ -627,6 +629,15 @@ TEST_P(CoarseWithMemoryStrategyTest, coarseTest_basic_free_cb_fails) {
627629
}
628630

629631
TEST_P(CoarseWithMemoryStrategyTest, coarseTest_split_cb_fails) {
632+
if (coarse_params.allocation_strategy ==
633+
UMF_COARSE_MEMORY_STRATEGY_FASTEST) {
634+
// This test is designed for the UMF_COARSE_MEMORY_STRATEGY_FASTEST_BUT_ONE
635+
// and UMF_COARSE_MEMORY_STRATEGY_CHECK_ALL_SIZE strategies,
636+
// because the UMF_COARSE_MEMORY_STRATEGY_FASTEST strategy
637+
// looks always for a block of size greater by the page size.
638+
return;
639+
}
640+
630641
umf_memory_provider_handle_t malloc_memory_provider;
631642
umf_result = umfMemoryProviderCreate(&UMF_MALLOC_MEMORY_PROVIDER_OPS, NULL,
632643
&malloc_memory_provider);
@@ -702,9 +713,10 @@ TEST_P(CoarseWithMemoryStrategyTest, coarseTest_split_cb_fails) {
702713

703714
TEST_P(CoarseWithMemoryStrategyTest, coarseTest_merge_cb_fails) {
704715
// preallocate some memory and initialize the vector with zeros
705-
const size_t buff_size = 10 * MB;
716+
const size_t buff_size = 10 * MB + coarse_params.page_size;
706717
std::vector<char> buffer(buff_size, 0);
707-
void *buf = (void *)buffer.data();
718+
void *buf = (void *)ALIGN_UP_SAFE((uintptr_t)buffer.data(),
719+
coarse_params.page_size);
708720
ASSERT_NE(buf, nullptr);
709721

710722
coarse_params.cb.alloc = NULL;
@@ -901,6 +913,15 @@ TEST_P(CoarseWithMemoryStrategyTest, coarseTest_provider_alloc_not_set) {
901913
}
902914

903915
TEST_P(CoarseWithMemoryStrategyTest, coarseTest_basic) {
916+
if (coarse_params.allocation_strategy ==
917+
UMF_COARSE_MEMORY_STRATEGY_FASTEST) {
918+
// This test is designed for the UMF_COARSE_MEMORY_STRATEGY_FASTEST_BUT_ONE
919+
// and UMF_COARSE_MEMORY_STRATEGY_CHECK_ALL_SIZE strategies,
920+
// because the UMF_COARSE_MEMORY_STRATEGY_FASTEST strategy
921+
// looks always for a block of size greater by the page size.
922+
return;
923+
}
924+
904925
umf_memory_provider_handle_t malloc_memory_provider;
905926
umf_result = umfMemoryProviderCreate(&UMF_MALLOC_MEMORY_PROVIDER_OPS, NULL,
906927
&malloc_memory_provider);
@@ -1065,6 +1086,15 @@ TEST_P(CoarseWithMemoryStrategyTest, coarseTest_basic) {
10651086
}
10661087

10671088
TEST_P(CoarseWithMemoryStrategyTest, coarseTest_simple1) {
1089+
if (coarse_params.allocation_strategy ==
1090+
UMF_COARSE_MEMORY_STRATEGY_FASTEST) {
1091+
// This test is designed for the UMF_COARSE_MEMORY_STRATEGY_FASTEST_BUT_ONE
1092+
// and UMF_COARSE_MEMORY_STRATEGY_CHECK_ALL_SIZE strategies,
1093+
// because the UMF_COARSE_MEMORY_STRATEGY_FASTEST strategy
1094+
// looks always for a block of size greater by the page size.
1095+
return;
1096+
}
1097+
10681098
umf_memory_provider_handle_t malloc_memory_provider;
10691099
umf_result = umfMemoryProviderCreate(&UMF_MALLOC_MEMORY_PROVIDER_OPS, NULL,
10701100
&malloc_memory_provider);
@@ -1106,8 +1136,9 @@ TEST_P(CoarseWithMemoryStrategyTest, coarseTest_simple1) {
11061136
ASSERT_NE(t[i], nullptr);
11071137
}
11081138

1109-
if (max_alloc_size == 0) {
1110-
max_alloc_size = coarse_get_stats(ch).alloc_size;
1139+
size_t alloc_size = coarse_get_stats(ch).alloc_size;
1140+
if (alloc_size > max_alloc_size) {
1141+
max_alloc_size = alloc_size;
11111142
}
11121143

11131144
for (int i = 0; i < nptrs; i++) {
@@ -1253,9 +1284,10 @@ TEST_P(CoarseWithMemoryStrategyTest, coarseTest_alignment_provider) {
12531284

12541285
TEST_P(CoarseWithMemoryStrategyTest, coarseTest_alignment_fixed_memory) {
12551286
// preallocate some memory and initialize the vector with zeros
1256-
const size_t alloc_size = 40 * MB;
1287+
const size_t alloc_size = 40 * MB + coarse_params.page_size;
12571288
std::vector<char> buffer(alloc_size, 0);
1258-
void *buf = (void *)buffer.data();
1289+
void *buf = (void *)ALIGN_UP_SAFE((uintptr_t)buffer.data(),
1290+
coarse_params.page_size);
12591291
ASSERT_NE(buf, nullptr);
12601292

12611293
coarse_params.cb.alloc = NULL;

0 commit comments

Comments
 (0)