Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modern static memory buffer with FIXED size #208

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions erpc_c/setup/erpc_mbf_setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,23 @@ typedef struct ErpcMessageBufferFactory *erpc_mbf_t;
// API
////////////////////////////////////////////////////////////////////////////////

#if __cplusplus > 201703L
#include "erpc_setup_mbf_static_fixed.h"

/*!
* @brief Create MessageBuffer factory which is using static fixed allocated buffers.
*/
template <size_t BUFFER_COUNT, size_t WORD_COUNT, class WORD_SIZE=uint8_t>
erpc_mbf_t erpc_mbf_static_fixed_init(void){
static ManuallyConstructed<StaticFixedMessageBufferFactory<BUFFER_COUNT, WORD_COUNT, WORD_SIZE>> s_msgFactory;
s_msgFactory.construct();
return reinterpret_cast<erpc_mbf_t>(s_msgFactory.get());
return nullptr;
}
#endif

#ifdef __cplusplus

extern "C" {
#endif

Expand Down
114 changes: 114 additions & 0 deletions erpc_c/setup/erpc_setup_mbf_static_fixed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,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_