Removing the --socket-mem eal parameter
[samplevnf.git] / VNFs / DPPD-PROX / tools / flow_extract / mappedfile.cpp
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
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 <cstdlib>
18 #include <cstdio>
19 #include <unistd.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <iostream>
23 #include <cerrno>
24 #include <sys/mman.h>
25 #include <cstring>
26
27 #include "mappedfile.hpp"
28
29 static void zeroOutFile(int fd, size_t size)
30 {
31         void *empty = calloc(1, 4096);
32
33         while (size > 4096) {
34                 write(fd, empty, 4096);
35                 size -= 4096;
36         }
37         write(fd, empty, size);
38         free(empty);
39 }
40
41 int MappedFile::open(const string& filePath, size_t size)
42 {
43         mappedFileSize = size;
44
45         fd = ::open(filePath.c_str(), O_RDWR | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
46         if (fd < 0) {
47                 cerr << "Failed to open file " << filePath << ":" << strerror(errno) << endl;
48                 return -1;
49         }
50
51         zeroOutFile(fd, size);
52         data = mmap(NULL, mappedFileSize, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
53
54         if (data == MAP_FAILED) {
55                 cerr << "Failed to map file: " << strerror(errno) << endl;
56                 return -1;
57         }
58         return 0;
59 }
60
61 static size_t getFileSize(const string& filePath)
62 {
63         struct stat s;
64         if (stat(filePath.c_str(), &s))
65                 return -1;
66
67         return s.st_size;
68 }
69
70 int MappedFile::open(const string& filePath)
71 {
72         mappedFileSize = getFileSize(filePath);
73
74         fd = ::open(filePath.c_str(), O_RDONLY);
75         if (fd < 0) {
76                 cerr << "Failed to open file " << filePath << ":" << strerror(errno) << endl;
77                 return -1;
78         }
79
80         data = mmap(NULL, mappedFileSize, PROT_READ, MAP_SHARED, fd, 0);
81
82         if (data == MAP_FAILED) {
83                 cerr << "Failed to map file: " << strerror(errno) << endl;
84                 return -1;
85         }
86         return 0;
87 }
88
89 int MappedFile::sync()
90 {
91         if (msync(data, mappedFileSize, MS_SYNC) == -1) {
92                 cerr << "Failed to sync: " << strerror(errno) << endl;
93                 return -1;
94         }
95         return 0;
96 }
97
98
99 void MappedFile::close()
100 {
101         sync();
102         munmap(data, mappedFileSize);
103         ::close(fd);
104 }
105
106 size_t MappedFile::size() const
107 {
108         return mappedFileSize;
109 }