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