µ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-2023 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit/.
11 */
12
13#ifndef CMSIS_PLUS_MEMORY_MALLOC_H_
14#define CMSIS_PLUS_MEMORY_MALLOC_H_
15
16// ----------------------------------------------------------------------------
17
18#if defined(__cplusplus)
19
20// ----------------------------------------------------------------------------
21
22#if defined(OS_USE_OS_APP_CONFIG_H)
23#include <cmsis-plus/os-app-config.h>
24#endif
25
26#include <cmsis-plus/rtos/os.h>
27
28// ----------------------------------------------------------------------------
29
30#pragma GCC diagnostic push
31#if defined(__clang__)
32#pragma clang diagnostic ignored "-Wc++98-compat"
33#endif
34
35// ----------------------------------------------------------------------------
36
37namespace os
38{
39 namespace memory
40 {
41
42#pragma GCC diagnostic push
43#if defined(__clang__)
44#pragma clang diagnostic ignored "-Wweak-vtables"
45#endif
46
47 // ========================================================================
48
62 {
63 public:
64
74
78 malloc_memory_resource (const char* name);
79
87 operator= (const malloc_memory_resource&) = delete;
89 operator= (malloc_memory_resource&&) = delete;
90
98 ~malloc_memory_resource () override;
99
104 protected:
105
117 virtual void*
118 do_allocate (std::size_t bytes, std::size_t alignment) override;
119
128 virtual void
129 do_deallocate (void* addr, std::size_t bytes, std::size_t alignment)
130 noexcept override;
131
135 };
136
137 // ======================================================================
138
151 {
152 public:
153
162 ~new_delete_memory_resource () override = default;
163
168 protected:
169
181 virtual void*
182 do_allocate (size_t bytes, size_t alignment) override;
183
192 virtual void
193 do_deallocate (void* addr, size_t bytes, size_t alignment)
194 noexcept override;
195
200 };
201
202#pragma GCC diagnostic pop
203
204 // -------------------------------------------------------------------------
205 } /* namespace memory */
206} /* namespace os */
207
208// ===== Inline & template implementations ====================================
209
210namespace os
211{
212 namespace memory
213 {
214
215 // ========================================================================
216
217 inline
219 {
220 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
221 }
222
223 inline
225 rtos::memory::memory_resource
226 { name }
227 {
228 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
229 }
230
231 inline
233 {
234 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
235 }
236
237#pragma GCC diagnostic push
238#if defined(__clang__)
239#pragma clang diagnostic ignored "-Wunused-parameter"
240#elif defined(__GNUC__)
241// Needed because 'alignment' is used only in trace calls.
242#pragma GCC diagnostic ignored "-Wunused-parameter"
243#endif
244
245 inline void*
247 std::size_t alignment)
248 {
249 // Ignore alignment for now.
250 void* mem = std::malloc (bytes);
251#if defined(OS_TRACE_LIBCPP_MEMORY_RESOURCE)
252 trace::printf ("%s(%u,%u)=%p @%p %s\n", __func__, bytes, alignment, mem,
253 this, name ());
254#endif
255
256 return mem;
257 }
258
259 inline void
260 malloc_memory_resource::do_deallocate (void* addr, std::size_t bytes,
261 std::size_t alignment) noexcept
262 {
263#if defined(OS_TRACE_LIBCPP_MEMORY_RESOURCE)
264 trace::printf ("%s(%p,%u,%u) @%p %s\n", __func__, addr, bytes, alignment,
265 this, name ());
266#endif
267 // Ignore size and alignment for now.
268 std::free (addr);
269 }
270
271#pragma GCC diagnostic pop
272
273 // ========================================================================
274
275#pragma GCC diagnostic push
276#if defined(__clang__)
277#pragma clang diagnostic ignored "-Wunused-parameter"
278#elif defined(__GNUC__)
279// Needed because 'alignment' is used only in trace calls.
280#pragma GCC diagnostic ignored "-Wunused-parameter"
281#endif
282
283 inline void*
284 new_delete_memory_resource::do_allocate (size_t bytes, size_t alignment)
285 {
286 // Ignore alignment for now.
287 void* mem = ::operator new (bytes);
288#if defined(OS_TRACE_LIBCPP_MEMORY_RESOURCE)
289 trace::printf ("%s(%u,%u)=%p @%p %s\n", __func__, bytes, alignment, mem,
290 this, name ());
291#endif
292 allocated_chunks_++;
293 return mem;
294 }
295
296 inline void
298 size_t alignment) noexcept
299 {
300#if defined(OS_TRACE_LIBCPP_MEMORY_RESOURCE)
301 trace::printf ("%s(%p,%u,%u) @%p %s\n", __func__, addr, bytes, alignment,
302 this, name ());
303#endif
304 // Ignore size and alignment for now.
305 ::operator delete (addr);
306 allocated_chunks_--;
307 }
308
309#pragma GCC diagnostic pop
310
311 // ==========================================================================
312 } /* namespace memory */
313} /* namespace os */
314
315#pragma GCC diagnostic pop
316
317// ----------------------------------------------------------------------------
318
319#endif /* __cplusplus */
320
321// ----------------------------------------------------------------------------
322
323#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:62
malloc_memory_resource()
Default constructor. Construct a memory manager object instance.
Definition malloc.h:218
virtual void do_deallocate(void *addr, std::size_t bytes, std::size_t alignment) noexcept override
Implementation of the memory deallocator.
Definition malloc.h:260
virtual void * do_allocate(std::size_t bytes, std::size_t alignment) override
Implementation of the memory allocator.
Definition malloc.h:246
~malloc_memory_resource() override
Destruct the memory manager object instance.
Definition malloc.h:232
A memory manager that allocates memory via the system operator new and deallocates via operator delet...
Definition malloc.h:151
~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:284
virtual void do_deallocate(void *addr, size_t bytes, size_t alignment) noexcept override
Implementation of the memory deallocator.
Definition malloc.h:297
const char * name(void) const
Get object name.
Definition os-decls.h:759
Memory resource manager (abstract class).
Definition os-memory.h:160
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:60
System namespace.
Single file µOS++ RTOS definitions.