1824b2739f90a95b358c484797a3adcc1eb89ce5
[apps/agl-service-unicens.git] / ucs2-lib / src / ucs_pool.c
1 /*------------------------------------------------------------------------------------------------*/
2 /* UNICENS V2.1.0-3491                                                                            */
3 /* Copyright (c) 2017 Microchip Technology Germany II GmbH & Co. KG.                              */
4 /*                                                                                                */
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.                                                            */
9 /*                                                                                                */
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.                                                   */
14 /*                                                                                                */
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/>.                          */
17 /*                                                                                                */
18 /* You may also obtain this software under a propriety license from Microchip.                    */
19 /* Please contact Microchip for further information.                                              */
20 /*------------------------------------------------------------------------------------------------*/
21
22 /*!
23  * \file
24  * \brief Implementation of message pool class
25  *
26  * \cond UCS_INTERNAL_DOC
27  * \addtogroup  G_POOL
28  * @{
29  */
30
31 /*------------------------------------------------------------------------------------------------*/
32 /* Includes                                                                                       */
33 /*------------------------------------------------------------------------------------------------*/
34 #include "ucs_pool.h"
35 #include "ucs_misc.h"
36 #include "ucs_trace.h"
37
38 /*------------------------------------------------------------------------------------------------*/
39 /* Internal constants                                                                             */
40 /*------------------------------------------------------------------------------------------------*/
41
42 /*------------------------------------------------------------------------------------------------*/
43 /* Implementation                                                                                 */
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
50  */
51 void Pool_Ctor(CPool *self, CMessage messages[], uint16_t size, void *ucs_user_ptr)
52 {
53     uint16_t index;
54
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;
59
60     Dl_Ctor(&self->message_list, self->ucs_user_ptr); 
61
62     for (index = 0U; index < size; index++)
63     {
64         Msg_Ctor(&messages[index]);
65         Msg_SetPoolReference(&messages[index], self);
66         Dl_InsertTail(&self->message_list, Msg_GetNode(&messages[index]));
67     }
68 }
69
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.
73  *          Otherwise \c NULL.
74  */
75 CMessage* Pool_GetMsg(CPool *self)
76 {
77     CMessage *msg = NULL;
78     CDlNode *node = Dl_PopHead(&self->message_list);
79
80     if (node != NULL)
81     {
82         msg = (CMessage*)node->data_ptr;
83     }
84
85     return msg;
86 }
87
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. 
91  */
92 void Pool_ReturnMsg(CMessage *msg_ptr)
93 {
94     CPool *pool_ptr = (CPool*)Msg_GetPoolReference(msg_ptr);
95
96     if (pool_ptr != NULL)
97     {
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));
100     }
101     else
102     {
103         TR_ERROR((0U, "[POOL]", "Pool_ReturnMsg(): released msg_ptr=0x%p without pool reference", 1U, msg_ptr));
104     }
105 }
106
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
110  */
111 uint16_t Pool_GetCurrentSize(CPool *self)
112 {
113     uint16_t list_size = Dl_GetSize(&self->message_list);
114
115     return list_size;
116 }
117
118 /*!
119  * @}
120  * \endcond
121  */
122
123 /*------------------------------------------------------------------------------------------------*/
124 /* End of file                                                                                    */
125 /*------------------------------------------------------------------------------------------------*/
126