IntrOS
Loading...
Searching...
No Matches
osrawbuffer.h
1/******************************************************************************
2
3 @file IntrOS: osrawbuffer.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_RAW_H
33#define __INTROS_RAW_H
34
35#include "oskernel.h"
36#include "osclock.h"
37
43
47
48typedef struct __raw raw_t;
49
50struct __raw
51{
52 obj_t obj;
53
54 size_t count;
55 size_t limit;
56
57 size_t head;
58 size_t tail;
59 char * data;
60};
61
62typedef struct __raw raw_id [];
63
64/******************************************************************************
65 *
66 * Name : _RAW_INIT
67 *
68 * Description : create and initialize a raw buffer object
69 *
70 * Parameters
71 * limit : size of a buffer (max number of stored bytes)
72 * data : raw buffer data
73 *
74 * Return : raw buffer object
75 *
76 * Note : for internal use
77 *
78 ******************************************************************************/
79
80#define _RAW_INIT( _limit, _data ) { _OBJ_INIT(), 0, _limit, 0, 0, _data }
81
82/******************************************************************************
83 *
84 * Name : _RAW_DATA
85 *
86 * Description : create a raw buffer data
87 *
88 * Parameters
89 * limit : size of a buffer (max number of stored bytes)
90 *
91 * Return : raw buffer data
92 *
93 * Note : for internal use
94 *
95 ******************************************************************************/
96
97#ifndef __cplusplus
98#define _RAW_DATA( _limit ) (char[_limit]){ 0 }
99#endif
100
101/******************************************************************************
102 *
103 * Name : _VA_RAW
104 *
105 * Description : calculate buffer size from optional parameter
106 *
107 * Note : for internal use
108 *
109 ******************************************************************************/
110
111#define _VA_RAW( _limit, _size ) ( (_size + 0) ? ((_limit) * (_size + 0)) : (_limit) )
112
121#define OS_RAW_BUFFER( buf, limit, ... ) \
122 char buf[_VA_RAW(limit, __VA_ARGS__)]
123
132#define OS_RAW( raw, limit, ... ) \
133 static char raw##__buf[_VA_RAW(limit, __VA_ARGS__)]; \
134 raw_t raw[] = { _RAW_INIT( _VA_RAW(limit, __VA_ARGS__), raw##__buf ) }
135
136/** static alias of OS_RAW() */
137#define static_RAW( raw, limit, ... ) \
138 static char raw##__buf[_VA_RAW(limit, __VA_ARGS__)]; \
139 static raw_t raw[] = { _RAW_INIT( _VA_RAW(limit, __VA_ARGS__), raw##__buf ) }
140
151
152#ifndef __cplusplus
153#define RAW_INIT( limit, ... ) \
154 _RAW_INIT( _VA_RAW(limit, __VA_ARGS__), _RAW_DATA( _VA_RAW(limit, __VA_ARGS__) ) )
155#endif
156
167
168#ifndef __cplusplus
169#define RAW_CREATE( limit, ... ) \
170 { RAW_INIT ( _VA_RAW(limit, __VA_ARGS__) ) }
171/** alias of RAW_CREATE() */
172#define RAW_NEW \
173 RAW_CREATE
174#endif
175
176#ifdef __cplusplus
177extern "C" {
178#endif
179
188void raw_init( raw_t *raw, void *data, size_t bufsize );
189
198void raw_reset( raw_t *raw );
199
201__STATIC_INLINE
202void raw_kill( raw_t *raw ) { raw_reset(raw); }
203
217int raw_take( raw_t *raw, void *data, size_t size, size_t *read );
218
220__STATIC_INLINE
221int raw_tryWait( raw_t *raw, void *data, size_t size, size_t *read ) { return raw_take(raw, data, size, read); }
222
240int raw_waitFor( raw_t *raw, void *data, size_t size, size_t *read, cnt_t delay );
241
257int raw_waitUntil( raw_t *raw, void *data, size_t size, size_t *read, cnt_t time );
258
271
272__STATIC_INLINE
273int raw_wait( raw_t *raw, void *data, size_t size, size_t *read ) { return raw_waitFor(raw, data, size, read, INFINITE); }
274
288int raw_give( raw_t *raw, const void *data, size_t size );
289
307int raw_sendFor( raw_t *raw, const void *data, size_t size, cnt_t delay );
308
324int raw_sendUntil( raw_t *raw, const void *data, size_t size, cnt_t time );
325
338
339__STATIC_INLINE
340int raw_send( raw_t *raw, const void *data, size_t size ) { return raw_sendFor(raw, data, size, INFINITE); }
341
354int raw_push( raw_t *raw, const void *data, size_t size );
355
364size_t raw_count( raw_t *raw );
365
374size_t raw_space( raw_t *raw );
375
384size_t raw_limit( raw_t *raw );
385
386#ifdef __cplusplus
387}
388#endif
389
390/* -------------------------------------------------------------------------- */
391
392#if defined(__cplusplus)
393namespace intros {
394
401
402template<size_t limit_>
403struct RawBufferT : public __raw
404{
406 constexpr
407 RawBufferT(): __raw _RAW_INIT(limit_, data_) {}
408
409 ~RawBufferT() { assert(__raw::obj.queue == nullptr); }
410
411 /// move constructor
412 RawBufferT( RawBufferT&& _src ): __raw _RAW_INIT(limit_, data_)
413 {
414 assert(_src.__raw::count == 0 && _src.obj.queue == nullptr);
415 (void) _src;
416 }
417
418 RawBufferT( const RawBufferT& ) = delete;
419 RawBufferT& operator=( RawBufferT&& ) = delete;
420 RawBufferT& operator=( const RawBufferT& ) = delete;
422 void reset () { raw_reset (this); }
423 void kill () { raw_kill (this); }
424 int take ( void *_data, size_t _size, size_t *_read = nullptr ) { return raw_take (this, _data, _size, _read); }
425 int tryWait ( void *_data, size_t _size, size_t *_read = nullptr ) { return raw_tryWait (this, _data, _size, _read); }
426 template<typename T>
427 int waitFor ( void *_data, size_t _size, size_t *_read, const T& _delay ) { return raw_waitFor (this, _data, _size, _read, Clock::count(_delay)); }
428 template<typename T>
429 int waitUntil( void *_data, size_t _size, size_t *_read, const T& _time ) { return raw_waitUntil(this, _data, _size, _read, Clock::until(_time)); }
430 int wait ( void *_data, size_t _size, size_t *_read = nullptr ) { return raw_wait (this, _data, _size, _read); }
431 int give ( const void *_data, size_t _size ) { return raw_give (this, _data, _size); }
432 template<typename T>
433 int sendFor ( const void *_data, size_t _size, const T& _delay ) { return raw_sendFor (this, _data, _size, Clock::count(_delay)); }
434 template<typename T>
435 int sendUntil( const void *_data, size_t _size, const T& _time ) { return raw_sendUntil(this, _data, _size, Clock::until(_time)); }
436 int send ( const void *_data, size_t _size ) { return raw_send (this, _data, _size); }
437 int push ( const void *_data, size_t _size ) { return raw_push (this, _data, _size); }
438 size_t count () { return raw_count (this); }
439 size_t space () { return raw_space (this); }
440 size_t limit () { return raw_limit (this); }
441
442 private:
443 char data_[limit_];
444};
445
453
454template<unsigned limit_, class C>
455struct RawBufferTT : public RawBufferT<limit_*sizeof(C)>
456{
458 constexpr
459 RawBufferTT(): RawBufferT<limit_*sizeof(C)>() {}
461 int take ( C *_data ) { return raw_take (this, _data, sizeof(C), nullptr); }
462 int tryWait ( C *_data ) { return raw_tryWait (this, _data, sizeof(C), nullptr); }
463 template<typename T>
464 int waitFor ( C *_data, const T& _delay ) { return raw_waitFor (this, _data, sizeof(C), nullptr, Clock::count(_delay)); }
465 template<typename T>
466 int waitUntil( C *_data, const T& _time ) { return raw_waitUntil(this, _data, sizeof(C), nullptr, Clock::until(_time)); }
467 int wait ( C *_data ) { return raw_wait (this, _data, sizeof(C), nullptr); }
468 int give ( const C *_data ) { return raw_give (this, _data, sizeof(C)); }
469 template<typename T>
470 int sendFor ( const C *_data, const T& _delay ) { return raw_sendFor (this, _data, sizeof(C), Clock::count(_delay)); }
471 template<typename T>
472 int sendUntil( const C *_data, const T& _time ) { return raw_sendUntil(this, _data, sizeof(C), Clock::until(_time)); }
473 int send ( const C *_data ) { return raw_send (this, _data, sizeof(C)); }
474 int push ( const C *_data ) { return raw_push (this, _data, sizeof(C)); }
475};
476
477} // namespace
478#endif//__cplusplus
479
480/* -------------------------------------------------------------------------- */
481
483
484#endif//__INTROS_RAW_H
#define INFINITE
infinite waiting
Definition osbase.h:250
char data[]
message data
Definition osmessagequeue.h:54
int raw_take(raw_t *raw, void *data, size_t size, size_t *read)
try to transfer data from the raw buffer object, don't wait if the raw buffer object is empty
int raw_sendFor(raw_t *raw, const void *data, size_t size, cnt_t delay)
try to transfer data to the raw buffer object, wait for given duration of time while the raw buffer o...
int raw_wait(raw_t *raw, void *data, size_t size, size_t *read)
try to transfer data from the raw buffer object, wait indefinitely while the raw buffer object is emp...
Definition osrawbuffer.h:272
size_t raw_limit(raw_t *raw)
return the size of the raw buffer
void raw_reset(raw_t *raw)
reset the raw buffer object and wake up all waiting tasks with 'E_STOPPED' event value
int raw_tryWait(raw_t *raw, void *data, size_t size, size_t *read)
alias of raw_take()
Definition osrawbuffer.h:220
int raw_waitUntil(raw_t *raw, void *data, size_t size, size_t *read, cnt_t time)
try to transfer data from the raw buffer object, wait until given timepoint while the raw buffer obje...
int raw_give(raw_t *raw, const void *data, size_t size)
try to transfer data to the raw buffer object, don't wait if the raw buffer object is full
size_t raw_space(raw_t *raw)
return the amount of free space in the raw buffer
int raw_push(raw_t *raw, const void *data, size_t size)
try to transfer data to the raw buffer object, remove the oldest data if the raw buffer object is ful...
int raw_sendUntil(raw_t *raw, const void *data, size_t size, cnt_t time)
try to transfer data to the raw buffer object, wait until given timepoint while the raw buffer object...
size_t raw_count(raw_t *raw)
return the amount of data contained in the raw buffer
int raw_waitFor(raw_t *raw, void *data, size_t size, size_t *read, cnt_t delay)
try to transfer data from the raw buffer object, wait for given duration of time while the raw buffer...
void raw_init(raw_t *raw, void *data, size_t bufsize)
initialize a raw buffer object
void raw_kill(raw_t *raw)
alias of raw_reset()
Definition osrawbuffer.h:201
int raw_send(raw_t *raw, const void *data, size_t size)
try to transfer data to the raw buffer object, wait indefinitely while the raw buffer object is full
Definition osrawbuffer.h:339
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
int sendFor(const C *_data, const T &_delay)
raw_sendFor()
Definition osrawbuffer.h:469
int send(const C *_data)
raw_send()
Definition osrawbuffer.h:472
int waitFor(C *_data, const T &_delay)
raw_waitFor()
Definition osrawbuffer.h:463
int push(const C *_data)
raw_push()
Definition osrawbuffer.h:473
int take(C *_data)
raw_take()
Definition osrawbuffer.h:460
int give(const C *_data)
raw_give()
Definition osrawbuffer.h:467
constexpr RawBufferTT()
constructor, see the constructor parameters of the class
Definition osrawbuffer.h:458
int sendUntil(const C *_data, const T &_time)
raw_sendUntil()
Definition osrawbuffer.h:471
int waitUntil(C *_data, const T &_time)
raw_waitUntil()
Definition osrawbuffer.h:465
int tryWait(C *_data)
raw_tryWait()
Definition osrawbuffer.h:461
int wait(C *_data)
raw_wait()
Definition osrawbuffer.h:466
create and initialize a raw buffer object
Definition osrawbuffer.h:403
int waitFor(void *_data, size_t _size, size_t *_read, const T &_delay)
raw_waitFor()
Definition osrawbuffer.h:426
void reset()
raw_reset()
Definition osrawbuffer.h:421
int waitUntil(void *_data, size_t _size, size_t *_read, const T &_time)
raw_waitUntil()
Definition osrawbuffer.h:428
int take(void *_data, size_t _size, size_t *_read=nullptr)
raw_take()
Definition osrawbuffer.h:423
int give(const void *_data, size_t _size)
raw_give()
Definition osrawbuffer.h:430
void kill()
raw_kill()
Definition osrawbuffer.h:422
constexpr RawBufferT()
constructor, see the constructor parameters of the class
Definition osrawbuffer.h:406
int wait(void *_data, size_t _size, size_t *_read=nullptr)
raw_wait()
Definition osrawbuffer.h:429
int send(const void *_data, size_t _size)
raw_send()
Definition osrawbuffer.h:435
int push(const void *_data, size_t _size)
raw_push()
Definition osrawbuffer.h:436
size_t count()
raw_count()
Definition osrawbuffer.h:437
int sendUntil(const void *_data, size_t _size, const T &_time)
raw_sendUntil()
Definition osrawbuffer.h:434
int tryWait(void *_data, size_t _size, size_t *_read=nullptr)
raw_tryWait()
Definition osrawbuffer.h:424
size_t space()
raw_space()
Definition osrawbuffer.h:438
size_t limit()
raw_limit()
Definition osrawbuffer.h:439
int sendFor(const void *_data, size_t _size, const T &_delay)
raw_sendFor()
Definition osrawbuffer.h:432