forked from CU-NVM/NUMATyping
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersistenttype.hpp
More file actions
149 lines (117 loc) · 4.13 KB
/
persistenttype.hpp
File metadata and controls
149 lines (117 loc) · 4.13 KB
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
#pragma once
#ifndef PERSISTENTTYPE_HPP
#define PERSISTENTTYPE_HPP
#include <utility>
#include <type_traits>
#include <memory>
#include <stdexcept>
#include <iostream>
#include <cassert>
#include "pmem_allocator.hpp"
template<typename T>
class PersistentAllocator {
public:
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
/* rebind + converting constructor */
template <typename U>
struct rebind {
using other = PersistentAllocator<U>;
};
template<typename U>
PersistentAllocator(const PersistentAllocator<U>&) noexcept {}
/* default constructor + copy constructor */
PersistentAllocator() noexcept {} //error handling. returns true if no exception handling is done
PersistentAllocator(const PersistentAllocator&) noexcept {}
/* memory allocation and deallocation */
pointer allocate(size_type n) {
return static_cast<pointer>(pmem_alloc(n * sizeof(T), alignof(T)));
}
void deallocate(pointer p, size_type n) noexcept {
pmem_free(p);
}
/* construct */
template<typename U, typename... Args>
void construct(U* p, Args&&... args) {
::new (static_cast<void*>(p)) U(std::forward<Args>(args)...);
}
};
template <typename T1, typename T2>
bool operator==(const PersistentAllocator<T1>&, const PersistentAllocator<T2>&) noexcept {
return true; // All instances of PersistentAllocator are considered equal
}
template <typename T1, typename T2>
bool operator!=(const PersistentAllocator<T1>&, const PersistentAllocator<T2>&) noexcept {
return false; // All instances of PersistentAllocator are considered equal
}
template<typename T, template<typename> class Alloc = PersistentAllocator, typename E = void>
class persistent;
template<typename T, template <typename> class Alloc>
class persistent<T, Alloc, typename std::enable_if<std::is_fundamental<T>::value || std::is_pointer<T>::value>::type>{
public:
T contents;
using allocator_type = Alloc<T>;
inline T load() const
__attribute__((always_inline)){
return contents;
}
inline void store(T data)
__attribute__((always_inline)){
int succ = pmemobj_tx_add_range_direct(&contents, sizeof(contents));
if(succ != 0) {
throw std::runtime_error("Failed to add range to transaction");
}
contents = data;
}
persistent(){store((T)0);};
persistent(const T& data){store(data);};
inline operator T&() {
return contents;
}
inline operator const T&() const {
return contents;
}
inline T operator->() {
static_assert(std::is_pointer<T>::value, "operator-> is only valid for pointer types");
return load();
}
inline T operator->() const {
static_assert(std::is_pointer<T>::value, "operator-> is only valid for pointer types");
return load();
}
static void* operator new(std::size_t sz){
allocator_type alloc;
return alloc.allocate(sz);
}
static void* operator new[](std::size_t sz){
allocator_type alloc;
return alloc.allocate(sz);
}
static void operator delete(void *ptr) {
allocator_type alloc;
alloc.deallocate(static_cast<typename allocator_type::pointer>(ptr), 1);
}
static void operator delete[](void *ptr) {
allocator_type alloc;
alloc.deallocate(static_cast<typename allocator_type::pointer>(ptr), 1);
}
persistent& operator=(const T& data){
store(data);
return *this;
}
};
template<typename T, template <typename> class Alloc>
class persistent<T, Alloc, typename std::enable_if<!std::is_fundamental<T>::value && !std::is_pointer<T>::value>::type>: public T{
public:
persistent() {}
template<typename... Args>
explicit persistent(Args&&... args) : T(std::forward<Args>(args)...) {}
/* These new and deleteoverloadings will only exist before clang tool transformation, pmem_alloc will be called from
within the specialized template */
};
#endif