-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.hpp
More file actions
166 lines (127 loc) · 4.85 KB
/
memory.hpp
File metadata and controls
166 lines (127 loc) · 4.85 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// ----------------------------------------------------------------------------
// Header file for the Memory class. memory.hpp
// Created by Ferhat Erata <ferhat.erata@yale.edu> on December 09, 2019.
// Copyright (c) 2019 Yale University. All rights reserved.
// -----------------------------------------------------------------------------
#ifndef MICROSAT_MEMORY_HPP
#define MICROSAT_MEMORY_HPP
// Use -DM_DEBUG on the g++ command line to build the debug version
#ifdef M_DEBUG
// Macro to print a variable
#define PRINT(a) cout << " [" << a << " ]" << endl;
#else
// alternative way to define them:
#define M_DEBUG 0
#define PRINT(a)
#endif
#include "tools.hpp"
#include <iostream>
#include <vector>
namespace microsat {
template <typename T> class FixedAllocator;
// -----------------------------------------------------------------------------
//
template <typename T> class Memory {
private:
const std::size_t max;
T* memory;
std::size_t used = 0;
public:
using value_type = T;
explicit Memory(int mem_max) noexcept : max(mem_max), memory(new int[max]) {
PRINT("Memory::mem_max " << mem_max * sizeof(T) << " bytes");
}
~Memory() {
PRINT("Memory::~Memory()");
delete[] memory;
}
[[nodiscard]] int mem_used() const { return used; } // make it const later
[[nodiscard]] int mem_max() const { return max; }
// -------------------------------------------------------------------------
// allocates memory for pointers
value_type* allocate(std::size_t mem_size) {
PRINT("Allocator::allocate " << mem_size * sizeof(T) << " bytes");
// In case the code is used within a code base
if (used + mem_size > max) {
throw std::runtime_error("Memory::allocate out of memory");
}
// Compute a pointer to the new memory location
value_type* store = memory + used;
used += mem_size; // Update the size of the used
PRINT("Allocator::used " << used);
return store;
}
// -------------------------------------------------------------------------
// makes a vector from fixed allocated array by copying its members
static auto make_vector(value_type* tp, std::size_t size) {
std::vector<value_type, FixedAllocator<value_type>> vector{
FixedAllocator<value_type>{tp, size}};
vector.reserve(size);
std::copy(tp, tp + size, std::back_inserter(vector));
return vector;
}
// -------------------------------------------------------------------------
// creates an empty vector from fixed allocated array
static auto create_vector(value_type* tp, std::size_t size) {
std::vector<value_type, FixedAllocator<value_type>> vector{
FixedAllocator<value_type>{tp, size}};
vector.reserve(size);
return vector;
}
void resize(std::size_t size) { used = size; };
value_type* get_raw_memory() { return memory; }
};
// -----------------------------------------------------------------------------
// Normal Allocator
template <typename T> class Allocator {
private:
Memory<T>& memory;
public:
using value_type = T;
explicit Allocator(Memory<T>& memory) : memory(memory){};
~Allocator() { PRINT("Allocator::~Allocator()"); }
value_type* allocate(std::size_t mem_size) {
return memory.allocate(mem_size);
}
void deallocate(value_type* p, std::size_t n) noexcept {
PRINT("Allocator::deallocate " << n * sizeof *p << " bytes");
}
[[nodiscard]] std::size_t max_size() const noexcept {
return memory.mem_max();
}
};
template <typename T, typename U>
bool operator==(const Allocator<T>&, const Allocator<U>&) {
return true;
}
template <typename T, typename U>
bool operator!=(const Allocator<T>& x, const Allocator<U>& y) {
return !(x == y);
}
// -----------------------------------------------------------------------------
// Fixed Allocator
template <typename T> class FixedAllocator {
private:
T* memory; // holding a share pointer
std::size_t max = 0;
public:
using value_type = T;
explicit FixedAllocator(T* memory, std::size_t max_size)
: memory(memory), max(max_size){};
~FixedAllocator() { PRINT("FixedAllocator::~FixedAllocator()"); }
value_type* allocate(std::size_t mem_size) { return memory; }
void deallocate(value_type* p, std::size_t n) noexcept {
PRINT("FixedAllocator::deallocate " << n * sizeof *p << " bytes");
}
[[nodiscard]] std::size_t max_size() const noexcept { return max; }
};
template <typename T, typename U>
bool operator==(const FixedAllocator<T>&, const FixedAllocator<U>&) {
return true;
}
template <typename T, typename U>
bool operator!=(const FixedAllocator<T>& x, const FixedAllocator<U>& y) {
return !(x == y);
}
} // namespace microsat
#endif // MICROSAT_MEMORY_HPP