[ JIRA Id - ODUHIGH-606 ] Fixing null memory size allocation issue
[o-du/l2.git] / src / 5gnrmac / mac_utils.h
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /* Contains common utility definitions to be used at MAC */
20
21 #define MAX_SHORT_BSR_TABLE_ENTRIES 32
22 #define MAX_LONG_BSR_TABLE_ENTRIES  256
23 #define MAC_MEM_REGION   4
24 #define MAC_POOL         1
25
26 /* allocate and zero out a static buffer */
27
28 #ifdef MEM_SIZE_CHECK
29 #define MAC_MEMORY_ALLOC_SIZE_LOG(_line, _func, _size) \
30 {\
31    DU_LOG("\n MAC line = %d, func = %s, _size= %d ", _line, _func, _size); \
32 }
33 #else
34 #define MAC_MEMORY_ALLOC_SIZE_LOG(_line, _func, _size) {}
35 #endif
36
37 #ifdef ODU_MEMORY_DEBUG_LOG
38 #define MAC_MEM_LOG(_macro, _file, _line, _func, _size, _datPtr)\
39 {\
40    printf("\n%s,=== %s +%d, %s, %lu, %p \n",           \
41          _macro, _file, _line, _func, (uint64_t)_size, _datPtr); \
42 }
43 #else
44 #define MAC_MEM_LOG(_macro, _file, _line, _func, _size, _dataPtr) {}
45 #endif
46
47 /* allocate and zero out a MAC static buffer */
48 #define MAC_ALLOC(_datPtr, _size)                            \
49 {                                                            \
50    MAC_MEMORY_ALLOC_SIZE_LOG(__LINE__, __FUNCTION__, _size);      \
51    uint8_t _ret;                                             \
52    _ret = SGetSBuf(MAC_MEM_REGION, MAC_POOL,                 \
53          (Data **)&_datPtr, _size);                               \
54    if(_ret == ROK)                                           \
55    {                                                         \
56       memset(_datPtr, 0, _size);                             \
57       MAC_MEM_LOG("MAC,ALLOC", __FILE__, __LINE__, __FUNCTION__, _size, _datPtr);\
58    }                                                         \
59    else                                                      \
60    {                                                         \
61       _datPtr = NULLP;                                       \
62    }                                                         \
63 }
64
65 /* free a static buffer */
66 #define MAC_FREE(_datPtr, _size)                             \
67 {                                                            \
68    if(_datPtr)                                               \
69    {                                                         \
70       MAC_MEM_LOG("MAC,FREE", __FILE__, __LINE__, __FUNCTION__, _size, _datPtr);\
71       SPutSBuf(MAC_MEM_REGION, MAC_POOL,                     \
72             (Data *)_datPtr, _size);                              \
73       _datPtr = NULLP;                                       \
74    }                                                         \
75 }
76
77 /* Allocate shared memory to be used for LWLC
78  * during inter-layer communication */
79 #define MAC_ALLOC_SHRABL_BUF(_buf, _size)                    \
80 {                                                            \
81    MAC_MEMORY_ALLOC_SIZE_LOG(__LINE__, __FUNCTION__, _size);      \
82    if(SGetStaticBuffer(MAC_MEM_REGION, MAC_POOL,             \
83             (Data **)&_buf, (Size) _size, 0) == ROK)              \
84    {                                                         \
85       MAC_MEM_LOG("MAC,ALLOC_SHRABL_BUF", __FILE__, __LINE__, __FUNCTION__, _size, _buf);\
86       memset((_buf), 0, _size);                              \
87    }                                                         \
88    else                                                      \
89    {                                                         \
90       (_buf) = NULLP;                                        \
91    }                                                         \
92 }
93
94 /* Free shared memory, received through LWLC */
95 #define MAC_FREE_SHRABL_BUF(_region, _pool,_buf, _size)      \
96 {                                                            \
97    if (_buf != NULLP)                                        \
98    {                                                         \
99       MAC_MEM_LOG("MAC,FREE_SHRABL_BUF", __FILE__, __LINE__, __FUNCTION__, _size, _buf);\
100       (Void) SPutStaticBuffer(_region, _pool,                \
101             (Data *) _buf, (Size) _size, 0);                 \
102       _buf = NULLP;                                          \
103    }                                                         \
104 }
105
106 /* Fill Pst structure for sending msg from MAC to DU APP */
107 #define FILL_PST_MAC_TO_DUAPP(_pst, _event)                     \
108 {                                                           \
109    _pst.selector  = ODU_SELECTOR_LWLC;                      \
110    _pst.srcEnt    = ENTMAC;                                  \
111    _pst.dstEnt    = ENTDUAPP;                               \
112    _pst.dstInst   = 0;                                      \
113    _pst.srcInst   = macCb.macInst;                          \
114    _pst.dstProcId = macCb.procId;                           \
115    _pst.srcProcId = macCb.procId;                           \
116    _pst.region = MAC_MEM_REGION;                            \
117    _pst.pool = MAC_POOL;                                    \
118    _pst.event = _event;                                     \
119    _pst.route = 0;                                          \
120    _pst.prior = 0;                                          \
121    _pst.intfVer = 0;                                        \
122 }
123
124 /* Fill Pst structure for sending msg from MAC to SCH */
125 #define FILL_PST_MAC_TO_SCH(_pst, _event)                       \
126 {                                                           \
127    _pst.selector  = ODU_SELECTOR_TC;                        \
128    _pst.srcEnt    = ENTMAC;                                  \
129    _pst.dstEnt    = ENTMAC;                                  \
130    _pst.dstInst   = 1;                                      \
131    _pst.srcInst   = macCb.macInst;                          \
132    _pst.dstProcId = macCb.procId;                           \
133    _pst.srcProcId = macCb.procId;                           \
134    _pst.region = MAC_MEM_REGION;                            \
135    _pst.pool = MAC_POOL;                                    \
136    _pst.event = _event;                                     \
137    _pst.route = 0;                                          \
138    _pst.prior = 0;                                          \
139    _pst.intfVer = 0;                                        \
140 }
141
142 /* Fills Pst to send msg to RLC */
143 #define FILL_PST_MAC_TO_RLC(_pst, _dstInst, _event)        \
144 {                                                      \
145    pst.selector  = ODU_SELECTOR_LWLC;                  \
146    pst.srcEnt    = ENTMAC;                              \
147    pst.dstEnt    = ENTRLC;                              \
148    pst.dstInst   = _dstInst;                           \
149    pst.srcInst   = macCb.macInst;                      \
150    pst.dstProcId = macCb.procId;                       \
151    pst.srcProcId = macCb.procId;                       \
152    pst.region    = MAC_MEM_REGION;                     \
153    pst.pool      = MAC_POOL;                           \
154    pst.event     = _event;                             \
155    pst.route     = 0;                                  \
156    pst.prior     = 0;                                  \
157    pst.intfVer   = 0;                                  \
158 }
159
160 /* Function declaration */
161 extern uint32_t shortBsrBytesTable[MAX_SHORT_BSR_TABLE_ENTRIES];
162 extern uint32_t longBsrBytesTable[MAX_LONG_BSR_TABLE_ENTRIES];
163
164 /**********************************************************************
165          End of file
166 **********************************************************************/