SDSL 3.0.3
Succinct Data Structure Library
Loading...
Searching...
No Matches
ram_fs.hpp
Go to the documentation of this file.
1// Copyright (c) 2016, the SDSL Project Authors. All rights reserved.
2// Please see the AUTHORS file for details. Use of this source code is governed
3// by a BSD license that can be found in the LICENSE file.
8#ifndef INCLUDED_SDSL_RAM_FS
9#define INCLUDED_SDSL_RAM_FS
10
11#include <cstdio>
12#include <iterator>
13#include <map>
14#include <mutex>
15#include <string>
16#include <type_traits>
17#include <utility>
18#include <vector>
19
21
22namespace sdsl
23{
24
25namespace ram_fs
26{
27
29inline bool exists(std::string const & name)
30{
31 auto & rf = memory_monitor::ram_fs();
32 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
33 return rf.m_map.find(name) != rf.m_map.end();
34}
35
36inline void store(std::string const & name, content_type data)
37{
38 auto & rf = memory_monitor::ram_fs();
39 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
40 if (!exists(name))
41 {
42 std::string cname = name;
43 rf.m_map.insert(std::make_pair(std::move(cname), std::move(data)));
44 }
45 else
46 {
47 rf.m_map[name] = std::move(data);
48 }
49}
50
52inline size_t file_size(std::string const & name)
53{
54 auto & rf = memory_monitor::ram_fs();
55 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
56 if (exists(name))
57 {
58 return rf.m_map[name].size();
59 }
60 else
61 {
62 return 0;
63 }
64}
65
67inline content_type & content(std::string const & name)
68{
69 auto & rf = memory_monitor::ram_fs();
70 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
71 return rf.m_map[name];
72}
73
75inline int remove(std::string const & name)
76{
77 auto & rf = memory_monitor::ram_fs();
78 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
79 if (exists(name))
80 {
81 rf.m_map.erase(name);
82 }
83 return 0;
84}
85
87inline int rename(const std::string old_filename, const std::string new_filename)
88{
89 auto & rf = memory_monitor::ram_fs();
90 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
91 rf.m_map[new_filename] = std::move(rf.m_map[old_filename]);
92 remove(old_filename);
93 return 0;
94}
95
97inline int open(std::string const & name)
98{
99 auto & rf = memory_monitor::ram_fs();
100 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
101 if (!exists(name))
102 {
103 store(name, content_type{});
104 }
105 int fd = -2;
106 auto largest_fd = rf.m_fd_map.rbegin()->first;
107 if (largest_fd < 0)
108 {
109 auto smallest_fd = rf.m_fd_map.begin()->first;
110 fd = smallest_fd - 1;
111 }
112 else
113 {
114 rf.m_fd_map.erase(largest_fd);
115 fd = -largest_fd;
116 }
117 rf.m_fd_map[fd] = name;
118 return fd;
119}
120
122inline int close(int const fd)
123{
124 auto & rf = memory_monitor::ram_fs();
125 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
126 if (fd >= -1)
127 return -1;
128 if (rf.m_fd_map.count(fd) == 0)
129 {
130 return -1;
131 }
132 else
133 {
134 rf.m_fd_map.erase(fd);
135 rf.m_fd_map[-fd] = "";
136 }
137 return 0;
138}
139
141inline content_type & content(int const fd)
142{
143 auto & rf = memory_monitor::ram_fs();
144 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
145 auto name = rf.m_fd_map[fd];
146 return rf.m_map[name];
147}
148
150inline int truncate(int const fd, size_t new_size)
151{
152 auto & rf = memory_monitor::ram_fs();
153 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
154 if (rf.m_fd_map.count(fd) == 0)
155 return -1;
156 auto name = rf.m_fd_map[fd];
157 rf.m_map[name].reserve(new_size);
158 rf.m_map[name].resize(new_size, 0);
159 return 0;
160}
161
163inline size_t file_size(int const fd)
164{
165 auto & rf = memory_monitor::ram_fs();
166 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
167 if (rf.m_fd_map.count(fd) == 0)
168 return 0;
169 auto name = rf.m_fd_map[fd];
170 return rf.m_map[name].size();
171}
172
173} // end namespace ram_fs
174
176inline bool is_ram_file(std::string const & file)
177{
178 if (file.size() > 0)
179 {
180 if (file[0] == '@')
181 {
182 return true;
183 }
184 }
185 return false;
186}
187
189inline bool is_ram_file(int const fd)
190{
191 return fd < -1;
192}
193
195inline std::string ram_file_name(std::string const & file)
196{
197 if (is_ram_file(file))
198 {
199 return file;
200 }
201 else
202 {
203 return "@" + file;
204 }
205}
206
208inline std::string disk_file_name(std::string const & file)
209{
210 if (!is_ram_file(file))
211 {
212 return file;
213 }
214 else
215 {
216 return file.substr(1);
217 }
218}
219
221inline int remove(std::string const & file)
222{
223 if (is_ram_file(file))
224 {
225 return ram_fs::remove(file);
226 }
227 else
228 {
229 return std::remove(file.c_str());
230 }
231}
232
234inline int rename(std::string const & old_filename, std::string const & new_filename)
235{
236 if (is_ram_file(old_filename))
237 {
238 if (!is_ram_file(new_filename))
239 { // error, if new file is not also RAM-file
240 return -1;
241 }
242 return ram_fs::rename(old_filename, new_filename);
243 }
244 else
245 {
246 return std::rename(old_filename.c_str(), new_filename.c_str());
247 }
248}
249
250} // end namespace sdsl
251#endif
static ramfs_storage & ram_fs()
memory_tracking.hpp contains two function for allocating and deallocating memory
size_t file_size(std::string const &name)
Get the file size.
Definition ram_fs.hpp:52
void store(std::string const &name, content_type data)
Definition ram_fs.hpp:36
content_type & content(std::string const &name)
Get the content.
Definition ram_fs.hpp:67
int close(int const fd)
Get fd for file.
Definition ram_fs.hpp:122
int truncate(int const fd, size_t new_size)
Get the content with fd.
Definition ram_fs.hpp:150
bool exists(std::string const &name)
Check if the file exists.
Definition ram_fs.hpp:29
int open(std::string const &name)
Get fd for file.
Definition ram_fs.hpp:97
int remove(std::string const &name)
Remove the file with key name
Definition ram_fs.hpp:75
int rename(const std::string old_filename, const std::string new_filename)
Rename the file. Change key old_filename into new_filename.
Definition ram_fs.hpp:87
std::vector< char, track_allocator< char > > content_type
Namespace for the succinct data structure library.
std::string disk_file_name(std::string const &file)
Returns for a RAM-file the corresponding disk file name.
Definition ram_fs.hpp:208
int remove(std::string const &)
Remove a file.
Definition ram_fs.hpp:221
std::string ram_file_name(std::string const &file)
Returns the corresponding RAM-file name for file.
Definition ram_fs.hpp:195
int rename(std::string const &old_filename, std::string const &new_filename)
Rename a file.
Definition ram_fs.hpp:234
bool is_ram_file(std::string const &file)
Determines if the given file is a RAM-file.
Definition ram_fs.hpp:176