Skip to content

Commit

Permalink
Add test for _aligned_realloc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandr-Konovalov committed Mar 18, 2024
1 parent 064abad commit 28b32fb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
16 changes: 8 additions & 8 deletions test/pstl_offload/memory/allocation_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ struct allocs {
void* aligned_realloc_ptr;
#endif // __linux__

void *new_ptr;
void *arr_new_ptr;
void *nothrow_new_ptr;
void *arr_nothrow_new_ptr;
void *aligned_new_ptr;
void *aligned_nothrow_new_ptr;
void *aligned_arr_new_ptr;
void *aligned_nothrow_arr_new_ptr;
void* new_ptr;
void* arr_new_ptr;
void* nothrow_new_ptr;
void* arr_nothrow_new_ptr;
void* aligned_new_ptr;
void* aligned_nothrow_new_ptr;
void* aligned_arr_new_ptr;
void* aligned_nothrow_arr_new_ptr;

static constexpr std::size_t alloc_size = 1024;
static constexpr std::size_t alignment = 16;
Expand Down
5 changes: 4 additions & 1 deletion test/pstl_offload/memory/interop_allocs.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ void check_memory_ownership(const allocs &na, sycl::usm::alloc expected_type) {
"Unexpected pointer type");
EXPECT_TRUE(sycl::get_pointer_type(na.realloc_ptr, memory_context) == expected_type,
"Unexpected pointer type");
#if __linux__
EXPECT_TRUE(sycl::get_pointer_type(na.memalign_ptr, memory_context) == expected_type,
"Unexpected pointer type");
#if __linux__
EXPECT_TRUE(sycl::get_pointer_type(na.posix_memalign_ptr, memory_context) == expected_type,
"Unexpected pointer type");
EXPECT_TRUE(sycl::get_pointer_type(na.aligned_alloc_ptr, memory_context) == expected_type,
Expand All @@ -38,6 +38,9 @@ void check_memory_ownership(const allocs &na, sycl::usm::alloc expected_type) {
"Unexpected pointer type");
EXPECT_TRUE(sycl::get_pointer_type(na.libc_memalign_ptr, memory_context) == expected_type,
"Unexpected pointer type");
#elif _WIN64
EXPECT_TRUE(sycl::get_pointer_type(na.aligned_realloc_ptr, memory_context) == expected_type,
"Unexpected pointer type");
#endif // __linux__

EXPECT_TRUE(sycl::get_pointer_type(na.new_ptr, memory_context) == expected_type,
Expand Down
36 changes: 29 additions & 7 deletions test/pstl_offload/memory/usm_memory_alignment.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ void test_alignment_allocation(AllocatingFunction allocate, DeallocatingFunction

for (std::size_t size : sizes) {
void* ptr = allocate(size, alignment);
EXPECT_TRUE(ptr, "nullptr returned by allocation");
EXPECT_TRUE(std::uintptr_t(ptr) % alignment == 0, "The returned pointer is not properly aligned");
deallocate(ptr, alignment);
deallocate(ptr, size, alignment);
}
}
}
Expand Down Expand Up @@ -80,17 +81,17 @@ void test_new_alignment() {
return ::operator new[](size, std::align_val_t(alignment), std::nothrow);
};

auto delete_deallocate = [](void* ptr, std::size_t alignment) {
auto delete_deallocate = [](void* ptr, std::size_t, std::size_t alignment) {
return ::operator delete(ptr, std::align_val_t(alignment));
};
auto delete_nothrow_deallocate = [](void* ptr, std::size_t alignment) {
auto delete_nothrow_deallocate = [](void* ptr, std::size_t, std::size_t alignment) {
return ::operator delete(ptr, std::align_val_t(alignment), std::nothrow);
};

auto delete_array_deallocate = [](void* ptr, std::size_t alignment) {
auto delete_array_deallocate = [](void* ptr, std::size_t, std::size_t alignment) {
return ::operator delete[](ptr, std::align_val_t(alignment));
};
auto delete_array_nothrow_deallocate = [](void* ptr, std::size_t alignment) {
auto delete_array_nothrow_deallocate = [](void* ptr, std::size_t, std::size_t alignment) {
return ::operator delete[](ptr, std::align_val_t(alignment), std::nothrow);
};

Expand Down Expand Up @@ -119,7 +120,7 @@ int main() {
return __libc_memalign(alignment, size);
};

auto free_deallocate = [](void* ptr, std::size_t) {
auto free_deallocate = [](void* ptr, std::size_t, std::size_t) {
free(ptr);
};

Expand All @@ -131,11 +132,32 @@ int main() {
auto _aligned_malloc_allocate = [](std::size_t size, std::size_t alignment) {
return _aligned_malloc(size, alignment);
};
auto _aligned_free_deallocate = [](void* ptr, std::size_t) {
auto _aligned_free_deallocate = [](void* ptr, std::size_t, std::size_t) {
_aligned_free(ptr);
};

test_alignment_allocation(_aligned_malloc_allocate, _aligned_free_deallocate);

auto _aligned_realloc_allocate = [](std::size_t size, std::size_t alignment) {
return _aligned_realloc(nullptr, size, alignment);
};
auto _aligned_realloc_existing_allocate = [](std::size_t size, std::size_t alignment) {
// "It's an error to reallocate memory and change the alignment of a block.",
// so have to re-use the alignment.
void* ptr = _aligned_malloc(size, alignment);
EXPECT_TRUE(ptr, "nullptr returned");
EXPECT_TRUE(std::uintptr_t(ptr) % alignment == 0, "The returned pointer is not properly aligned");
// this can be called with zero size, but _aligned_realloc() in zero case became _aligned_free()
return _aligned_realloc(ptr, size ? size : 1, alignment);
};
auto _aligned_realloc_deallocate = [](void* ptr, std::size_t size, std::size_t alignment) {
EXPECT_TRUE(_aligned_msize(ptr, alignment, 0) >= size, "Invalid size returned by _aligned_msize");
void * ret = _aligned_realloc(ptr, 0, alignment);
EXPECT_TRUE(ret == nullptr, "_aligned_realloc(ptr, 0, alignment) must return nullptr");
};

test_alignment_allocation(_aligned_realloc_allocate, _aligned_realloc_deallocate);
test_alignment_allocation(_aligned_realloc_existing_allocate, _aligned_realloc_deallocate);
#endif

test_new_alignment();
Expand Down

0 comments on commit 28b32fb

Please sign in to comment.