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
5 changes: 4 additions & 1 deletion src/provider/provider_file_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ static umf_result_t file_alloc_aligned(file_memory_provider_t *file_provider,
return UMF_RESULT_ERROR_UNKNOWN;
}

assert(file_provider->offset_mmap <= file_provider->size_mmap);

if (file_provider->size_mmap - file_provider->offset_mmap < size) {
umf_result = file_mmap_aligned(file_provider, size, alignment);
if (umf_result != UMF_RESULT_SUCCESS) {
Expand All @@ -454,7 +456,8 @@ static umf_result_t file_alloc_aligned(file_memory_provider_t *file_provider,
size_t new_offset_fd =
file_provider->offset_fd + new_offset_mmap - file_provider->offset_mmap;

if (file_provider->size_mmap - new_offset_mmap < size) {
// new_offset_mmap can be greater than file_provider->size_mmap
if (file_provider->size_mmap < size + new_offset_mmap) {
umf_result = file_mmap_aligned(file_provider, size, alignment);
if (umf_result != UMF_RESULT_SUCCESS) {
utils_mutex_unlock(&file_provider->lock);
Expand Down
79 changes: 67 additions & 12 deletions test/poolFixtures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,42 @@ TEST_P(umfPoolTest, allocFree) {
auto *ptr = umfPoolMalloc(pool.get(), allocSize);
ASSERT_NE(ptr, nullptr);
std::memset(ptr, 0, allocSize);
umfPoolFree(pool.get(), ptr);
umf_result_t umf_result = umfPoolFree(pool.get(), ptr);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
}

TEST_P(umfPoolTest, allocFreeNonAlignedSizes) {
for (const auto &allocSize : nonAlignedAllocSizes) {
auto *ptr = umfPoolMalloc(pool.get(), allocSize);
ASSERT_NE(ptr, nullptr);
std::memset(ptr, 0, allocSize);
umfPoolFree(pool.get(), ptr);
umf_result_t umf_result = umfPoolFree(pool.get(), ptr);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
}
}

TEST_P(umfPoolTest, allocFreeAligned) {
// ::aligned_alloc(alignment=4096, size=1) does not work under sanitizers for unknown reason
#if defined(_WIN32) || defined(__SANITIZE_ADDRESS__) || \
defined(__SANITIZE_THREAD__)
// TODO: implement support for windows
GTEST_SKIP();
#else
if (!umf_test::isAlignedAllocSupported(pool.get())) {
GTEST_SKIP();
}

size_t alignment = 4 * 1024; // 4kB
void *ptr = umfPoolAlignedMalloc(pool.get(), 1, alignment);
ASSERT_NE(ptr, nullptr);
ASSERT_TRUE(reinterpret_cast<uintptr_t>(ptr) % alignment == 0);
*(reinterpret_cast<unsigned char *>(ptr)) = (unsigned char)0xFF;

umf_result_t umf_result = umfPoolFree(pool.get(), ptr);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
#endif
}

TEST_P(umfPoolTest, reallocFree) {
if (!umf_test::isReallocSupported(pool.get())) {
GTEST_SKIP();
Expand All @@ -160,7 +184,8 @@ TEST_P(umfPoolTest, reallocFree) {
auto *new_ptr = umfPoolRealloc(pool.get(), ptr, allocSize * multiplier);
ASSERT_NE(new_ptr, nullptr);
std::memset(new_ptr, 0, allocSize * multiplier);
umfPoolFree(pool.get(), new_ptr);
umf_result_t umf_result = umfPoolFree(pool.get(), new_ptr);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
}

TEST_P(umfPoolTest, callocFree) {
Expand All @@ -174,7 +199,8 @@ TEST_P(umfPoolTest, callocFree) {
for (size_t i = 0; i < num; ++i) {
ASSERT_EQ(((int *)ptr)[i], 0);
}
umfPoolFree(pool.get(), ptr);
umf_result_t umf_result = umfPoolFree(pool.get(), ptr);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
}

void pow2AlignedAllocHelper(umf_memory_pool_handle_t pool) {
Expand All @@ -195,9 +221,31 @@ void pow2AlignedAllocHelper(umf_memory_pool_handle_t pool) {
}

for (auto &ptr : allocs) {
umfPoolFree(pool, ptr);
umf_result_t umf_result = umfPoolFree(pool, ptr);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
}
}

// ::aligned_alloc(alignment=4096, size=1) does not work under sanitizers for unknown reason
#if !defined(__SANITIZE_ADDRESS__) && !defined(__SANITIZE_THREAD__)
// the same for size = 1
for (size_t alignment = 1; alignment <= maxAlignment; alignment <<= 1) {
std::vector<void *> allocs;

for (size_t alloc = 0; alloc < numAllocs; alloc++) {
auto *ptr = umfPoolAlignedMalloc(pool, 1, alignment);
ASSERT_NE(ptr, nullptr);
ASSERT_TRUE(reinterpret_cast<uintptr_t>(ptr) % alignment == 0);
*(reinterpret_cast<unsigned char *>(ptr)) = (unsigned char)0xFF;
allocs.push_back(ptr);
}

for (auto &ptr : allocs) {
umf_result_t umf_result = umfPoolFree(pool, ptr);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
}
}
#endif
}

TEST_P(umfPoolTest, pow2AlignedAlloc) {
Expand Down Expand Up @@ -227,7 +275,8 @@ TEST_P(umfPoolTest, multiThreadedMallocFree) {
}

for (auto allocation : allocations) {
umfPoolFree(inPool, allocation);
umf_result_t umf_result = umfPoolFree(inPool, allocation);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
}
};

Expand Down Expand Up @@ -280,7 +329,8 @@ TEST_P(umfPoolTest, multiThreadedReallocFree) {
for (auto allocation : allocations) {
auto *ptr =
umfPoolRealloc(inPool, allocation, allocSize * multiplier);
umfPoolFree(inPool, ptr);
umf_result_t umf_result = umfPoolFree(inPool, ptr);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
}
};

Expand Down Expand Up @@ -310,7 +360,8 @@ TEST_P(umfPoolTest, multiThreadedCallocFree) {
}

for (auto allocation : allocations) {
umfPoolFree(inPool, allocation);
umf_result_t umf_result = umfPoolFree(inPool, allocation);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
}
};

Expand All @@ -335,7 +386,8 @@ TEST_P(umfPoolTest, multiThreadedMallocFreeRandomSizes) {
}

for (auto allocation : allocations) {
umfPoolFree(inPool, allocation);
umf_result_t umf_result = umfPoolFree(inPool, allocation);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
}
};

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

for (int i = 0; i < expectedRecycledPoolAllocs; i++) {
umfPoolFree(hPool, allocations.back());
umf_result_t umf_result = umfPoolFree(hPool, allocations.back());
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
allocations.pop_back();
}

Expand All @@ -385,7 +438,8 @@ TEST_P(umfMemTest, outOfMem) {
}

for (auto allocation : allocations) {
umfPoolFree(hPool, allocation);
umf_result_t umf_result = umfPoolFree(hPool, allocation);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
}
}

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

umfPoolFree(pool.get(), ptr);
umf_result_t umf_result = umfPoolFree(pool.get(), ptr);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
}
}
}
Expand Down