SDSL 3.0.1
Succinct Data Structure Library
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 <map>
12#include <mutex>
13#include <string>
14#include <vector>
15
17#include <sdsl/uintx_t.hpp>
18
19namespace sdsl
20{
21
22namespace ram_fs
23{
24
26inline bool exists(const std::string & name)
27{
28 auto & rf = memory_monitor::ram_fs();
29 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
30 return rf.m_map.find(name) != rf.m_map.end();
31}
32
33inline void store(const std::string & name, content_type data)
34{
35 auto & rf = memory_monitor::ram_fs();
36 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
37 if (!exists(name))
38 {
39 std::string cname = name;
40 rf.m_map.insert(std::make_pair(std::move(cname), std::move(data)));
41 }
42 else
43 {
44 rf.m_map[name] = std::move(data);
45 }
46}
47
49inline size_t file_size(const std::string & name)
50{
51 auto & rf = memory_monitor::ram_fs();
52 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
53 if (exists(name)) { return rf.m_map[name].size(); }
54 else
55 {
56 return 0;
57 }
58}
59
61inline content_type & content(const std::string & name)
62{
63 auto & rf = memory_monitor::ram_fs();
64 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
65 return rf.m_map[name];
66}
67
69inline int remove(const std::string & name)
70{
71 auto & rf = memory_monitor::ram_fs();
72 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
73 if (exists(name)) { rf.m_map.erase(name); }
74 return 0;
75}
76
78inline int rename(const std::string old_filename, const std::string new_filename)
79{
80 auto & rf = memory_monitor::ram_fs();
81 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
82 rf.m_map[new_filename] = std::move(rf.m_map[old_filename]);
83 remove(old_filename);
84 return 0;
85}
86
88inline int open(const std::string & name)
89{
90 auto & rf = memory_monitor::ram_fs();
91 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
92 if (!exists(name)) { store(name, content_type{}); }
93 int fd = -2;
94 auto largest_fd = rf.m_fd_map.rbegin()->first;
95 if (largest_fd < 0)
96 {
97 auto smallest_fd = rf.m_fd_map.begin()->first;
98 fd = smallest_fd - 1;
99 }
100 else
101 {
102 rf.m_fd_map.erase(largest_fd);
103 fd = -largest_fd;
104 }
105 rf.m_fd_map[fd] = name;
106 return fd;
107}
108
110inline int close(const int fd)
111{
112 auto & rf = memory_monitor::ram_fs();
113 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
114 if (fd >= -1) return -1;
115 if (rf.m_fd_map.count(fd) == 0) { return -1; }
116 else
117 {
118 rf.m_fd_map.erase(fd);
119 rf.m_fd_map[-fd] = "";
120 }
121 return 0;
122}
123
125inline content_type & content(const int fd)
126{
127 auto & rf = memory_monitor::ram_fs();
128 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
129 auto name = rf.m_fd_map[fd];
130 return rf.m_map[name];
131}
132
134inline int truncate(const int fd, size_t new_size)
135{
136 auto & rf = memory_monitor::ram_fs();
137 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
138 if (rf.m_fd_map.count(fd) == 0) return -1;
139 auto name = rf.m_fd_map[fd];
140 rf.m_map[name].reserve(new_size);
141 rf.m_map[name].resize(new_size, 0);
142 return 0;
143}
144
146inline size_t file_size(const int fd)
147{
148 auto & rf = memory_monitor::ram_fs();
149 std::lock_guard<std::recursive_mutex> lock(rf.m_rlock);
150 if (rf.m_fd_map.count(fd) == 0) return 0;
151 auto name = rf.m_fd_map[fd];
152 return rf.m_map[name].size();
153}
154
155} // end namespace ram_fs
156
158inline bool is_ram_file(const std::string & file)
159{
160 if (file.size() > 0)
161 {
162 if (file[0] == '@') { return true; }
163 }
164 return false;
165}
166
168inline bool is_ram_file(const int fd)
169{
170 return fd < -1;
171}
172
174inline std::string ram_file_name(const std::string & file)
175{
176 if (is_ram_file(file)) { return file; }
177 else
178 {
179 return "@" + file;
180 }
181}
182
184inline std::string disk_file_name(const std::string & file)
185{
186 if (!is_ram_file(file)) { return file; }
187 else
188 {
189 return file.substr(1);
190 }
191}
192
194inline int remove(const std::string & file)
195{
196 if (is_ram_file(file)) { return ram_fs::remove(file); }
197 else
198 {
199 return std::remove(file.c_str());
200 }
201}
202
204inline int rename(const std::string & old_filename, const std::string & new_filename)
205{
206 if (is_ram_file(old_filename))
207 {
208 if (!is_ram_file(new_filename))
209 { // error, if new file is not also RAM-file
210 return -1;
211 }
212 return ram_fs::rename(old_filename, new_filename);
213 }
214 else
215 {
216 return std::rename(old_filename.c_str(), new_filename.c_str());
217 }
218}
219
220} // end namespace sdsl
221#endif
static ramfs_storage & ram_fs()
memory_tracking.hpp contains two function for allocating and deallocating memory
int open(const std::string &name)
Get fd for file.
Definition: ram_fs.hpp:88
bool exists(const std::string &name)
Check if the file exists.
Definition: ram_fs.hpp:26
void store(const std::string &name, content_type data)
Definition: ram_fs.hpp:33
int close(const int fd)
Get fd for file.
Definition: ram_fs.hpp:110
int remove(const std::string &name)
Remove the file with key name
Definition: ram_fs.hpp:69
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:78
size_t file_size(const std::string &name)
Get the file size.
Definition: ram_fs.hpp:49
std::vector< char, track_allocator< char > > content_type
content_type & content(const std::string &name)
Get the content.
Definition: ram_fs.hpp:61
int truncate(const int fd, size_t new_size)
Get the content with fd.
Definition: ram_fs.hpp:134
Namespace for the succinct data structure library.
bool is_ram_file(const std::string &file)
Determines if the given file is a RAM-file.
Definition: ram_fs.hpp:158
std::string ram_file_name(const std::string &file)
Returns the corresponding RAM-file name for file.
Definition: ram_fs.hpp:174
int rename(const std::string &old_filename, const std::string &new_filename)
Rename a file.
Definition: ram_fs.hpp:204
std::string disk_file_name(const std::string &file)
Returns for a RAM-file the corresponding disk file name.
Definition: ram_fs.hpp:184
int remove(const std::string &)
Remove a file.
Definition: ram_fs.hpp:194