Merge "correction in F1AP message based on wireshark logs"
[o-du/l2.git] / src / cm / cm_mblk.c
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 \f
19 /********************************************************************20**
20   
21      Name:     common memory allocation library 
22   
23      Type:     C include file
24   
25      Desc:     memory library routines 
26  
27      File:     cm_mblk.c
28   
29 *********************************************************************21*/
30
31
32 /* header include files (.h) */
33 #include "envopt.h"        /* environment options */
34 #include "envdep.h"        /* environment dependent */
35 #include "envind.h"        /* environment independent */
36 #include "gen.h"           /* general layer */
37 #include "ssi.h"           /* system services */
38 #include "cm_mblk.h"       /* Header file */
39
40 /* header/extern include files (.x) */
41 #include "gen.x"           /* general layer */
42 #include "ssi.x"           /* system services */
43 #include "cm_lib.x"        /* Common library */
44 #include "cm_mblk.x"       /* Typedef file */
45
46 #ifdef SS_LOCKLESS_MEMORY
47 EXTERN pthread_t tmpRegTidMap[20];
48 #define CM_MEM_GET_REGION(_region)                        \
49 {                                                         \
50    U8  _regCnt;                                           \
51    _region = 0xFF;                                        \
52                                                           \
53    for(_regCnt = 0; _regCnt < 12; _regCnt++)              \
54    {                                                      \
55       if(tmpRegTidMap[_regCnt] == pthread_self())         \
56       {                                                   \
57          _region = _regCnt;                               \
58          break;                                           \
59       }                                                   \
60    }                                                      \
61 }
62 #endif
63
64
65
66 PRIVATE Void cmAddMemNode ARGS((CmMemListCp *lCp,CmMemList *node)); 
67
68 \f
69 /*
70 *
71 *       Fun:   cmAllocEvnt
72 *
73 *       Desc:  This function allocates the first memory chunk,
74 *              which contains CmMemListCp structure at the top,
75 *              parcels the requested event structure out of this
76 *              chunk and return to the user.
77 *
78 *       Ret:   ROK 
79 *
80 *       Notes: None 
81 *
82 *       File:  cm_mblk.c
83 *
84 */
85   
86 #ifdef ANSI
87 S16 cmAllocEvnt
88 (
89 Size            evntSize,    /* Size of the Event structure */
90 Size            maxBlkSize,  /* Chunk Memory size */
91 Mem             *sMem,       /* Static memory region and pool */
92 Ptr             *ptr         /* Location to place allocated event ptr */
93 )
94 #else
95 S16 cmAllocEvnt (evntSize,maxBlkSize,sMem,ptr)
96 Size            evntSize;   /* Size of the Event structure */
97 Size            maxBlkSize; /* Memory size requested */
98 Mem             *sMem;      /* Static Memory region and pool */
99 Ptr             *ptr;       /* Location to place allocated event ptr */
100 #endif
101 {
102
103   Data          *allocPtr;  /* Allocated Memory Pointer */
104   CmMemList     *node;      /* Memory Link List Node */
105   CmMemListCp   *memCp;     /* memory Link list control point */
106   CmMemCb       *cb;        /* Allocated Memory Control Block */
107
108
109   /* Validation check */
110 #ifndef LTE_ENB_PAL
111   if( evntSize > maxBlkSize)
112   {
113      printf("\nNot Allocating memory for Event\n");
114 #ifdef ALIGN_64BIT
115      printf("eventSize [%d] greater than maxBlkSize [%d]\n",
116            evntSize, maxBlkSize);
117 #else
118      printf("eventSize [%ld] greater than maxBlkSize [%ld]\n",
119            evntSize, maxBlkSize);
120 #endif
121      return RFAILED;
122   }
123 #endif
124  
125   /* Allocate memory for the first Memory Chunk */
126   /* Allocated memory should be maxBlkSize + sizeof(CmMemList) */
127 #ifdef SS_LOCKLESS_MEMORY
128    if(SGetStaticBuffer(sMem->region, sMem->pool, (Data **)&allocPtr,
129                        (Size)(maxBlkSize + sizeof(CmMemList)), 0) != ROK)
130 #else
131   if (SGetSBuf(sMem->region, sMem->pool, 
132                (Data **)&allocPtr, 
133                (Size)(maxBlkSize + sizeof(CmMemList))) != ROK)
134 #endif /* SS_LOCKLESS_MEMORY */
135
136      return RFAILED;
137
138   /* Reset the contents */
139   memset(allocPtr, 0, 
140            (maxBlkSize + sizeof(CmMemList)) );
141
142   /* The above allocated memory chunk is structured as follows 
143
144   +-------------------+
145   |  CmMemList        |
146   +-------------------+   <---- Event Structure begins here
147   |  CmMemListCp      |      ^
148   +-------------------+      |
149   |  Event data part  |      |  evntSize
150   +-------------------+      |
151        ....                  |
152        ....                  \/ 
153   +-------------------+    
154   
155   */
156
157   /* Overlay CmMemList structure on top of this allocated chunk */
158   node = (CmMemList *)allocPtr;
159   /* cm_mblk_c_001.101: update size value */
160   node->size = maxBlkSize;
161
162   /* Get to CmMemListCp start */
163   memCp = (CmMemListCp *) ((PTR)allocPtr + sizeof(CmMemList));
164   
165   /* Initialise memListCp */
166   CM_INIT_MEMCP( memCp,maxBlkSize,sMem);
167
168   /* Add link list node to link list */
169   cmAddMemNode(memCp, node); 
170
171   /* Get pointer to memCb inside MemListCp */
172   cb = (CmMemCb *)&(memCp->memCb);
173
174   /* Align evntSize, if necessary */
175   CM_ALIGN_SIZE(evntSize);
176
177   cb->memAllocated = (evntSize + sizeof(CmMemList) );
178   cb->initPtr = (PTR)allocPtr;
179   *ptr = (Ptr) ((PTR)allocPtr + sizeof(CmMemList));
180   cb->runPtr = ((PTR)(*ptr) + evntSize);
181
182   return ROK;
183
184 } /* End of cmAllocEvnt */
185
186 \f
187 /*
188 *
189 *       Fun:   cmInitMemCp
190 *
191 *       Desc:  This function intialises Memory Link List
192 *              Control point  
193 *
194 *       Ret:   ROK 
195 *
196 *       Notes: None 
197 *
198 *       File:  cm_mblk.c
199 *
200 */
201   
202 #ifdef ANSI
203 Void cmInitMemCp
204 (
205 CmMemListCp     *memCp,     /* Memory control pointer */
206 Size            maxBlkSize, /* Chunk Memory size */
207 Mem             *sMem       /* Static memory region and pool */
208 )
209 #else
210 Void cmInitMemCp (memCp,maxBlkSize,sMem)
211 CmMemListCp     *memCp;     /* Memory control pointer */
212 Size            maxBlkSize; /* Memory size requested */
213 Mem             *sMem;      /* Static Memory region and pool */
214 #endif
215 {
216
217
218   /* Intialise Memory Control Point */
219   CM_INIT_MEMCP(memCp,maxBlkSize,sMem);
220
221   RETVOID;
222
223 } /* End of cmInitMemCp */ 
224
225 \f
226 /*
227 *
228 *       Fun:   cmGetMem
229 *
230 *       Desc:  This function parcels memory from memory chunks 
231 *              It allocated big chunk as and when required
232 *
233 *       Ret:   ROK 
234 *
235 *       Notes: None 
236 *
237 *       File:  cm_mblk.c
238 *
239 */
240   
241 #ifdef ANSI
242 S16 cmGetMem
243 (
244 Ptr           memPtr,    /* Pointer to memCp */
245 Size          size,      /* Memory size requested */
246 Ptr           *allocPtr  /* location to place pointer */
247 )
248 #else
249 S16 cmGetMem (memPtr,size,allocPtr)
250 Ptr          memPtr;    /* Pointer to memCp */
251 Size         size;      /* Memory size requested */
252 Ptr          *allocPtr; /* location to place pointer */
253 #endif
254 {
255  
256   CmMemCb   *cb;       /* Pointer to Memory Control Block */
257   CmMemList *node;     /* Memory List node */
258   CmMemListCp *memCp;  /* Memory Control Point */
259   /* cm_mblk_c_001.101: added local variable */
260   Size       blkSize;  /* required block size */
261
262
263   memCp = (CmMemListCp *)memPtr;
264   cb = &memCp->memCb; 
265
266   /* Make requested size aligned, if necessary */
267   CM_ALIGN_SIZE(size);
268
269   /* cm_mblk_c_001.101 : If the requested size is greater than the configured
270    * max size, then allocate a new block with the requested size. This will
271    * enable us to handle large allocation requests in H.323 stack for certain
272    * information elements. This will not impact any normal allocation request
273    * as we fall back to the configured size */
274    if( size > cb->maxSize)
275      blkSize = size;
276    else
277      blkSize = cb->maxSize;
278  
279   if( cb->initPtr)
280   { 
281     /* if a chunk is already there */
282     if( (cb->memAllocated + size) <= 
283            (U32)(cb->maxSize + sizeof(CmMemList)) )
284     {
285       /* Requested memory is available in present chunk */
286       *allocPtr = (Ptr) cb->runPtr;
287       cb->memAllocated += size;
288       cb->runPtr += size;
289       return ROK;
290     }
291   }
292   
293   /* For all other cases, We need to allocate a new memory chunk */
294   /* Allocate buffer */
295   /* cm_mblk_c_001.101: use blkSize instead of cb->maxSize */
296 #ifdef SS_LOCKLESS_MEMORY
297    if(SGetStaticBuffer(cb->sMem.region, cb->sMem.pool, 
298                        (Data **)&(cb->initPtr),
299                        (Size)(blkSize + sizeof(CmMemList)), 0) != ROK)
300 #else
301   if (SGetSBuf(cb->sMem.region, cb->sMem.pool, 
302               (Data **)&(cb->initPtr),
303               (Size)(blkSize + sizeof(CmMemList)) )  != ROK)
304 #endif /* SS_LOCKLESS_MEMORY */
305      return RFAILED;
306
307   /* Reset the contents */
308   /* Initialise above allocated structure */
309   /* cm_mblk_c_001.101: use blkSize instead of cb->maxSize */
310   memset(&(cb->initPtr), 0, 
311            (blkSize + sizeof(CmMemList) ));
312
313   /* The above allocated memory chunk is structured as follows 
314
315   +-------------------+
316   |  CmMemList        |
317   +-------------------+
318       Data Portion
319        ....
320        ....
321   +-------------------+    */
322
323   /* Overlay CmMemList structure on top of this allocated chunk */
324   node = (CmMemList *)cb->initPtr;
325   /* cm_mblk_c_001.101: update size */
326   node->size = blkSize;
327
328   /* Add link list node to link list */
329   cmAddMemNode(memCp, node); 
330
331   cb->memAllocated = (size + sizeof(CmMemList) );
332   *allocPtr = (Ptr) ((PTR)cb->initPtr + sizeof(CmMemList));
333   cb->runPtr = ((PTR)(*allocPtr) + size);
334
335   return ROK;   
336
337 } /* End of cmGetMem */
338
339
340 #ifdef TFU_ALLOC_EVENT_NO_INIT
341 \f
342 /*
343 *
344 *       Fun:   cmAllocEvntNoInit
345 *
346 *       Desc:  This function allocates the first memory chunk,
347 *              which contains CmMemListCp structure at the top,
348 *              parcels the requested event structure out of this
349 *              chunk and return to the user.
350 *
351 *       Ret:   ROK 
352 *
353 *       Notes: None 
354 *
355 *       File:  cm_mblk.c
356 *
357 */
358   
359 #ifdef ANSI
360 S16 cmAllocEvntNoInit
361 (
362 Size            evntSize,    /* Size of the Event structure */
363 Size            maxBlkSize,  /* Chunk Memory size */
364 Mem             *sMem,       /* Static memory region and pool */
365 Ptr             *ptr         /* Location to place allocated event ptr */
366 )
367 #else
368 S16 cmAllocEvntNoInit (evntSize,maxBlkSize,sMem,ptr)
369 Size            evntSize;   /* Size of the Event structure */
370 Size            maxBlkSize; /* Memory size requested */
371 Mem             *sMem;      /* Static Memory region and pool */
372 Ptr             *ptr;       /* Location to place allocated event ptr */
373 #endif
374 {
375
376   Data          *allocPtr;  /* Allocated Memory Pointer */
377   CmMemList     *node;      /* Memory Link List Node */
378   CmMemListCp   *memCp;     /* memory Link list control point */
379   CmMemCb       *cb;        /* Allocated Memory Control Block */
380
381
382   /* Validation check */
383   if( evntSize > maxBlkSize)
384      return RFAILED;
385  
386   /* Allocate memory for the first Memory Chunk */
387   /* Allocated memory should be maxBlkSize + sizeof(CmMemList) */
388 #ifdef SS_LOCKLESS_MEMORY
389    if(SGetStaticBuffer(sMem->region, sMem->pool, (Data **)&allocPtr,
390                        (Size)(maxBlkSize + sizeof(CmMemList)), 0) != ROK)
391 #else
392   if (SGetSBuf(sMem->region, sMem->pool, 
393                (Data **)&allocPtr, 
394                (Size)(maxBlkSize + sizeof(CmMemList))) != ROK)
395 #endif /* SS_LOCKLESS_MEMORY */
396
397      return RFAILED;
398
399   /* Reset the contents */
400   memset(allocPtr, 0, 
401            (sizeof(CmMemList)) );
402
403   /* The above allocated memory chunk is structured as follows 
404
405   +-------------------+
406   |  CmMemList        |
407   +-------------------+   <---- Event Structure begins here
408   |  CmMemListCp      |      ^
409   +-------------------+      |
410   |  Event data part  |      |  evntSize
411   +-------------------+      |
412        ....                  |
413        ....                  \/ 
414   +-------------------+    
415   
416   */
417
418   /* Overlay CmMemList structure on top of this allocated chunk */
419   node = (CmMemList *)allocPtr;
420   /* cm_mblk_c_001.101: update size value */
421   node->size = maxBlkSize;
422
423   /* Get to CmMemListCp start */
424   memCp = (CmMemListCp *) ((PTR)allocPtr + sizeof(CmMemList));
425   
426   /* Initialise memListCp */
427   CM_INIT_MEMCP( memCp,maxBlkSize,sMem);
428
429   /* Add link list node to link list */
430   cmAddMemNode(memCp, node); 
431
432   /* Get pointer to memCb inside MemListCp */
433   cb = (CmMemCb *)&(memCp->memCb);
434
435   /* Align evntSize, if necessary */
436   CM_ALIGN_SIZE(evntSize);
437
438   cb->memAllocated = (evntSize + sizeof(CmMemList) );
439   cb->initPtr = (PTR)allocPtr;
440   *ptr = (Ptr) ((PTR)allocPtr + sizeof(CmMemList));
441   cb->runPtr = ((PTR)(*ptr) + evntSize);
442
443   return ROK;
444
445 } /* End of cmAllocEvntNoInit */
446
447 /*
448 *
449 *       Fun:   cmGetMemNoInit
450 *
451 *       Desc:  This function parcels memory from memory chunks 
452 *              It allocated big chunk as and when required
453 *
454 *       Ret:   ROK 
455 *
456 *       Notes: None 
457 *
458 *       File:  cm_mblk.c
459 *
460 */
461   
462 #ifdef ANSI
463 S16 cmGetMemNoInit
464 (
465 Ptr           memPtr,    /* Pointer to memCp */
466 Size          size,      /* Memory size requested */
467 Ptr           *allocPtr  /* location to place pointer */
468 )
469 #else
470 S16 cmGetMemNoInit (memPtr,size,allocPtr)
471 Ptr          memPtr;    /* Pointer to memCp */
472 Size         size;      /* Memory size requested */
473 Ptr          *allocPtr; /* location to place pointer */
474 #endif
475 {
476  
477   CmMemCb   *cb;       /* Pointer to Memory Control Block */
478   CmMemList *node;     /* Memory List node */
479   CmMemListCp *memCp;  /* Memory Control Point */
480   /* cm_mblk_c_001.101: added local variable */
481   Size       blkSize;  /* required block size */
482
483
484   memCp = (CmMemListCp *)memPtr;
485   cb = &memCp->memCb; 
486
487   /* Make requested size aligned, if necessary */
488   CM_ALIGN_SIZE(size);
489
490   /* cm_mblk_c_001.101 : If the requested size is greater than the configured
491    * max size, then allocate a new block with the requested size. This will
492    * enable us to handle large allocation requests in H.323 stack for certain
493    * information elements. This will not impact any normal allocation request
494    * as we fall back to the configured size */
495    if( size > cb->maxSize)
496      blkSize = size;
497    else
498      blkSize = cb->maxSize;
499  
500   if( cb->initPtr)
501   { 
502     /* if a chunk is already there */
503     if( (cb->memAllocated + size) <= 
504            (U32)(cb->maxSize + sizeof(CmMemList)) )
505     {
506       /* Requested memory is available in present chunk */
507       *allocPtr = (Ptr) cb->runPtr;
508       //memset(*allocPtr, (U8 )0, 
509         //   (PTR)(size) );
510       cb->memAllocated += size;
511       cb->runPtr += size;
512       return ROK;
513     }
514   }
515   
516   /* For all other cases, We need to allocate a new memory chunk */
517   /* Allocate buffer */
518   /* cm_mblk_c_001.101: use blkSize instead of cb->maxSize */
519 #ifdef SS_LOCKLESS_MEMORY
520    if(SGetStaticBuffer(cb->sMem.region, cb->sMem.pool, 
521                        (Data **)&(cb->initPtr),
522                        (Size)(blkSize + sizeof(CmMemList)), 0) != ROK)
523 #else
524   if (SGetSBuf(cb->sMem.region, cb->sMem.pool, 
525               (Data **)&(cb->initPtr),
526               (Size)(blkSize + sizeof(CmMemList)) )  != ROK)
527 #endif /* SS_LOCKLESS_MEMORY */
528      return RFAILED;
529
530   /* Reset the contents */
531   /* Initialise above allocated structure */
532   /* cm_mblk_c_001.101: use blkSize instead of cb->maxSize */
533   memset(&(cb->initPtr), 0, 
534            (sizeof(CmMemList)));
535   /* The above allocated memory chunk is structured as follows 
536
537   +-------------------+
538   |  CmMemList        |
539   +-------------------+
540       Data Portion
541        ....
542        ....
543   +-------------------+    */
544
545   /* Overlay CmMemList structure on top of this allocated chunk */
546   node = (CmMemList *)cb->initPtr;
547   /* cm_mblk_c_001.101: update size */
548   node->size = blkSize;
549
550   /* Add link list node to link list */
551   cmAddMemNode(memCp, node); 
552
553   cb->memAllocated = (size + sizeof(CmMemList) );
554   *allocPtr = (Ptr) ((PTR)cb->initPtr + sizeof(CmMemList));
555   cb->runPtr = ((PTR)(*allocPtr) + size);
556
557   return ROK;   
558
559 } /* End of cmGetMemNoInit */
560
561
562
563
564 #endif
565 \f
566 /*
567 *
568 *       Fun:   cmFreeMem
569 *
570 *       Desc:  This function frees memory chunks after
571 *              traversing link list
572 *
573 *       Ret:   ROK 
574 *
575 *       Notes: None 
576 *
577 *       File:  cm_mblk.c
578 *
579 */
580   
581 #ifdef ANSI
582 Void cmFreeMem
583 (
584 Ptr    memPtr      /* Link List CP */
585 )
586 #else
587 Void cmFreeMem (memPtr)
588 Ptr    memPtr;     /* Link List CP */
589 #endif
590 {
591   Mem         sMem;      /* Static Memory region and pool */
592   S32         count;     /* Count of linked blocks */ 
593   /* cm_mblk_c_001.101: removed local variable maxSize */
594   CmMemList   *node;     /* Pointer to link list node */
595   CmMemList   *prevNode; /* Pointer to previous node */
596   CmMemListCp *lcp;      /* Memory Link List */
597
598
599   lcp = (CmMemListCp *)memPtr;
600
601   sMem.region = lcp->memCb.sMem.region;
602   sMem.pool   = lcp->memCb.sMem.pool;
603   count       = lcp->count; 
604   /* cm_mblk_c_001.101: removed getting maxSize value */
605
606   /* Free Memory by traversing Back to Front */
607   node = lcp->last;
608
609   /* Initialise memCp running variables */
610   /* User may want to reuse same memCp for further */
611   /* allocations, if memCp was not a part of */
612   /* event structure */
613   CM_INIT_MEMCPVAR(lcp);
614
615   while(count && node)
616   {
617     prevNode = node->prev;
618
619     /* Release complete memory for present chunk */
620     /* cm_mblk_c_001.101: use node->size instead of maxSize */
621     if( node )
622     {
623 #ifdef SS_LOCKLESS_MEMORY
624        SPutStaticBuffer(sMem.region, sMem.pool,
625                         (Data *)node, (node->size + sizeof(CmMemList)), 0);
626 #else
627        SPutSBuf(sMem.region,sMem.pool, 
628                 (Data *)node, (node->size + sizeof(CmMemList)));
629 #endif /* SS_LOCKLESS_MEMORY */
630     }
631     node = prevNode;
632     count--;
633   }
634
635   RETVOID;   
636
637 } /* End of cmFreeMem */
638
639 \f
640 /*
641 *
642 *       Fun:   cmAddMemNode
643 *
644 *       Desc:  adds node to Memory linked list after last.
645 *
646 *       Ret:   ROK   - ok
647 *
648 *       Notes: None
649 *
650 *       File:  cm_mblk.c
651 *
652 */
653 #ifdef ANSI
654 PRIVATE Void cmAddMemNode
655 (
656 CmMemListCp *lCp,               /* list control point */
657 CmMemList   *node               /* node to be added */
658 )
659 #else 
660 PRIVATE Void cmAddMemNode (lCp, node)
661 CmMemListCp *lCp;               /* list control point */
662 CmMemList   *node;              /* node to be added */
663 #endif
664 {
665
666    lCp->count++;
667
668    node->prev = lCp->last;
669    node->next = NULLP;
670    lCp->last = node;
671    
672    if (!node->prev)
673    {
674       lCp->first = node;
675       RETVOID;
676    }
677    
678    node->prev->next = node;
679    RETVOID;
680
681 } /* end of cmAddMemNode */
682
683
684 \f
685 /*
686 *
687 *       Fun:   cmGetMemStatus
688 *
689 *       Desc:  This function returns the static memory status with
690 *              parameters such as memory  region and pool etc
691 *
692 *       Ret:   ROK 
693 *
694 *       Notes: None 
695 *
696 *       File:  cm_mblk.c
697 *
698 */
699   
700 #ifdef ANSI
701 Void cmGetMemStatus
702 (
703 Ptr             memPtr,    /* Memory control pointer */
704 CmMemStatus     *status    /* memory region,pool and status */
705 )
706 #else
707 Void cmGetMemStatus (memPtr,status)
708 Ptr             memPtr;   /* Memory control pointer */
709 CmMemStatus     *status;  /* memory region,pool and status */
710 #endif
711 {
712
713   CmMemListCp *memCp;    /* Memory Link List */
714
715
716   memCp = (CmMemListCp *)memPtr;
717
718   /* Copy relevant parameters */
719   status->sMem.region  = memCp->memCb.sMem.region;
720   status->sMem.pool    = memCp->memCb.sMem.pool;
721   status->memBlkCnt    = memCp->count;
722   status->maxBlkSize   = memCp->memCb.maxSize;
723   status->memAllocated = memCp->memCb.memAllocated;
724
725   RETVOID;
726
727 } /* End of cmGetMemStatus */ 
728
729 /**********************************************************************
730          End of file
731 **********************************************************************/