Skip to content

Commit 6e49be4

Browse files
authored
Merge pull request #1247 from ldorau/Add_allocFreeAligned_test
Fix file_alloc_aligned() and add the allocFreeAligned test to poolFixtures.hpp
2 parents 52668c3 + 9990b34 commit 6e49be4

File tree

2 files changed

+71
-13
lines changed

2 files changed

+71
-13
lines changed

src/provider/provider_file_memory.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ static umf_result_t file_alloc_aligned(file_memory_provider_t *file_provider,
429429
return UMF_RESULT_ERROR_UNKNOWN;
430430
}
431431

432+
assert(file_provider->offset_mmap <= file_provider->size_mmap);
433+
432434
if (file_provider->size_mmap - file_provider->offset_mmap < size) {
433435
umf_result = file_mmap_aligned(file_provider, size, alignment);
434436
if (umf_result != UMF_RESULT_SUCCESS) {
@@ -454,7 +456,8 @@ static umf_result_t file_alloc_aligned(file_memory_provider_t *file_provider,
454456
size_t new_offset_fd =
455457
file_provider->offset_fd + new_offset_mmap - file_provider->offset_mmap;
456458

457-
if (file_provider->size_mmap - new_offset_mmap < size) {
459+
// new_offset_mmap can be greater than file_provider->size_mmap
460+
if (file_provider->size_mmap < size + new_offset_mmap) {
458461
umf_result = file_mmap_aligned(file_provider, size, alignment);
459462
if (umf_result != UMF_RESULT_SUCCESS) {
460463
utils_mutex_unlock(&file_provider->lock);

test/poolFixtures.hpp

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,42 @@ TEST_P(umfPoolTest, allocFree) {
136136
auto *ptr = umfPoolMalloc(pool.get(), allocSize);
137137
ASSERT_NE(ptr, nullptr);
138138
std::memset(ptr, 0, allocSize);
139-
umfPoolFree(pool.get(), ptr);
139+
umf_result_t umf_result = umfPoolFree(pool.get(), ptr);
140+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
140141
}
141142

142143
TEST_P(umfPoolTest, allocFreeNonAlignedSizes) {
143144
for (const auto &allocSize : nonAlignedAllocSizes) {
144145
auto *ptr = umfPoolMalloc(pool.get(), allocSize);
145146
ASSERT_NE(ptr, nullptr);
146147
std::memset(ptr, 0, allocSize);
147-
umfPoolFree(pool.get(), ptr);
148+
umf_result_t umf_result = umfPoolFree(pool.get(), ptr);
149+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
148150
}
149151
}
150152

153+
TEST_P(umfPoolTest, allocFreeAligned) {
154+
// ::aligned_alloc(alignment=4096, size=1) does not work under sanitizers for unknown reason
155+
#if defined(_WIN32) || defined(__SANITIZE_ADDRESS__) || \
156+
defined(__SANITIZE_THREAD__)
157+
// TODO: implement support for windows
158+
GTEST_SKIP();
159+
#else
160+
if (!umf_test::isAlignedAllocSupported(pool.get())) {
161+
GTEST_SKIP();
162+
}
163+
164+
size_t alignment = 4 * 1024; // 4kB
165+
void *ptr = umfPoolAlignedMalloc(pool.get(), 1, alignment);
166+
ASSERT_NE(ptr, nullptr);
167+
ASSERT_TRUE(reinterpret_cast<uintptr_t>(ptr) % alignment == 0);
168+
*(reinterpret_cast<unsigned char *>(ptr)) = (unsigned char)0xFF;
169+
170+
umf_result_t umf_result = umfPoolFree(pool.get(), ptr);
171+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
172+
#endif
173+
}
174+
151175
TEST_P(umfPoolTest, reallocFree) {
152176
if (!umf_test::isReallocSupported(pool.get())) {
153177
GTEST_SKIP();
@@ -160,7 +184,8 @@ TEST_P(umfPoolTest, reallocFree) {
160184
auto *new_ptr = umfPoolRealloc(pool.get(), ptr, allocSize * multiplier);
161185
ASSERT_NE(new_ptr, nullptr);
162186
std::memset(new_ptr, 0, allocSize * multiplier);
163-
umfPoolFree(pool.get(), new_ptr);
187+
umf_result_t umf_result = umfPoolFree(pool.get(), new_ptr);
188+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
164189
}
165190

166191
TEST_P(umfPoolTest, callocFree) {
@@ -174,7 +199,8 @@ TEST_P(umfPoolTest, callocFree) {
174199
for (size_t i = 0; i < num; ++i) {
175200
ASSERT_EQ(((int *)ptr)[i], 0);
176201
}
177-
umfPoolFree(pool.get(), ptr);
202+
umf_result_t umf_result = umfPoolFree(pool.get(), ptr);
203+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
178204
}
179205

180206
void pow2AlignedAllocHelper(umf_memory_pool_handle_t pool) {
@@ -195,9 +221,31 @@ void pow2AlignedAllocHelper(umf_memory_pool_handle_t pool) {
195221
}
196222

197223
for (auto &ptr : allocs) {
198-
umfPoolFree(pool, ptr);
224+
umf_result_t umf_result = umfPoolFree(pool, ptr);
225+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
199226
}
200227
}
228+
229+
// ::aligned_alloc(alignment=4096, size=1) does not work under sanitizers for unknown reason
230+
#if !defined(__SANITIZE_ADDRESS__) && !defined(__SANITIZE_THREAD__)
231+
// the same for size = 1
232+
for (size_t alignment = 1; alignment <= maxAlignment; alignment <<= 1) {
233+
std::vector<void *> allocs;
234+
235+
for (size_t alloc = 0; alloc < numAllocs; alloc++) {
236+
auto *ptr = umfPoolAlignedMalloc(pool, 1, alignment);
237+
ASSERT_NE(ptr, nullptr);
238+
ASSERT_TRUE(reinterpret_cast<uintptr_t>(ptr) % alignment == 0);
239+
*(reinterpret_cast<unsigned char *>(ptr)) = (unsigned char)0xFF;
240+
allocs.push_back(ptr);
241+
}
242+
243+
for (auto &ptr : allocs) {
244+
umf_result_t umf_result = umfPoolFree(pool, ptr);
245+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
246+
}
247+
}
248+
#endif
201249
}
202250

203251
TEST_P(umfPoolTest, pow2AlignedAlloc) {
@@ -227,7 +275,8 @@ TEST_P(umfPoolTest, multiThreadedMallocFree) {
227275
}
228276

229277
for (auto allocation : allocations) {
230-
umfPoolFree(inPool, allocation);
278+
umf_result_t umf_result = umfPoolFree(inPool, allocation);
279+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
231280
}
232281
};
233282

@@ -280,7 +329,8 @@ TEST_P(umfPoolTest, multiThreadedReallocFree) {
280329
for (auto allocation : allocations) {
281330
auto *ptr =
282331
umfPoolRealloc(inPool, allocation, allocSize * multiplier);
283-
umfPoolFree(inPool, ptr);
332+
umf_result_t umf_result = umfPoolFree(inPool, ptr);
333+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
284334
}
285335
};
286336

@@ -310,7 +360,8 @@ TEST_P(umfPoolTest, multiThreadedCallocFree) {
310360
}
311361

312362
for (auto allocation : allocations) {
313-
umfPoolFree(inPool, allocation);
363+
umf_result_t umf_result = umfPoolFree(inPool, allocation);
364+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
314365
}
315366
};
316367

@@ -335,7 +386,8 @@ TEST_P(umfPoolTest, multiThreadedMallocFreeRandomSizes) {
335386
}
336387

337388
for (auto allocation : allocations) {
338-
umfPoolFree(inPool, allocation);
389+
umf_result_t umf_result = umfPoolFree(inPool, allocation);
390+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
339391
}
340392
};
341393

@@ -375,7 +427,8 @@ TEST_P(umfMemTest, outOfMem) {
375427
ASSERT_NE(allocations.back(), nullptr);
376428

377429
for (int i = 0; i < expectedRecycledPoolAllocs; i++) {
378-
umfPoolFree(hPool, allocations.back());
430+
umf_result_t umf_result = umfPoolFree(hPool, allocations.back());
431+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
379432
allocations.pop_back();
380433
}
381434

@@ -385,7 +438,8 @@ TEST_P(umfMemTest, outOfMem) {
385438
}
386439

387440
for (auto allocation : allocations) {
388-
umfPoolFree(hPool, allocation);
441+
umf_result_t umf_result = umfPoolFree(hPool, allocation);
442+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
389443
}
390444
}
391445

@@ -490,7 +544,8 @@ TEST_P(umfPoolTest, mallocUsableSize) {
490544
// Make sure we can write to this memory
491545
memset(ptr, 123, result);
492546

493-
umfPoolFree(pool.get(), ptr);
547+
umf_result_t umf_result = umfPoolFree(pool.get(), ptr);
548+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
494549
}
495550
}
496551
}

0 commit comments

Comments
 (0)