-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoMalloc.h
More file actions
232 lines (197 loc) · 5.1 KB
/
AutoMalloc.h
File metadata and controls
232 lines (197 loc) · 5.1 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#pragma once
#include "CustomException.h"
namespace tbx
{
//////////////////////////////////////////////////////////////////////
// AutoMalloc
// This class acts similarly to a std::unique_ptr<t>, but is instead
// used for allocation/deallocation of arbitrary arrays of memory,
// not a single object.
// As the name implies, it uses malloc/free to manage the allocated memory
// This class uses C-array semantics, and can be used as a replacement for
// most such consumers (not transparently, however).
//////////////////////////////////////////////////////////////////////
template <typename T = unsigned char>
class AutoMalloc
{
public:
// NOTE:
// For auto pointer types:
// One can either have a ctor that takes the raw pointer type
// OR automatic conversion operators
// BUT NEVER BOTH!!!
//
// If you try to have them both, then C++ uses the conversion operators to feed the raw pointer ctor,
// effectively bypassing your copy ctor & leading to a instance of your smart pointer referring to a dead
// memory location instead of a valid entity.
//
// For our purposes, we disallow construction from a raw pointer, but allow automatic conversion
// to our underlying pointer type.
//
// explicit AutoMalloc(T * p) :
// m_ptr(p)
// {
// }
using Elem = T;
// allocate a block of given size (in bytes)
explicit AutoMalloc<T>(size_t count = 0) :
m_cbSize(count * sizeof(T)),
m_ptr(count ? (T*)std::malloc(m_cbSize) : nullptr)
{
if (!m_ptr && count)
throw std::bad_alloc();
}
// allocate a block of given size, and copy the specified pointer's contents into ours
AutoMalloc<T>(const T * pMemory, size_t count) :
m_cbSize(count * sizeof(T)),
m_ptr(count ? (T*)std::malloc(m_cbSize) : nullptr)
{
if (m_ptr)
std::memcpy(m_ptr, pMemory, m_cbSize);
}
~AutoMalloc<T>()
{
if (m_ptr)
free();
}
// copy semantics (non-copyable)
AutoMalloc<T>(const AutoMalloc<T> &) = delete;
AutoMalloc<T> & operator = (const AutoMalloc<T> &) = delete;
// move semantics
AutoMalloc<T>(AutoMalloc<T> && rhs) :
m_cbSize(rhs.size_in_bytes()),
m_ptr(rhs.release())
{
}
AutoMalloc<T> & operator = (AutoMalloc<T> && rhs) { return take(std::move(rhs)); }
AutoMalloc<T> & take(AutoMalloc<T> && that)
{
if (this != &that)
{
if (m_ptr)
free();
m_cbSize = that.m_cbSize;
m_ptr = that.release();
}
return *this;
}
// take another buffer as our own (this is a casting operation!)
template<typename U>
AutoMalloc<T> & take_cast(AutoMalloc<U> && that)
{
if (that.size_in_bytes() % sizeof(T) != 0)
throw CContextException(__FUNCTION__, "invalid conversion!");
return take((AutoMalloc<T>&&)that);
}
size_t size() const
{
return m_cbSize / sizeof(T);
}
size_t size_in_bytes() const
{
return m_cbSize;
}
// explicit buffer access
T * get()
{
return m_ptr;
}
const T * get() const
{
return m_ptr;
}
// implicit buffer access (auto conversions)
operator T * ()
{
return m_ptr;
}
operator const T * () const
{
return m_ptr;
}
// release our buffer to caller (we relinquish ownership & responsibility for it)
T * release()
{
T * p = m_ptr;
m_cbSize = 0;
m_ptr = nullptr;
return p;
}
// deallocates us
void free()
{
if (m_ptr)
{
std::free(m_ptr);
m_ptr = nullptr;
m_cbSize = 0;
}
TBX_POSTCONDITION(!m_cbSize);
TBX_POSTCONDITION(!m_ptr);
}
// synonym for free to make us more interchangeable with std smart pointers
void reset() { free(); }
// erases our contents
void erase(int zero_value = 0)
{
std::memset(m_ptr, zero_value, m_cbSize);
}
// reallocate us
// zero count is equivalent to calling free()
// failure to allocate new size is also equivalent to free()
AutoMalloc & realloc(size_t count)
{
if (count)
{
m_cbSize = count * sizeof(T);
auto * new_ptr = (T*)::realloc(m_ptr, m_cbSize);
if (new_ptr)
m_ptr = new_ptr;
else
free();
}
else
free();
return *this;
}
// returns a new copy of ourselves
AutoMalloc<T> copy() const
{
return AutoMalloc<T>(m_ptr, m_cbSize);
}
// copies the given memory into ourself (and resizes us accordingly)
template <typename X>
AutoMalloc<T> & copy(const X * ptr, size_t count_of_x)
{
realloc(MulDiv(count_of_x, sizeof(X), sizeof(T)));
std::memmove(m_ptr, ptr, m_cbSize);
return *this;
}
// swap our contents
void swap(AutoMalloc<T> & rhs)
{
// if swap<size_t> and swap<T*> cannot throw, then neither can we
std::swap(m_cbSize, rhs.m_cbSize);
std::swap(m_ptr, rhs.m_ptr);
}
private:
size_t m_cbSize; // size of allocated block (in bytes)
T * m_ptr; // pointer to allocated memory
};
template <typename T>
inline void free(AutoMalloc<T> & p)
{
p.free();
}
template <typename T>
inline void swap(AutoMalloc<T> & lhs, AutoMalloc<T> & rhs)
{
lhs.swap(rhs);
}
// ensure that Zero(AutoMalloc<T>&) resolves properly
template <typename T>
inline void Zero(AutoMalloc<T> & p, int zero_value = 0)
{
p.erase(zero_value);
}
} // namespace tbx