µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
block-pool.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_BLOCK_POOL_H_
14#define CMSIS_PLUS_MEMORY_BLOCK_POOL_H_
15
16// ----------------------------------------------------------------------------
17
18#if defined(__cplusplus)
19
20// ----------------------------------------------------------------------------
21
22#include <cmsis-plus/rtos/os.h>
23
24// ----------------------------------------------------------------------------
25
26#pragma GCC diagnostic push
27#if defined(__clang__)
28#pragma clang diagnostic ignored "-Wc++98-compat"
29#endif
30
31// ----------------------------------------------------------------------------
32
33namespace os
34{
35 namespace memory
36 {
37
38 // ========================================================================
39
57 {
58 public:
59
72 block_pool (std::size_t blocks, std::size_t block_size_bytes, void* addr,
73 std::size_t bytes);
74
83 block_pool (const char* name, std::size_t blocks,
84 std::size_t block_size_bytes, void* addr, std::size_t bytes);
85
86 protected:
87
92 block_pool (const char* name);
93
94 public:
95
100 // The rule of five.
101 block_pool (const block_pool&) = delete;
102 block_pool (block_pool&&) = delete;
104 operator= (const block_pool&) = delete;
106 operator= (block_pool&&) = delete;
107
115 virtual
116 ~block_pool () override;
117
122 protected:
123
138 void
139 internal_construct_ (std::size_t blocks, std::size_t block_size_bytes,
140 void* addr, std::size_t bytes) noexcept;
141
149 void
150 internal_reset_ (void) noexcept;
151
158 virtual void*
159 do_allocate (std::size_t bytes, std::size_t alignment) override;
160
169 virtual void
170 do_deallocate (void* addr, std::size_t bytes, std::size_t alignment)
171 noexcept override;
172
179 virtual std::size_t
180 do_max_size (void) const noexcept override;
181
189 virtual void
190 do_reset (void) noexcept override;
191
196 protected:
197
206 void* pool_addr_ = nullptr;
207
211 void* volatile first_ = nullptr;
212
216 std::size_t blocks_ = 0;
217
221 std::size_t block_size_bytes_ = 0;
222
226 volatile std::size_t count_ = 0;
227
232 };
233
234 // ========================================================================
235
248 template<typename T, std::size_t N>
250 {
251 public:
252
256 using value_type = T;
257
258 static_assert(sizeof(value_type) >= sizeof(void*),
259 "Template type T must be large enough to store a pointer.");
260
264 static const std::size_t blocks = N;
265
277
282 block_pool_typed_inclusive (const char* name);
283
284 public:
285
290 // The rule of five.
294 operator= (const block_pool_typed_inclusive&) = delete;
296 operator= (block_pool_typed_inclusive&&) = delete;
297
305 virtual
306 ~block_pool_typed_inclusive () override;
307
312 protected:
313
321 typename std::aligned_storage<sizeof(value_type), alignof(value_type)>::type arena_[blocks];
322
327 };
328
329 // ========================================================================
330
345 template<typename T, typename A = os::rtos::memory::allocator<T>>
347 {
348 public:
349
353 using value_type = T;
354
355 static_assert(sizeof(value_type) >= sizeof(void*),
356 "Template type T must be large enough to store a pointer.");
357
361 using allocator_type = A;
362
366 using allocator_traits = std::allocator_traits<A>;
367
368 // It is recommended to have the same type, but at least the types
369 // should have the same size.
370 static_assert(sizeof(value_type) == sizeof(typename allocator_traits::value_type),
371 "The allocator must be parametrised with a type of same size.");
372
384 block_pool_typed_allocated (std::size_t blocks,
385 const allocator_type& allocator =
386 allocator_type ());
387
395 block_pool_typed_allocated (const char* name, std::size_t blocks,
396 const allocator_type& allocator =
397 allocator_type ());
398
399 public:
400
405 // The rule of five.
409 operator= (const block_pool_typed_allocated&) = delete;
411 operator= (block_pool_typed_allocated&&) = delete;
412
420 virtual
421 ~block_pool_typed_allocated () override;
422
427 protected:
428
441 allocator_type* allocator_ = nullptr;
442
447 };
448
449 // -------------------------------------------------------------------------
450 } /* namespace memory */
451} /* namespace os */
452
453// ===== Inline & template implementations ====================================
454
455namespace os
456{
457 namespace memory
458 {
459
460 // ========================================================================
461
462 inline
463 block_pool::block_pool (const char* name) :
464 rtos::memory::memory_resource
465 { name }
466 {
467 }
468
469 inline
470 block_pool::block_pool (std::size_t blocks, std::size_t block_size_bytes,
471 void* addr, std::size_t bytes) :
473 { nullptr, blocks, block_size_bytes, addr, bytes }
474 {
475 }
476
477 inline
478 block_pool::block_pool (const char* name, std::size_t blocks,
479 std::size_t block_size_bytes, void* addr,
480 std::size_t bytes) :
481 rtos::memory::memory_resource
482 { name }
483 {
484 trace::printf ("%s(%u,%u,%p,%u) @%p %s\n", __func__, blocks,
485 block_size_bytes, addr, bytes, this, this->name ());
486
487 internal_construct_ (blocks, block_size_bytes, addr, bytes);
488 }
489
490 // ========================================================================
491
492 template<typename T, std::size_t N>
493 inline
496 {
497 }
498
499 template<typename T, std::size_t N>
500 inline
502 const char* name) :
504 { name }
505 {
506 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
507
508 internal_construct_ (blocks, sizeof(value_type), &arena_[0],
509 sizeof(arena_));
510 }
511
512 template<typename T, std::size_t N>
514 {
515 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
516 }
517
518 // ========================================================================
519
520 template<typename T, typename A>
521 inline
523 std::size_t blocks, const allocator_type& allocator) :
524 block_pool_typed_allocated (nullptr, blocks, allocator)
525 {
526 }
527
528 template<typename T, typename A>
530 const char* name, std::size_t blocks, const allocator_type& allocator) :
532 { name }
533 {
534 trace::printf ("%s(%u,%p) @%p %s\n", __func__, blocks, &allocator, this,
535 this->name ());
536
537 // Remember the allocator, it'll be used by the destructor.
538 allocator_ =
539 static_cast<allocator_type*> (&const_cast<allocator_type&> (allocator));
540
541 void* addr = allocator_->allocate (blocks);
542 if (addr == nullptr)
543 {
545 }
546
547 internal_construct_ (blocks, sizeof(value_type), addr,
548 blocks * sizeof(value_type));
549 }
550
551 template<typename T, typename A>
553 {
554 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
555
556 // Skip in case a derived class did the deallocation.
557 if (allocator_ != nullptr)
558 {
559 allocator_->deallocate (
560 static_cast<typename allocator_traits::pointer> (pool_addr_),
561 blocks_);
562
563 // Prevent another deallocation.
564 allocator_ = nullptr;
565 }
566 }
567
568 // --------------------------------------------------------------------------
569
570 } /* namespace memory */
571} /* namespace os */
572
573#pragma GCC diagnostic pop
574
575// ----------------------------------------------------------------------------
576
577#endif /* __cplusplus */
578
579// ----------------------------------------------------------------------------
580
581#endif /* CMSIS_PLUS_MEMORY_BLOCK_POOL_H_ */
Memory resource managing a dynamically allocated pool. of same size blocks of type T.
Definition block-pool.h:347
A allocator_type
Standard allocator type definition.
Definition block-pool.h:361
T value_type
Standard allocator type definition.
Definition block-pool.h:353
virtual ~block_pool_typed_allocated() override
Destruct the memory resource object instance.
Definition block-pool.h:552
block_pool_typed_allocated(std::size_t blocks, const allocator_type &allocator=allocator_type())
Construct a memory resource object instance.
Definition block-pool.h:522
std::allocator_traits< A > allocator_traits
Standard allocator traits definition.
Definition block-pool.h:366
Memory resource managing an internal pool. of same size blocks of type T.
Definition block-pool.h:250
block_pool_typed_inclusive(void)
Construct a memory resource object instance.
Definition block-pool.h:494
T value_type
Standard allocator type definition.
Definition block-pool.h:256
static const std::size_t blocks
Local constant based on template definition.
Definition block-pool.h:264
virtual ~block_pool_typed_inclusive() override
Destruct the memory resource object instance.
Definition block-pool.h:513
Memory resource managing a pool of same size blocks, using an existing arena.
Definition block-pool.h:57
block_pool(std::size_t blocks, std::size_t block_size_bytes, void *addr, std::size_t bytes)
Construct a memory resource object instance.
Definition block-pool.h:470
virtual void * do_allocate(std::size_t bytes, std::size_t alignment) override
Implementation of the memory allocator.
virtual std::size_t do_max_size(void) const noexcept override
Implementation of the function to get max size.
void internal_construct_(std::size_t blocks, std::size_t block_size_bytes, void *addr, std::size_t bytes) noexcept
Internal function to construct the memory resource object instance.
virtual void do_deallocate(void *addr, std::size_t bytes, std::size_t alignment) noexcept override
Implementation of the memory deallocator.
void internal_reset_(void) noexcept
Internal function to reset the memory resource object.
virtual ~block_pool() override
Destruct the memory resource object instance.
virtual void do_reset(void) noexcept override
Implementation of the function to reset the memory manager.
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
void __throw_bad_alloc(void)
System namespace.
Single file µOS++ RTOS definitions.