forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Build] fix file_data_loader.cpp build issues for windows
Two build issues have been addressed in this pull request: 1. Avoid closing when fd_ is -1, as this would cause a crash on Windows. 2. Introduce separate headers for Windows and Unix, enabling Windows builds to utilize a distinct header and implement pread. For pytorch#4661
- Loading branch information
1 parent
a79b1a6
commit c709a7d
Showing
5 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <unistd.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <executorch/runtime/platform/compiler.h> // For ssize_t. | ||
#include <io.h> | ||
|
||
#include <windows.h> | ||
|
||
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<int32_t>::max() in | ||
// file_data_loader.cpp. | ||
#undef max |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters