µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
malloc.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2016 Liviu Ionescu.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#ifndef CMSIS_PLUS_MEMORY_MALLOC_H_
29#define CMSIS_PLUS_MEMORY_MALLOC_H_
30
31// ----------------------------------------------------------------------------
32
33#if defined(__cplusplus)
34
35// ----------------------------------------------------------------------------
36
37#include <cmsis-plus/rtos/os.h>
38
39// ----------------------------------------------------------------------------
40
41#pragma GCC diagnostic push
42
43#if defined(__clang__)
44#pragma clang diagnostic ignored "-Wc++98-compat"
45#endif
46
47// ----------------------------------------------------------------------------
48
49namespace os
50{
51 namespace memory
52 {
53
54#pragma GCC diagnostic push
55#if defined(__clang__)
56#pragma clang diagnostic ignored "-Wweak-vtables"
57#endif
58
59 // ========================================================================
60
74 {
75 public:
76
86
90 malloc_memory_resource (const char* name);
91
99 operator= (const malloc_memory_resource&) = delete;
101 operator= (malloc_memory_resource&&) = delete;
102
110 ~malloc_memory_resource () override;
111
116 protected:
117
129 virtual void*
130 do_allocate (std::size_t bytes, std::size_t alignment) override;
131
140 virtual void
141 do_deallocate (void* addr, std::size_t bytes, std::size_t alignment)
142 noexcept override;
143
147 };
148
149 // ======================================================================
150
163 {
164 public:
165
174 ~new_delete_memory_resource () override = default;
175
180 protected:
181
193 virtual void*
194 do_allocate (size_t bytes, size_t alignment) override;
195
204 virtual void
205 do_deallocate (void* addr, size_t bytes, size_t alignment)
206 noexcept override;
207
212 };
213
214#pragma GCC diagnostic pop
215
216 // -------------------------------------------------------------------------
217 } /* namespace memory */
218} /* namespace os */
219
220// ===== Inline & template implementations ====================================
221
222namespace os
223{
224 namespace memory
225 {
226
227 // ========================================================================
228
229 inline
231 {
232 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
233 }
234
235 inline
237 rtos::memory::memory_resource
238 { name }
239 {
240 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
241 }
242
243 inline
245 {
246 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
247 }
248
249#pragma GCC diagnostic push
250// Needed because 'alignment' is used only in trace calls.
251#pragma GCC diagnostic ignored "-Wunused-parameter"
252
253 inline void*
255 std::size_t alignment)
256 {
257 // Ignore alignment for now.
258 void* mem = std::malloc (bytes);
259#if defined(OS_TRACE_LIBCPP_MEMORY_RESOURCE)
260 trace::printf ("%s(%u,%u)=%p @%p %s\n", __func__, bytes, alignment, mem,
261 this, name ());
262#endif
263
264 return mem;
265 }
266
267 inline void
268 malloc_memory_resource::do_deallocate (void* addr, std::size_t bytes,
269 std::size_t alignment) noexcept
270 {
271#if defined(OS_TRACE_LIBCPP_MEMORY_RESOURCE)
272 trace::printf ("%s(%p,%u,%u) @%p %s\n", __func__, addr, bytes, alignment,
273 this, name ());
274#endif
275 // Ignore size and alignment for now.
276 std::free (addr);
277 }
278
279#pragma GCC diagnostic pop
280
281 // ========================================================================
282
283#pragma GCC diagnostic push
284// Needed because 'alignment' is used only in trace calls.
285#pragma GCC diagnostic ignored "-Wunused-parameter"
286
287 inline void*
288 new_delete_memory_resource::do_allocate (size_t bytes, size_t alignment)
289 {
290 // Ignore alignment for now.
291 void* mem = ::operator new (bytes);
292#if defined(OS_TRACE_LIBCPP_MEMORY_RESOURCE)
293 trace::printf ("%s(%u,%u)=%p @%p %s\n", __func__, bytes, alignment, mem,
294 this, name ());
295#endif
296 allocated_chunks_++;
297 return mem;
298 }
299
300 inline void
302 size_t alignment) noexcept
303 {
304#if defined(OS_TRACE_LIBCPP_MEMORY_RESOURCE)
305 trace::printf ("%s(%p,%u,%u) @%p %s\n", __func__, addr, bytes, alignment,
306 this, name ());
307#endif
308 // Ignore size and alignment for now.
309 ::operator delete (addr);
310 allocated_chunks_--;
311 }
312
313#pragma GCC diagnostic pop
314
315 // ==========================================================================
316 } /* namespace memory */
317} /* namespace os */
318
319#pragma GCC diagnostic pop
320
321// ----------------------------------------------------------------------------
322
323#endif /* __cplusplus */
324
325// ----------------------------------------------------------------------------
326
327#endif /* CMSIS_PLUS_MEMORY_MALLOC_H_ */
A memory manager that allocates memory via the system std::malloc() and deallocates via std::free().
Definition malloc.h:74
malloc_memory_resource()
Default constructor. Construct a memory manager object instance.
Definition malloc.h:230
virtual void do_deallocate(void *addr, std::size_t bytes, std::size_t alignment) noexcept override
Implementation of the memory deallocator.
Definition malloc.h:268
virtual void * do_allocate(std::size_t bytes, std::size_t alignment) override
Implementation of the memory allocator.
Definition malloc.h:254
~malloc_memory_resource() override
Destruct the memory manager object instance.
Definition malloc.h:244
A memory manager that allocates memory via the system operator new and deallocates via operator delet...
Definition malloc.h:163
~new_delete_memory_resource() override=default
Destruct the memory manager object instance.
virtual void * do_allocate(size_t bytes, size_t alignment) override
Implementation of the memory allocator.
Definition malloc.h:288
virtual void do_deallocate(void *addr, size_t bytes, size_t alignment) noexcept override
Implementation of the memory deallocator.
Definition malloc.h:301
const char * name(void) const
Get object name.
Definition os-decls.h:774
Memory resource manager (abstract class).
Definition os-memory.h:165
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:74
System namespace.
Single file µOS++ RTOS definitions.