µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
lifo.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_LIFO_H_
29#define CMSIS_PLUS_MEMORY_LIFO_H_
30
31// ----------------------------------------------------------------------------
32
33#if defined(__cplusplus)
34
35// ----------------------------------------------------------------------------
36
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 // ========================================================================
55
89 {
90 public:
91
102 lifo (void* addr, std::size_t bytes);
103
110 lifo (const char* name, void* addr, std::size_t bytes);
111
112 protected:
113
118 lifo () = default;
119
124 lifo (const char* name);
125
126 public:
127
132 // The rule of five.
133 lifo (const lifo&) = delete;
134 lifo (lifo&&) = delete;
135 lifo&
136 operator= (const lifo&) = delete;
137 lifo&
138 operator= (lifo&&) = delete;
139
147 virtual
148 ~lifo () override;
149
154 protected:
155
167 virtual void*
168 do_allocate (std::size_t bytes, std::size_t alignment) override;
169
174 };
175
176 // ========================================================================
177
190 template<std::size_t N>
191 class lifo_inclusive : public lifo
192 {
193 public:
194
198 static const std::size_t bytes = N;
199
210 lifo_inclusive (void);
211
216 lifo_inclusive (const char* name);
217
218 public:
219
224 // The rule of five.
225 lifo_inclusive (const lifo_inclusive&) = delete;
226 lifo_inclusive (lifo_inclusive&&) = delete;
228 operator= (const lifo_inclusive&) = delete;
230 operator= (lifo_inclusive&&) = delete;
231
239 virtual
241
246 protected:
247
255 char arena_[bytes];
256
261 };
262
263 // ========================================================================
264
277 template<typename A = os::rtos::memory::allocator<char>>
278 class lifo_allocated : public lifo
279 {
280 public:
281
285 using value_type = char;
286
290 using allocator_type = A;
291
295 using allocator_traits = std::allocator_traits<A>;
296
297 // It is recommended to have the same type, but at least the types
298 // should have the same size.
299 static_assert(sizeof(value_type) == sizeof(typename allocator_traits::value_type),
300 "The allocator must be parametrised with a type of same size.");
301
313 lifo_allocated (std::size_t bytes, const allocator_type& allocator =
314 allocator_type ());
315
323 lifo_allocated (const char* name, std::size_t bytes,
324 const allocator_type& allocator = allocator_type ());
325
326 public:
327
332 // The rule of five.
333 lifo_allocated (const lifo_allocated&) = delete;
334 lifo_allocated (lifo_allocated&&) = delete;
336 operator= (const lifo_allocated&) = delete;
338 operator= (lifo_allocated&&) = delete;
339
347 virtual
349
354 protected:
355
368 allocator_type* allocator_ = nullptr;
369
374 };
375
376 // -------------------------------------------------------------------------
377 } /* namespace memory */
378} /* namespace os */
379
380// ===== Inline & template implementations ====================================
381
382namespace os
383{
384 namespace memory
385 {
386
387 // ========================================================================
388
389 inline
390 lifo::lifo (void* addr, std::size_t bytes) :
392 { nullptr, addr, bytes }
393 {
394 trace::printf ("%s(%p,%u) @%p %s\n", __func__, addr, bytes, this,
395 this->name ());
396 }
397
398 inline
399 lifo::lifo (const char* name, void* addr, std::size_t bytes) :
401 { name, addr, bytes }
402 {
403 trace::printf ("%s(%p,%u) @%p %s\n", __func__, addr, bytes, this,
404 this->name ());
405 }
406
407 // ========================================================================
408
409 template<std::size_t N>
410 inline
412 lifo_inclusive (nullptr)
413 {
414 ;
415 }
416
417 template<std::size_t N>
418 inline
420 lifo
421 { name }
422 {
423 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
424
425 internal_construct_ (&arena_[0], bytes);
426 }
427
428 template<std::size_t N>
430 {
431 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
432 }
433
434 // ========================================================================
435
436 template<typename A>
437 inline
439 const allocator_type& allocator) :
440 lifo_allocated (nullptr, bytes, allocator)
441 {
442 ;
443 }
444
445 template<typename A>
446 lifo_allocated<A>::lifo_allocated (const char* name, std::size_t bytes,
447 const allocator_type& allocator) :
448 lifo
449 { name }
450 {
451 trace::printf ("%s(%u) @%p %s\n", __func__, bytes, this, this->name ());
452
453 // Remember the allocator, it'll be used by the destructor.
454 allocator_ =
455 static_cast<allocator_type*> (&const_cast<allocator_type&> (allocator));
456
457 void* addr = allocator_->allocate (bytes);
458 if (addr == nullptr)
459 {
461 }
462
463 internal_construct_ (addr, bytes);
464 }
465
466 template<typename A>
468 {
469 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
470
471 // Skip in case a derived class did the deallocation.
472 if (allocator_ != nullptr)
473 {
474 allocator_->deallocate (
475 static_cast<typename allocator_traits::pointer> (arena_addr_),
476 total_bytes_);
477
478 // Prevent another deallocation.
479 allocator_ = nullptr;
480 }
481 }
482
483 // --------------------------------------------------------------------------
484
485 } /* namespace memory */
486} /* namespace os */
487
488#pragma GCC diagnostic pop
489
490// ----------------------------------------------------------------------------
491
492#endif /* __cplusplus */
493
494// ----------------------------------------------------------------------------
495
496#endif /* CMSIS_PLUS_MEMORY_LIFO_H_ */
Memory resource implementing the first fit, top-down allocation policies, using an existing arena.
Memory resource implementing the LIFO allocation policies, using a dynamically allocated arena.
Definition lifo.h:279
char value_type
Standard allocator type definition.
Definition lifo.h:285
std::allocator_traits< A > allocator_traits
Standard allocator traits definition.
Definition lifo.h:295
lifo_allocated(std::size_t bytes, const allocator_type &allocator=allocator_type())
Construct a memory resource object instance.
Definition lifo.h:438
virtual ~lifo_allocated()
Destruct the memory resource object instance.
Definition lifo.h:467
A allocator_type
Standard allocator type definition.
Definition lifo.h:290
Memory resource implementing the LIFO allocation policies, using an internal arena.
Definition lifo.h:192
static const std::size_t bytes
Local constant based on template definition.
Definition lifo.h:198
lifo_inclusive(void)
Construct a memory resource object instance.
Definition lifo.h:411
virtual ~lifo_inclusive()
Destruct the memory resource object instance.
Definition lifo.h:429
Memory resource implementing the LIFO allocation/deallocation policies, using an existing arena.
Definition lifo.h:89
lifo(const char *name)
Construct a named memory resource object instance.
lifo()=default
Default constructor. Construct a memory resource object instance.
virtual ~lifo() override
Destruct the memory resource object instance.
Definition lifo.cpp:46
virtual void * do_allocate(std::size_t bytes, std::size_t alignment) override
Implementation of the memory allocator.
Definition lifo.cpp:80
const char * name(void) const
Get object name.
Definition os-decls.h:774
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:74
void __throw_bad_alloc(void)
System namespace.