-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathmiracle_ptr.cc
205 lines (171 loc) · 4.55 KB
/
miracle_ptr.cc
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
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* This file demonstrates how the snmalloc library could be implemented to
* provide a miracle pointer like feature. This is not a hardened
* implementation and is purely for illustrative purposes.
*
* Do not use as is.
*/
#ifdef SNMALLOC_THREAD_SANITIZER_ENABLED
int main()
{
return 0;
}
#else
# include "test/setup.h"
# include <atomic>
# include <iostream>
# include <memory>
# include <snmalloc/backend/globalconfig.h>
# include <snmalloc/snmalloc_core.h>
namespace snmalloc
{
// Instantiate the allocator with a client meta data provider that uses an
// atomic size_t to store the reference count.
using Alloc = snmalloc::LocalAllocator<snmalloc::StandardConfigClientMeta<
ArrayClientMetaDataProvider<std::atomic<size_t>>>>;
}
# define SNMALLOC_PROVIDE_OWN_CONFIG
# include <snmalloc/snmalloc.h>
SNMALLOC_SLOW_PATH void error(std::string msg)
{
std::cout << msg << std::endl;
abort();
}
SNMALLOC_FAST_PATH_INLINE void check(bool b, std::string msg)
{
if (SNMALLOC_UNLIKELY(!b))
error(msg);
}
namespace snmalloc::miracle
{
// snmalloc meta-data representation
// * 2n + 1: Represents an object that has not been deallocated with n
// additional references to it
// * 2n : Represents a deallocated object that
// has n additional references to it
inline void* malloc(size_t size)
{
auto p = snmalloc::libc::malloc(size);
if (SNMALLOC_UNLIKELY(p == nullptr))
return nullptr;
snmalloc::libc::get_client_meta_data(p) = 1;
return p;
}
inline void free(void* ptr)
{
if (ptr == nullptr)
return;
// TODO could build a check into this that it is the start of the object?
auto previous =
snmalloc::libc::get_client_meta_data(ptr).fetch_add((size_t)-1);
if (SNMALLOC_LIKELY(previous == 1))
{
std::cout << "Freeing " << ptr << std::endl;
snmalloc::libc::free(ptr);
return;
}
check((previous & 1) == 1, "Double free detected");
// We have additional references to this object.
// We should not free it.
// TOOD this assumes this is not an internal pointer.
memset(ptr, 0, snmalloc::libc::malloc_usable_size(ptr));
}
inline void acquire(void* p)
{
auto previous =
snmalloc::libc::get_client_meta_data(p).fetch_add((size_t)2);
// Can we take new pointers to a deallocated object?
check((previous & 1) == 1, "Acquiring a deallocated object");
}
inline void release(void* p)
{
auto previous =
snmalloc::libc::get_client_meta_data(p).fetch_add((size_t)-2);
if (previous > 2)
return;
check(previous == 2, "Releasing an object with insufficient references");
std::cout << "Freeing from release " << p << std::endl;
snmalloc::libc::free(p);
}
/**
* This class can be used to replace a raw pointer. It will automatically use
* the underlying backup reference counting design from the miracle pointer
* docs.
*/
template<typename T>
class raw_ptr
{
T* p;
public:
raw_ptr() : p(nullptr) {}
raw_ptr(T* p) : p(p)
{
snmalloc::miracle::acquire(p);
}
T& operator*()
{
return *p;
}
~raw_ptr()
{
if (p == nullptr)
return;
snmalloc::miracle::release(p);
}
raw_ptr(const raw_ptr& rp) : p(rp.p)
{
snmalloc::miracle::acquire(p);
}
raw_ptr& operator=(const raw_ptr& other)
{
p = other.p;
snmalloc::miracle::acquire(other.p);
return *this;
}
raw_ptr(raw_ptr&& other) : p(other.p)
{
other.p = nullptr;
}
raw_ptr& operator=(raw_ptr&& other)
{
p = other.p;
other.p = nullptr;
return *this;
}
};
} // namespace snmalloc::miracle
/**
* Overload new and delete to use the "miracle pointer" implementation.
*/
void* operator new(size_t size)
{
return snmalloc::miracle::malloc(size);
}
void operator delete(void* p)
{
snmalloc::miracle::free(p);
}
void operator delete(void* p, size_t)
{
snmalloc::miracle::free(p);
}
int main()
{
# ifndef SNMALLOC_PASS_THROUGH
snmalloc::miracle::raw_ptr<int> p;
{
auto up1 = std::make_unique<int>(41);
auto up = std::make_unique<int>(42);
auto up2 = std::make_unique<int>(40);
auto up3 = std::make_unique<int>(39);
p = up.get();
check(*p == 42, "Failed to set p");
}
// Still safe to access here. The unique_ptr has been destroyed, but the
// raw_ptr has kept the memory live.
// Current implementation zeros the memory when the unique_ptr is destroyed.
check(*p == 0, "Failed to keep memory live");
# endif
return 0;
}
#endif