This repository was archived by the owner on Dec 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_allocator.h
192 lines (191 loc) · 4.86 KB
/
disk_allocator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifndef DISK_ALLOCATOR
#define DISK_ALLOCATOR
#include <cstdio>
#include <iostream>
#include <cinttypes>
#include <fstream>
using namespace std;
// allocates one t at a time
template <typename t>
struct disk_allocator
{
// https://en.cppreference.com/w/cpp/io/c/FILE
// thanks to The Cherno or whoever else taught me C++
inline static FILE *dstream = nullptr, *sstream = nullptr; // for data and memory spaces
inline static void seek(FILE *s, const size_t &l) { fseek(s, l, SEEK_SET); }
inline static void seek(const size_t &l) { fseek(dstream, l, SEEK_SET); }
// https://en.cppreference.com/w/cpp/io/basic_ostream
// https://huixie90.github.io/Almost-always-const-auto-ref
// https://en.cppreference.com/w/cpp/io/basic_fstream
// https://en.cppreference.com/w/cpp/io/c/fread
inline static size_t read(auto &x, FILE *s)
{
return fread((void *)&x, sizeof(x), 1, s);
}
// https://en.cppreference.com/w/cpp/io/c/fwrite
inline static size_t write(auto &x, FILE *s)
{
return fwrite((void *)&x, sizeof(x), 1, s);
}
struct pointer;
struct reference
{
t d;
size_t l;
inline void read()
{
seek(l);
disk_allocator<t>::read(d, dstream);
}
// https://stackoverflow.com/a/31211386
reference() { l = SIZE_MAX; }
reference(const size_t &p) : l(p) { read(); }
reference(const reference &r) : d(r.d), l(r.l) {}
void write() const
{
// cout<<"write "<<d.d<<' '<<l<<' '<<ftell(dstream);
seek(l);
disk_allocator<t>::write(d, dstream);
}
// = for another reference should go through constructor instead of this
reference &operator=(const auto &o)
{
d = o;
write();
return *this;
}
// reference& operator=(initializer_list<auto> l) { d=move(l); write(); return *this; }
operator t &() const
{
return d;
}
// let ->() call &d->()
t *operator->() { return &d; }
pointer operator&() const
{
return pointer{l};
}
// auto operator <=>(const reference& o) const { return d<=>o.d; }
//~reference()=default;
//~reference() { cout<<"destroy "<<d.d<<endl<<l<<endl; write(); }
};
// https://stackoverflow.com/a/16998837
struct pointer
{
// https://en.cppreference.com/w/cpp/memory/pointer_traits
// https://en.cppreference.com/w/cpp/language/typedef
size_t l;
pointer() : l(SIZE_MAX) {}
pointer(const size_t &location) : l(location) {}
pointer(nullptr_t) : l(SIZE_MAX) {}
reference operator*() const
{
reference d{l};
return d;
}
reference operator[](size_t i) const
{
auto p = l + i;
reference d{p};
return d;
}
bool operator==(const pointer &p) const { return l == p.l; }
// SIZE_MAX for nullptr equivalent
bool operator!() const { return l == SIZE_MAX; }
operator bool() const { return l != SIZE_MAX; }
bool operator==(nullptr_t) const { return operator!(); }
bool operator==(const t *p) const
{
if (p)
return false; // for pointer to RAM location and not disk
return operator!(); // they can be equal if they are nullptrs
}
auto operator<=>(const pointer &p) const { return l <=> p.l; }
reference operator->() const
{
reference d{l};
return d;
}
ptrdiff_t operator-(const pointer &p) { return l - p.l; }
auto operator++()
{
++l;
return *this;
}
auto operator--()
{
--l;
return *this;
}
auto operator+=(integral auto i)
{
l += i;
return *this;
}
};
// https://en.cppreference.com/w/cpp/io/basic_fstream/basic_fstream
static void init(FILE *ds, FILE *ss)
{
dstream = ds;
sstream = ss;
}
static void init()
{
dstream = tmpfile();
sstream = tmpfile();
// let it start at 0
push_space(0);
}
// reads spacestream and moves it backwards
static size_t take_space()
{
// https://en.cppreference.com/w/cpp/io/basic_istream/seekg
constexpr auto s = sizeof(size_t);
fseek(sstream, -s, SEEK_CUR);
size_t l;
read(l, sstream);
fseek(sstream, -s, SEEK_CUR); // assuming seek back at 0 would still be at 0
return l;
}
static void push_space(const size_t &l)
{
// cout<<"push space "<<spacestream.tellp()<<' '<<l<<' ';
write(l, sstream);
}
// thanks to en.cppreference.com
// does not default construct the memory
pointer allocate()
{
size_t l = take_space();
// cout<<"allocate "<<l<<endl;
if (!ftell(sstream))
push_space(l + sizeof(t));
return pointer{l};
}
void deallocate(pointer &&p)
{
// cout<<"deallocate "<<p.l<<endl;
push_space(move(p.l));
}
// the allocator can stop working correctly after deallocating a deallocated pointer
void deallocate(pointer &p)
{
deallocate(move(p));
p.l = SIZE_MAX;
}
void deallocate(reference &r)
{
push_space(move(r.l));
r.l = SIZE_MAX;
}
// https://en.cppreference.com/w/cpp/named_req/Allocator
constexpr disk_allocator() noexcept {}
// template<class w> constexpr disk_allocator(const disk_allocator<w>& da) { fs=move(da.fs); }
};
/*
template<typename t> inline void update(const disk_allocator<t>::pointer::reference& r)
{
r.write();
}
*/
#endif /* DISK_ALLOCATOR */