OmniSciDB  2e3a973ef4
omnisci_fs.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "OSDependent/omnisci_fs.h"
18 
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 
22 #include <fcntl.h>
23 #include <io.h>
24 
25 #include <windows.h>
26 
27 #include <memoryapi.h>
28 
29 #include "Logger/Logger.h"
30 
31 namespace omnisci {
32 
33 size_t file_size(const int fd) {
34  struct _stat64i32 buf;
35  const auto err = _fstat64i32(fd, &buf);
36  CHECK_EQ(0, err);
37  return buf.st_size;
38 }
39 
40 void* checked_mmap(const int fd, const size_t sz) {
41  auto handle = _get_osfhandle(fd);
42  HANDLE map_handle =
43  CreateFileMapping(reinterpret_cast<HANDLE>(handle), NULL, PAGE_READWRITE, 0, 0, 0);
44  CHECK(map_handle);
45  auto map_ptr = MapViewOfFile(map_handle, FILE_MAP_WRITE | FILE_MAP_READ, 0, 0, sz);
46  CHECK(map_ptr);
47  CHECK(CloseHandle(map_handle) != 0);
48  return map_ptr;
49 }
50 
51 void checked_munmap(void* addr, size_t length) {
52  CHECK(UnmapViewOfFile(addr) != 0);
53 }
54 
55 int msync(void* addr, size_t length, bool async) {
56  auto err = FlushViewOfFile(addr, length);
57  return err != 0 ? 0 : -1;
58 }
59 
60 int fsync(int fd) {
61  // TODO: FlushFileBuffers
62  auto file = _fdopen(fd, "a+");
63  return fflush(file);
64 }
65 
66 int open(const char* path, int flags, int mode) {
67  return _open(path, flags, mode);
68 }
69 
70 void close(const int fd) {
71  _close(fd);
72 }
73 
74 int get_page_size() {
75  return 4096; // TODO: reasonable guess for now
76 }
77 
78 } // namespace omnisci
#define CHECK_EQ(x, y)
Definition: Logger.h:205
int open(const char *path, int flags, int mode)
Definition: omnisci_fs.cpp:64
void * checked_mmap(const int fd, const size_t sz)
Definition: omnisci_fs.cpp:38
int64_t const int32_t sz
void close(const int fd)
Definition: omnisci_fs.cpp:68
int fsync(int fd)
Definition: omnisci_fs.cpp:60
int msync(void *addr, size_t length, bool async)
Definition: omnisci_fs.cpp:55
void checked_munmap(void *addr, size_t length)
Definition: omnisci_fs.cpp:51
#define CHECK(condition)
Definition: Logger.h:197
int get_page_size()
Definition: omnisci_fs.cpp:27
size_t file_size(const int fd)
Definition: omnisci_fs.cpp:31