Debian Codegraph @

mmappable_vector.h

raw symbols SCIP
1 #ifndef _MMAPPABLE_VECTOR_H
2 #define _MMAPPABLE_VECTOR_H
3
4 #include <memory>
5 #include <string>
6 #include <stdio.h>
7 #include <vector>
8 #include "mmap_allocator.h"
9
10 namespace mmap_allocator_namespace {
11 template <typename T, typename A = mmap_allocator<T> >
12 class mmappable_vector: public std::vector<T, A> {
13 public:
14 typedef std::vector<T, A> Base;
15
16 typedef typename Base::const_iterator const_iterator;
17 typedef typename Base::iterator iterator;
18 typedef T value_type;
19 typedef A allocator_type;
20
21 mmappable_vector():
22 Base()
23 {
24 }
25
26 mmappable_vector(const mmappable_vector<T, A> &other):
27 Base(other)
28 {
29 }
30
31 explicit mmappable_vector(size_t n):
32 Base()
33 {
34 mmap_file(n);
35 }
36
37 explicit mmappable_vector(A alloc):
38 Base(alloc)
39 {
40 }
41
42 mmappable_vector(iterator from, iterator to):
43 Base(from, to)
44 {
45 }
46
47 template <typename Iter>
48 mmappable_vector(Iter first, Iter last, A a = A()):
49 Base(first, last, a)
50 {
51 }
52
53 mmappable_vector(int n, T val, A alloc):
54 Base(n, val, alloc)
55 {
56 }
57
58 mmappable_vector(int n, T val):
59 Base(n, val)
60 {
61 }
62
63 mmappable_vector(std::vector<T,std::allocator<T> > v):
64 std::vector<T,std::allocator<T> >(v)
65 {
66 }
67
68 /* Use this only when the allocator is already initialized. */
69 void mmap_file(size_t n)
70 {
71 Base::reserve(n);
72 _M_set_finish(n);
73 }
74
75 void mmap_file(std::string filename, enum access_mode access_mode, const off_t offset, const size_t n, int flags = 0)
76 {
77 if (Base::size() > 0) {
78 throw mmap_allocator_exception("Remapping currently not implemented.");
79 }
80 #ifdef __GNUC__
81 #if __GNUC__ == 3
82 A *the_allocator = static_cast<A*>(&(this->Base::_M_impl));
83 #else
84 A *the_allocator = &Base::_M_get_Tp_allocator();
85 #endif
86 #else
87 #error "Not GNU C++, please either implement me or use GCC"
88 #endif
89 the_allocator->filename = filename;
90 the_allocator->offset = offset;
91 the_allocator->access_mode = access_mode;
92 the_allocator->map_whole_file = (flags & MAP_WHOLE_FILE) != 0;
93 the_allocator->allow_remap = (flags & ALLOW_REMAP) != 0;
94 the_allocator->bypass_file_pool = (flags & BYPASS_FILE_POOL) != 0;
95 the_allocator->keep_forever = (flags & KEEP_FOREVER) != 0;
96
97 mmap_file(n);
98 }
99
100 void munmap_file(void)
101 {
102 size_t n = Base::size();
103 #ifdef __GNUC__
104 Base::_M_deallocate(Base::_M_impl._M_start, n);
105 Base::_M_impl._M_start = 0;
106 Base::_M_impl._M_finish = 0;
107 Base::_M_impl._M_end_of_storage = 0;
108 #else
109 #error "Not GNU C++, please either implement me or use GCC"
110 #endif
111 }
112
113 private:
114 void _M_set_finish(size_t n)
115 {
116 #ifdef __GNUC__
117 Base::_M_impl._M_finish = Base::_M_impl._M_start + n;
118 #else
119 #error "Not GNU C++, please either implement me or use GCC"
120 #endif
121 }
122 };
123
124 template <typename T>
125 std::vector<T> to_std_vector(const mmappable_vector<T> &v)
126 {
127 return std::vector<T>(v.begin(), v.end());
128 }
129
130 template <typename T>
131 mmappable_vector<T> to_mmappable_vector(const std::vector<T> &v)
132 {
133 return mmappable_vector<T>(v.begin(), v.end());
134 }
135 }
136
137 #endif /* MMAPPABLE_VECTOR_H */