diff --git a/CMakeLists.txt b/CMakeLists.txt index 99c8b7f69f..72464c282b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -785,6 +785,11 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER) endif() add_executable(executor_runner ${_executor_runner__srcs}) + if(WIN32) + target_include_directories(executor_runner PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/extension/data_loader/Windows) + else() + target_include_directories(executor_runner PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/extension/data_loader/Unix) + endif() if(CMAKE_BUILD_TYPE STREQUAL "Release") if(APPLE) target_link_options(executor_runner PRIVATE "LINKER:-dead_strip") diff --git a/extension/data_loader/CMakeLists.txt b/extension/data_loader/CMakeLists.txt index c9503e78e2..c978d98f61 100644 --- a/extension/data_loader/CMakeLists.txt +++ b/extension/data_loader/CMakeLists.txt @@ -20,6 +20,11 @@ list(TRANSFORM _extension_data_loader__srcs PREPEND "${EXECUTORCH_ROOT}/") add_library(extension_data_loader ${_extension_data_loader__srcs}) target_link_libraries(extension_data_loader executorch) target_include_directories(extension_data_loader PUBLIC ${EXECUTORCH_ROOT}/..) +if(WIN32) + target_include_directories(extension_data_loader PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/Windows) +else() + target_include_directories(extension_data_loader PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/Unix) +endif() target_compile_options(extension_data_loader PUBLIC ${_common_compile_options}) # Install libraries diff --git a/extension/data_loader/Unix/file.h b/extension/data_loader/Unix/file.h new file mode 100644 index 0000000000..270794748f --- /dev/null +++ b/extension/data_loader/Unix/file.h @@ -0,0 +1,11 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include diff --git a/extension/data_loader/Windows/file.h b/extension/data_loader/Windows/file.h new file mode 100644 index 0000000000..3a9699f1eb --- /dev/null +++ b/extension/data_loader/Windows/file.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include // For ssize_t. +#include + +#include + +inline ssize_t pread(int __fd, void* __buf, size_t __nbytes, size_t __offset) { + OVERLAPPED overlapped; /* The offset for ReadFile. */ + memset(&overlapped, 0, sizeof(overlapped)); + overlapped.Offset = __offset; + overlapped.OffsetHigh = __offset >> 32; + + BOOL result; /* The result of ReadFile. */ + DWORD bytes_read; /* The number of bytes read. */ + HANDLE file = (HANDLE)_get_osfhandle(__fd); + + result = ReadFile(file, __buf, __nbytes, &bytes_read, &overlapped); + DWORD error = GetLastError(); + if (!result) { + if (error == ERROR_IO_PENDING) { + result = GetOverlappedResult(file, &overlapped, &bytes_read, TRUE); + if (!result) { + error = GetLastError(); + } + } + } + if (!result) { + // Translate error into errno. + switch (error) { + case ERROR_HANDLE_EOF: + errno = 0; + break; + default: + errno = EIO; + break; + } + return -1; + } + return bytes_read; +} + +// To avoid conflicts with std::numeric_limits::max() in +// file_data_loader.cpp. +#undef max diff --git a/extension/data_loader/file_data_loader.cpp b/extension/data_loader/file_data_loader.cpp index 1d097cfd98..cae27d944f 100644 --- a/extension/data_loader/file_data_loader.cpp +++ b/extension/data_loader/file_data_loader.cpp @@ -7,6 +7,7 @@ */ #include +#include #include #include @@ -17,7 +18,6 @@ #include #include #include -#include #include #include @@ -69,8 +69,10 @@ FileDataLoader::~FileDataLoader() { // file_name_ can be nullptr if this instance was moved from, but freeing a // null pointer is safe. std::free(const_cast(file_name_)); - // fd_ can be -1 if this instance was moved from, but closing a negative fd is - // safe (though it will return an error). + // fd_ can be -1 if this instance was moved from. + if (fd_ == -1) { + return; + } ::close(fd_); }