1 /*------------------------------------------------------------------------------------------------*/
2 /* UNICENS V2.1.0-3491 */
3 /* Copyright (c) 2017 Microchip Technology Germany II GmbH & Co. KG. */
5 /* This program is free software: you can redistribute it and/or modify */
6 /* it under the terms of the GNU General Public License as published by */
7 /* the Free Software Foundation, either version 2 of the License, or */
8 /* (at your option) any later version. */
10 /* This program is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU General Public License for more details. */
15 /* You should have received a copy of the GNU General Public License */
16 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* You may also obtain this software under a propriety license from Microchip. */
19 /* Please contact Microchip for further information. */
20 /*------------------------------------------------------------------------------------------------*/
24 * \brief Implementation of message pool class
26 * \cond UCS_INTERNAL_DOC
31 /*------------------------------------------------------------------------------------------------*/
33 /*------------------------------------------------------------------------------------------------*/
36 #include "ucs_trace.h"
38 /*------------------------------------------------------------------------------------------------*/
39 /* Internal constants */
40 /*------------------------------------------------------------------------------------------------*/
42 /*------------------------------------------------------------------------------------------------*/
44 /*------------------------------------------------------------------------------------------------*/
45 /*! \brief Constructor of message pool class
46 * \param self The instance
47 * \param messages Reference to an array of message objects
48 * \param size Number of message objects the \c messages array is comprising.
49 * \param ucs_user_ptr User reference that needs to be passed in every callback function
51 void Pool_Ctor(CPool *self, CMessage messages[], uint16_t size, void *ucs_user_ptr)
55 MISC_MEM_SET(self, 0, sizeof(*self));
56 self->ucs_user_ptr = ucs_user_ptr;
57 self->initial_size = size;
58 self->messages = messages;
60 Dl_Ctor(&self->message_list, self->ucs_user_ptr);
62 for (index = 0U; index < size; index++)
64 Msg_Ctor(&messages[index]);
65 Msg_SetPoolReference(&messages[index], self);
66 Dl_InsertTail(&self->message_list, Msg_GetNode(&messages[index]));
70 /*! \brief Retrieves a message object from the pool
71 * \param self The instance
72 * \return Reference to the CMessage structure if a message is available.
75 CMessage* Pool_GetMsg(CPool *self)
78 CDlNode *node = Dl_PopHead(&self->message_list);
82 msg = (CMessage*)node->data_ptr;
88 /*! \brief Returns a message object to the pool pre-assigned pool
89 * \param msg_ptr Reference to the message object which needs
90 * to be returned to the pool.
92 void Pool_ReturnMsg(CMessage *msg_ptr)
94 CPool *pool_ptr = (CPool*)Msg_GetPoolReference(msg_ptr);
98 TR_ASSERT(pool_ptr->ucs_user_ptr, "[POOL]", (Pool_GetCurrentSize(pool_ptr) < pool_ptr->initial_size));
99 Dl_InsertTail(&pool_ptr->message_list, Msg_GetNode(msg_ptr));
103 TR_ERROR((0U, "[POOL]", "Pool_ReturnMsg(): released msg_ptr=0x%p without pool reference", 1U, msg_ptr));
107 /*! \brief Retrieves the current number of available message objects in the pool
108 * \param self The instance
109 * \return The current number of available message objects in the pool
111 uint16_t Pool_GetCurrentSize(CPool *self)
113 uint16_t list_size = Dl_GetSize(&self->message_list);
123 /*------------------------------------------------------------------------------------------------*/
125 /*------------------------------------------------------------------------------------------------*/