Skip to content

Commit 99675ec

Browse files
Merge branch 'ACRIOS-Systems-feature/staticAllocation' into develop
2 parents 810916e + dab9fd1 commit 99675ec

40 files changed

Lines changed: 283 additions & 112 deletions

erpc_c/config/erpc_config.h

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Copyright (c) 2016, Freescale Semiconductor, Inc.
33
* Copyright 2016-2020 NXP
4-
* Copyright 2020 ACRIOS Systems s.r.o.
4+
* Copyright 2020-2021 ACRIOS Systems s.r.o.
55
* All rights reserved.
66
*
77
*
@@ -23,39 +23,77 @@
2323

2424
//! @name Threading model options
2525
//@{
26-
#define ERPC_THREADS_NONE (0) //!< No threads.
27-
#define ERPC_THREADS_PTHREADS (1) //!< POSIX pthreads.
28-
#define ERPC_THREADS_FREERTOS (2) //!< FreeRTOS.
29-
#define ERPC_THREADS_ZEPHYR (3) //!< ZEPHYR.
30-
#define ERPC_THREADS_MBED (4) //!< Mbed OS
31-
#define ERPC_THREADS_WIN32 (5) //!< WIN32
32-
#define ERPC_THREADS_THREADX (6) //!< THREADX
26+
#define ERPC_ALLOCATION_POLICY_DYNAMIC (0U) //!< Dynamic allocation policy
27+
#define ERPC_ALLOCATION_POLICY_STATIC (1U) //!< Static allocation policy
3328

34-
#define ERPC_NOEXCEPT_DISABLED (0) //!< Disabling noexcept feature.
35-
#define ERPC_NOEXCEPT_ENABLED (1) //!< Enabling noexcept feature.
29+
#define ERPC_THREADS_NONE (0U) //!< No threads.
30+
#define ERPC_THREADS_PTHREADS (1U) //!< POSIX pthreads.
31+
#define ERPC_THREADS_FREERTOS (2U) //!< FreeRTOS.
32+
#define ERPC_THREADS_ZEPHYR (3U) //!< ZEPHYR.
33+
#define ERPC_THREADS_MBED (4U) //!< Mbed OS
34+
#define ERPC_THREADS_WIN32 (5U) //!< WIN32
35+
#define ERPC_THREADS_THREADX (6U) //!< THREADX
3636

37-
#define ERPC_NESTED_CALLS_DISABLED (0) //!< No nested calls support.
38-
#define ERPC_NESTED_CALLS_ENABLED (1) //!< Nested calls support.
37+
#define ERPC_NOEXCEPT_DISABLED (0U) //!< Disabling noexcept feature.
38+
#define ERPC_NOEXCEPT_ENABLED (1U) //!< Enabling noexcept feature.
3939

40-
#define ERPC_NESTED_CALLS_DETECTION_DISABLED (0) //!< Nested calls detection disabled.
41-
#define ERPC_NESTED_CALLS_DETECTION_ENABLED (1) //!< Nested calls detection enabled.
40+
#define ERPC_NESTED_CALLS_DISABLED (0U) //!< No nested calls support.
41+
#define ERPC_NESTED_CALLS_ENABLED (1U) //!< Nested calls support.
4242

43-
#define ERPC_MESSAGE_LOGGING_DISABLED (0) //!< Trace functions disabled.
44-
#define ERPC_MESSAGE_LOGGING_ENABLED (1) //!< Trace functions enabled.
43+
#define ERPC_NESTED_CALLS_DETECTION_DISABLED (0U) //!< Nested calls detection disabled.
44+
#define ERPC_NESTED_CALLS_DETECTION_ENABLED (1U) //!< Nested calls detection enabled.
4545

46-
#define ERPC_TRANSPORT_MU_USE_MCMGR_DISABLED (0) //!< Do not use MCMGR for MU ISR management.
47-
#define ERPC_TRANSPORT_MU_USE_MCMGR_ENABLED (1) //!< Use MCMGR for MU ISR management.
46+
#define ERPC_MESSAGE_LOGGING_DISABLED (0U) //!< Trace functions disabled.
47+
#define ERPC_MESSAGE_LOGGING_ENABLED (1U) //!< Trace functions enabled.
4848

49-
#define ERPC_PRE_POST_ACTION_DISABLED (0) //!< Pre post shim callbacks functions disabled.
50-
#define ERPC_PRE_POST_ACTION_ENABLED (1) //!< Pre post shim callback functions enabled.
49+
#define ERPC_TRANSPORT_MU_USE_MCMGR_DISABLED (0U) //!< Do not use MCMGR for MU ISR management.
50+
#define ERPC_TRANSPORT_MU_USE_MCMGR_ENABLED (1U) //!< Use MCMGR for MU ISR management.
5151

52-
#define ERPC_PRE_POST_ACTION_DEFAULT_DISABLED (0) //!< Pre post shim default callbacks functions disabled.
53-
#define ERPC_PRE_POST_ACTION_DEFAULT_ENABLED (1) //!< Pre post shim default callback functions enabled.
52+
#define ERPC_PRE_POST_ACTION_DISABLED (0U) //!< Pre post shim callbacks functions disabled.
53+
#define ERPC_PRE_POST_ACTION_ENABLED (1U) //!< Pre post shim callback functions enabled.
54+
55+
#define ERPC_PRE_POST_ACTION_DEFAULT_DISABLED (0U) //!< Pre post shim default callbacks functions disabled.
56+
#define ERPC_PRE_POST_ACTION_DEFAULT_ENABLED (1U) //!< Pre post shim default callback functions enabled.
5457
//@}
5558

5659
//! @name Configuration options
5760
//@{
5861

62+
//! @def ERPC_ALLOCATION_POLICY
63+
//!
64+
//! @brief Choose which allocation policy should be used.
65+
//!
66+
//! Set ERPC_ALLOCATION_POLICY_DYNAMIC if dynamic allocations should be used.
67+
//! Set ERPC_ALLOCATION_POLICY_STATIC if static allocations should be used.
68+
//!
69+
//! Default value is ERPC_ALLOCATION_POLICY_DYNAMIC or in case of FreeRTOS it can be auto-detected if __has_include() is supported
70+
//! by compiler. Uncomment comment bellow to use static allocation policy.
71+
//! In case of static implementation user need consider another values to set (ERPC_CODEC_COUNT,
72+
//! ERPC_MESSAGE_LOGGERS_COUNT, ERPC_CLIENTS_THREADS_AMOUNT).
73+
// #define ERPC_ALLOCATION_POLICY (ERPC_ALLOCATION_POLICY_STATIC)
74+
75+
//! @def ERPC_CODEC_COUNT
76+
//!
77+
//! @brief Set amount of codecs objects used simultaneously in case of ERPC_ALLOCATION_POLICY is set to
78+
//! ERPC_ALLOCATION_POLICY_STATIC. For example if client or server is used in one thread then 1. If both are used in one thread per
79+
//! each then 2, ... Default value 2.
80+
// #define ERPC_CODEC_COUNT (2U)
81+
82+
//! @def ERPC_MESSAGE_LOGGERS_COUNT
83+
//!
84+
//! @brief Set amount of message loggers objects used simultaneously in case of ERPC_ALLOCATION_POLICY is set to
85+
//! ERPC_ALLOCATION_POLICY_STATIC.
86+
//! For example if client or server is used in one thread then 1. If both are used in one thread per each then 2, ...
87+
//! For arbitrated client 1 is enough.
88+
//! Default value 0 (May not be used).
89+
// #define ERPC_MESSAGE_LOGGERS_COUNT (0U)
90+
91+
//! @def ERPC_CLIENTS_THREADS_AMOUNT
92+
//!
93+
//! @brief Set amount of client threads objects used in case of ERPC_ALLOCATION_POLICY is set to ERPC_ALLOCATION_POLICY_STATIC.
94+
//! Default value 1 (Most of current cases).
95+
// #define ERPC_CLIENTS_THREADS_AMOUNT (1U)
96+
5997
//! @def ERPC_THREADS
6098
//!
6199
//! @brief Select threading model.

erpc_c/infra/erpc_basic_codec.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
#include "erpc_basic_codec.h"
1212

13+
#include "erpc_manually_constructed.h"
14+
15+
#if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_DYNAMIC
16+
#include <new>
17+
#endif
1318
#include <cassert>
1419

1520
using namespace erpc;
@@ -370,3 +375,15 @@ void BasicCodec::readCallback(funPtr callbacks1, funPtr *callback2)
370375
// callbacks = callback directly
371376
*callback2 = callbacks1;
372377
}
378+
379+
ERPC_MANUALLY_CONSTRUCTED_ARRAY_STATIC(BasicCodec, s_basicCodecManual, ERPC_CODEC_COUNT);
380+
381+
Codec *BasicCodecFactory ::create()
382+
{
383+
ERPC_CREATE_NEW_OBJECT(BasicCodec, s_basicCodecManual, ERPC_CODEC_COUNT)
384+
}
385+
386+
void BasicCodecFactory ::dispose(Codec *codec)
387+
{
388+
ERPC_DESTROY_OBJECT(codec, s_basicCodecManual, ERPC_CODEC_COUNT)
389+
}

erpc_c/infra/erpc_basic_codec.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
#include "erpc_codec.h"
1515

16-
#include <new>
17-
1816
/*!
1917
* @addtogroup infra_codec
2018
* @{
@@ -385,14 +383,14 @@ class BasicCodecFactory : public CodecFactory
385383
*
386384
* @return Pointer to created codec.
387385
*/
388-
virtual Codec *create(void) override { return new (std::nothrow) BasicCodec; }
386+
virtual Codec *create(void) override;
389387

390388
/*!
391389
* @brief Dispose codec.
392390
*
393391
* @param[in] codec Codec to dispose.
394392
*/
395-
virtual void dispose(Codec *codec) override { delete codec; }
393+
virtual void dispose(Codec *codec) override;
396394
};
397395

398396
} // namespace erpc

erpc_c/infra/erpc_manually_constructed.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
#ifndef _EMBEDDED_RPC__MANUALLY_CONSTRUCTED_H_
1212
#define _EMBEDDED_RPC__MANUALLY_CONSTRUCTED_H_
13+
14+
#include "erpc_config_internal.h"
15+
1316
#include <new>
1417
#include <stdint.h>
1518

@@ -154,6 +157,14 @@ class ManuallyConstructed
154157
}
155158
}
156159

160+
/*!
161+
* @brief Returns information if object is free or is used.
162+
*
163+
* @return true Object is constructed and used.
164+
* @return false Object wasn't constructer or it is destructed and free.
165+
*/
166+
bool isUsed() { return m_isConstructed; }
167+
157168
protected:
158169
/*!
159170
* @brief Storage for the object.
@@ -170,6 +181,56 @@ class ManuallyConstructed
170181
bool m_isConstructed = false;
171182
};
172183

184+
#define ERPC_MANUALLY_CONSTRUCTED(class, variableName) static ManuallyConstructed<class> variableName
185+
#define ERPC_MANUALLY_CONSTRUCTED_ARRAY(class, variableName, dimension) \
186+
ERPC_MANUALLY_CONSTRUCTED(class, variableName)[dimension]
187+
188+
#if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_STATIC
189+
#define ERPC_MANUALLY_CONSTRUCTED_STATIC(class, variableName) ERPC_MANUALLY_CONSTRUCTED(class, variableName)
190+
#else
191+
#define ERPC_MANUALLY_CONSTRUCTED_STATIC(class, variableName)
192+
#endif
193+
194+
#if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_STATIC
195+
#define ERPC_MANUALLY_CONSTRUCTED_ARRAY_STATIC(class, variableName, dimension) \
196+
ERPC_MANUALLY_CONSTRUCTED_ARRAY(class, variableName, dimension)
197+
#else
198+
#define ERPC_MANUALLY_CONSTRUCTED_ARRAY_STATIC(class, variableName, dimension)
199+
#endif
200+
201+
#if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_DYNAMIC
202+
#define ERPC_CREATE_NEW_OBJECT(class, arrayOfObjects, numberOfObjects, ...) \
203+
return new (std::nothrow) class(__VA_ARGS__);
204+
205+
#define ERPC_DESTROY_OBJECT(object, ...) delete object;
206+
207+
#elif ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_STATIC
208+
#define ERPC_CREATE_NEW_OBJECT(class, arrayOfObjects, numberOfObjects, ...) \
209+
uint8_t objectsIterator; \
210+
class *ret = NULL; \
211+
for (objectsIterator = 0; objectsIterator < numberOfObjects; objectsIterator++) \
212+
{ \
213+
if (!arrayOfObjects[objectsIterator].isUsed()) \
214+
{ \
215+
arrayOfObjects[objectsIterator].construct(__VA_ARGS__); \
216+
ret = arrayOfObjects[objectsIterator].get(); \
217+
break; \
218+
} \
219+
} \
220+
return ret;
221+
222+
#define ERPC_DESTROY_OBJECT(object, arrayOfObjects, numberOfObjects) \
223+
uint8_t objectsIterator; \
224+
for (objectsIterator = 0; objectsIterator < numberOfObjects; objectsIterator++) \
225+
{ \
226+
if (object == arrayOfObjects[objectsIterator].get()) \
227+
{ \
228+
arrayOfObjects[objectsIterator].destroy(); \
229+
break; \
230+
} \
231+
}
232+
#endif
233+
173234
} // namespace erpc
174235

175236
/*! @} */

erpc_c/infra/erpc_message_loggers.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212
#include "erpc_message_loggers.h"
1313

14+
#include "erpc_manually_constructed.h"
15+
16+
#if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_DYNAMIC
1417
#include <new>
18+
#endif
1519

1620
using namespace erpc;
1721
using namespace std;
@@ -20,6 +24,8 @@ using namespace std;
2024
// Code
2125
////////////////////////////////////////////////////////////////////////////////
2226

27+
ERPC_MANUALLY_CONSTRUCTED_ARRAY_STATIC(MessageLogger, s_messageLoggersManual, ERPC_MESSAGE_LOGGERS_COUNT);
28+
2329
MessageLoggers::~MessageLoggers(void)
2430
{
2531
MessageLogger *logger;
@@ -28,7 +34,7 @@ MessageLoggers::~MessageLoggers(void)
2834
{
2935
logger = m_logger;
3036
m_logger = m_logger->getNext();
31-
delete logger;
37+
ERPC_DESTROY_OBJECT(logger, s_messageLoggersManual, ERPC_MESSAGE_LOGGERS_COUNT)
3238
}
3339
}
3440

@@ -40,7 +46,7 @@ bool MessageLoggers::addMessageLogger(Transport *transport)
4046

4147
if (transport != NULL)
4248
{
43-
logger = new (nothrow) MessageLogger(transport);
49+
logger = create(transport);
4450
if (logger != NULL)
4551
{
4652
if (m_logger == NULL)
@@ -84,4 +90,10 @@ erpc_status_t MessageLoggers::logMessage(MessageBuffer *msg)
8490

8591
return err;
8692
}
93+
94+
MessageLogger *MessageLoggers::create(Transport *transport)
95+
{
96+
ERPC_CREATE_NEW_OBJECT(MessageLogger, s_messageLoggersManual, ERPC_MESSAGE_LOGGERS_COUNT, transport)
97+
}
98+
8799
#endif /* ERPC_MESSAGE_LOGGING */

erpc_c/infra/erpc_message_loggers.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ class MessageLoggers
109109
* @return The eRPC status based on transport type.
110110
*/
111111
erpc_status_t logMessage(MessageBuffer *msg);
112+
113+
private:
114+
/**
115+
* @brief This function create new MessageLogger object
116+
*
117+
* @param[in] transport Transport used by MessageLogger.
118+
*
119+
* @return MessageLogger* Returns new MessageLogger object.
120+
*/
121+
MessageLogger *create(Transport *transport);
112122
};
113123

114124
} // namespace erpc

erpc_c/infra/erpc_transport_arbitrator.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
#include "erpc_transport_arbitrator.h"
1111

12-
#include "erpc_config_internal.h"
12+
#include "erpc_manually_constructed.h"
1313

1414
#include <cassert>
1515
#include <cstdio>
@@ -25,6 +25,9 @@ using namespace erpc;
2525
// Code
2626
////////////////////////////////////////////////////////////////////////////////
2727

28+
ERPC_MANUALLY_CONSTRUCTED_ARRAY_STATIC(TransportArbitrator::PendingClientInfo, s_pendingClientInfoArray,
29+
ERPC_CLIENTS_THREADS_AMOUNT);
30+
2831
TransportArbitrator::TransportArbitrator(void)
2932
: Transport()
3033
, m_sharedTransport(NULL)
@@ -169,6 +172,9 @@ erpc_status_t TransportArbitrator::clientReceive(client_token_t token)
169172
return kErpcStatus_Success;
170173
}
171174

175+
TransportArbitrator::PendingClientInfo *TransportArbitrator::createPendingClient(void){ ERPC_CREATE_NEW_OBJECT(
176+
TransportArbitrator::PendingClientInfo, s_pendingClientInfoArray, ERPC_CLIENTS_THREADS_AMOUNT) }
177+
172178
TransportArbitrator::PendingClientInfo *TransportArbitrator::addPendingClient(void)
173179
{
174180
Mutex::Guard lock(m_clientListMutex);
@@ -177,7 +183,7 @@ TransportArbitrator::PendingClientInfo *TransportArbitrator::addPendingClient(vo
177183
PendingClientInfo *info = NULL;
178184
if (!m_clientFreeList)
179185
{
180-
info = new PendingClientInfo();
186+
info = createPendingClient();
181187
}
182188
else
183189
{
@@ -241,7 +247,7 @@ void TransportArbitrator::freeClientList(PendingClientInfo *list)
241247
{
242248
temp = info;
243249
info = info->m_next;
244-
delete temp;
250+
ERPC_DESTROY_OBJECT(temp, s_pendingClientInfoArray, ERPC_CLIENTS_THREADS_AMOUNT)
245251
}
246252
}
247253

erpc_c/infra/erpc_transport_arbitrator.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,6 @@ class TransportArbitrator : public Transport
129129
*/
130130
virtual bool hasMessage(void) override;
131131

132-
protected:
133-
Transport *m_sharedTransport; //!< Transport being shared through this arbitrator.
134-
Codec *m_codec; //!< Codec used to read incoming message headers.
135-
136132
/*!
137133
* @brief Request info for a client trying to receive a response.
138134
*/
@@ -154,10 +150,21 @@ class TransportArbitrator : public Transport
154150
~PendingClientInfo(void);
155151
};
156152

153+
protected:
154+
Transport *m_sharedTransport; //!< Transport being shared through this arbitrator.
155+
Codec *m_codec; //!< Codec used to read incoming message headers.
156+
157157
PendingClientInfo *m_clientList; //!< Active client receive requests.
158158
PendingClientInfo *m_clientFreeList; //!< Unused client receive info structs.
159159
Mutex m_clientListMutex; //!< Mutex guarding the client active and free lists.
160160

161+
/*!
162+
* @brief Create a Pending Client object.
163+
*
164+
* @return PendingClientInfo* Return created object.
165+
*/
166+
PendingClientInfo *createPendingClient(void);
167+
161168
/*!
162169
* @brief This function adds pending client.
163170
*

0 commit comments

Comments
 (0)