-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy patherpc_setup_mbf_static_fixed.h
More file actions
114 lines (106 loc) · 3.29 KB
/
erpc_setup_mbf_static_fixed.h
File metadata and controls
114 lines (106 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright (c) 2014-2016, Freescale Semiconductor, Inc.
* Copyright 2016-2020 NXP
* Copyright 2021 ACRIOS Systems s.r.o.
* Copyright 2021 DroidDrive GmbH
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef _ERPC_SETUP_MBF_STATIC_FIXED_H_
#define _ERPC_SETUP_MBF_STATIC_FIXED_H_
#include "erpc_config_internal.h"
#include "erpc_manually_constructed.h"
#include "erpc_mbf_setup.h"
#include "erpc_message_buffer.h"
#include <assert.h>
#include <string.h>
#include <array>
using namespace erpc;
////////////////////////////////////////////////////////////////////////////////
// Classes
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Static Message buffer factory
*/
template <size_t BUFFER_COUNT, size_t WORD_COUNT, class WORD_SIZE=uint8_t>
class StaticFixedMessageBufferFactory : public MessageBufferFactory
{
private:
static constexpr std::array<bool, BUFFER_COUNT> init_free_buffer_bitmap() {
std::array<bool, BUFFER_COUNT> a{};
for(size_t i = 0; i < BUFFER_COUNT; i++) {
a[i] = true;
}
return a;
};
template<typename T>
static constexpr void init_buffer(T* b) {
for(size_t j = 0; j < WORD_COUNT; j++){
b[j] = T{0};
}
return;
};
static constexpr std::array<std::array<WORD_SIZE, WORD_COUNT>, BUFFER_COUNT> init_buffers() {
std::array<std::array<WORD_SIZE, WORD_COUNT>, BUFFER_COUNT> bufs = {{}};
for(size_t i = 0; i < BUFFER_COUNT; i++) {
init_buffer(bufs[i].data());
}
return bufs;
};
public:
/*!
* @brief Constructor.
*/
StaticFixedMessageBufferFactory(void) {}
/*!
* @brief This function creates new message buffer.
*
* @return MessageBuffer New created MessageBuffer.
*/
virtual MessageBuffer create(void)
{
WORD_SIZE* bufPtr = nullptr;
/// check free buffers and return first free one
for(size_t i = 0; i < BUFFER_COUNT; i++){
bool free = free_buffer_bitmap_[i];
if(free)
{
free_buffer_bitmap_[i] = false;
bufPtr = static_cast<WORD_SIZE*>(buffers_[i].data());
break;
}
}
// if free buffer found, return that
assert(nullptr != bufPtr);
uint8_t* buf = static_cast<uint8_t*>(bufPtr);
return MessageBuffer(buf, WORD_COUNT);
}
/*!
* @brief This function disposes message buffer.
*
* @param[in] buf MessageBuffer to dispose.
*/
virtual void dispose(MessageBuffer *buf)
{
assert(buf);
uint8_t* bufPtr = buf->get();
if (bufPtr != nullptr)
{
// check buf ptr against all current buffers to see which one it was
for(size_t i = 0; i < BUFFER_COUNT; i++)
{
uint8_t* internalBufPtr = static_cast<uint8_t*>(buffers_[i].data());
if(bufPtr == internalBufPtr){
free_buffer_bitmap_[i] = true;
init_buffer(bufPtr);
}
}
}
}
protected:
static inline auto free_buffer_bitmap_ = init_free_buffer_bitmap();
static inline auto buffers_ = init_buffers();
};
#endif // _ERPC_SETUP_MBF_STATIC_FIXED_H_