|
17 | 17 | #if !defined(ASIO_WINDOWS) |
18 | 18 | #include <fcntl.h> |
19 | 19 | #include <unistd.h> |
| 20 | + |
20 | 21 | #include <cstdlib> |
21 | 22 | #include <cstring> |
22 | 23 | #endif |
@@ -934,107 +935,118 @@ TEST_CASE("large_file_write_with_pool_test") { |
934 | 935 | #if !defined(ASIO_WINDOWS) |
935 | 936 | constexpr size_t DIRECT_IO_ALIGNMENT = 512; |
936 | 937 |
|
937 | | -inline void* aligned_alloc_direct_io(size_t size) { |
938 | | - void* ptr = nullptr; |
| 938 | +inline void *aligned_alloc_direct_io(size_t size) { |
| 939 | + void *ptr = nullptr; |
939 | 940 | if (posix_memalign(&ptr, DIRECT_IO_ALIGNMENT, size) != 0) { |
940 | 941 | return nullptr; |
941 | 942 | } |
942 | 943 | return ptr; |
943 | 944 | } |
944 | 945 |
|
945 | | -inline void aligned_free_direct_io(void* ptr) { |
946 | | - free(ptr); |
947 | | -} |
| 946 | +inline void aligned_free_direct_io(void *ptr) { free(ptr); } |
948 | 947 |
|
949 | 948 | inline size_t align_offset_for_direct_io(size_t offset) { |
950 | 949 | return (offset / DIRECT_IO_ALIGNMENT) * DIRECT_IO_ALIGNMENT; |
951 | 950 | } |
952 | 951 |
|
953 | 952 | inline size_t align_size_for_direct_io(size_t size) { |
954 | | - return ((size + DIRECT_IO_ALIGNMENT - 1) / DIRECT_IO_ALIGNMENT) * DIRECT_IO_ALIGNMENT; |
| 953 | + return ((size + DIRECT_IO_ALIGNMENT - 1) / DIRECT_IO_ALIGNMENT) * |
| 954 | + DIRECT_IO_ALIGNMENT; |
955 | 955 | } |
956 | 956 |
|
957 | 957 | template <coro_io::execution_type execute_type> |
958 | 958 | void test_direct_io_read_write(std::string_view filename) { |
959 | 959 | size_t file_size = 8 * KB; |
960 | 960 | create_files({std::string(filename)}, file_size); |
961 | | - |
| 961 | + |
962 | 962 | auto executor = coro_io::get_global_block_executor(); |
963 | 963 | coro_io::basic_random_coro_file<execute_type> file(executor); |
964 | | - |
| 964 | + |
965 | 965 | bool opened = file.open(filename, std::ios::in | std::ios::out, true); |
966 | 966 | if (!opened) { |
967 | 967 | WARN("Direct I/O not supported on this filesystem, skipping test"); |
968 | 968 | return; |
969 | 969 | } |
970 | | - |
| 970 | + |
971 | 971 | CHECK(file.is_open()); |
972 | | - |
| 972 | + |
973 | 973 | size_t aligned_size = align_size_for_direct_io(block_size); |
974 | | - void* aligned_buf = aligned_alloc_direct_io(aligned_size); |
| 974 | + void *aligned_buf = aligned_alloc_direct_io(aligned_size); |
975 | 975 | REQUIRE(aligned_buf != nullptr); |
976 | | - |
| 976 | + |
977 | 977 | struct BufferGuard { |
978 | | - void* ptr; |
| 978 | + void *ptr; |
979 | 979 | ~BufferGuard() { |
980 | 980 | if (ptr) { |
981 | 981 | aligned_free_direct_io(ptr); |
982 | 982 | } |
983 | 983 | } |
984 | 984 | } guard{aligned_buf}; |
985 | | - |
986 | | - char* buf = static_cast<char*>(aligned_buf); |
987 | | - |
| 985 | + |
| 986 | + char *buf = static_cast<char *>(aligned_buf); |
| 987 | + |
988 | 988 | size_t aligned_offset = align_offset_for_direct_io(0); |
989 | 989 | size_t aligned_read_size = align_size_for_direct_io(block_size); |
990 | | - |
991 | | - auto [ec, bytes_read] = async_simple::coro::syncAwait( |
| 990 | + |
| 991 | + std::error_code ec; |
| 992 | + size_t bytes_read; |
| 993 | + std::tie(ec, bytes_read) = async_simple::coro::syncAwait( |
992 | 994 | file.async_read_at(aligned_offset, buf, aligned_read_size)); |
993 | | - |
| 995 | + |
994 | 996 | CHECK(!ec); |
995 | 997 | CHECK(bytes_read == aligned_read_size); |
996 | 998 | CHECK(!file.eof()); |
997 | | - |
| 999 | + |
998 | 1000 | std::memset(buf, 'X', aligned_read_size); |
999 | | - |
1000 | | - auto [write_ec, bytes_written] = async_simple::coro::syncAwait( |
| 1001 | + |
| 1002 | + std::error_code write_ec; |
| 1003 | + size_t bytes_written; |
| 1004 | + std::tie(write_ec, bytes_written) = async_simple::coro::syncAwait( |
1001 | 1005 | file.async_write_at(aligned_offset, std::string_view(buf, aligned_read_size))); |
1002 | | - |
| 1006 | + |
1003 | 1007 | CHECK(!write_ec); |
1004 | 1008 | CHECK(bytes_written == aligned_read_size); |
1005 | | - |
| 1009 | + |
1006 | 1010 | std::memset(buf, 0, aligned_read_size); |
1007 | | - auto [read_ec, read_bytes] = async_simple::coro::syncAwait( |
| 1011 | + std::error_code read_ec; |
| 1012 | + size_t read_bytes; |
| 1013 | + std::tie(read_ec, read_bytes) = async_simple::coro::syncAwait( |
1008 | 1014 | file.async_read_at(aligned_offset, buf, aligned_read_size)); |
1009 | | - |
| 1015 | + |
1010 | 1016 | CHECK(!read_ec); |
1011 | 1017 | CHECK(read_bytes == aligned_read_size); |
1012 | | - |
| 1018 | + |
1013 | 1019 | for (size_t i = 0; i < aligned_read_size; ++i) { |
1014 | 1020 | CHECK(buf[i] == 'X'); |
1015 | 1021 | } |
1016 | | - |
1017 | | - for (size_t offset = 0; offset < file_size - aligned_read_size; offset += aligned_read_size) { |
| 1022 | + |
| 1023 | + for (size_t offset = 0; offset < file_size - aligned_read_size; |
| 1024 | + offset += aligned_read_size) { |
1018 | 1025 | size_t aligned_off = align_offset_for_direct_io(offset); |
1019 | | - |
1020 | | - std::memset(buf, 'A' + (offset / aligned_read_size) % 26, aligned_read_size); |
1021 | | - auto [w_ec, w_bytes] = async_simple::coro::syncAwait( |
1022 | | - file.async_write_at(aligned_off, std::string_view(buf, aligned_read_size))); |
| 1026 | + |
| 1027 | + std::memset(buf, 'A' + (offset / aligned_read_size) % 26, |
| 1028 | + aligned_read_size); |
| 1029 | + std::error_code w_ec; |
| 1030 | + size_t w_bytes; |
| 1031 | + std::tie(w_ec, w_bytes) = async_simple::coro::syncAwait(file.async_write_at( |
| 1032 | + aligned_off, std::string_view(buf, aligned_read_size))); |
1023 | 1033 | CHECK(!w_ec); |
1024 | 1034 | CHECK(w_bytes == aligned_read_size); |
1025 | | - |
| 1035 | + |
1026 | 1036 | std::memset(buf, 0, aligned_read_size); |
1027 | | - auto [r_ec, r_bytes] = async_simple::coro::syncAwait( |
| 1037 | + std::error_code r_ec; |
| 1038 | + size_t r_bytes; |
| 1039 | + std::tie(r_ec, r_bytes) = async_simple::coro::syncAwait( |
1028 | 1040 | file.async_read_at(aligned_off, buf, aligned_read_size)); |
1029 | 1041 | CHECK(!r_ec); |
1030 | 1042 | CHECK(r_bytes == aligned_read_size); |
1031 | | - |
| 1043 | + |
1032 | 1044 | char expected = 'A' + (offset / aligned_read_size) % 26; |
1033 | 1045 | for (size_t i = 0; i < aligned_read_size; ++i) { |
1034 | 1046 | CHECK(buf[i] == expected); |
1035 | 1047 | } |
1036 | 1048 | } |
1037 | | - |
| 1049 | + |
1038 | 1050 | file.close(); |
1039 | 1051 | } |
1040 | 1052 | #endif |
@@ -1062,19 +1074,19 @@ TEST_CASE("direct io alignment test") { |
1062 | 1074 | CHECK(align_offset_for_direct_io(512) == 512); |
1063 | 1075 | CHECK(align_offset_for_direct_io(1000) == 512); |
1064 | 1076 | CHECK(align_offset_for_direct_io(1024) == 1024); |
1065 | | - |
| 1077 | + |
1066 | 1078 | CHECK(align_size_for_direct_io(0) == 0); |
1067 | 1079 | CHECK(align_size_for_direct_io(100) == 512); |
1068 | 1080 | CHECK(align_size_for_direct_io(512) == 512); |
1069 | 1081 | CHECK(align_size_for_direct_io(1000) == 1024); |
1070 | 1082 | CHECK(align_size_for_direct_io(1024) == 1024); |
1071 | | - |
1072 | | - void* ptr1 = aligned_alloc_direct_io(1024); |
| 1083 | + |
| 1084 | + void *ptr1 = aligned_alloc_direct_io(1024); |
1073 | 1085 | REQUIRE(ptr1 != nullptr); |
1074 | 1086 | CHECK(reinterpret_cast<uintptr_t>(ptr1) % DIRECT_IO_ALIGNMENT == 0); |
1075 | 1087 | aligned_free_direct_io(ptr1); |
1076 | | - |
1077 | | - void* ptr2 = aligned_alloc_direct_io(4096); |
| 1088 | + |
| 1089 | + void *ptr2 = aligned_alloc_direct_io(4096); |
1078 | 1090 | REQUIRE(ptr2 != nullptr); |
1079 | 1091 | CHECK(reinterpret_cast<uintptr_t>(ptr2) % DIRECT_IO_ALIGNMENT == 0); |
1080 | 1092 | aligned_free_direct_io(ptr2); |
|
0 commit comments