UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
Malloc.hxx
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*\
2 | |
3 | Copyright (C) 2020 |
4 | |
5 | , __ , __ |
6 | /|/ \ /|/ \ |
7 | | __/ _ ,_ | __/ _ ,_ |
8 | | \|/ / | | | | \|/ / | | | |
9 | |(__/|__/ |_/ \_/|/|(__/|__/ |_/ \_/|/ |
10 | /| /| |
11 | \| \| |
12 | |
13 | Enrico Bertolazzi |
14 | Dipartimento di Ingegneria Industriale |
15 | Università degli Studi di Trento |
16 | email: enrico.bertolazzi@unitn.it |
17 | |
18\*--------------------------------------------------------------------------*/
19
20//
21// file: Malloc.hxx
22//
23
24/*\
25:|: ____ _ _ __
26:|: / ___| _ _ (_)_ __ | |_ ___ _ __ / _| __ _ ___ ___
27:|: | | _| |_ _| |_| | '_ \| __/ _ \ '__| |_ / _` |/ __/ _ \
28:|: | |__|_ _|_ _| | | | | || __/ | | _| (_| | (_| __/
29:|: \____||_| |_| |_|_| |_|\__\___|_| |_| \__,_|\___\___|
30\*/
31
32namespace Utils {
33
38
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40 using std::int64_t;
41 using std::string;
42 using std::mutex;
43 using std::size_t;
44 #endif
45
47 extern std::mutex MallocMutex;
48
50 extern int64_t CountAlloc;
51 extern int64_t CountFreed;
52 extern int64_t AllocatedBytes;
53 extern int64_t MaximumAllocatedBytes;
54 extern bool MallocDebug;
55
57
61 string out_bytes( size_t nb );
62
63 /*\
64 :|: __ __ _ _
65 :|: | \/ | __ _| | | ___ ___
66 :|: | |\/| |/ _` | | |/ _ \ / __|
67 :|: | | | | (_| | | | (_) | (__
68 :|: |_| |_|\__,_|_|_|\___/ \___|
69 \*/
70
72
78 template <typename T>
79 class Malloc {
80 public:
82 using valueType = T;
83
84 private:
85
86 string m_name;
87 size_t m_num_total_values{0};
88 size_t m_num_total_reserved{0};
89 size_t m_num_allocated{0};
90 valueType * m_p_memory{nullptr};
91
93 void allocate_internal( size_t n );
94
96 void memory_exausted( size_t sz );
97
99 void pop_exausted( size_t sz );
100
101 public:
102
104 Malloc( Malloc<T> const & ) = delete;
105
107 Malloc<T> const & operator = ( Malloc<T> const & ) const = delete;
108
110
113 explicit
114 Malloc( string name )
115 : m_name(std::move(name))
116 { }
117
119
123
125
128 void allocate( size_t n );
129
130 template <typename T2>
131 void
132 allocate(T2 n) {
133 static_assert(std::is_integral<T2>::value, "allocate() accepts only integral types!");
134 allocate(static_cast<size_t>(n));
135 }
136
138
141 void reallocate( size_t n );
142
143 template <typename T2>
144 void
146 static_assert(std::is_integral<T2>::value, "reallocate() accepts only integral types!");
147 reallocate(static_cast<size_t>(n));
148 }
149
151 void free() { m_num_total_values = m_num_allocated = 0; }
152
154 void hard_free();
155
157
160 size_t size() const { return m_num_total_values; }
161
163
167 T * operator () ( size_t sz ) {
168 size_t offs = m_num_allocated;
169 m_num_allocated += sz;
170 if ( m_num_allocated > m_num_total_values ) memory_exausted( sz );
171 return m_p_memory + offs;
172 }
173
174 template <typename T2>
175 T* operator() ( T2 sz ) {
176 static_assert(std::is_integral<T2>::value, "operator() accepts only integral types!");
177 return (*this)(static_cast<size_t>(sz));
178 }
179
180
183 void
184 pop( size_t sz ) {
185 if ( sz > m_num_allocated ) pop_exausted( sz );
186 m_num_allocated -= sz;
187 }
188
189 template <typename T2>
190 void
191 pop( T2 n ) {
192 static_assert(std::is_integral<T2>::value, "pop() accepts only integral types!");
193 pop(static_cast<size_t>(n));
194 }
195
197
201 T * malloc( size_t n );
202
203 template <typename T2>
204 T *
205 malloc( T2 n ) {
206 static_assert(std::is_integral<T2>::value, "malloc() accepts only integral types!");
207 return malloc(static_cast<size_t>(n));
208 }
209
211
215 T * realloc( size_t n );
216
217 template <typename T2>
218 T *
219 realloc( T2 n ) {
220 static_assert(std::is_integral<T2>::value, "realloc() accepts only integral types!");
221 return realloc(static_cast<size_t>(n));
222 }
223
225
228 bool is_empty() const { return m_num_allocated >= m_num_total_values; }
229
231
234 void must_be_empty( string_view where ) const;
235
237
241 string info( string_view where ) const;
242
243 };
244
245 extern template class Malloc<char>;
246 extern template class Malloc<uint16_t>;
247 extern template class Malloc<int16_t>;
248 extern template class Malloc<uint32_t>;
249 extern template class Malloc<int32_t>;
250 extern template class Malloc<uint64_t>;
251 extern template class Malloc<int64_t>;
252 extern template class Malloc<float>;
253 extern template class Malloc<double>;
254
255 extern template class Malloc<void*>;
256 extern template class Malloc<char*>;
257 extern template class Malloc<uint16_t*>;
258 extern template class Malloc<int16_t*>;
259 extern template class Malloc<uint32_t*>;
260 extern template class Malloc<int32_t*>;
261 extern template class Malloc<uint64_t*>;
262 extern template class Malloc<int64_t*>;
263 extern template class Malloc<float*>;
264 extern template class Malloc<double*>;
265
266 /*\
267 :|: __ __ _ _ _____ _ _
268 :|: | \/ | __ _| | | ___ ___| ___(_)_ _____ __| |
269 :|: | |\/| |/ _` | | |/ _ \ / __| |_ | \ \/ / _ \/ _` |
270 :|: | | | | (_| | | | (_) | (__| _| | |> < __/ (_| |
271 :|: |_| |_|\__,_|_|_|\___/ \___|_| |_/_/\_\___|\__,_|
272 \*/
273
275
281 template <typename T, std::size_t mem_size>
283 public:
285 using valueType = T;
286
287 private:
288
289 string m_name;
290 size_t m_num_allocated{0};
291 valueType m_data[mem_size];
292
293 public:
294
296 MallocFixed(MallocFixed<T,mem_size> const &) = delete; // blocco costruttore di copia
297
299 MallocFixed<T,mem_size> const & operator = (MallocFixed<T,mem_size> const &) const = delete; // blocco copia
300
302
305 explicit
306 MallocFixed( string name )
307 : m_name(std::move(name))
308 {}
309
311 ~MallocFixed() = default;
312
314 void free() { m_num_allocated = 0; }
315
317
320 static size_t size() { return mem_size; }
321
323
327 T * operator () ( size_t sz );
328
330
333 void pop( size_t sz );
334
336
339 bool is_empty() const { return m_num_allocated >= mem_size; }
340
341 };
342
344
345}
346
347//
348// eof: Malloc.hxx
349//
MallocFixed(string name)
Constructor.
Definition Malloc.hxx:306
void free()
Free memory without deallocating the pointer.
Definition Malloc.hxx:314
~MallocFixed()=default
Destructor.
bool is_empty() const
Check if the memory is fully allocated.
Definition Malloc.hxx:339
void pop(size_t sz)
Free memory for sz objects.
MallocFixed(MallocFixed< T, mem_size > const &)=delete
Copy constructor is deleted.
static size_t size()
Get the number of allocated objects.
Definition Malloc.hxx:320
T valueType
Type alias for the type of objects managed by this allocator.
Definition Malloc.hxx:285
Class for dynamic memory allocation of objects.
Definition Malloc.hxx:79
void allocate(size_t n)
Allocate memory for n objects, error if already allocated.
T valueType
Type alias for the type of objects managed by this allocator.
Definition Malloc.hxx:82
~Malloc()
Destructor.
Definition Malloc.hxx:122
bool is_empty() const
Check if the memory is fully allocated.
Definition Malloc.hxx:228
void must_be_empty(string_view where) const
Ensure that memory is fully used.
void hard_free()
Free memory and deallocate the pointer.
size_t size() const
Get the number of allocated objects.
Definition Malloc.hxx:160
void free()
Free memory without deallocating the pointer.
Definition Malloc.hxx:151
Malloc(Malloc< T > const &)=delete
Copy constructor is deleted.
void allocate(T2 n)
Definition Malloc.hxx:132
string info(string_view where) const
Get memory allocation information.
Malloc(string name)
Constructor.
Definition Malloc.hxx:114
void reallocate(size_t n)
Reallocate memory for n objects, even if already allocated.
void pop(size_t sz)
Free memory for sz objects.
Definition Malloc.hxx:184
Malloc< T > const & operator=(Malloc< T > const &) const =delete
Assignment operator is deleted.
T * realloc(size_t n)
Reallocate memory for n objects.
T * realloc(T2 n)
Definition Malloc.hxx:219
void pop(T2 n)
Definition Malloc.hxx:191
T * malloc(size_t n)
Allocate memory for n objects.
T * malloc(T2 n)
Definition Malloc.hxx:205
T * operator()(size_t sz)
Allocate memory for sz objects and return the pointer.
Definition Malloc.hxx:167
void reallocate(T2 n)
Definition Malloc.hxx:145
std::mutex MallocMutex
Global mutex for thread-safe memory operations.
int64_t AllocatedBytes
int64_t CountAlloc
Global variables for tracking memory allocation statistics.
int64_t MaximumAllocatedBytes
string out_bytes(size_t nb)
Utility function to convert byte size into a human-readable format.
bool MallocDebug
int64_t CountFreed
Definition SystemUtils.cc:39