IntrOS
Loading...
Searching...
No Matches
osmemorypool.h
1/******************************************************************************
2
3 @file IntrOS: osmemorypool.h
4 @author Rajmund Szymanski
5 @date 17.11.2025
6 @brief This file contains definitions for IntrOS.
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 __INTROS_MEM_H
33#define __INTROS_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
182void mem_init( mem_t *mem, size_t size, que_t *data, size_t bufsize );
183
192void mem_reset( mem_t *mem );
193
195__STATIC_INLINE
196void mem_kill( mem_t *mem ) { mem_reset(mem); }
197
209int mem_take( mem_t *mem, void **data );
210
212__STATIC_INLINE
213int mem_tryWait( mem_t *mem, void **data ) { return mem_take(mem, data); }
214
230int mem_waitFor( mem_t *mem, void **data, cnt_t delay );
231
245int mem_waitUntil( mem_t *mem, void **data, cnt_t time );
246
257
258__STATIC_INLINE
259int mem_wait( mem_t *mem, void **data ) { return mem_waitFor(mem, data, INFINITE); }
260
268void mem_give( mem_t *mem, void *data );
269
270#ifdef __cplusplus
271}
272#endif
273
274/* -------------------------------------------------------------------------- */
275
276#if defined(__cplusplus)
277namespace intros {
278
286
287template<unsigned limit_, size_t size_>
288struct MemoryPoolT : public __mem
289{
291 MemoryPoolT(): __mem _MEM_INIT(limit_, MEM_SIZE(size_), data_) {}
292
293 ~MemoryPoolT() { assert(__mem::lst.obj.queue == nullptr); }
294
295 /// move constructor
296 MemoryPoolT( MemoryPoolT&& _src ): __mem _MEM_INIT(limit_, MEM_SIZE(size_), data_)
297 {
298 assert(_src.limit == limit_ && _src.lst.obj.queue == nullptr); // the pool has not been used (the buffer is bound at the first use)
299 (void) _src;
300 }
301
302 MemoryPoolT( const MemoryPoolT& ) = delete;
303 MemoryPoolT& operator=( MemoryPoolT&& ) = delete;
304 MemoryPoolT& operator=( const MemoryPoolT& ) = delete;
306 void reset () { mem_reset (this); }
307 void kill () { mem_kill (this); }
308 int take ( void **_data ) { return mem_take (this, _data); }
309 int tryWait ( void **_data ) { return mem_tryWait (this, _data); }
310 template<typename T>
311 int waitFor ( void **_data, const T& _delay ) { return mem_waitFor (this, _data, Clock::count(_delay)); }
312 template<typename T>
313 int waitUntil( void **_data, const T& _time ) { return mem_waitUntil(this, _data, Clock::until(_time)); }
314 int wait ( void **_data ) { return mem_wait (this, _data); }
315 void give ( void *_data ) { mem_give (this, _data); }
316
317 private:
318 que_t data_[limit_ * (1 + MEM_SIZE(size_))];
319};
320
328
329template<unsigned limit_, class C>
330struct MemoryPoolTT : public MemoryPoolT<limit_, sizeof(C)>
331{
333 MemoryPoolTT(): MemoryPoolT<limit_, sizeof(C)>() {}
335 int take ( C **_data ) { return mem_take (this, reinterpret_cast<void **>(_data)); }
336 int tryWait ( C **_data ) { return mem_tryWait (this, reinterpret_cast<void **>(_data)); }
337 template<typename T>
338 int waitFor ( C **_data, const T& _delay ) { return mem_waitFor (this, reinterpret_cast<void **>(_data), Clock::count(_delay)); }
339 template<typename T>
340 int waitUntil( C **_data, const T& _time ) { return mem_waitUntil(this, reinterpret_cast<void **>(_data), Clock::until(_time)); }
341 int wait ( C **_data ) { return mem_wait (this, reinterpret_cast<void **>(_data)); }
342};
343
344} // namespace
345#endif//__cplusplus
346
347/* -------------------------------------------------------------------------- */
348
350
351#endif//__INTROS_MEM_H
#define INFINITE
infinite waiting
Definition osbase.h:250
void mem_kill(mem_t *mem)
alias of mem_reset()
Definition osmemorypool.h:195
void mem_give(mem_t *mem, void *data)
transfer memory object to the memory pool object,
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:212
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:258
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_reset(mem_t *mem)
reset the memory pool object and wake up all waiting tasks with 'E_STOPPED' event value
char data[]
message data
Definition osmessagequeue.h:54
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:109
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:102
MemoryPoolTT()
constructor, see the constructor parameters of the class
Definition osmemorypool.h:332
int wait(C **_data)
mem_wait()
Definition osmemorypool.h:340
int waitUntil(C **_data, const T &_time)
mem_waitUntil()
Definition osmemorypool.h:339
int waitFor(C **_data, const T &_delay)
mem_waitFor()
Definition osmemorypool.h:337
int tryWait(C **_data)
mem_tryWait()
Definition osmemorypool.h:335
int take(C **_data)
mem_take()
Definition osmemorypool.h:334
create and initialize a memory pool object
Definition osmemorypool.h:288
int wait(void **_data)
mem_wait()
Definition osmemorypool.h:313
void reset()
mem_reset()
Definition osmemorypool.h:305
int tryWait(void **_data)
mem_tryWait()
Definition osmemorypool.h:308
void kill()
mem_kill()
Definition osmemorypool.h:306
int waitUntil(void **_data, const T &_time)
mem_waitUntil()
Definition osmemorypool.h:312
int waitFor(void **_data, const T &_delay)
mem_waitFor()
Definition osmemorypool.h:310
void give(void *_data)
mem_give()
Definition osmemorypool.h:314
int take(void **_data)
mem_take()
Definition osmemorypool.h:307
MemoryPoolT()
constructor, see the constructor parameters of the class
Definition osmemorypool.h:290