Provide a pread implementation for MSVC (#1666)

This commit is contained in:
Cheng 2024-12-11 08:55:53 +09:00 committed by GitHub
parent 92ab6bdeb8
commit 56db268f47
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,6 +5,12 @@
#include <limits>
#include <sstream>
// Used by pread implementation.
#ifdef _MSC_VER
#define NOMINMAX
#include <windows.h>
#endif
#include "mlx/io/load.h"
#include "mlx/ops.h"
#include "mlx/primitives.h"
@ -100,6 +106,29 @@ Dtype dtype_from_array_protocol(std::string_view t) {
"[from_str] Invalid array protocol type-string: " + std::string(t));
}
#ifdef _MSC_VER
// There is no pread on Windows, emulate it with ReadFile.
int64_t pread(int fd, void* buf, uint64_t size, uint64_t offset) {
HANDLE file = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
if (file == INVALID_HANDLE_VALUE) {
return -1;
}
OVERLAPPED overlapped = {0};
overlapped.Offset = offset & 0xFFFFFFFF;
overlapped.OffsetHigh = (offset >> 32) & 0xFFFFFFFF;
DWORD bytes_read;
if (!ReadFile(file, buf, size, &bytes_read, &overlapped)) {
if (GetLastError() != ERROR_HANDLE_EOF) {
return -1;
}
}
return bytes_read;
}
#endif
} // namespace
/** Save array to out stream in .npy format */