|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <cstddef> |
| 5 | +#include <cstdint> |
| 6 | +#include <cstdio> |
| 7 | +#include <cstdlib> |
| 8 | +#include <limits> |
| 9 | + |
| 10 | +#if defined(_WIN32) |
| 11 | +#ifndef NOMINMAX |
| 12 | +#define NOMINMAX |
| 13 | +#endif |
| 14 | +#ifndef WIN32_LEAN_AND_MEAN |
| 15 | +#define WIN32_LEAN_AND_MEAN |
| 16 | +#endif |
| 17 | +#include <fcntl.h> |
| 18 | +#include <io.h> |
| 19 | +#include <malloc.h> |
| 20 | +#include <process.h> |
| 21 | +#include <sys/stat.h> |
| 22 | +#include <windows.h> |
| 23 | +#else |
| 24 | +#include <fcntl.h> |
| 25 | +#include <sys/mman.h> |
| 26 | +#include <sys/stat.h> |
| 27 | +#include <unistd.h> |
| 28 | +#endif |
| 29 | + |
| 30 | +namespace vllm::support { |
| 31 | + |
| 32 | +inline constexpr double kPi = 3.141592653589793238462643383279502884; |
| 33 | + |
| 34 | +#if defined(_WIN32) |
| 35 | + |
| 36 | +#ifndef O_CLOEXEC |
| 37 | +#define O_CLOEXEC 0 |
| 38 | +#endif |
| 39 | + |
| 40 | +#ifndef O_NOFOLLOW |
| 41 | +#define O_NOFOLLOW 0 |
| 42 | +#endif |
| 43 | + |
| 44 | +inline int OpenFile(const char* path, int flags) { |
| 45 | + return _open(path, flags | _O_BINARY); |
| 46 | +} |
| 47 | + |
| 48 | +inline int OpenFile(const char* path, int flags, int mode) { |
| 49 | + return _open(path, flags | _O_BINARY, mode); |
| 50 | +} |
| 51 | + |
| 52 | +inline int CloseFile(int fd) { return _close(fd); } |
| 53 | + |
| 54 | +inline std::intptr_t ReadFile(int fd, void* buffer, std::size_t size) { |
| 55 | + const auto chunk = static_cast<unsigned int>( |
| 56 | + std::min<std::size_t>(size, |
| 57 | + static_cast<std::size_t>( |
| 58 | + std::numeric_limits<unsigned int>::max()))); |
| 59 | + return _read(fd, buffer, chunk); |
| 60 | +} |
| 61 | + |
| 62 | +inline std::intptr_t WriteFile(int fd, const void* buffer, std::size_t size) { |
| 63 | + const auto chunk = static_cast<unsigned int>( |
| 64 | + std::min<std::size_t>(size, |
| 65 | + static_cast<std::size_t>( |
| 66 | + std::numeric_limits<unsigned int>::max()))); |
| 67 | + return _write(fd, buffer, chunk); |
| 68 | +} |
| 69 | + |
| 70 | +inline void* MapReadOnlyFile(int fd, std::size_t size) { |
| 71 | + if (size == 0) { |
| 72 | + return nullptr; |
| 73 | + } |
| 74 | + const auto os_handle = _get_osfhandle(fd); |
| 75 | + if (os_handle == -1) { |
| 76 | + return nullptr; |
| 77 | + } |
| 78 | + HANDLE mapping = CreateFileMappingA( |
| 79 | + reinterpret_cast<HANDLE>(os_handle), nullptr, PAGE_READONLY, |
| 80 | + static_cast<DWORD>(static_cast<std::uint64_t>(size) >> 32), |
| 81 | + static_cast<DWORD>(static_cast<std::uint64_t>(size) & 0xffffffffu), |
| 82 | + nullptr); |
| 83 | + if (mapping == nullptr) { |
| 84 | + return nullptr; |
| 85 | + } |
| 86 | + void* view = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, size); |
| 87 | + CloseHandle(mapping); |
| 88 | + return view; |
| 89 | +} |
| 90 | + |
| 91 | +inline int UnmapFile(void* address, std::size_t /*size*/) { |
| 92 | + if (address == nullptr) { |
| 93 | + return 0; |
| 94 | + } |
| 95 | + return UnmapViewOfFile(address) ? 0 : -1; |
| 96 | +} |
| 97 | + |
| 98 | +inline long HostPageSize() { |
| 99 | + SYSTEM_INFO system_info{}; |
| 100 | + GetSystemInfo(&system_info); |
| 101 | + return system_info.dwPageSize > 0 |
| 102 | + ? static_cast<long>(system_info.dwPageSize) |
| 103 | + : 4096L; |
| 104 | +} |
| 105 | + |
| 106 | +inline int CurrentProcessId() { return _getpid(); } |
| 107 | + |
| 108 | +inline int FileDescriptorFromFile(std::FILE* file) { return _fileno(file); } |
| 109 | + |
| 110 | +inline bool TruncateFile(int fd, std::uint64_t size) { return _chsize_s(fd, size) == 0; } |
| 111 | + |
| 112 | +inline bool SetEnvVar(const char* name, const char* value) { |
| 113 | + return _putenv_s(name, value) == 0; |
| 114 | +} |
| 115 | + |
| 116 | +inline bool UnsetEnvVar(const char* name) { return _putenv_s(name, "") == 0; } |
| 117 | + |
| 118 | +inline void* AlignedAlloc(std::size_t alignment, std::size_t size) { |
| 119 | + return _aligned_malloc(size, alignment); |
| 120 | +} |
| 121 | + |
| 122 | +inline void AlignedFree(void* pointer) { _aligned_free(pointer); } |
| 123 | + |
| 124 | +#else |
| 125 | + |
| 126 | +inline int OpenFile(const char* path, int flags) { return ::open(path, flags); } |
| 127 | + |
| 128 | +inline int OpenFile(const char* path, int flags, int mode) { |
| 129 | + return ::open(path, flags, mode); |
| 130 | +} |
| 131 | + |
| 132 | +inline int CloseFile(int fd) { return ::close(fd); } |
| 133 | + |
| 134 | +inline ssize_t ReadFile(int fd, void* buffer, std::size_t size) { |
| 135 | + return ::read(fd, buffer, size); |
| 136 | +} |
| 137 | + |
| 138 | +inline ssize_t WriteFile(int fd, const void* buffer, std::size_t size) { |
| 139 | + return ::write(fd, buffer, size); |
| 140 | +} |
| 141 | + |
| 142 | +inline void* MapReadOnlyFile(int fd, std::size_t size) { |
| 143 | + void* mapped = ::mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 144 | + return mapped == MAP_FAILED ? nullptr : mapped; |
| 145 | +} |
| 146 | + |
| 147 | +inline int UnmapFile(void* address, std::size_t size) { |
| 148 | + if (address == nullptr) { |
| 149 | + return 0; |
| 150 | + } |
| 151 | + return ::munmap(address, size); |
| 152 | +} |
| 153 | + |
| 154 | +inline long HostPageSize() { |
| 155 | + const long page_size = ::sysconf(_SC_PAGESIZE); |
| 156 | + return page_size > 0 ? page_size : 4096L; |
| 157 | +} |
| 158 | + |
| 159 | +inline int CurrentProcessId() { return ::getpid(); } |
| 160 | + |
| 161 | +inline int FileDescriptorFromFile(std::FILE* file) { return ::fileno(file); } |
| 162 | + |
| 163 | +inline bool TruncateFile(int fd, std::uint64_t size) { |
| 164 | + return ::ftruncate(fd, static_cast<off_t>(size)) == 0; |
| 165 | +} |
| 166 | + |
| 167 | +inline bool SetEnvVar(const char* name, const char* value) { |
| 168 | + return ::setenv(name, value, 1) == 0; |
| 169 | +} |
| 170 | + |
| 171 | +inline bool UnsetEnvVar(const char* name) { return ::unsetenv(name) == 0; } |
| 172 | + |
| 173 | +inline void* AlignedAlloc(std::size_t alignment, std::size_t size) { |
| 174 | + return std::aligned_alloc(alignment, size); |
| 175 | +} |
| 176 | + |
| 177 | +inline void AlignedFree(void* pointer) { std::free(pointer); } |
| 178 | + |
| 179 | +#endif |
| 180 | + |
| 181 | +} // namespace vllm::support |
0 commit comments