µ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++ 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_BLOCK_POOL_H_
13#define CMSIS_PLUS_MEMORY_BLOCK_POOL_H_
14
15// ----------------------------------------------------------------------------
16
17#if defined(__cplusplus)
18
19// ----------------------------------------------------------------------------
20
21#include <cmsis-plus/rtos/os.h>
22
23// ----------------------------------------------------------------------------
24
25#pragma GCC diagnostic push
26#if defined(__clang__)
27#pragma clang diagnostic ignored "-Wc++98-compat"
28#endif
29
30// ----------------------------------------------------------------------------
31
32namespace os
33{
34 namespace memory
35 {
36
37 // ========================================================================
38
56 {
57 public:
70 block_pool (std::size_t blocks, std::size_t block_size_bytes, void* addr,
71 std::size_t bytes);
72
81 block_pool (const char* name, std::size_t blocks,
82 std::size_t block_size_bytes, void* addr, std::size_t bytes);
83
84 protected:
89 block_pool (const char* name);
90
91 public:
96 // The rule of five.
97 block_pool (const block_pool&) = delete;
98 block_pool (block_pool&&) = delete;
100 operator= (const block_pool&)
101 = delete;
103 operator= (block_pool&&)
104 = delete;
105
113 virtual ~block_pool () override;
114
119 protected:
135 void
136 internal_construct_ (std::size_t blocks, std::size_t block_size_bytes,
137 void* addr, std::size_t bytes) noexcept;
138
146 void
147 internal_reset_ (void) noexcept;
148
155 virtual void*
156 do_allocate (std::size_t bytes, std::size_t alignment) override;
157
166 virtual void
167 do_deallocate (void* addr, std::size_t bytes,
168 std::size_t alignment) noexcept override;
169
176 virtual std::size_t
177 do_max_size (void) const noexcept override;
178
186 virtual void
187 do_reset (void) noexcept override;
188
193 protected:
202 void* pool_addr_ = nullptr;
203
207 void* volatile first_ = nullptr;
208
212 std::size_t blocks_ = 0;
213
217 std::size_t block_size_bytes_ = 0;
218
222 volatile std::size_t count_ = 0;
223
227 };
228
229 // ========================================================================
230
243 template <typename T, std::size_t N>
245 {
246 public:
250 using value_type = T;
251
252 static_assert (
253 sizeof (value_type) >= sizeof (void*),
254 "Template type T must be large enough to store a pointer.");
255
259 static const std::size_t blocks = N;
260
272
277 block_pool_typed_inclusive (const char* name);
278
279 public:
284 // The rule of five.
288 operator= (const block_pool_typed_inclusive&)
289 = delete;
291 operator= (block_pool_typed_inclusive&&)
292 = delete;
293
301 virtual ~block_pool_typed_inclusive () override;
302
307 protected:
315 typename std::aligned_storage<sizeof (value_type),
316 alignof (value_type)>::type arena_[blocks];
317
321 };
322
323 // ========================================================================
324
339 template <typename T, typename A = os::rtos::memory::allocator<T>>
341 {
342 public:
346 using value_type = T;
347
348 static_assert (
349 sizeof (value_type) >= sizeof (void*),
350 "Template type T must be large enough to store a pointer.");
351
355 using allocator_type = A;
356
360 using allocator_traits = std::allocator_traits<A>;
361
362 // It is recommended to have the same type, but at least the types
363 // should have the same size.
364 static_assert (
365 sizeof (value_type)
366 == sizeof (typename allocator_traits::value_type),
367 "The allocator must be parametrised with a type of same size.");
368
380 block_pool_typed_allocated (std::size_t blocks,
381 const allocator_type& allocator
382 = allocator_type ());
383
391 block_pool_typed_allocated (const char* name, std::size_t blocks,
392 const allocator_type& allocator
393 = allocator_type ());
394
395 public:
400 // The rule of five.
404 operator= (const block_pool_typed_allocated&)
405 = delete;
407 operator= (block_pool_typed_allocated&&)
408 = delete;
409
417 virtual ~block_pool_typed_allocated () override;
418
423 protected:
437 allocator_type* allocator_ = nullptr;
438
442 };
443
444 // -------------------------------------------------------------------------
445 } /* namespace memory */
446} /* namespace os */
447
448// ===== Inline & template implementations ====================================
449
450namespace os
451{
452 namespace memory
453 {
454
455 // ========================================================================
456
457 inline block_pool::block_pool (const char* name)
458 : rtos::memory::memory_resource{ name }
459 {
460 }
461
462 inline block_pool::block_pool (std::size_t blocks,
463 std::size_t block_size_bytes, void* addr,
464 std::size_t bytes)
465 : block_pool{ nullptr, blocks, block_size_bytes, addr, bytes }
466 {
467 }
468
469 inline block_pool::block_pool (const char* name, std::size_t blocks,
470 std::size_t block_size_bytes, void* addr,
471 std::size_t bytes)
472 : rtos::memory::memory_resource{ name }
473 {
474 trace::printf ("%s(%u,%u,%p,%u) @%p %s\n", __func__, blocks,
475 block_size_bytes, addr, bytes, this, this->name ());
476
477 internal_construct_ (blocks, block_size_bytes, addr, bytes);
478 }
479
480 // ========================================================================
481
482 template <typename T, std::size_t N>
485 {
486 }
487
488 template <typename T, std::size_t N>
490 const char* name)
491 : block_pool{ name }
492 {
493 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
494
495 internal_construct_ (blocks, sizeof (value_type), &arena_[0],
496 sizeof (arena_));
497 }
498
499 template <typename T, std::size_t N>
501 {
502 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
503 }
504
505 // ========================================================================
506
507 template <typename T, typename A>
509 std::size_t blocks, const allocator_type& allocator)
510 : block_pool_typed_allocated (nullptr, blocks, allocator)
511 {
512 }
513
514 template <typename T, typename A>
516 const char* name, std::size_t blocks, const allocator_type& allocator)
517 : block_pool{ name }
518 {
519 trace::printf ("%s(%u,%p) @%p %s\n", __func__, blocks, &allocator, this,
520 this->name ());
521
522 // Remember the allocator, it'll be used by the destructor.
523 allocator_ = static_cast<allocator_type*> (
524 &const_cast<allocator_type&> (allocator));
525
526 void* addr = allocator_->allocate (blocks);
527 if (addr == nullptr)
528 {
530 }
531
532 internal_construct_ (blocks, sizeof (value_type), addr,
533 blocks * sizeof (value_type));
534 }
535
536 template <typename T, typename A>
538 {
539 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
540
541 // Skip in case a derived class did the deallocation.
542 if (allocator_ != nullptr)
543 {
544 allocator_->deallocate (
545 static_cast<typename allocator_traits::pointer> (pool_addr_),
546 blocks_);
547
548 // Prevent another deallocation.
549 allocator_ = nullptr;
550 }
551 }
552
553 // ------------------------------------------------------------------------
554
555 } /* namespace memory */
556} /* namespace os */
557
558#pragma GCC diagnostic pop
559
560// ----------------------------------------------------------------------------
561
562#endif /* __cplusplus */
563
564// ----------------------------------------------------------------------------
565
566#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:341
A allocator_type
Standard allocator type definition.
Definition block-pool.h:355
T value_type
Standard allocator type definition.
Definition block-pool.h:346
virtual ~block_pool_typed_allocated() override
Destruct the memory resource object instance.
Definition block-pool.h:537
block_pool_typed_allocated(std::size_t blocks, const allocator_type &allocator=allocator_type())
Construct a memory resource object instance.
Definition block-pool.h:508
std::allocator_traits< A > allocator_traits
Standard allocator traits definition.
Definition block-pool.h:360
Memory resource managing an internal pool. of same size blocks of type T.
Definition block-pool.h:245
block_pool_typed_inclusive(void)
Construct a memory resource object instance.
Definition block-pool.h:483
T value_type
Standard allocator type definition.
Definition block-pool.h:250
static const std::size_t blocks
Local constant based on template definition.
Definition block-pool.h:259
virtual ~block_pool_typed_inclusive() override
Destruct the memory resource object instance.
Definition block-pool.h:500
Memory resource managing a pool of same size blocks, using an existing arena.
Definition block-pool.h:56
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:462
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: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
void __throw_bad_alloc(void)
System namespace.
Single file µOS++ RTOS definitions.