Memory handling fixes
[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 MAC_MEM_REGION   4
23 #define MAC_POOL         1
24
25 #ifdef ODU_MEMORY_DEBUG_LOG
26 #define MAC_MEM_LOG(_macro, _file, _line, _func, _size, _datPtr)\
27 {\
28    printf("\n%s=== %s +%d, %s, %d, %p\n",           \
29       _macro, _file, _line, _func, _size, _datPtr); \
30 }
31 #else
32 #define MAC_MEM_LOG(_macro, _file, _line, _func, _size, _dataPtr) {}
33 #endif
34
35 /* allocate and zero out a MAC static buffer */
36 #define MAC_ALLOC(_datPtr, _size)                            \
37 {                                                            \
38    uint8_t _ret;                                             \
39    _ret = SGetSBuf(MAC_MEM_REGION, MAC_POOL,                 \
40          (Data **)&_datPtr, _size);                               \
41    if(_ret == ROK)                                           \
42    {                                                         \
43       memset(_datPtr, 0, _size);                             \
44       MAC_MEM_LOG("MAC_ALLOC", __FILE__, __LINE__, __FUNCTION__, _size, _datPtr);\
45    }                                                         \
46    else                                                      \
47    {                                                         \
48       _datPtr = NULLP;                                       \
49    }                                                         \
50 }
51
52 /* free a static buffer */
53 #define MAC_FREE(_datPtr, _size)                             \
54 {                                                            \
55    if(_datPtr)                                               \
56    {                                                         \
57       MAC_MEM_LOG("MAC_FREE", __FILE__, __LINE__, __FUNCTION__, _size, _datPtr);\
58       SPutSBuf(MAC_MEM_REGION, MAC_POOL,                     \
59             (Data *)_datPtr, _size);                              \
60       _datPtr = NULLP;                                       \
61    }                                                         \
62 }
63
64 /* Allocate shared memory to be used for LWLC
65  * during inter-layer communication */
66 #define MAC_ALLOC_SHRABL_BUF(_buf, _size)                    \
67 {                                                            \
68    if(SGetStaticBuffer(MAC_MEM_REGION, MAC_POOL,             \
69             (Data **)&_buf, (Size) _size, 0) == ROK)              \
70    {                                                         \
71       MAC_MEM_LOG("MAC_ALLOC_SHRABL_BUF", __FILE__, __LINE__, __FUNCTION__, _size, _buf);\
72       memset((_buf), 0, _size);                              \
73    }                                                         \
74    else                                                      \
75    {                                                         \
76       (_buf) = NULLP;                                        \
77    }                                                         \
78 }
79
80 /* Free shared memory, received through LWLC */
81 #define MAC_FREE_SHRABL_BUF(_region, _pool,_buf, _size)      \
82 {                                                            \
83    if (_buf != NULLP)                                        \
84    {                                                         \
85       MAC_MEM_LOG("MAC_FREE_SHRABL_BUF", __FILE__, __LINE__, __FUNCTION__, _size, _buf);\
86       (Void) SPutStaticBuffer(_region, _pool,                \
87             (Data *) _buf, (Size) _size, 0);                 \
88       _buf = NULLP;                                          \
89    }                                                         \
90 }
91
92 /* Fill Pst structure for sending msg from MAC to DU APP */
93 #define FILL_PST_MAC_TO_DUAPP(_pst, _event)                     \
94 {                                                           \
95    _pst.selector  = ODU_SELECTOR_LWLC;                      \
96    _pst.srcEnt    = ENTMAC;                                  \
97    _pst.dstEnt    = ENTDUAPP;                               \
98    _pst.dstInst   = 0;                                      \
99    _pst.srcInst   = macCb.macInst;                          \
100    _pst.dstProcId = macCb.procId;                           \
101    _pst.srcProcId = macCb.procId;                           \
102    _pst.region = MAC_MEM_REGION;                            \
103    _pst.pool = MAC_POOL;                                    \
104    _pst.event = _event;                                     \
105    _pst.route = 0;                                          \
106    _pst.prior = 0;                                          \
107    _pst.intfVer = 0;                                        \
108 }
109
110 /* Fill Pst structure for sending msg from MAC to SCH */
111 #define FILL_PST_MAC_TO_SCH(_pst, _event)                       \
112 {                                                           \
113    _pst.selector  = ODU_SELECTOR_TC;                        \
114    _pst.srcEnt    = ENTMAC;                                  \
115    _pst.dstEnt    = ENTMAC;                                  \
116    _pst.dstInst   = 1;                                      \
117    _pst.srcInst   = macCb.macInst;                          \
118    _pst.dstProcId = macCb.procId;                           \
119    _pst.srcProcId = macCb.procId;                           \
120    _pst.region = MAC_MEM_REGION;                            \
121    _pst.pool = MAC_POOL;                                    \
122    _pst.event = _event;                                     \
123    _pst.route = 0;                                          \
124    _pst.prior = 0;                                          \
125    _pst.intfVer = 0;                                        \
126 }
127
128 /* Fills Pst to send msg to RLC */
129 #define FILL_PST_MAC_TO_RLC(_pst, _dstInst, _event)        \
130 {                                                      \
131    pst.selector  = ODU_SELECTOR_LWLC;                  \
132    pst.srcEnt    = ENTMAC;                              \
133    pst.dstEnt    = ENTRLC;                              \
134    pst.dstInst   = _dstInst;                           \
135    pst.srcInst   = macCb.macInst;                      \
136    pst.dstProcId = macCb.procId;                       \
137    pst.srcProcId = macCb.procId;                       \
138    pst.region    = MAC_MEM_REGION;                     \
139    pst.pool      = MAC_POOL;                           \
140    pst.event     = _event;                             \
141    pst.route     = 0;                                  \
142    pst.prior     = 0;                                  \
143    pst.intfVer   = 0;                                  \
144 }
145
146 /* Function declaration */
147 uint16_t getNewCrnti(uint8_t *crntiMap);
148 uint32_t shortBsrBytesTable[MAX_SHORT_BSR_TABLE_ENTRIES];
149 /**********************************************************************
150          End of file
151 **********************************************************************/