34#pragma clang diagnostic ignored "-Wc++98-compat"
194#if defined(OS_TRACE_RTOS_MEMPOOL)
203#if defined(OS_TRACE_RTOS_MEMPOOL)
204 trace::printf (
"%s() @%p %s\n", __func__,
this, this->name ());
240 memory_pool::memory_pool (std::size_t blocks, std::size_t block_size_bytes,
244 {
nullptr, blocks, block_size_bytes, attr, allocator }
278 std::size_t block_size_bytes,
284#if defined(OS_TRACE_RTOS_MEMPOOL)
285 trace::printf (
"%s() @%p %s %u %u\n", __func__,
this, this->name (),
286 blocks, block_size_bytes);
289 if (attr.mp_pool_address !=
nullptr)
292 internal_construct_ (blocks, block_size_bytes, attr,
nullptr, 0);
296 allocator_ = &allocator;
300 allocated_pool_size_elements_ = (compute_allocated_size_bytes<
301 typename allocator_type::value_type> (blocks, block_size_bytes)
302 +
sizeof(
typename allocator_type::value_type) - 1)
303 /
sizeof(
typename allocator_type::value_type);
305 allocated_pool_addr_ =
306 const_cast<allocator_type&
> (allocator).allocate (
307 allocated_pool_size_elements_);
309 internal_construct_ (
313 allocated_pool_addr_,
314 allocated_pool_size_elements_
315 *
sizeof(
typename allocator_type::value_type));
324 memory_pool::internal_construct_ (std::size_t blocks,
325 std::size_t block_size_bytes,
326 const attributes& attr,
328 std::size_t pool_size_bytes)
333#if !defined(OS_USE_RTOS_PORT_MEMORY_POOL)
334 clock_ = attr.clock !=
nullptr ? attr.clock : &
sysclock;
338 assert(blocks_ == blocks);
345 + __SIZEOF_POINTER__ - 1))
349 if (pool_address !=
nullptr)
352 assert(attr.mp_pool_address ==
nullptr);
354 pool_addr_ = pool_address;
355 pool_size_bytes_ = pool_size_bytes;
359 pool_addr_ = attr.mp_pool_address;
360 pool_size_bytes_ = attr.mp_pool_size_bytes;
364 void* p = pool_addr_;
365 std::size_t sz = pool_size_bytes_;
366 pool_addr_ =
static_cast<char*
> (std::align (__SIZEOF_POINTER__,
367 blocks_ * block_size_bytes_,
370#if defined(OS_TRACE_RTOS_MEMPOOL)
372 blocks_, block_size_bytes_, pool_addr_, pool_size_bytes_);
375 std::size_t storage_size = compute_allocated_size_bytes<void*> (
376 blocks_, block_size_bytes_);
378 if (pool_addr_ !=
nullptr)
415#if defined(OS_TRACE_RTOS_MEMPOOL)
420 assert(list_.empty ());
422 typedef typename std::allocator_traits<allocator_type>::pointer pointer;
424 if (allocated_pool_addr_ !=
nullptr)
427 static_cast<pointer
> (allocated_pool_addr_),
428 allocated_pool_size_elements_);
441 memory_pool::internal_init_ (
void)
446 char* p =
static_cast<char*
> (pool_addr_);
447 for (std::size_t i = 1; i < blocks_; ++i)
450 char* pn = p + block_size_bytes_;
453 *(
static_cast<void**
> (
static_cast<void*
> (p))) = pn;
459 *(
static_cast<void**
> (
static_cast<void*
> (p))) =
nullptr;
472 memory_pool::internal_try_first_ (
void)
474 if (first_ !=
nullptr)
476 void* p =
static_cast<void*
> (first_);
477 first_ = *(
static_cast<void**
> (first_));
479#pragma GCC diagnostic push
480#if defined(__clang__)
481#pragma clang diagnostic ignored "-Wdeprecated-volatile"
484#pragma GCC diagnostic pop
518#if defined(OS_TRACE_RTOS_MEMPOOL)
535 p = internal_try_first_ ();
538#if defined(OS_TRACE_RTOS_MEMPOOL)
560 p = internal_try_first_ ();
563#if defined(OS_TRACE_RTOS_MEMPOOL)
571 scheduler::internal_link_node (list_, node);
580 scheduler::internal_unlink_node (node);
584#if defined(OS_TRACE_RTOS_MEMPOOL)
614#if defined(OS_TRACE_RTOS_MEMPOOL)
619 assert(port::interrupts::is_priority_valid ());
626 p = internal_try_first_ ();
630#if defined(OS_TRACE_RTOS_MEMPOOL)
679#if defined(OS_TRACE_RTOS_MEMPOOL)
681 static_cast<unsigned int> (timeout),
this,
name ());
697 p = internal_try_first_ ();
700#if defined(OS_TRACE_RTOS_MEMPOOL)
721 { timeout_timestamp, crt_thread };
729 p = internal_try_first_ ();
732#if defined(OS_TRACE_RTOS_MEMPOOL)
741 scheduler::internal_link_node (list_, node, clock_list,
752 scheduler::internal_unlink_node (node, timeout_node);
756#if defined(OS_TRACE_RTOS_MEMPOOL)
762 if (clock_->steady_now () >= timeout_timestamp)
764#if defined(OS_TRACE_RTOS_MEMPOOL)
787#if defined(OS_TRACE_RTOS_MEMPOOL)
792 assert(port::interrupts::is_priority_valid ());
795 if ((block < pool_addr_)
797 >= (
static_cast<char*
> (pool_addr_) + blocks_ * block_size_bytes_)))
799#if defined(OS_TRACE_RTOS_MEMPOOL)
800 trace::printf (
"%s(%p) EINVAL @%p %s\n", __func__, block,
this,
815 *(
static_cast<void**
> (block)) = first_;
820#pragma GCC diagnostic push
821#if defined(__clang__)
822#pragma clang diagnostic ignored "-Wdeprecated-volatile"
825#pragma GCC diagnostic pop
845#if defined(OS_TRACE_RTOS_MEMPOOL)
Ordered list of time stamp nodes.
const char * name(void) const
Get object name.
Double linked list node, with time stamp and thread.
Double linked list node, with thread reference.
Interrupts critical section RAII helper.
Standard allocator based on the RTOS system default memory manager.
void deallocate(value_type *addr, std::size_t elements) noexcept
Deallocate the number of memory blocks of type value_type.
Synchronised memory pool, using the default RTOS allocator.
memory_pool(std::size_t blocks, std::size_t block_size_bytes, const attributes &attr=initializer, const allocator_type &allocator=allocator_type())
Construct a memory pool object instance.
void * timed_alloc(clock::duration_t timeout)
Allocate a memory block with timeout.
void * try_alloc(void)
Try to allocate a memory block.
void * alloc(void)
Allocate a memory block.
result_t free(void *block)
Free the memory block.
result_t reset(void)
Reset the memory pool.
virtual ~memory_pool()
Destruct the memory pool object instance.
POSIX compliant thread, using the default RTOS allocator.
int printf(const char *format,...)
Write a formatted string to the trace device.
port::clock::duration_t duration_t
Type of variables holding clock durations.
port::clock::timestamp_t timestamp_t
Type of variables holding clock time stamps.
clock_systick sysclock
The system clock object instance.
uint16_t size_t
Type of memory pool size storage.
static const attributes initializer
Default memory pool initialiser.
bool in_handler_mode(void)
Check if the CPU is in handler mode.
@ ok
Function completed; no errors or events occurred.
bool locked(void)
Check if the scheduler is locked.
thread & thread(void)
Get the current running thread.
uint32_t result_t
Type of values returned by RTOS functions.
#define os_assert_throw(__e, __er)
Assert or throw a system error exception.
#define os_assert_err(__e, __er)
Assert or return an error.
Single file µOS++ RTOS definitions.