StateOS
Loading...
Searching...
No Matches
osmemorypool.h
1/******************************************************************************
2
3 @file StateOS: osmemorypool.h
4 @author Rajmund Szymanski
5 @date 17.11.2025
6 @brief This file contains definitions for StateOS.
7
8 ******************************************************************************
9
10 Copyright (c) 2018-2022 Rajmund Szymanski. All rights reserved.
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to
14 deal in the Software without restriction, including without limitation the
15 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 sell copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included
20 in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 IN THE SOFTWARE.
29
30 ******************************************************************************/
31
32#ifndef __STATEOS_MEM_H
33#define __STATEOS_MEM_H
34
35#include "oskernel.h"
36#include "osclock.h"
37#include "oslist.h"
38
44
45/* -------------------------------------------------------------------------- */
46
47#define MEM_SIZE( size ) \
48 ALIGNED_SIZE( size, sizeof(que_t) )
49
53
54typedef struct __mem mem_t;
55
56struct __mem
57{
58 lst_t lst;
59
60 unsigned limit;
61 unsigned size;
62 que_t * data;
63};
64
65typedef struct __mem mem_id [];
66
67/******************************************************************************
68 *
69 * Name : _MEM_INIT
70 *
71 * Description : create and initialize a memory pool object
72 *
73 * Parameters
74 * limit : size of a buffer (max number of objects)
75 * size : size of memory object (in sizeof(que_t) units)
76 * data : memory pool data buffer
77 *
78 * Return : memory pool object
79 *
80 * Note : for internal use
81 *
82 ******************************************************************************/
83
84#define _MEM_INIT( _limit, _size, _data ) { _LST_INIT(), _limit, _size, _data }
85
86/******************************************************************************
87 *
88 * Name : _MEM_DATA
89 *
90 * Description : create a memory pool data buffer
91 *
92 * Parameters
93 * limit : size of a buffer (max number of objects)
94 * size : size of memory object (in sizeof(que_t) units)
95 *
96 * Return : memory pool data buffer
97 *
98 * Note : for internal use
99 *
100 ******************************************************************************/
101
102#ifndef __cplusplus
103#define _MEM_DATA( _limit, _size ) (que_t[_limit * (1 + _size)]){ { NULL } }
104#endif
105
114#define OS_MEM_BUFFER( buf, limit, size ) \
115 que_t buf[limit * (1 + MEM_SIZE(size))]
116
125#define OS_MEM( mem, limit, size ) \
126 static que_t mem##__buf[limit * (1 + MEM_SIZE(size))]; \
127 mem_t mem[] = { _MEM_INIT( limit, MEM_SIZE(size), mem##__buf ) }
128
129/** static alias of OS_MEM() */
130#define static_MEM( mem, limit, size ) \
131 static que_t mem##__buf[limit * (1 + MEM_SIZE(size))]; \
132 static mem_t mem[] = { _MEM_INIT( limit, MEM_SIZE(size), mem##__buf ) }
133
144
145#ifndef __cplusplus
146#define MEM_INIT( limit, size ) \
147 _MEM_INIT( limit, MEM_SIZE(size), _MEM_DATA(limit, MEM_SIZE(size)) )
148#endif
149
160
161#ifndef __cplusplus
162#define MEM_CREATE( limit, size ) \
163 { MEM_INIT ( limit, size ) }
164/** alias of MEM_CREATE() */
165#define MEM_NEW \
166 MEM_CREATE
167#endif
168
169#ifdef __cplusplus
170extern "C" {
171#endif
172
184void mem_init( mem_t *mem, size_t size, que_t *data, size_t bufsize );
185
198mem_t *mem_create( unsigned limit, size_t size );
199
201__STATIC_INLINE
202mem_t *mem_new( unsigned limit, size_t size ) { return mem_create(limit, size); }
203
212void mem_reset( mem_t *mem );
213
215__STATIC_INLINE
216void mem_kill( mem_t *mem ) { mem_reset(mem); }
217
226void mem_destroy( mem_t *mem );
227
229__STATIC_INLINE
230void mem_delete( mem_t *mem ) { mem_destroy(mem); }
231
246int mem_take( mem_t *mem, void **data );
247
249__STATIC_INLINE
250int mem_tryWait( mem_t *mem, void **data ) { return mem_take(mem, data); }
251
253__STATIC_INLINE
254int mem_takeISR( mem_t *mem, void **data ) { return mem_take(mem, data); }
255
274int mem_waitFor( mem_t *mem, void **data, cnt_t delay );
275
292int mem_waitUntil( mem_t *mem, void **data, cnt_t time );
293
307
308__STATIC_INLINE
309int mem_wait( mem_t *mem, void **data ) { return mem_waitFor(mem, data, INFINITE); }
310
321void mem_give( mem_t *mem, void *data );
322
324__STATIC_INLINE
325void mem_giveISR( mem_t *mem, void *data ) { mem_give(mem, data); }
326
327#ifdef __cplusplus
328}
329#endif
330
331/* -------------------------------------------------------------------------- */
332
333#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
334namespace stateos {
335
343
344template<unsigned limit_, size_t size_>
345struct MemoryPoolT : public __mem
346{
348 MemoryPoolT(): __mem _MEM_INIT(limit_, MEM_SIZE(size_), data_) {}
349
350 ~MemoryPoolT() { assert(__mem::lst.obj.queue == nullptr); }
351
352 /// move constructor
353 MemoryPoolT( MemoryPoolT&& _src ): __mem _MEM_INIT(limit_, MEM_SIZE(size_), data_)
354 {
355 assert(_src.limit == limit_ && _src.lst.obj.queue == nullptr); // the pool has not been used (the buffer is bound at the first use)
356 (void) _src;
357 }
358
359 MemoryPoolT( const MemoryPoolT& ) = delete;
360 MemoryPoolT& operator=( MemoryPoolT&& ) = delete;
361 MemoryPoolT& operator=( const MemoryPoolT& ) = delete;
363 void reset () { mem_reset (this); }
364 void kill () { mem_kill (this); }
365 void destroy () { mem_destroy (this); }
366 int take ( void **_data ) { return mem_take (this, _data); }
367 int tryWait ( void **_data ) { return mem_tryWait (this, _data); }
368 int takeISR ( void **_data ) { return mem_takeISR (this, _data); }
369 template<typename T>
370 int waitFor ( void **_data, const T& _delay ) { return mem_waitFor (this, _data, Clock::count(_delay)); }
371 template<typename T>
372 int waitUntil( void **_data, const T& _time ) { return mem_waitUntil(this, _data, Clock::until(_time)); }
373 int wait ( void **_data ) { return mem_wait (this, _data); }
374 void give ( void *_data ) { mem_give (this, _data); }
375 void giveISR ( void *_data ) { mem_giveISR (this, _data); }
376
377#if __cplusplus >= 201402L
378 using Ptr = std::unique_ptr<MemoryPoolT<limit_, size_>>;
379#else
381#endif
382
394
395 static
396 Ptr Create()
397 {
398 auto mem = new (std::nothrow) MemoryPoolT<limit_, size_>();
399 if (mem != nullptr)
400 mem->__mem::lst.obj.res = mem;
401 return Ptr(mem);
402 }
403
404 private:
405 que_t data_[limit_ * (1 + MEM_SIZE(size_))];
406};
407
415
416template<unsigned limit_, class C>
417struct MemoryPoolTT : public MemoryPoolT<limit_, sizeof(C)>
418{
420 MemoryPoolTT(): MemoryPoolT<limit_, sizeof(C)>() {}
422 int take ( C **_data ) { return mem_take (this, reinterpret_cast<void **>(_data)); }
423 int tryWait ( C **_data ) { return mem_tryWait (this, reinterpret_cast<void **>(_data)); }
424 int takeISR ( C **_data ) { return mem_takeISR (this, reinterpret_cast<void **>(_data)); }
425 template<typename T>
426 int waitFor ( C **_data, const T& _delay ) { return mem_waitFor (this, reinterpret_cast<void **>(_data), Clock::count(_delay)); }
427 template<typename T>
428 int waitUntil( C **_data, const T& _time ) { return mem_waitUntil(this, reinterpret_cast<void **>(_data), Clock::until(_time)); }
429 int wait ( C **_data ) { return mem_wait (this, reinterpret_cast<void **>(_data)); }
430
431#if __cplusplus >= 201402L
432 using Ptr = std::unique_ptr<MemoryPoolTT<limit_, C>>;
433#else
434 using Ptr = MemoryPoolTT<limit_, C> *;
435#endif
436
448
449 static
450 Ptr Create()
451 {
452 auto mem = new (std::nothrow) MemoryPoolTT<limit_, C>();
453 if (mem != nullptr)
454 mem->__mem::lst.obj.res = mem;
455 return Ptr(mem);
456 }
457
458};
459
460} // namespace
461#endif//__cplusplus
462
463/* -------------------------------------------------------------------------- */
464
466
467#endif//__STATEOS_MEM_H
#define INFINITE
infinite waiting
Definition osbase.h:261
void mem_kill(mem_t *mem)
alias of mem_reset()
Definition osmemorypool.h:215
void mem_delete(mem_t *mem)
alias of mem_destroy()
Definition osmemorypool.h:229
void mem_give(mem_t *mem, void *data)
transfer memory object to the memory pool object,
int mem_takeISR(mem_t *mem, void **data)
ISR alias of mem_take().
Definition osmemorypool.h:253
int mem_take(mem_t *mem, void **data)
try to get memory object from the memory pool object, don't wait if the memory pool object is empty
int mem_tryWait(mem_t *mem, void **data)
alias of mem_take()
Definition osmemorypool.h:249
int mem_waitUntil(mem_t *mem, void **data, cnt_t time)
try to get memory object from the memory pool object, wait until given timepoint while the memory poo...
int mem_wait(mem_t *mem, void **data)
try to get memory object from the memory pool object, wait indefinitely while the memory pool object ...
Definition osmemorypool.h:308
int mem_waitFor(mem_t *mem, void **data, cnt_t delay)
try to get memory object from the memory pool object, wait for given duration of time while the memor...
void mem_init(mem_t *mem, size_t size, que_t *data, size_t bufsize)
initialize a memory pool object
void mem_giveISR(mem_t *mem, void *data)
ISR alias of mem_give().
Definition osmemorypool.h:324
void mem_reset(mem_t *mem)
reset the memory pool object and wake up all waiting tasks with 'E_STOPPED' event value
void mem_destroy(mem_t *mem)
reset the memory pool object, wake up all waiting tasks with 'E_DELETED' event value and free allocat...
mem_t * mem_new(unsigned limit, size_t size)
alias of mem_create()
Definition osmemorypool.h:201
mem_t * mem_create(unsigned limit, size_t size)
create and initialize a new memory pool object
char data[]
message data
Definition osmessagequeue.h:54
static constexpr rep count(const std::chrono::duration< _Rep, _Period > &_delay)
delay in ticks, rounded up to whole ticks; negative delay: IMMEDIATE, delay out of range: INFINITE
Definition osclock.h:109
static constexpr rep until(const std::chrono::time_point< Clock, _Duration > &_time)
time point in ticks, rounded up to whole ticks (only a time point of this clock)
Definition osclock.h:116
MemoryPoolTT()
constructor, see the constructor parameters of the class
Definition osmemorypool.h:419
static Ptr Create()
create dynamic object with manageable resources
Definition osmemorypool.h:449
int takeISR(C **_data)
mem_takeISR()
Definition osmemorypool.h:423
int waitUntil(C **_data, const T &_time)
mem_waitUntil()
Definition osmemorypool.h:427
int wait(C **_data)
mem_wait()
Definition osmemorypool.h:428
int take(C **_data)
mem_take()
Definition osmemorypool.h:421
std::unique_ptr< MemoryPoolTT< limit_, C > > Ptr
result type of Create()
Definition osmemorypool.h:431
int tryWait(C **_data)
mem_tryWait()
Definition osmemorypool.h:422
int waitFor(C **_data, const T &_delay)
mem_waitFor()
Definition osmemorypool.h:425
create and initialize a memory pool object
Definition osmemorypool.h:345
void give(void *_data)
mem_give()
Definition osmemorypool.h:373
int take(void **_data)
mem_take()
Definition osmemorypool.h:365
static Ptr Create()
create dynamic object with manageable resources
Definition osmemorypool.h:395
int takeISR(void **_data)
mem_takeISR()
Definition osmemorypool.h:367
void kill()
mem_kill()
Definition osmemorypool.h:363
void destroy()
mem_destroy()
Definition osmemorypool.h:364
int waitUntil(void **_data, const T &_time)
mem_waitUntil()
Definition osmemorypool.h:371
int tryWait(void **_data)
mem_tryWait()
Definition osmemorypool.h:366
std::unique_ptr< MemoryPoolT< limit_, size_ > > Ptr
result type of Create()
Definition osmemorypool.h:377
int waitFor(void **_data, const T &_delay)
mem_waitFor()
Definition osmemorypool.h:369
int wait(void **_data)
mem_wait()
Definition osmemorypool.h:372
void giveISR(void *_data)
mem_giveISR()
Definition osmemorypool.h:374
MemoryPoolT()
constructor, see the constructor parameters of the class
Definition osmemorypool.h:347
void reset()
mem_reset()
Definition osmemorypool.h:362