remove conflict
[c11concurrency-benchmarks.git] / mabain / src / file_io.h
1 /**
2  * Copyright (C) 2017 Cisco Inc.
3  *
4  * This program is free software: you can redistribute it and/or  modify
5  * it under the terms of the GNU General Public License, version 2,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 // @author Changxue Deng <chadeng@cisco.com>
18
19 #ifndef __FILE_IO_H__
20 #define __FILE_IO_H__
21
22 #include <string>
23 #include <iostream>
24
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29
30 namespace mabain {
31
32 #define MMAP_ANONYMOUS_MODE 0x80000000 // This bit should not be used in fcntl.h.
33
34 // This is the basic file io class
35 class FileIO
36 {
37 public:
38     FileIO(const std::string &fpath, int oflags, int fmode, bool sync);
39     virtual ~FileIO();
40
41     int  Open();
42     int  TruncateFile(off_t filesize);
43     bool IsOpen() const;
44     void Close();
45
46     size_t Write(const void *data, size_t bytes);
47     size_t Read(void *buff, size_t bytes);
48     off_t  SetOffset(off_t offset);
49
50     virtual size_t RandomWrite(const void *data, size_t size, off_t offset);
51     virtual size_t RandomRead(void *buff, size_t size, off_t offset);
52     virtual void   Flush();
53
54     const std::string& GetFilePath() const;
55
56 protected:
57     std::string path;
58     int options;
59     bool sync_on_write;
60
61     void* MapFile(size_t size, int prot, int flags, off_t offset);
62
63 private:
64     int mode;
65
66     int fd;
67 };
68
69 }
70
71 #endif