Memory handling fixes
[o-du/l2.git] / src / cm / cm_mem.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 Manager 
22  
23      Type:     C source file
24  
25      Desc:     C source code for the Commom Memory Manager module. 
26  
27      File:     cm_mem.c
28  
29      Sid:      cm_mem.c@@/main/28 - Fri Aug 26 13:52:41 2011
30  
31      Prg:      rm
32  
33 *********************************************************************21*/
34
35 \f
36 /************************************************************************
37
38 The following functions are provided in this file.
39  
40     cmMmRegInit     Memory Region Initialization.
41     cmMmRegDeInit   Memory Region Deinitialization.
42
43 ************************************************************************/
44
45 \f
46 /* header include files (.h) */
47 #include "envopt.h"        /* environment options */
48 #include "envdep.h"        /* environment dependent */
49 #include "envind.h"        /* environment independent */
50  
51 #include "gen.h"           /* general */
52 #include "ssi.h"           /* system services */
53 #include "cm_mem.h"        /* Common memory manager cm_mem_c_001.main_15 */ 
54 #ifdef SS_MEM_LEAK_STS
55 #include <stdio.h>
56 #include <string.h>
57 #include <stdlib.h>
58 #ifdef SS_MEM_LEAK_SOL
59 #include <ucontext.h>
60 #include <dlfcn.h>
61 #include <sys/machelf.h>
62 #else /* SS_MEM_LEAK_SOL */
63 #include <execinfo.h>
64 #endif /* SS_MEM_LEAK_SOL */
65 #include <sys/types.h>
66 #include "cm_hash.h" 
67 #endif /* SS_MEM_LEAK_STS */
68
69 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
70 #include "cm_hash.h"       /* common hash functions */
71 #endif
72 #ifdef SS_MULTICORE_SUPPORT /* cm_mem_c_001.main_14 */
73 #include "ss_dep.h"        /* implementation-specific */
74 #include "ss_queue.h"      /* queues */
75 #include "ss_task.h"       /* tasking */
76 #endif 
77 #ifdef SS_MULTICORE_SUPPORT
78 #include "ss_dep.h"        /* implementation-specific */
79 #include "ss_queue.h"      /* queues */
80 #include "ss_task.h"       /* tasking */
81 #endif
82
83 /* header/extern include files (.x) */
84 #include "gen.x"           /* general */
85 #include "ssi.x"           /* system services */
86 #ifdef SS_MULTICORE_SUPPORT
87 #include "ss_dep.x"        /* implementation-specific */
88 #include "ss_queue.x"      /* queues */
89 #include "ss_task.x"           /* system services */
90 #endif
91 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
92 #include "cm_hash.x"       /* common hash functions */
93 #endif
94 #include "cm_mem.x"        /* Common memory manager */ 
95 /* cm_mem_c_001.main_28 : Fix for compilation warning */
96 #include "cm_lib.x"        /* common library functions */
97 /* cm_mem_c_001.main_15: Addition  */
98 #ifdef SS_MEM_LEAK_STS  
99 #include "cm_hash.x"       /* common hash functions */
100 #endif /* SS_MEM_LEAK_STS */
101
102 #ifdef USE_PURE
103 #include <stdlib.h>
104 #endif /* USE_PURE */
105 #ifdef SS_MULTICORE_SUPPORT
106 #include "ss_dep.x"        /* implementation-specific */
107 #include "ss_queue.x"      /* queues */
108 #include "ss_task.x"           /* system services */
109 #endif
110
111 #ifdef USE_PURE
112 #include <stdlib.h>
113 #endif /* USE_PURE */
114
115 #ifdef SS_LIGHT_MEM_LEAK_STS
116    MemLeakCb gmemLkCb;
117    uint32_t queueIndxAllocCnt =0;
118    uint32_t queueIndxFreeCnt =0;
119    uint32_t allocQueueFullCnt =0;
120    uint32_t allocQueueEmptyCnt =0;
121 #endif
122 \f
123 /* local defines */
124
125 /* local typedefs */
126  
127 /* local externs */
128  
129 /* forward references */
130 /* cm_mem_c_001.main_12 - prototype is changed to accept memType(static/dynamic) */
131 /* cm_mem_c_001.main_15: Addition */
132 /* cm_mem_c_001.main_22: Fixing warnings on GCC compiler*/
133 #ifdef __cplusplus
134  extern "C" {
135 #endif
136
137 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
138 uint32_t memFreeCount=0;
139 uint32_t cmFreeCaller[4]={0};
140 uint32_t cmAllocCaller[4]={0};
141 Data *startPtr256=NULLP;
142 Data *startPtr128=NULLP;
143 uint32_t cmMemInfo128[100000][2]={0, 0};
144 uint32_t cmMemInfo256[100000][2]={0, 0};
145 Data *startPtr512=NULLP;
146 Data *startPtr768=NULLP;
147 Data *startPtr1664=NULLP;
148 Data *startPtr4800=NULLP;
149 Data *startPtr9920=NULLP;
150 Size regMemSize=0;
151 #endif
152 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
153 //extern CmMmRegCb *mtCMMRegCb[SS_MAX_REGS];
154 #endif
155 #ifdef SS_HISTOGRAM_SUPPORT
156 #ifdef SSI_DEBUG_LEVEL1
157 static S16 cmAlloc ARGS((Void *regionCb, Size *size, uint32_t flags, Data **ptr, 
158             uint32_t memType, uint32_t line, uint8_t *fileName, uint8_t entId, Bool hstReg));
159 static S16 cmHeapAlloc ARGS((CmMmHeapCb *heapCb, Data **ptr, Size *size, uint32_t memType, uint32_t line, uint8_t *fileName, uint8_t entId, Bool hstReg));
160 /*cm_mem_c_001.main_20-added new functionto allocate memory from new region*/
161 #else
162 static S16 cmHeapAlloc ARGS((CmMmHeapCb *heapCb, Data **ptr, Size *size,
163        uint32_t line, uint8_t *fileName, uint8_t entId, Bool hstReg));
164 static S16 cmAlloc ARGS((Void *regionCb, Size *size, uint32_t flags, Data **ptr, 
165             uint32_t line, uint8_t *fileName, uint8_t entId, Bool hstReg));
166 #endif /* SSI_DEBUG_LEVEL1 */
167
168 static S16 cmFree  ARGS((Void *regionCb, Data *ptr, Size size, uint32_t line, 
169             uint8_t *fileName, uint8_t entId, Bool hstReg));
170
171 static S16 cmHeapFree  ARGS((CmMmHeapCb *heapCb, Data *ptr, Size size,
172             uint32_t line, uint8_t *fileName, uint8_t entId, Bool hstReg));
173 #else /* no histogram support */
174 /* cm_mem_c_001.main_12 - prototype is changed to accept memType(static/dynamic) */
175 #ifdef SSI_DEBUG_LEVEL1
176 static S16 cmHeapAlloc ARGS((CmMmHeapCb *heapCb, Data **ptr, Size *size, uint32_t memType));
177 #else
178 #ifndef USE_PURE
179 static S16 cmHeapAlloc ARGS((CmMmHeapCb *heapCb, Data **ptr, Size *size));
180 #endif/*USE_PURE*/
181 #endif /* SSI_DEBUG_LEVEL1 */
182 #ifndef USE_PURE
183 static S16 cmHeapFree  ARGS((CmMmHeapCb *heapCb, Data *ptr, Size size));
184 #endif/*USE_PURE*/
185 /*  cm_mem_c_001.main_15 :Additions */
186 #ifdef SS_LIGHT_MEM_LEAK_STS
187 static S16 cmAlloc ARGS((Void *regionCb, Size *size, uint32_t flags, Data **ptr, uint32_t memType, uint32_t lineNo,uint8_t *funcName ));
188 static S16 cmFree  ARGS((Void *regionCb, Data *ptr, Size size, uint32_t lineNo, uint8_t* funcName));
189 #else /*SS_LIGHT_MEM_LEAK_STS */
190 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
191 static S16 cmAlloc ARGS((Void *regionCb, Size *size, uint32_t flags, Data **ptr, uint32_t memType));
192 #else
193 #ifdef T2K_MEM_LEAK_DBG
194 static S16 cmAlloc ARGS((Void *regionCb, Size *size, uint32_t flags, Data **ptr, char*  file, uint32_t    line));
195 #else
196 static S16 cmAlloc ARGS((Void *regionCb, Size *size, uint32_t flags, Data **ptr));
197 #endif
198 #endif /* SSI_DEBUG_LEVEL1 */
199 #ifdef T2K_MEM_LEAK_DBG
200 static S16 cmFree  ARGS((Void *regionCb, Data *ptr, Size size, char* file, uint32_t line));
201 #else
202 static S16 cmFree  ARGS((Void *regionCb, Data *ptr, Size size));
203 #endif
204 #endif /* SS_HISTOGRAM_SUPPORT */
205 #endif /*SS_LIGHT_MEM_LEAK_STS*/
206 /*cm_mem_c_001.main_23 Removed support of SSI_DEBUG_LEVEL1 and SS_HISTOGRAM_SUPPORT for SS_FAP*/
207 #ifdef SS_FAP
208 static S16 cmAllocWL ARGS((Void *regionCb, Size *size, uint32_t flags, Data **ptr));
209 static S16 cmFreeWL  ARGS((Void *regionCb, Data *ptr, Size size));
210 #endif
211
212 static S16 cmCtl   ARGS((Void *regionCb, Event event, SMemCtl *memCtl));
213
214 static Void cmMmHeapInit ARGS((Data *memAddr, CmMmHeapCb *heapCb, Size size));
215 static Void cmMmBktInit ARGS((Data **memAddr, CmMmRegCb *regCb, 
216                               CmMmRegCfg *cfg, uint16_t bktIdx, uint16_t *lstMapIdx));
217
218 /* cm_mem_c_001.main_12 - addition of protoypes for sanity check and hash list functions */
219 #ifdef SSI_DEBUG_LEVEL1
220 static S16 cmMmBktSanityChk ARGS((CmMmBkt *bkt));
221 static S16 cmMmHeapSanityChk ARGS((CmMmHeapCb *heapCb));
222 static S16 cmMmHashFunc ARGS((CmMmHashListCp *hashListCp, uint32_t key, uint16_t *idx ));
223 static S16 cmMmHashListInit ARGS((CmMmHashListCp *hashListCp, uint16_t nmbBins,
224                                   Region region, Pool pool));
225 static S16 cmMmHashListDeinit ARGS((CmMmHashListCp *hashListCp, Region region, Pool pool));
226 static S16 cmMmHashListInsert ARGS((CmMmHashListCp *hashListCp, uint32_t key));
227 #endif /* SSI_DEBUG_LEVEL1 */
228 /*   cm_mem_c_001.main_15 : Addtions */
229 #ifdef SS_HISTOGRAM_SUPPORT
230 static S16 cmHstGrmAllocInsert ARGS((CmHstGrmHashListCp *hashListCp, uint32_t blkSz, uint32_t *reqSz, uint32_t line, uint8_t *fileName, uint8_t entId));
231 static S16 cmHstGrmFreeInsert ARGS((CmHstGrmHashListCp* hashListCp, uint32_t blkSz, uint32_t line, uint8_t *fileName, uint8_t entId));
232 static S16 cmHstGrmHashListInit ARGS((CmHstGrmHashListCp *hashListCp));
233 static S16 cmHstGrmHashListDeInit ARGS((CmHstGrmHashListCp *hashListCp));
234 static S16 cmHstGrmGetHashIdxAndKey ARGS((uint8_t *fileName, uint32_t line, uint32_t *binIdx, uint32_t *key));
235 static S16 cmHstGrmFindEntry ARGS((CmHstGrmHashListCp *hashListCp, uint32_t key, uint32_t *binIdx, CmMemEntries **entry));
236 static S16 cmHstGrmFillEntry ARGS((CmMemEntries *entry, uint32_t key, uint32_t line, uint8_t *fileName, uint8_t entId));
237 #endif /* SS_HISTOGRAM_SUPPORT */
238
239 /* cm_mem_c_001.main_22: Fixing warnings on GCC compiler */
240 #ifdef __cplusplus
241 }
242 #endif
243
244 /* public variable declarations */
245 #ifdef USE_PURE
246 Size avail_size;
247 #endif /* USE_PURE */
248 /*  cm_mem_c_001.main_15:Additions */
249 #ifdef SS_MEM_LEAK_STS 
250 MemUsrMdlStr   memUsrMdlStr[]=
251 {
252    MEMRAW2STR(DEFAULT, STACK),
253    MEMRAW2STR(tc, PDCP_LAYER),
254    MEMRAW2STR(Tc, PDCP_LAYER),
255    MEMRAW2STR(mg, GCP_LAYER),
256    MEMRAW2STR(Mg, GCP_LAYER),
257    {NULLP, NULLP}
258 };
259
260 MemLkCb memLkCb;
261 #endif /* SS_MEM_LEAK_STS */
262 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
263 MemLkInfo memLk;
264 #endif
265
266
267
268 #ifdef T2K_MEM_LEAK_DBG
269 typedef struct {
270 uint8_t  minBktSzBitMask;  /* minimum allocation size  in Log(x)base 2, where X is minimum bucket size in region */ 
271 uint16_t minBktSzMins1; /* X-1   */
272 }RegMinBktSzInfo;
273 static RegMinBktSzInfo regMinBktSzInfo[SS_MAX_REGS] = {{8,0xFF},{7,0x7F},{7,0x7F},{7,0x7F}}; 
274 RegionMemLeakInfo regMemLeakInfo; 
275
276 uint32_t getT2kMemLeakIndex(uint64_t address, Region region)
277 {
278    return ((address - regMemLeakInfo.regStartAddr[region]) >> regMinBktSzInfo[region].minBktSzBitMask);
279 }
280
281 static uint32_t t2kMemAllocTick;
282 static uint32_t smallTick;
283
284 void InsertToT2kMemLeakInfo(uint64_t address, uint32_t size, uint32_t lineNo, char* fileName, Region region)
285 {
286
287    T2kMeamLeakInfo *leakInfo;
288   /* if(!startMemLeak)
289    {
290       return;
291    }*/
292
293    uint32_t index1 = getT2kMemLeakIndex(address,region);
294
295    if(((uint64_t)(address - regMemLeakInfo.regStartAddr[region]) & regMinBktSzInfo[region].minBktSzMins1) !=0)
296    {
297       DU_LOG("\nINFO  --> address in InsertToT2kMemLeakInfo is %ld size = %d file is %s line is %d \n", address, size, fileName, lineNo);
298    }
299
300    leakInfo = (((T2kMeamLeakInfo*)(regMemLeakInfo.gMemLeakInfo[region])) + index1);
301    if(leakInfo->address == 0)
302    {
303       leakInfo->address = address;
304       leakInfo->size = size;
305       leakInfo->lineNo = lineNo;
306       leakInfo->fileName = fileName;
307       leakInfo->age = t2kMemAllocTick; 
308       leakInfo->prevRemLineNo = 0;
309       leakInfo->prevRemFileName = '\0';
310
311       //DU_LOG("\nINFO  --> InsertToT2kMemLeakInfo  the adress from List  Address = %x, index1 = %d   \
312       from File=%s, line=%d \n",address,index1,fileName,lineNo);
313       if(smallTick++ == 4096)
314       {
315          smallTick = 0;
316               leakInfo->age = (++t2kMemAllocTick); 
317       }
318    }
319    else
320    {
321          DU_LOG("\nERROR  --> Something is wrong, trying to insert %ld index1 = %d file is %s line is %d \n",address, index1, fileName, lineNo);
322          DU_LOG("\nINFO  --> Address present :%ld, from File:%s, Line:%d, Size:%d, Age:%d",
323                leakInfo->address, leakInfo->fileName,
324                leakInfo->lineNo, leakInfo->size,
325                leakInfo->age);
326    }
327 }
328
329
330 void RemoveFromT2kMemLeakInfo(uint64_t address, char *file, uint32_t line,Region region)
331 {
332    T2kMeamLeakInfo *leakInfo;
333
334    /* if(!startMemLeak)
335    {
336       return ROK;
337    }*/
338
339    uint32_t index1 = getT2kMemLeakIndex(address, region);
340
341    if(index1 >= T2K_MEM_LEAK_INFO_TABLE_SIZE)
342    {
343       DU_LOG("\nERROR  --> index1 out of range = %d address is %ld file = %s line = %d. We are going to crash!!!\n",
344               index1,
345               address,
346               file,
347               line);
348    }
349    leakInfo = (((T2kMeamLeakInfo*)(regMemLeakInfo.gMemLeakInfo[region])) + index1);
350    if(leakInfo->address == address)
351    {
352       
353       leakInfo->address = 0;
354       leakInfo->age = 0; 
355       leakInfo->prevRemLineNo = leakInfo->lineNo;
356       leakInfo->prevRemFileName = leakInfo->fileName; 
357       leakInfo->lastDelLineNum = line;
358       leakInfo->lastDelFileName = file; 
359    }
360    else
361    {
362          DU_LOG("\nERROR  --> Something is wrong, trying to remove %ld index1 = %d  from File=%s, line=%d address present is %ld region%d \n",address, index1, file,line,leakInfo->address,region);
363
364          DU_LOG("\nINFO  -->  Last Del file %s line %d\n",leakInfo->lastDelFileName,
365                  leakInfo->lastDelLineNum);
366
367          if(leakInfo->prevRemFileName != NULLP)
368          {
369              DU_LOG("\nINFO  --> Previous File:%s, Previous Line:%d\n",
370                   leakInfo->prevRemFileName, leakInfo->prevRemLineNo);
371          }
372    }
373 }
374
375 void DumpT2kMemLeakInfoToFile()
376 {
377    int i,reg;
378    T2kMeamLeakInfo *leakInfo;
379   
380    FILE *fp = fopen("memLeakInfo_reg.txt","wb");
381
382    if(fp == NULL)
383    {
384       DU_LOG("\nERROR  --> Could not open file for dumping mem leak info\n");
385       return;
386    }
387    for(reg=0; reg <regMemLeakInfo.numActvRegions; reg++)
388    { 
389       fprintf(fp, "REGION %d LEAKS START\n",reg);
390
391
392       for(i = 0; i< T2K_MEM_LEAK_INFO_TABLE_SIZE; i++)
393       {
394          leakInfo = (((T2kMeamLeakInfo*)(regMemLeakInfo.gMemLeakInfo[reg])) + i);
395          if(leakInfo->address != 0)
396          {
397             char* onlyFileName = rindex(leakInfo->fileName,'/');
398             if(onlyFileName == NULL)
399             {
400                onlyFileName = leakInfo->fileName;
401             }
402
403             fprintf(fp, "%ld  s=%d  a=%d  l=%d  f=%s\n",leakInfo->address,
404                   leakInfo->size,
405                   leakInfo->age,
406                   leakInfo->lineNo,
407                   onlyFileName);
408          }
409       }
410       fprintf(fp, "REGION %d LEAKS END\n",reg);
411    }
412    fprintf(fp,"Current t2kMemAllocTick = %d\n",t2kMemAllocTick);
413
414    fclose(fp);
415 }
416 #endif /* T2K_MEM_LEAK_DBG */
417
418 /* cm_mem_c_008.104 - Addition for memory calculator tool */
419 #ifdef MEMCAL_DEBUG
420 static Txt prntBuf[200];        /* print buffer */
421 static uint8_t tryHeap=0;
422 #endif 
423
424 /* cm_mem_c_001.main_12 - addition for ssi enhancements prints */
425 /* cm_mem_c_001.main_20 Additions */
426 #if (defined(SSI_DEBUG_LEVEL1) || defined(SS_HISTOGRAM_SUPPORT) || \
427     defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
428 #ifdef DEBUGP
429 static Txt dbgPrntBuf[200];        /* print buffer */
430 #endif /* DEBUGP */
431 #endif /*SSI_DEBUG_LEVEL1 || SS_HISTOGRAM_SUPPORT */
432
433 uint32_t num_times = 0;
434 #ifdef SSI_MEM_CORR_PREVENTION
435 uint32_t cmDblFreeAttempts = 0;
436 #endif
437 /* private variable declarations */
438
439 \f
440 /*
441 *
442 *       Fun:   cmMmRegInit
443 *
444 *       Desc:  Configure the memory region for allocation. The function 
445 *              registers the memory region with System Service by calling
446 *              SRegRegion.
447 *
448 *
449 *       Ret:   ROK     - successful, 
450 *              RFAILED - unsuccessful.
451 *
452 *       Notes: The memory owner calls this function to initialize the memory 
453 *              manager with the information of the memory region. Before 
454 *              calling this function, the memory owner should allocate memory 
455 *              for the memory region. The memory owner should also provide the 
456 *              memory for the control block needed by the memory manager. The 
457 *              memory owner should allocate the memory for the region control 
458 *              block as cachable memory. This may increase the average 
459 *              throughput in allocation and deallocation as the region control
460 *              block is mostly accessed by the CMM.
461 *
462 *       File:  cm_mem.c
463 *
464 */
465 S16 cmMmRegInit(Region region,CmMmRegCb *regCb,CmMmRegCfg  *cfg)
466 {
467    Data *memAddr;
468    uint16_t bktIdx;
469    uint16_t lstMapIdx;
470
471 #if (ERRCLASS & ERRCLS_INT_PAR)
472    Size  lstQnSize;
473    Size  bktBlkSize;
474         Txt   errMsg[256] = {'\0'};
475 #endif
476 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
477    uint16_t            offset;
478    CmMmBlkHdr     ptrHdr;
479 #endif
480
481
482 #if (ERRCLASS & ERRCLS_INT_PAR)
483
484    /* error check on parameters */
485    if ((regCb == NULLP) || (cfg == NULLP)) 
486    {
487       return RFAILED;
488    }
489    
490    /* Error check on the configuration fields */
491    if ((!cfg->size) || (cfg->vAddr == NULLP) || 
492         (cfg->numBkts > CMM_MAX_BKT_ENT)) 
493    {
494       return RFAILED;
495    }
496    /* Check if the quantum size is power of 2 */
497    if ((cfg->numBkts) &&
498        ((cfg->bktQnSize - 1) & (cfg->bktQnSize)))
499    {
500       /* cm_mem_c_001.main_20 Addition */
501                 sprintf(errMsg,"\n cmMmRegInit() failed, check if BktQuantum size might not be power of 2 \n");
502                 SPrint(errMsg);
503       return RFAILED;
504    }
505
506    /* 
507     * Check if the size of the memory region is enough, whether bucket sizes
508     * are multiples of quantumn size, and also whether two consecutive buckets
509     *  falls within same quanta.
510     */
511    lstQnSize      = cfg->bktQnSize;
512    regCb->bktSize = 0;
513
514    for ( bktIdx =0; bktIdx < cfg->numBkts; bktIdx++)
515    {
516       /* check if bucket size is mutiple of quantum size */
517       if (cfg->bktCfg[bktIdx].size % cfg->bktQnSize)
518       {
519           /* cm_mem_c_001.main_20 Addition */
520 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/          
521 #ifdef ALIGN_64BIT          
522                          sprintf(errMsg,"\n cmMmRegInit() failed, Bkt:%d size:%u not multiple of quantum size:%u\
523                                                         \n",bktIdx,cfg->bktCfg[bktIdx].size,cfg->bktQnSize);
524 #else                     
525                          sprintf(errMsg,"\n cmMmRegInit() failed, Bkt:%d size:%lu not multiple of quantum size:%lu\
526                                                         \n",bktIdx,cfg->bktCfg[bktIdx].size,cfg->bktQnSize);
527 #endif                     
528                          SPrint(errMsg);
529           return RFAILED;
530       }
531
532       if ((bktBlkSize = cfg->bktCfg[bktIdx].size) < lstQnSize)
533       {
534          /* 
535           * Two consecutive buckets are not separated by quantum size.
536           */
537           /* cm_mem_c_001.main_20 Addition */
538                          sprintf(errMsg,"\n cmMmRegInit() failed, Two consecutive buckets are not separated by quantum size \n");
539                          SPrint(errMsg);
540           return RFAILED;
541       }
542       /* cm_mem_c_001.main_20 Addition */
543                 if (((cfg->bktCfg[bktIdx].size) /\
544                                 cfg->bktQnSize) > CMM_MAX_MAP_ENT)
545                 {
546                   /* Error check whether the size of the mapping table is sufficient */
547 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/          
548 #ifdef ALIGN_64BIT          
549                           sprintf(errMsg,"\n cmMmRegInit() failed, check maxBucketSize/BktQuantumSize(%u)\
550                                 \n      should be less than CMM_MAX_MAP_ENT:%d \n",cfg->bktQnSize,CMM_MAX_MAP_ENT);
551 #else                     
552                           sprintf(errMsg,"\n cmMmRegInit() failed, check maxBucketSize/BktQuantumSize(%lu)\
553                                 \n      should be less than CMM_MAX_MAP_ENT:%d \n",cfg->bktQnSize,CMM_MAX_MAP_ENT);
554 #endif                     
555                                 SPrint(errMsg);
556                           return RFAILED;
557                 }
558
559
560       regCb->bktSize += (cfg->bktCfg[bktIdx].size * 
561                          cfg->bktCfg[bktIdx].numBlks); 
562     
563       if (regCb->bktSize > cfg->size)
564       {
565          /* Size of the memory region is less than the required size */
566                 
567                         sprintf(errMsg,"\n cmMmRegInit() failed, Size of the memory region is less than the required size \n");
568                         SPrint(errMsg);
569          return RFAILED;
570       }
571
572       lstQnSize = ((bktBlkSize / cfg->bktQnSize) + 1) * cfg->bktQnSize;
573    }
574
575 #endif
576
577    /* Initialize the region control block */
578    regCb->region = region;
579    regCb->regInfo.regCb = regCb;
580    regCb->regInfo.start = cfg->vAddr;
581    regCb->regInfo.size  = cfg->size;
582
583 #ifdef USE_PURE
584    avail_size = cfg->size;
585 #endif /* USE_PURE */
586
587    if ( cfg->chFlag & CMM_REG_OUTBOARD)
588    {
589       /* Out_of_board memory */
590       regCb->regInfo.flags = CMM_REG_OUTBOARD;
591    } 
592   else
593    {
594       regCb->regInfo.flags = 0;
595    }
596
597
598 /* Initialize the memory manager function handlers */
599   /*cm_mem_c_001.main_21-registering new alloc function for new region*/
600 #ifdef SS_FAP  
601   if(region == SS_WL_REGION)
602   {
603      regCb->regInfo.alloc = cmAllocWL;
604      regCb->regInfo.free  = cmFreeWL;
605   }
606   else
607 #endif
608   {
609      /* Initialize the memory manager function handlers */
610      regCb->regInfo.alloc = cmAlloc;
611      regCb->regInfo.free  = cmFree;
612   }
613    regCb->regInfo.ctl   = cmCtl;
614
615    /* Initialize the physical address */
616    if ((regCb->chFlag = cfg->chFlag) & CMM_REG_PHY_VALID)
617    {
618       regCb->pAddr = cfg->pAddr;
619    }
620
621    /* Initial address of the memory region block */
622    memAddr    = cfg->vAddr;
623
624    /* Initialize the fields related to the bucket pool */
625    regCb->bktMaxBlkSize = 0;
626    regCb->bktSize       = 0; 
627
628    if (cfg->numBkts > 0 && cfg->numBkts < CMM_MAX_BKT_ENT)
629    {
630       /* Last bucket has the maximum size */
631       regCb->bktMaxBlkSize = cfg->bktCfg[cfg->numBkts - 1].size;
632    
633       /* Get the power of the bktQnSize */
634       regCb->bktQnPwr = 0; 
635       while( !((cfg->bktQnSize >> regCb->bktQnPwr) & 0x01))
636       {
637          regCb->bktQnPwr++;
638       }
639     
640       /* Initilaize the bktIndex of the map entries to FF */
641       for ( lstMapIdx = 0; lstMapIdx < CMM_MAX_MAP_ENT; lstMapIdx++)
642       {
643          regCb->mapTbl[lstMapIdx].bktIdx = 0xFF;
644       }
645   
646       lstMapIdx = 0;
647       for ( bktIdx = 0; bktIdx < cfg->numBkts; bktIdx++)
648       {
649          /* Allocate the lock for the bucket pool */
650          /* cm_mem_c_001.main_13 : Replaced SInitLock with WTInitLock for NT */
651 #ifdef SS_WIN
652          if (WTInitLock (&(regCb->bktTbl[bktIdx].bktLock), cfg->lType) != ROK)
653 #else
654          if (SInitLock (&(regCb->bktTbl[bktIdx].bktLock), cfg->lType) != ROK)
655 #endif
656          {
657             /* Free the initialzed lock for the earlier buckets. */
658             for ( ;bktIdx > 0;)
659             {
660                /* cm_mem_c_001.main_13: Replaced SDestroyLock with 
661                   WTDestroyLock for NT */
662                /*  cm_mem_c_001.main_24 fix for memory corruption*/
663                --bktIdx;
664 #ifdef SS_WIN
665                WTDestroyLock(&(regCb->bktTbl[bktIdx].bktLock));
666 #else
667                SDestroyLock(&(regCb->bktTbl[bktIdx].bktLock));
668 #endif
669             }
670
671             return RFAILED;
672          }
673
674          cmMmBktInit( &memAddr, regCb, cfg, bktIdx, &lstMapIdx); 
675       }
676
677       /* Used while freeing the bktLock in cmMmRegDeInit */
678       regCb->numBkts = cfg->numBkts;
679    }
680
681    /* 
682     * Initialize the heap pool if size the memory region region is more
683     * than the size of the bucket pool 
684     */
685     regCb->heapSize = 0;
686     regCb->heapFlag = FALSE;
687
688     /* Align the memory address */
689     memAddr = (Data *)(PTRALIGN(memAddr));
690
691     regCb->heapSize = cfg->vAddr + cfg->size - memAddr;  
692
693     /* 
694      * Round the heap size so that the heap size is multiple 
695      * of CMM_MINBUFSIZE 
696      */
697     regCb->heapSize -= (regCb->heapSize %  CMM_MINBUFSIZE);
698
699     if (regCb->heapSize)
700     {
701        /* Allocate the lock for the heap pool */
702        /* cm_mem_c_001.main_13 : Replaced SInitLock with WTInitLock for NT */
703 #ifdef SS_WIN
704        if (WTInitLock (&regCb->heapCb.heapLock, cfg->lType) != ROK)
705 #else
706        if (SInitLock (&regCb->heapCb.heapLock, cfg->lType) != ROK)
707 #endif
708        {
709               bktIdx = cfg->numBkts;/* ccpu00125353: warning fix */
710           if (bktIdx != 0)
711           {
712              /* Free the initialzed locks of the buckets */
713              for (; bktIdx > 0;)
714              {
715                 /* cm_mem_c_001.main_13: Replaced SDestroyLock with
716                    WTDestroyLock for NT */
717                /*  cm_mem_c_001.main_24 fix for memory corruption*/
718                --bktIdx;
719 #ifdef SS_WIN
720                 WTDestroyLock(&(regCb->bktTbl[bktIdx].bktLock));
721 #else
722                 SDestroyLock(&(regCb->bktTbl[bktIdx].bktLock));
723 #endif
724              }
725           }
726
727           return RFAILED;
728        }
729         
730        regCb->heapFlag = TRUE;
731        cmMmHeapInit(memAddr, &(regCb->heapCb), regCb->heapSize); 
732     }
733
734     /* Call SRegRegion to register the memory region with SSI */
735     if (SRegRegion(region, &regCb->regInfo) != ROK)
736     {
737        return RFAILED;
738     }
739
740 /* cm_mem_c_001.main_12 - addition for initializing the hash table */
741 #ifdef SSI_DEBUG_LEVEL1
742     /* Initialize the region level hash table for debug info storage */
743     if (cmMmHashListInit(&regCb->hashListCp, CMM_STAT_HASH_TBL_LEN, region, 0) != ROK)
744     {
745        return RFAILED;
746     }
747 #endif /* SSI_DEBUG_LEVEL1 */
748 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
749     /* Initialize the hast list to maintain the SSI memory information for Broadcom */
750     offset = (uint16_t)((PTR)(&ptrHdr.ent) - (PTR) &ptrHdr);
751     DU_LOG("\nINFO  --> offset is %d region %d\n", offset, region);
752     if(cmHashListInit(&regCb->brdcmSsiLstCp, 1000, offset, FALSE, 
753        CM_HASH_KEYTYPE_UINT32_MOD, region, 0) != ROK)
754     {
755        return RFAILED;
756     }
757 #endif /* SSI_DEBUG_LEVEL1 */
758
759     return ROK;
760 } /* end of cmMmRegInit*/
761
762
763 \f
764 /*
765 *
766 *       Fun:   cmMmRegDeInit
767 *
768 *       Desc:  Deinitialize the memory region. The function call SDeregRegion
769 *              to deregister the memory region with System Service.
770 *
771 *
772 *       Ret:   ROK     - successful
773 *              RFAILED - unsuccessful.
774 *
775 *       Notes: The memory owner calls this function to deinitialize the region.
776 *              The memory manager does not return the memory to the system. 
777 *              Before calling this function, the memory owner must be sure that 
778 *              no layer is using any memory block from this region. On 
779 *              successful return from the function, any request to the memory 
780 *              manager to allocate/deallocate memory will fail. The memory owner
781 *              can reuse the memory for other region or return the memory to the
782 *              system memory pool.
783 *
784 *
785 *
786 *       File:  cm_mem.c
787 *
788 */
789 S16 cmMmRegDeInit(CmMmRegCb   *regCb)
790 {
791    uint16_t  bktIdx; 
792
793
794 #if (ERRCLASS & ERRCLS_INT_PAR)
795   
796    /* error check on parameters */
797    if (regCb == NULLP)
798    {
799       return RFAILED;
800    }
801
802 #endif
803
804 /* cm_mem_c_001.main_12 - addition for deinitializing the hash table */
805 #ifdef SSI_DEBUG_LEVEL1
806     /* Deinitialize the hash table used for debug info storage at region level */
807     if (cmMmHashListDeinit(&regCb->hashListCp, regCb->region, 0) != ROK)
808     {
809         return RFAILED;
810     }
811 #endif /* SSI_DEBUG_LEVEL1 */
812 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
813     /* Deinitialize the hash table used for broadcom ssi instrumentation */
814     if (cmHashListDeinit(&regCb->brdcmSsiLstCp) != ROK)
815     {
816         return RFAILED;
817     }
818 #endif
819
820    /* Call SDeregRegion first to deregister the memory region with SSI */
821    (Void) SDeregRegion (regCb->region);
822
823    if (regCb->bktSize)
824    {
825       /* Bucket pool is configured */
826
827       /* Free the initialzed locks of the buckets */
828       for ( bktIdx = regCb->numBkts; bktIdx > 0;)
829       {
830           /* cm_mem_c_001.main_13: Replaced SDestroyLock with
831              WTDestroyLock for NT */
832           /*  cm_mem_c_001.main_24 fix for memory corruption*/
833           --bktIdx;
834 #ifdef SS_WIN
835           WTDestroyLock(&(regCb->bktTbl[bktIdx].bktLock));
836 #else
837           SDestroyLock(&(regCb->bktTbl[bktIdx].bktLock));
838 #endif
839 /*  cm_mem_c_001.main_15:Additions */
840 #ifdef SS_HISTOGRAM_SUPPORT 
841    /* De-initialise the memory histogram hash list */
842    cmHstGrmHashListDeInit(&(regCb->bktTbl[bktIdx].hstGrmHashListCp));
843 #endif /* SS_HISTOGRAM_SUPPORT */
844       }
845    }
846
847    if (regCb->heapFlag)
848    {
849       /* Heap pool is configured */
850
851       /* cm_mem_c_001.main_13: Replaced SDestroyLock with
852          WTDestroyLock for NT */
853 #ifdef SS_WIN
854       WTDestroyLock(&regCb->heapCb.heapLock);
855 #else
856       SDestroyLock(&regCb->heapCb.heapLock);
857 #endif
858    }
859
860    return ROK;
861
862 } /* end of cmMmRegDeInit */
863
864 #ifdef CM_MEM_OVERUSED
865 #define OVERUSED(_bkt) (((_bkt)->numAlloc * 100) / (_bkt)->numBlks > 80)
866
867 int g_overused[5] = {0};
868 #endif
869 \f
870 #ifdef CM_MEM_PRINT_DEFINED
871 volatile int gSubIndex = 0;
872 int regAllocBktSts[10][2][6]={{0}};
873 int regFreeBktSts[10][2][6]={{0}};
874 #endif
875 \f
876 /*
877 *
878 *       Fun:   cmAlloc
879 *
880 *       Desc:  Allocate a memory block for the memory region.
881 *
882 *
883 *       Ret:   ROK     - successful
884 *              RFAILED - unsuccessful.
885 *
886 *       Notes: 
887 *              The function allocates a memory block of size atleast equal to 
888 *              the requested size. The size parameter will be updated with the 
889 *              actual size of the memory block allocated for the request. The 
890 *              CMM tries to allocate the memory block form the bucket pool. If
891 *              there is no memory in the bucket the CMM allocates the memory 
892 *              block form the heap pool. This function is always called by the
893 *              System Service module.
894 *    
895 *              The caller of the function should try to use the out value of 
896 *              the size while returning the memory block to the region. However 
897 *              the current design of the memory manager does not enforce to pass
898 *              the actual size of the memory block.  (Due to the SGetSBuf 
899 *              semantics the layer will not able to pass the correct size of the
900 *              memory block while calling SPutSBuf).
901 *
902 *
903 *       File:  cm_mem.c
904 *
905 */
906 /* cm_mem_c_001.main_12 - addition to accept new parameter memType(static/dynamic) */
907
908 /* cm_mem_c_001.main_15 : Additions */
909 #ifdef SS_HISTOGRAM_SUPPORT
910 #ifdef SSI_DEBUG_LEVEL1
911 static S16  cmAlloc
912 (
913 Void         *regionCb,
914 Size         *size,
915 uint32_t     flags,
916 Data         **ptr,
917 uint32_t     memType,
918 uint32_t     line,
919 uint8_t     *fileName,
920 uint8_t      entId,
921 Bool         hstReg
922 )
923 #else
924 static S16  cmAlloc
925 (
926 Void         *regionCb,
927 Size         *size,
928 uint32_t     flags,
929 Data         **ptr,
930 uint32_t     line,
931 uint8_t      *fileName,
932 uint8_t      entId,
933 Bool         hstReg
934 )
935 #endif /* SSI_DEBUG_LEVEL1 */
936 #else
937 #ifdef SS_LIGHT_MEM_LEAK_STS
938 static S16  cmAlloc
939 (
940 Void         *regionCb,
941 Size         *size,
942 uint32_t     flags,
943 Data         **ptr,
944 uint32_t     memType,
945 uint32_t     lineNo,
946 uint8_t      *funcName
947 )
948 #else /*SS_LIGHT_MEM_LEAK_STS */
949 #ifdef SSI_DEBUG_LEVEL1
950 static S16  cmAlloc
951 (
952 Void         *regionCb,
953 Size         *size,
954 uint32_t     flags,
955 Data         **ptr,
956 uint32_t     memType
957 )
958 #else
959 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
960 static S16  cmAlloc
961 (
962 Void         *regionCb,
963 Size         *size,
964 uint32_t     flags,
965 Data         **ptr,
966 uint32_t     memType
967 )
968 #else
969 #ifdef T2K_MEM_LEAK_DBG
970 static S16  cmAlloc
971 (
972  Void         *regionCb,
973  Size         *size,
974  uint32_t     flags,
975  Data         **ptr,
976  char*        file,
977  uint32_t     line
978  )
979 #else
980 static S16  cmAlloc
981 (
982 Void         *regionCb,
983 Size         *size,
984 uint32_t     flags,
985 Data         **ptr 
986 )
987 #endif
988 #endif /* BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1 */
989 #endif /* SSI_DEBUG_LEVEL1 */
990 #endif /*SS_LIGHT_MEM_LEAK_STS */
991 /* cm_mem_c_001.main_15: Additions */
992 #endif /* SS_HISTOGRAM_SUPPORT */
993
994 {
995 /* cm_mem_c_001.main_26 : Fixes for Compilation Warnings */
996 #ifndef USE_PURE
997    uint16_t  idx;
998    CmMmBkt   *bkt;
999 #endif
1000    CmMmRegCb *regCb;
1001 /* cm_mem_c_001.main_26 : Fixes for Compilation Warnings */
1002 #ifndef USE_PURE
1003    uint16_t        cnt;
1004 #endif
1005 /*   cm_mem_c_001.main_15 : Additions */
1006 #if (defined(SS_MEM_LEAK_STS) || defined( BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
1007    Size       reqSz;
1008 #endif /* SS_MEM_LEAK_STS */
1009 /* cm_mem_c_001.main_12 - addition to hold the allocated block */
1010 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
1011    CmMmBlkHdr *alocBlk;
1012 #endif /* SSI_DEBUG_LEVEL1 */
1013 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
1014    CmMmBlkHdr    *ptrHdr;
1015 #endif
1016 /*   cm_mem_c_001.main_15 : Additions */
1017 #ifdef SS_HISTOGRAM_SUPPORT
1018         S8 hstGrmBuf[256];
1019 #endif /* SS_HISTOGRAM_SUPPORT */
1020 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
1021          uint16_t memIndex=0;
1022 Data  *nextptr1;
1023 Data  *prevptr1;
1024 Data  **nextptr;
1025 Data  **prevptr;
1026       nextptr=&nextptr1;
1027       prevptr=&prevptr1;
1028 #endif
1029
1030    
1031 #ifndef USE_MEMCAL
1032    UNUSED(flags);
1033 #endif
1034 /*  cm_mem_c_001.main_15 : Additions */
1035 #if (defined(SS_MEM_LEAK_STS) || defined( BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
1036    reqSz = *size;
1037 #endif /* SS_MEM_LEAK_STS */
1038
1039    regCb = (CmMmRegCb *)regionCb;
1040
1041 #if (ERRCLASS & ERRCLS_INT_PAR)
1042
1043    /* error check on parameters */
1044    if ((regCb == NULLP) || (size == NULLP) || !(*size) || (ptr == NULLP))
1045    {
1046       return RFAILED;
1047    }
1048 #endif
1049   
1050 /* cm_mem_c_001.main_12 - addition for checking memType parameter */
1051 #ifdef SSI_DEBUG_LEVEL1
1052 #if (ERRCLASS & ERRCLS_INT_PAR)
1053       if ((memType != CMM_STATIC_MEM_FLAG) && (memType != CMM_DYNAMIC_MEM_FLAG))
1054       {
1055          return RFAILED;
1056       }
1057 #endif /* (ERRCLASS & ERRCLS_INT_PAR) */
1058 #endif /* SSI_DEBUG_LEVEL1 */
1059
1060 #ifndef USE_PURE
1061
1062    if (flags)
1063       num_times++;
1064
1065 /* cm_mem_c_001.main_12 - addition to insert the size into hash list */
1066 #ifdef SSI_DEBUG_LEVEL1
1067    /* Update the hash list */
1068    if (cmMmHashListInsert(&(regCb->hashListCp), *size) != ROK)
1069    {
1070       /* display that, this entry could not be made in the hash list */
1071 #ifdef DEBUGP
1072       /* display an error message here */
1073 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/          
1074 #ifdef ALIGN_64BIT          
1075       sprintf(dbgPrntBuf, "\n Could not make an entry for size %u in hash table of region %d \n",
1076                            *size, regCb->region);
1077 #else                           
1078       sprintf(dbgPrntBuf, "\n Could not make an entry for size %lu in hash table of region %d \n",
1079                            *size, regCb->region);
1080 #endif                           
1081       SDisplay(0, dbgPrntBuf);
1082 #endif /* DEBUGP */
1083    }
1084 #endif /* SSI_DEBUG_LEVEL1 */
1085
1086    /* 
1087     * Check if the requested size is less than or equal to the maximum block 
1088     * size in the bucket. 
1089     */
1090    if ( *size <= regCb->bktMaxBlkSize)
1091    {
1092       /* Get the map to the mapping table */
1093       idx = ((*size - 1) >> regCb->bktQnPwr);
1094
1095 #if (ERRCLASS & ERRCLS_DEBUG)
1096       if (regCb->mapTbl[idx].bktIdx == 0xFF)
1097       { 
1098          /* Some fatal error in the map table initialization. */
1099          return RFAILED;
1100       }
1101 #endif
1102
1103       /* Dequeue the memory block and return it to the user */
1104       bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]); 
1105
1106       cnt = 0;
1107       /* While loop is introduced to use the "break statement inside */
1108       while (cnt < 1)
1109       {
1110          /*
1111           * Check if the size request is not greater than the size available
1112           * in the bucket
1113           */
1114          if (*size > bkt->size)
1115          {
1116             /* Try to go to the next bucket if available */
1117             if((idx < (CMM_MAX_MAP_ENT - 1)) &&
1118                (regCb->mapTbl[++idx].bktIdx != 0xFF))
1119             {
1120                bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]);
1121             }
1122             else
1123             {
1124                /* This is the last bucket, try to allocate from heap */
1125                break;
1126             }
1127          }
1128
1129          /* Acquire the bucket lock */
1130          /* cm_mem_c_001.main_13 : Replaced SLock with WTLock for NT */
1131 #ifdef SS_WIN
1132          (Void) WTLock(&(bkt->bktLock));
1133 #else
1134          (Void) SLock(&(bkt->bktLock));
1135 #endif
1136 #ifdef XEON_SPECIFIC_CHANGES         
1137          CM_MEM_ALLOC_STS(regCb->region, idx); 
1138 #endif         
1139
1140 #if (ERRCLASS & ERRCLS_DEBUG)
1141          regCb->mapTbl[idx].numReq++;
1142 #endif /* (ERRCLASS & ERRCLS_DEBUG) */
1143
1144 /* cm_mem_c_001.main_12 - addition for sanity check before allocation */
1145 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
1146       /* increment the allocation attempt counter at bucket level */
1147       bkt->numAllocAttempts++;
1148
1149       /* detect trampling if any and call sanity check. This is done for (bkt->nextBlk) as
1150          the allocation is always from (bkt->nextBlk) */
1151       if (bkt->nextBlk)
1152       {
1153 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1154          if(cmMmRegIsBlkSane(bkt->nextBlk, bkt->size) != ROK)
1155 #else
1156          if (cmMmRegIsBlkSane(bkt->nextBlk) != ROK)
1157 #endif
1158          {
1159                /* detected a trampled memory block in this bucket */
1160 #ifdef DEBUGP
1161                /* display an error message here */
1162 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/          
1163 #ifdef ALIGN_64BIT          
1164                sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, size %u bytes \n",
1165                                     (void *)bkt->nextBlk, regCb->mapTbl[idx].bktIdx, *size);
1166 #else                                    
1167                sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, size %lu bytes \n",
1168                                     (void *)bkt->nextBlk, regCb->mapTbl[idx].bktIdx, *size);
1169 #endif                                    
1170                SDisplay(0, dbgPrntBuf);
1171 #endif /* DEBUGP */
1172                abort();
1173 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1174                if (cmMmBktSanityChk(bkt) == RTRAMPLINGNOK)
1175                {
1176                   /* Release the lock */
1177                   /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1178 #ifdef SS_WIN
1179                   (Void) WTUnlock(&(bkt->bktLock));
1180 #else
1181                   (Void) SUnlock(&(bkt->bktLock));
1182 #endif
1183                   /* handle RTRAMPLINGNOK in SAlloc/SGetSBuf */
1184                   return (RTRAMPLINGNOK);
1185                }
1186                else
1187 #endif
1188                {
1189                   /* Release the lock */
1190                   /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1191 #ifdef SS_WIN
1192                   (Void) WTUnlock(&(bkt->bktLock));
1193 #else
1194                   (Void) SUnlock(&(bkt->bktLock));
1195 #endif
1196                   /* return RFAILED */
1197                   return RFAILED;
1198                }
1199          }
1200       }
1201       *ptr = (Data *)(bkt->nextBlk) + (sizeof(CmMmBlkHdr)); /* ccpu00125353: warning fix */
1202 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
1203       ptrHdr = (CmMmBlkHdr *)bkt->nextBlk;
1204 #endif
1205 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1206       /* Initialize the elements with 0xAB */
1207       memset(*ptr, 0xAB, *size);
1208 #endif
1209 //      DU_LOG("\nINFO  --> Pointer allocated %8p size %d\n", *ptr, *size);
1210       /* Store this pointer in hash list */
1211       if ((bkt->nextBlk) && *ptr)
1212 #elif SS_LIGHT_MEM_LEAK_STS
1213       *ptr = (Data *)(bkt->nextBlk) + (sizeof(CmMmBlkHdr)); /* ccpu00125353: warning fix */
1214       if ((bkt->nextBlk) && *ptr)
1215 #else
1216          *ptr = bkt->next;/* ccpu00125353: warning fix */
1217          if (*ptr != NULLP)
1218 #endif /* SSI_DEBUG_LEVEL1 */
1219          {
1220 /* cm_mem_c_001.main_12 - addition for header */
1221 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
1222       /* point to next block header */
1223          alocBlk = bkt->nextBlk;
1224          bkt->nextBlk = (CmMmBlkHdr *)(bkt->nextBlk->nextBlk);
1225 #else
1226 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
1227    if(bkt->size == 128)
1228    {
1229       memIndex = (*ptr - startPtr128) / 128;
1230    }
1231    if(bkt->size == 256)
1232    {
1233       memIndex = (*ptr - startPtr256) / 256;
1234    }
1235 #if 1
1236          if (*((uint32_t *)(*ptr + 4)) != 0xDEADDEAD && *((uint32_t *)(*ptr + 80)) != 0xDEADDEAD && *((uint32_t *)(*ptr + 24)) != 0xDEADDEAD)
1237          {
1238           }
1239             if(bkt->size == 256)
1240           {  
1241           }
1242          }
1243 #endif
1244 #endif /* MS_MBUF_CORRUPTION */
1245 #ifdef SSI_MEM_CORR_PREVENTION
1246             *(((uint32_t *)(*ptr)) + 2) = 0;
1247 #endif
1248
1249 #ifdef T2K_MEM_LEAK_DBG 
1250
1251             {
1252                /* Lock before the transaction-start */
1253                pthread_mutex_lock(&(regMemLeakInfo.memLock[regCb->region]));
1254                InsertToT2kMemLeakInfo( (uint64_t)*ptr,*size,line,file,regCb->region);
1255                /* UnLock after the transaction */
1256                pthread_mutex_unlock(&(regMemLeakInfo.memLock[regCb->region]));
1257             }
1258 #endif   /* T2K_MEM_LEAK_DBG */ 
1259
1260             bkt->next = *((CmMmEntry **)(bkt->next));
1261 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
1262          *nextptr = bkt->next;
1263          if (*((uint32_t *)(*nextptr + 4)) != 0xDEADDEAD && *((uint32_t *)(*nextptr + 80)) != 0xDEADDEAD && *((uint32_t *)(*nextptr + 24)) != 0xDEADDEAD)
1264          {
1265             if(bkt->size == 128)
1266                *prevptr = startPtr128 + ((memIndex-1)*128);
1267             if(bkt->size == 256)
1268                *prevptr = startPtr256 + ((memIndex-1)*256);
1269             if(bkt->size == 128)
1270                memIndex = (*nextptr - startPtr128) / 128;
1271             if(bkt->size == 256)
1272                memIndex = (*nextptr - startPtr256) / 256;
1273             nextptr1=NULLP;
1274             *nextptr1=9;
1275          }
1276       if(bkt->size == 128)
1277       {
1278          memIndex = (*ptr - startPtr128) / 128;
1279          cmMemInfo128[memIndex][0] = cmAllocCaller[MxGetCpuID()];
1280       }
1281       if(bkt->size == 256)
1282       {
1283          memIndex = (*ptr - startPtr256) / 256;
1284          cmMemInfo256[memIndex][0] = cmAllocCaller[MxGetCpuID()];
1285       }
1286       cmAllocCaller[MxGetCpuID()] = NULLP;
1287          *((uint32_t *)(*ptr + 4)) = 0x00000000;
1288          *((uint32_t *)(*ptr + 124)) = 0;
1289          *((uint32_t *)(*ptr + 24)) = 0x00000000;
1290          *((uint32_t *)(*ptr + 44)) = 0x00000000;            
1291          *((uint32_t *)(*ptr + 80)) = 0x00000000;
1292          *((uint32_t *)(*ptr + 116)) = 0x00000000;
1293 #endif
1294 #endif /* SSI_DEBUG_LEVEL1 */
1295
1296 /* cache_coherency_changes */
1297 #ifdef LOWERARM
1298       MxDMB();
1299 #endif
1300             /* 
1301              * Increment the statistics variable of number of memory block 
1302              * allocated 
1303              */
1304             bkt->numAlloc++;
1305             if (bkt->numAlloc > bkt->maxAlloc)
1306             {
1307                bkt->maxAlloc = bkt->numAlloc;
1308             }
1309 #ifdef CM_MEM_OVERUSED
1310             {
1311                if (g_overused[bktIdx] == 0 && OVERUSED(bkt))
1312                {
1313                   g_overused[bktIdx] = 1;
1314                   /*DU_LOG("\nINFO  --> cmAlloc: bktIdx %u overused %u numAlloc %u\n", bktIdx, g_overused[bktIdx], bkt->numAlloc); */
1315                }
1316             }
1317 #endif
1318 /* cm_mem_c_001.main_12 - addition for header manipulation */
1319 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
1320       /* update the size for which this memory block has been allocated */
1321       alocBlk->requestedSize = *size;
1322       /* update the memory block header */
1323       CMM_RESET_FREE_FLAG(alocBlk->memFlags);
1324       if (memType == CMM_STATIC_MEM_FLAG)
1325       {
1326          CMM_SET_STATIC_FLAG(alocBlk->memFlags);
1327          /* add it to the static memory allocated */
1328          bkt->staticMemUsed += bkt->size;
1329       }
1330       else
1331       {
1332          CMM_SET_DYNAMIC_FLAG(alocBlk->memFlags);
1333          /* add it to the dynamic memory allocated */
1334          bkt->dynamicMemUsed += bkt->size;
1335       }
1336 #elif SS_LIGHT_MEM_LEAK_STS 
1337       alocBlk->requestedSize = *size;
1338       alocBlk->lineNo        = lineNo;
1339       alocBlk->currFuncName  = funcName;
1340       if(gmemLkCb.isStarted == TRUE)
1341       {
1342          alocBlk->allocQueueIndx = cmStorAllocBlk(alocBlk);
1343       }
1344 #endif /* SSI_DEBUG_LEVEL1 */
1345
1346             if (((bkt->size - (*size)) >> regCb->bktQnPwr) && flags)
1347             {
1348                bkt->bktNoFitCnt++;
1349 #ifdef MEMCAL_DEBUG
1350 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/          
1351 #ifdef ALIGN_64BIT          
1352                sprintf(prntBuf,
1353    "[MEM_CAL_CNTA] %u bytes request not fitted in bkt %d [size %u bytes] %u times\n", *size, regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktNoFitCnt++);
1354 #else   
1355                sprintf(prntBuf,
1356    "[MEM_CAL_CNTA] %lu bytes request not fitted in bkt %d [size %lu bytes] %lu times\n", *size, regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktNoFitCnt++);
1357 #endif   
1358                SDisplay(0, prntBuf);
1359 #endif
1360             }
1361
1362 #ifdef MEMCAL_DEBUG
1363             if (flags)
1364             {
1365                sprintf(prntBuf,
1366     "SGetSBuf:%08lu:Size Bucket Id:%03d  Times:%05lu  Pointer: %8p\n",
1367                     *size, regCb->mapTbl[idx].bktIdx, num_times, *ptr);
1368                SDisplay(0, prntBuf);
1369             }
1370 #endif /* MEMCAL_DEBUG */
1371  /*  cm_mem_c_001.main_15 : Additions */
1372 #ifdef SS_HISTOGRAM_SUPPORT
1373             /* If If Tapa task (entId)is registerd for histogram then insert Memrory allocated
1374              * information into the hash list */
1375             if(hstReg)
1376             {
1377                if (cmHstGrmAllocInsert(&(bkt->hstGrmHashListCp), bkt->size, size, line, fileName, entId) != ROK)
1378                {
1379                  sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
1380                                           SPrint(hstGrmBuf);
1381                }
1382             }/* End of if */
1383
1384 #endif /* SS_HISTOGRAM_SUPPORT */
1385         
1386             /* Update the size parameter */
1387             *size = bkt->size;
1388 #ifdef SS_MEM_LEAK_STS
1389         /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
1390          cmStorAllocBlk((PTR)*ptr, (Size) reqSz, (Size) *size,
1391                           regCb->mapTbl[idx].bktIdx);
1392 #elif BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
1393          cmStorAllocBlk(ptrHdr, (Size)reqSz, (Size) *size,
1394                        regCb->mapTbl[idx].bktIdx, regCb);
1395 #endif /* SS_MEM_LEAK_STS */
1396
1397       /* cm_mem_c_008.104 - Addition for memory calculator tool */
1398
1399             /* Release the lock */
1400             /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1401 #ifdef SS_WIN
1402             (Void) WTUnlock(&(bkt->bktLock));
1403 #else
1404             (Void) SUnlock(&(bkt->bktLock));
1405 #endif
1406
1407             return ROK;
1408          }
1409          else if (flags)
1410          {
1411             bkt->bktFailCnt++;
1412 #ifdef MEMCAL_DEBUG
1413 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/          
1414 #ifdef ALIGN_64BIT          
1415  sprintf(prntBuf,
1416             "[MEM_CAL_CNTB] Allocation failed in bucket %d [ size %u bytes], %u times\n", regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktFailCnt);
1417 #else            
1418  sprintf(prntBuf,
1419             "[MEM_CAL_CNTB] Allocation failed in bucket %d [ size %lu bytes], %lu times\n", regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktFailCnt);
1420 #endif            
1421             SDisplay(0, prntBuf);
1422 #endif
1423          }
1424
1425 #if (ERRCLASS & ERRCLS_DEBUG)
1426          regCb->mapTbl[idx].numFailure++;
1427 #endif /* (ERRCLASS & ERRCLS_DEBUG) */
1428
1429          /* Release the lock */
1430          /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1431 #ifdef SS_WIN
1432             (Void) WTUnlock(&(bkt->bktLock));
1433 #else
1434             (Void) SUnlock(&(bkt->bktLock));
1435 #endif
1436          cnt = cnt + 1;
1437       }
1438    }
1439    else
1440    {
1441       if (flags)
1442       {
1443          regCb->heapCb.heapAllocCnt++;
1444 #ifdef MEMCAL_DEBUG
1445 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/          
1446 #ifdef ALIGN_64BIT          
1447          sprintf(prntBuf,
1448                  "[MEM_CAL_CNTC]  No bucket block configured for %u bytes \n Number of blocks allocated from heap = %u\n",*size,
1449                  regCb->heapCb.heapAllocCnt);
1450 #else                 
1451          sprintf(prntBuf,
1452                  "[MEM_CAL_CNTC]  No bucket block configured for %lu bytes \n Number of blocks allocated from heap = %lu\n",*size,
1453                  regCb->heapCb.heapAllocCnt);
1454 #endif                 
1455          SDisplay(0, prntBuf);
1456 #endif
1457       }
1458    }
1459
1460    /* Memory not available in the bucket pool */
1461    if (regCb->heapFlag &&  (*size < regCb->heapSize))
1462    {
1463 #ifdef MEMCAL_DEBUG
1464       if (flags) tryHeap = 1;
1465 #endif
1466       /* 
1467        * The heap memory block is available. Allocate the memory block from
1468        * heap pool.
1469        */ 
1470 /* cm_mem_c_001.main_15: Additions */
1471 #ifdef SS_HISTOGRAM_SUPPORT  
1472 /* cm_mem_c_001.main_12 - addition for passing an extra parameter */
1473 #ifdef SSI_DEBUG_LEVEL1
1474        return (cmHeapAlloc(&(regCb->heapCb), ptr, size, memType, line, fileName, entId, hstReg));
1475 #else
1476        return (cmHeapAlloc(&(regCb->heapCb), ptr, size, line, fileName, entId, hstReg));
1477 #endif /* SSI_DEBUG_LEVEL1 */
1478 #else
1479 /* cm_mem_c_001.main_12 - addition for passing an extra parameter */
1480 #ifdef SSI_DEBUG_LEVEL1
1481        return (cmHeapAlloc(&(regCb->heapCb), ptr, size, memType));
1482 #else
1483        return (cmHeapAlloc(&(regCb->heapCb), ptr, size));
1484 #endif /* SSI_DEBUG_LEVEL1 */
1485 #endif /* SS_HISTOGRAM_SUPPORT */
1486    }
1487
1488    /* No memory available */
1489    return RFAILED;
1490 #else /* use pure is on */
1491 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/   
1492 #ifdef SS_4GMX_LCORE
1493    *ptr = (Data*) MxHeapAlloc(SsiHeap, *size);
1494    memset(ptr, 0, *size);
1495 #else
1496    *ptr = (Data*) malloc(*size);
1497 #endif
1498    if ( (*ptr) == NULLP)
1499        return RFAILED;
1500    avail_size -= *size;
1501    return ROK;
1502 #endif /* USE_PURE */
1503
1504 } /* end of cmAlloc */
1505
1506 \f
1507 /*
1508 *
1509 *       Fun:   cmFree
1510 *
1511 *       Desc:  Return the memory block for the memory region.
1512 *
1513 *
1514 *       Ret:   ROK     - successful
1515 *              RFAILED - unsuccessful.
1516 *
1517 *       Notes: The user calls this function to return the previously allocated 
1518 *              memory block to the memory region. The memory manager does not 
1519 *              check the validity of the state of the memory block(like whether 
1520 *              it was allocated earlier). The caller must be sure that, the 
1521 *              address specified in the parameter 'ptr' is valid and was 
1522 *              allocated previously from same region.
1523 *
1524 *
1525 *       File:  cm_mem.c
1526 *
1527 */
1528
1529 /*  cm_mem_c_001.main_15 : Additions */
1530 #ifdef SS_LIGHT_MEM_LEAK_STS
1531 static S16  cmFree
1532 (
1533 Void     *regionCb,
1534 Data     *ptr, 
1535 Size     size,
1536 uint32_t lineNo,
1537 uint8_t  *funcName
1538 )
1539 #else /*SS_LIGHT_MEM_LEAK_STS */
1540 #ifdef SS_HISTOGRAM_SUPPORT
1541 static S16  cmFree
1542 (
1543 Void     *regionCb,
1544 Data     *ptr,
1545 Size     size,
1546 uint32_t line,
1547 uint8_t  *fileName,
1548 uint8_t  entId,
1549 Bool     hstReg
1550 )
1551
1552 #else
1553 #ifdef T2K_MEM_LEAK_DBG
1554 static S16  cmFree
1555 (
1556 Void   *regionCb,
1557 Data   *ptr,
1558 Size   size,
1559 char*  file,
1560 uint32_t line
1561  )
1562 #else /* T2K_MEM_LEAK_DBG */
1563 static S16  cmFree
1564 (
1565 Void   *regionCb,
1566 Data   *ptr, 
1567 Size    size
1568 )
1569 #endif
1570 /*  cm_mem_c_001.main_15 : Additions */
1571 #endif /* SS_HISTOGRAM_SUPPORT */ 
1572 #endif /*SS_LIGHT_MEM_LEAK_STS */
1573
1574 {
1575 /* cm_mem_c_001.main_26 : Fixes for Compilation Warnings */
1576 #ifndef USE_PURE
1577    uint16_t        idx;
1578    CmMmBkt   *bkt;
1579 #endif
1580    CmMmRegCb *regCb;
1581 /* cm_mem_c_001.main_12 - addition for holding the free pointer */
1582 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
1583    CmMmBlkHdr *ptrHdr;
1584 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1585    CmMmBlkHdr *lastHdr;
1586 #endif
1587 #endif /* SSI_DEBUG_LEVEL1 */
1588 /*  cm_mem_c_001.main_15 : Additions */
1589 #ifdef SS_HISTOGRAM_SUPPORT 
1590         S8 hstGrmBuf[256];
1591 #endif /* SS_HISTOGRAM_SUPPORT */
1592 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption*/
1593          uint16_t memIndex=0;
1594 #endif
1595
1596
1597    regCb = (CmMmRegCb *)regionCb;
1598
1599 #ifndef USE_PURE
1600 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1601    /* Check if the memory block is from the memory region */
1602    if (ptr >= ((CmMmRegCb *)regCb)->regInfo.start +
1603                ((CmMmRegCb *)regCb)->regInfo.size) 
1604    {
1605       return RFAILED;
1606    }
1607 #endif
1608 #if (ERRCLASS & ERRCLS_INT_PAR)
1609
1610    /* error check on parameters */
1611    if ((regCb == NULLP) || (!size) || (ptr == NULLP))
1612    {
1613       return RFAILED;
1614    }
1615
1616 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1617    /* Check if the memory block is from the memory region */
1618    if (ptr >= ((CmMmRegCb *)regCb)->regInfo.start +
1619                ((CmMmRegCb *)regCb)->regInfo.size) 
1620    {
1621       return RFAILED;
1622    }
1623 #endif
1624         /* cm_mem_c_001.main_20 Addition */
1625         if (ptr < regCb->regInfo.start)
1626         {
1627           return RFAILED;
1628         }
1629
1630 #endif
1631
1632    /* 
1633     * Check if the memory block was allocated from the bucket pool. 
1634     */
1635
1636    if (ptr < (regCb->regInfo.start + regCb->bktSize))
1637    {
1638 #ifdef T2K_MEM_LEAK_DBG
1639       {
1640          pthread_mutex_lock(&(regMemLeakInfo.memLock[regCb->region]));
1641          RemoveFromT2kMemLeakInfo((uint64_t)ptr , file,line,regCb->region);
1642          pthread_mutex_unlock(&(regMemLeakInfo.memLock[regCb->region]));
1643       }
1644 #endif
1645       /* The memory block was allocated from the bucket pool */
1646
1647       /* Get the map to the mapping table */
1648       idx = ((size - 1) >> regCb->bktQnPwr);
1649
1650 #if (ERRCLASS & ERRCLS_DEBUG)
1651       if (regCb->mapTbl[idx].bktIdx == 0xFF)
1652       { 
1653          /* Some fatal error in the map table initialization. */
1654          return RFAILED;
1655       }
1656 #endif
1657
1658       /* Enqueue the memory block and return it to the user */
1659       bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]); 
1660
1661       /*
1662        * Check if the size is not greater than the size available
1663        * in the bucket. If so, then the buffer must have been allocated
1664        * from next bucket.  We don't need to check the validity of the
1665        * next bucket, otherwise buffer must have been allocated from heap
1666        * pool.
1667        */
1668        if (size > bkt->size)
1669        {
1670           bkt = &(regCb->bktTbl[regCb->mapTbl[++idx].bktIdx]);
1671        }
1672
1673       /* Acquire the bucket lock */
1674       /* cm_mem_c_001.main_13 : Replaced SLock with WTLock for NT */
1675 #ifdef SS_WIN
1676       (Void) WTLock(&(bkt->bktLock));
1677 #else
1678       (Void) SLock(&(bkt->bktLock));
1679 #endif
1680 #ifdef XEON_SPECIFIC_CHANGES         
1681      CM_MEM_FREE_STS(regCb->region, idx);
1682 #endif    
1683 /* cache_coherency_changes */
1684 #ifdef LOWERARM
1685       MxDMB();
1686 #endif
1687
1688 /* cm_mem_c_001.main_12 - addition for sanity check and free */
1689 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
1690       /* increment the dealloc attempt counter at bucket level */
1691       bkt->numDeallocAttempts++;
1692
1693       /* Check the memFlags to see whether this block was allocated */
1694       ptrHdr = (CmMmBlkHdr *) (ptr - sizeof(CmMmBlkHdr));
1695 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
1696       cmRlsAllocBlk(ptrHdr, regCb);
1697 #endif
1698 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1699       /* Check for ptr size */
1700       if(((ptrHdr->requestedSize - size) % size) != 0)
1701       {
1702 #ifdef DEBUGP
1703          sprintf(dbgPrntBuf, "Passed size (%d) does not match with allocated \
1704                size(%d) %8p, Bucket Id:%03d\n",
1705                size, ptrHdr->requestedSize, ptr, regCb->mapTbl[idx].bktIdx);
1706 #endif
1707          DU_LOG("\nERROR  --> Passed size (%d) does not match with allocated \
1708                size(%d) %8p, Bucket Id:%03d\n",
1709                size, ptrHdr->requestedSize, ptr, regCb->mapTbl[idx].bktIdx);
1710          abort();
1711       }
1712       /* Validate the tail part to see if there is any over run */
1713 //      DU_LOG("\nINFO  --> Pointer free request %8p, size %d\n", ptr, size);
1714 #endif
1715
1716       /* validate the block to be freed for trampling */
1717 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1718       if(cmMmRegIsBlkSane(ptrHdr, bkt->size) != ROK)
1719 #else
1720       if (cmMmRegIsBlkSane(ptrHdr) != ROK)
1721 #endif
1722       {
1723           /* Handle error case of Memory trampling */
1724 #ifdef  DEBUGP
1725           /* display an error message here */
1726 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/          
1727 #ifdef ALIGN_64BIT          
1728           sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, size %u bytes \n",
1729                                ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
1730 #else                               
1731           sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, size %lu bytes \n",
1732                                ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
1733 #endif                               
1734           SDisplay(0, dbgPrntBuf);
1735 #endif /* DEBUGP */
1736           abort();
1737            /* 
1738            * if sanity check returns RTRAMPLINGOK then there is nothing to do
1739            * as the memory blk is already invalidated in cmMmBktSanityChk
1740            */
1741 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1742            if (cmMmBktSanityChk(bkt) == RTRAMPLINGOK)
1743            {
1744               bkt->numAlloc--;
1745
1746               /* Release the lock */
1747               /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1748 #ifdef SS_WIN
1749               (Void) WTUnlock(&(bkt->bktLock));
1750 #else
1751               (Void) SUnlock(&(bkt->bktLock));
1752 #endif
1753
1754               return ROK;
1755            }
1756            else
1757 #endif
1758            {
1759                /* 
1760                * this is the case where in the entire bucket has been made unusable
1761                * Release the lock 
1762                */
1763                /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1764 #ifdef SS_WIN
1765                (Void) WTUnlock(&(bkt->bktLock));
1766 #else
1767                (Void) SUnlock(&(bkt->bktLock));
1768 #endif
1769
1770                 /* handle RTRAMPLINGNOK in SFree/SPutSBuf */
1771                 return (RTRAMPLINGNOK);
1772            }
1773 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1774          DU_LOG("\nERROR  --> Memory signature is invalid\n");
1775          abort();
1776 #endif
1777       }
1778
1779       /* reset the size */
1780       ptrHdr->requestedSize = 0;
1781 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1782       /* Initialize the elements with 0xAB */
1783       memset(ptr, 0xAB, size);
1784 #endif
1785       /* check if the block to be freed is already having the state as FREE */
1786       if (CMM_IS_FREE(ptrHdr->memFlags))
1787       {
1788             /* Handle double deallocation error case */
1789 #ifdef DEBUGP
1790             /* display an error message here */
1791 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/          
1792 #ifdef ALIGN_64BIT          
1793          sprintf(dbgPrntBuf, "Attempt to double deallocate memory at: %8p, Bucket Id:%03d, size %u bytes \n",
1794                               ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
1795 #else                              
1796          sprintf(dbgPrntBuf, "Attempt to double deallocate memory at: %8p, Bucket Id:%03d, size %lu bytes \n",
1797                               ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
1798 #endif                              
1799          SDisplay(0, dbgPrntBuf);
1800 #endif /* DEBUGP */
1801
1802           /* Release the lock */
1803           /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1804 #ifdef SS_WIN
1805           (Void) WTUnlock(&(bkt->bktLock));
1806 #else
1807           (Void) SUnlock(&(bkt->bktLock));
1808 #endif
1809 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1810          DU_LOG("\nERROR  --> Attempt to double deallocate memory at: %8p, Bucket Id:%03d,\
1811                  size %u bytes \n", ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
1812           abort();
1813 #endif
1814           /* handle RDBLFREE in SFree/SPutSBuf */
1815           return (RDBLFREE);
1816       }
1817       if (CMM_IS_STATIC(ptrHdr->memFlags))
1818       {
1819          CMM_SET_FREE_FLAG(ptrHdr->memFlags);
1820          CMM_RESET_STATIC_FLAG(ptrHdr->memFlags);
1821          /* deduct it from the static memory count */
1822          bkt->staticMemUsed -= bkt->size;
1823       }
1824       else if (CMM_IS_DYNAMIC(ptrHdr->memFlags))
1825       {
1826          CMM_SET_FREE_FLAG(ptrHdr->memFlags);
1827          CMM_RESET_DYNAMIC_FLAG(ptrHdr->memFlags);
1828          /* deduct it from the dynamic memory count */
1829          bkt->dynamicMemUsed -= bkt->size;
1830       }
1831       else
1832       {
1833          /* This is a case similar to trampled memory */
1834 #ifdef  DEBUGP
1835 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/          
1836 #ifdef ALIGN_64BIT          
1837          sprintf(dbgPrntBuf, "Invalid memory flag: %u !!!\n", ptrHdr->memFlags);
1838 #else         
1839          sprintf(dbgPrntBuf, "Invalid memory flag: %lu !!!\n", ptrHdr->memFlags);
1840 #endif         
1841          SDisplay(0, dbgPrntBuf);
1842 #endif /* DEBUGP */
1843 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1844          if (cmMmBktSanityChk(bkt) == RTRAMPLINGOK)
1845          {
1846             /* do not add to the free list */
1847             bkt->numAlloc--;
1848
1849             /* Release the lock */
1850             /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1851 #ifdef SS_WIN
1852             (Void) WTUnlock(&(bkt->bktLock));
1853 #else
1854             (Void) SUnlock(&(bkt->bktLock));
1855 #endif
1856             return ROK;
1857          }
1858          else
1859 #endif
1860          {
1861             /* 
1862             * this is the case where in the entire bucket has been made unusable
1863             * Release the lock 
1864             */
1865             /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1866 #ifdef SS_WIN
1867             (Void) WTUnlock(&(bkt->bktLock));
1868 #else
1869             (Void) SUnlock(&(bkt->bktLock));
1870 #endif
1871
1872             /* handle RTRAMPLINGNOK in SFree/SPutSBuf */
1873             return (RTRAMPLINGNOK);
1874          }
1875       }
1876 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1877       /* Return the block to memory */
1878       ptrHdr->nextBlk = bkt->nextBlk;
1879       bkt->nextBlk = ptrHdr;
1880 #endif
1881 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1882       /* Move the ptr to end of the bucket */
1883       lastHdr = (CmMmBlkHdr *)bkt->lastBlk;
1884       lastHdr->nextBlk = ptrHdr;
1885       bkt->lastBlk = ptrHdr;
1886       ptrHdr->nextBlk = NULLP;
1887 #endif
1888 #elif SS_LIGHT_MEM_LEAK_STS
1889       ptrHdr = (CmMmBlkHdr *) (ptr - sizeof(CmMmBlkHdr));
1890       ptrHdr->lineNo        = lineNo;
1891       ptrHdr->currFuncName  =  funcName;
1892       if(gmemLkCb.isStarted == TRUE)
1893       {
1894          cmRlsAllocBlk(ptrHdr->allocQueueIndx);
1895       }
1896       ptrHdr->nextBlk = bkt->nextBlk;
1897       bkt->nextBlk = ptrHdr;
1898 #else
1899
1900 #ifndef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
1901 #ifdef SSI_MEM_CORR_PREVENTION
1902       if (*(((uint32_t *)(ptr)) + 2) == 0xdeaddead)
1903       {
1904          /* Do not free an already freed block to avoid corruption */
1905          cmDblFreeAttempts++;
1906          bkt->numAlloc++;
1907       }
1908       else
1909       {
1910          *((CmMmEntry **)bkt->last) = ptr;
1911          bkt->last = (CmMmEntry *)ptr;
1912          *((CmMmEntry **)ptr) = NULLP;
1913          *(((uint32_t *)(ptr)) + 2) = 0xdeaddead;
1914       }
1915 #else
1916       *((CmMmEntry **)ptr) =  bkt->next; 
1917       bkt->next = (CmMmEntry *)ptr;
1918 #endif
1919 #else
1920      if(memFreeCount >= 125000)
1921             memFreeCount = 0;
1922       if(bkt->size == 128)
1923       {
1924          Data *crashPtr=NULLP;
1925          if(((ptr - startPtr128) % 128) != 0)
1926          {
1927             *crashPtr = 9;
1928          }
1929          memIndex = (ptr - startPtr128) / 128;
1930       }
1931       if(bkt->size == 256)
1932       {
1933          Data *crashPtr=NULLP;
1934          if(((ptr - startPtr256) % 256) != 0)
1935          {
1936             *crashPtr = 9;
1937          }
1938          memIndex = (ptr - startPtr256) / 256;
1939       }
1940       if(bkt->size == 512)
1941       {
1942          Data *crashPtr=NULLP;
1943          if(((ptr - startPtr512) % 512) != 0)
1944          {
1945             *crashPtr = 9;
1946          }
1947       }
1948       if(bkt->size == 768)
1949       {
1950          Data *crashPtr=NULLP;
1951          if(((ptr - startPtr768) % 768) != 0)
1952          {
1953             *crashPtr = 9;
1954          }
1955       }
1956       if(bkt->size == 1664)
1957       {
1958          Data *crashPtr=NULLP;
1959          if(((ptr - startPtr1664) % 1664) != 0)
1960          {
1961             *crashPtr = 9;
1962          }
1963       }
1964       if(bkt->size == 4800)
1965       {
1966          Data *crashPtr=NULLP;
1967          if(((ptr - startPtr4800) % 4800) != 0)
1968          {
1969             *crashPtr = 9;
1970          }
1971       }
1972       if(bkt->size == 9920)
1973       {
1974          Data *crashPtr=NULLP;
1975          if(((ptr - startPtr9920) % 9920) != 0)
1976          {
1977             *crashPtr = 9;
1978          }
1979       }
1980       if (*((uint32_t *)(ptr + 4)) != 0xDEADDEAD)
1981       {
1982          *(uint32_t *)(ptr + 4) = 0xDEADDEAD;
1983       }
1984       else
1985       {
1986          Data *crashPtr=NULLP;
1987          *crashPtr = 9;
1988       }
1989       if (*((uint32_t *)(ptr + 24)) != 0xDEADDEAD)
1990       {
1991          *(uint32_t *)(ptr + 24) = 0xDEADDEAD;
1992       }
1993       else
1994       {
1995          Data *crashPtr=NULLP;
1996          *crashPtr = 9;
1997       }
1998       if (*((uint32_t *)(ptr + 44)) != 0xDEADDEAD)
1999       {
2000          *(uint32_t *)(ptr + 44) = 0xDEADDEAD;
2001       }
2002       else
2003       {
2004          Data *crashPtr=NULLP;
2005          *crashPtr = 9;
2006       }
2007       if (*((uint32_t *)(ptr + 80)) != 0xDEADDEAD)
2008       {
2009          *(uint32_t *)(ptr + 80) = 0xDEADDEAD;
2010       }
2011       else
2012       {
2013          Data *crashPtr=NULLP;
2014          *crashPtr = 9;
2015          /* Cause a crash to identify the caller */
2016       }
2017       *(uint32_t *)(ptr + 124) = memFreeCount++;
2018       (*(uint32_t *)(ptr + 116)) = cmFreeCaller[MxGetCpuID()];
2019       if(bkt->size == 128)
2020       {
2021          memIndex = (ptr - startPtr128) / 128;
2022          cmMemInfo128[memIndex][1] = cmFreeCaller[MxGetCpuID()];
2023       }
2024       if(bkt->size == 256)
2025       {
2026          memIndex = (ptr - startPtr256) / 256;
2027          cmMemInfo256[memIndex][1] = cmFreeCaller[MxGetCpuID()];
2028       }
2029       cmFreeCaller[MxGetCpuID()] = NULLP;
2030
2031       /* 
2032       Reverted: Removed functionality to move freed buffer to end of free List in bucket.
2033       This is impacting throughput.
2034       */
2035 #if 1
2036       *((CmMmEntry **)bkt->last) = ptr; 
2037       bkt->last = (CmMmEntry *)ptr;
2038       *((CmMmEntry **)ptr) = NULLP;
2039 #else
2040       *((CmMmEntry **)ptr) =  bkt->next; 
2041       bkt->next = (CmMmEntry *)ptr;
2042 #endif
2043 #endif
2044 #endif /* SSI_DEBUG_LEVEL1 */
2045
2046 /* cache_coherency_changes */
2047 #ifdef LOWERARM
2048       MxDMB();
2049 #endif
2050       /* 
2051       * Decrement the statistics variable of number of memory block 
2052       * allocated 
2053       */
2054       bkt->numAlloc--;
2055 /*  cm_mem_c_001.main_15 : Additions */
2056 #ifdef SS_HISTOGRAM_SUPPORT
2057         /* If If Tapa task (entId)is registerd for histogram then insert Memrory Freed
2058          * information into the hash list */
2059         if(hstReg)
2060         {
2061             if (cmHstGrmFreeInsert(&bkt->hstGrmHashListCp, bkt->size, line, fileName, entId) != ROK)
2062             {
2063                  sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
2064                                           SPrint(hstGrmBuf);
2065             }
2066          }/* End of if */
2067 #endif /* SS_HISTOGRAM_SUPPORT */
2068
2069 #ifdef SS_MEM_LEAK_STS
2070       /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
2071       cmRlsAllocBlk((PTR)ptr);
2072 #endif /* SS_MEM_LEAK_STS */
2073       /* Release the lock */
2074       /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
2075 #ifdef SS_WIN
2076       (Void) WTUnlock(&(bkt->bktLock));
2077 #else
2078       (Void) SUnlock(&(bkt->bktLock));
2079 #endif
2080
2081       return ROK;
2082    }
2083
2084    /* The memory block was allocated from the heap pool */ 
2085 /*  cm_mem_c_001.main_15 : Additions */
2086 #ifdef SS_HISTOGRAM_SUPPORT 
2087    return (cmHeapFree (&(regCb->heapCb), ptr, size, line, fileName, entId, hstReg));
2088 #else
2089    return (cmHeapFree (&(regCb->heapCb), ptr, size));
2090 #endif /* SS_HISTOGRAM_SUPPORT */
2091 #else /* use pure is on */
2092 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/   
2093 #ifdef SS_4GMX_LCORE
2094    (Void)MxHeapFree(SsiHeap, ptr);
2095 #else
2096    (Void)free(ptr);
2097 #endif
2098    avail_size += size;
2099    return ROK;
2100 #endif /* USE_PURE */
2101
2102
2103 } /* end of cmFree */
2104
2105 /*
2106  *
2107  *       Fun:   cmAllocNL
2108  *
2109  *       Desc:  Allocate a memory block for the memory region(No Lock).
2110  *
2111  *
2112  *       Ret:   ROK     - successful
2113  *              RFAILED - unsuccessful.
2114  *
2115  *       Notes: 
2116  *              The function allocates a memory block of size atleast equal to 
2117  *              the requested size. The size parameter will be updated with the 
2118  *              actual size of the memory block allocated for the request. The 
2119  *              CMM tries to allocate the memory block form the bucket pool. If
2120  *              there is no memory in the bucket the CMM allocates the memory 
2121  *              block form the heap pool. This function is always called by the
2122  *              System Service module.
2123  *    
2124  *              The caller of the function should try to use the out value of 
2125  *              the size while returning the memory block to the region. However 
2126  *              the current design of the memory manager does not enforce to pass
2127  *              the actual size of the memory block.  (Due to the SGetSBuf 
2128  *              semantics the layer will not able to pass the correct size of the
2129  *              memory block while calling SPutSBuf).
2130  *
2131  *
2132  *       File:  cm_mem.c
2133  *
2134  */
2135 /* cm_mem_c_001.main_12 - addition to accept new parameter memType(static/dynamic) */
2136
2137 /* cm_mem_c_001.main_15 : Additions */
2138 #ifdef SS_HISTOGRAM_SUPPORT
2139 #ifdef SSI_DEBUG_LEVEL1
2140 S16  cmAllocNL
2141 (
2142 Void   *regionCb,
2143 Size   *size,
2144 uint32_t flags,
2145 Data  **ptr,
2146 uint32_t memType,
2147 uint32_t line,
2148 uint8_t  *fileName,
2149 uint8_t  entId,
2150 Bool    hstReg
2151 )
2152 #else
2153 S16  cmAllocNL
2154 (
2155 Void   *regionCb,
2156 Size   *size,
2157 uint32_t flags,
2158 Data  **ptr,
2159 uint32_t line,
2160 uint8_t  *fileName,
2161 uint8_t  entId,
2162 Bool    hstReg
2163 )
2164 #endif /* SSI_DEBUG_LEVEL1 */
2165
2166 #else
2167 #ifdef SS_LIGHT_MEM_LEAK_STS
2168 S16  cmAllocNL
2169 (
2170 Void   *regionCb,
2171 Size   *size,
2172 uint32_t     flags,
2173 Data  **ptr,
2174 uint32_t     memType,
2175 uint32_t     lineNo,
2176 uint8_t     *funcName
2177 )
2178 #else /*SS_LIGHT_MEM_LEAK_STS */
2179 #ifdef SSI_DEBUG_LEVEL1
2180 S16  cmAllocNL
2181 (
2182 Void   *regionCb,
2183 Size   *size,
2184 uint32_t flags,
2185 Data  **ptr,
2186 uint32_t memType
2187 )
2188 #else
2189 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2190 S16  cmAllocNL
2191 (
2192 Void   *regionCb,
2193 Size   *size,
2194 uint32_t     flags,
2195 Data  **ptr,
2196 uint32_t     memType
2197 )
2198 #else
2199 S16  cmAllocNL
2200 (
2201 Void   *regionCb,
2202 Size   *size,
2203 uint32_t     flags,
2204 Data  **ptr 
2205 )
2206 #endif /* BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1 */
2207 #endif /* SSI_DEBUG_LEVEL1 */
2208    /* cm_mem_c_001.main_15: Additions */
2209 #endif /*SS_LIGHT_MEM_LEAK_STS */
2210 #endif /* SS_HISTOGRAM_SUPPORT */
2211
2212 {
2213    /* cm_mem_c_001.main_26 : Fixes for Compilation Warnings */
2214 #ifndef USE_PURE
2215    uint16_t        idx;
2216    CmMmBkt   *bkt;
2217 #endif
2218    CmMmRegCb *regCb;
2219    /* cm_mem_c_001.main_26 : Fixes for Compilation Warnings */
2220 #ifndef USE_PURE
2221    uint16_t        cnt;
2222 #endif
2223    /*   cm_mem_c_001.main_15 : Additions */
2224 #if (defined(SS_MEM_LEAK_STS) || defined( BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
2225    Size       reqSz = 0;
2226 #endif /* SS_MEM_LEAK_STS */
2227    /* cm_mem_c_001.main_12 - addition to hold the allocated block */
2228 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
2229    CmMmBlkHdr *alocBlk;
2230 #endif /* SSI_DEBUG_LEVEL1 */
2231 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
2232    CmMmBlkHdr *ptrHdr;
2233 #endif
2234    /*   cm_mem_c_001.main_15 : Additions */
2235 #ifdef SS_HISTOGRAM_SUPPORT
2236    S8 hstGrmBuf[256];
2237 #endif /* SS_HISTOGRAM_SUPPORT */
2238
2239
2240 #ifndef USE_MEMCAL
2241    UNUSED(flags);
2242 #endif
2243    /*  cm_mem_c_001.main_15 : Additions */
2244 #ifdef SS_MEM_LEAK_STS 
2245    reqSz = *size;
2246 #endif /* SS_MEM_LEAK_STS */
2247
2248    regCb = (CmMmRegCb *)regionCb;
2249
2250 #if (ERRCLASS & ERRCLS_INT_PAR)
2251
2252    /* error check on parameters */
2253    if ((regCb == NULLP) || (size == NULLP) || !(*size) || (ptr == NULLP))
2254    {
2255       return RFAILED;
2256    }
2257 #endif
2258
2259    /* cm_mem_c_001.main_12 - addition for checking memType parameter */
2260 #ifdef SSI_DEBUG_LEVEL1
2261 #if (ERRCLASS & ERRCLS_INT_PAR)
2262    if ((memType != CMM_STATIC_MEM_FLAG) && (memType != CMM_DYNAMIC_MEM_FLAG))
2263    {
2264       return RFAILED;
2265    }
2266 #endif /* (ERRCLASS & ERRCLS_INT_PAR) */
2267 #endif /* SSI_DEBUG_LEVEL1 */
2268
2269 #ifndef USE_PURE
2270
2271    if (flags)
2272       num_times++;
2273
2274    /* cm_mem_c_001.main_12 - addition to insert the size into hash list */
2275 #ifdef SSI_DEBUG_LEVEL1
2276    /* Update the hash list */
2277    if (cmMmHashListInsert(&(regCb->hashListCp), *size) != ROK)
2278    {
2279       /* display that, this entry could not be made in the hash list */
2280 #ifdef DEBUGP
2281       /* display an error message here */
2282 #ifdef ALIGN_64BIT          
2283       sprintf(dbgPrntBuf, "\n Could not make an entry for size %u \
2284             in hash table of region %d \n",
2285             *size, regCb->region);
2286 #else                           
2287       sprintf(dbgPrntBuf, "\n Could not make an entry for size %lu \
2288             in hash table of region %d \n",
2289             *size, regCb->region);
2290 #endif                           
2291       SDisplay(0, dbgPrntBuf);
2292 #endif /* DEBUGP */
2293    }
2294 #endif /* SSI_DEBUG_LEVEL1 */
2295
2296    /* 
2297     * Check if the requested size is less than or equal to the maximum block 
2298     * size in the bucket. 
2299     */
2300    if ( *size <= regCb->bktMaxBlkSize)
2301    {
2302       /* Get the map to the mapping table */
2303       idx = ((*size - 1) >> regCb->bktQnPwr);
2304
2305 #if (ERRCLASS & ERRCLS_DEBUG)
2306       if (regCb->mapTbl[idx].bktIdx == 0xFF)
2307       { 
2308          /* Some fatal error in the map table initialization. */
2309          return RFAILED;
2310       }
2311 #endif
2312
2313       /* Dequeue the memory block and return it to the user */
2314       bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]); 
2315
2316       cnt = 0;
2317       /* While loop is introduced to use the "break statement inside */
2318       while (cnt < 1)
2319       {
2320          /*
2321           * Check if the size request is not greater than the size available
2322           * in the bucket
2323           */
2324          if (*size > bkt->size)
2325          {
2326             /* Try to go to the next bucket if available */
2327             if((idx < (CMM_MAX_MAP_ENT - 1)) &&
2328                   (regCb->mapTbl[++idx].bktIdx != 0xFF))
2329             {
2330                bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]);
2331             }
2332             else
2333             {
2334                /* This is the last bucket, try to allocate from heap */
2335                break;
2336             }
2337          }
2338
2339 #if (ERRCLASS & ERRCLS_DEBUG)
2340          regCb->mapTbl[idx].numReq++;
2341 #endif /* (ERRCLASS & ERRCLS_DEBUG) */
2342
2343          /* cm_mem_c_001.main_12 - addition for sanity check before allocation */
2344 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
2345          /* increment the allocation attempt counter at bucket level */
2346          bkt->numAllocAttempts++;
2347
2348          /* detect trampling if any and call sanity check. This is done for (bkt->nextBlk) as
2349             the allocation is always from (bkt->nextBlk) */
2350          if (bkt->nextBlk)
2351          {
2352 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2353             if(cmMmRegIsBlkSane(bkt->nextBlk, bkt->size) != ROK)
2354 #else
2355             if (cmMmRegIsBlkSane(bkt->nextBlk) != ROK)
2356 #endif
2357             {
2358                /* detected a trampled memory block in this bucket */
2359 #ifdef DEBUGP
2360                /* display an error message here */
2361                /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/          
2362 #ifdef ALIGN_64BIT          
2363                sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, \
2364                      size %u bytes \n",
2365                      (void *)bkt->nextBlk, regCb->mapTbl[idx].bktIdx, *size);
2366 #else                                    
2367                sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, \
2368                      size %lu bytes \n",
2369                      (void *)bkt->nextBlk, regCb->mapTbl[idx].bktIdx, *size);
2370 #endif                                    
2371                SDisplay(0, dbgPrntBuf);
2372 #endif /* DEBUGP */
2373                abort();
2374
2375 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2376                if (cmMmBktSanityChk(bkt) == RTRAMPLINGNOK)
2377                {
2378                   /* handle RTRAMPLINGNOK in SAlloc/SGetSBuf */
2379                   return (RTRAMPLINGNOK);
2380                }
2381                else
2382                {
2383                   /* return RFAILED */
2384                   return RFAILED;
2385                }
2386 #endif
2387             }
2388          }
2389          *ptr = (Data *)(bkt->nextBlk) + (sizeof(CmMmBlkHdr)); 
2390 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
2391          ptrHdr = (CmMmBlkHdr *)bkt->nextBlk;
2392 #endif
2393 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2394          /* Initialize the elements with 0xAB */
2395          memset(*ptr, 0xAB, *size);
2396 #endif
2397          if ((bkt->nextBlk) && *ptr)
2398 #elif SS_LIGHT_MEM_LEAK_STS
2399       *ptr = (Data *)(bkt->nextBlk) + (sizeof(CmMmBlkHdr)); /* ccpu00125353: warning fix */
2400       if ((bkt->nextBlk) && *ptr)
2401 #else
2402             *ptr = bkt->next;/* ccpu00125353: warning fix */
2403          if (*ptr != NULLP)
2404 #endif /* SSI_DEBUG_LEVEL1 */
2405          {
2406             /* cm_mem_c_001.main_12 - addition for header */
2407 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
2408             /* point to next block header */
2409             alocBlk = bkt->nextBlk;
2410             bkt->nextBlk = (CmMmBlkHdr *)(bkt->nextBlk->nextBlk);
2411 #else
2412 #ifdef SSI_MEM_CORR_PREVENTION
2413             *(((uint32_t *)(*ptr)) + 2) = 0;
2414 #endif
2415             bkt->next = *((CmMmEntry **)(bkt->next));
2416 #endif /* SSI_DEBUG_LEVEL1 */
2417
2418             /* cache_coherency_changes */
2419 #ifdef LOWERARM
2420             MxDMB();
2421 #endif
2422             /* 
2423              * Increment the statistics variable of number of memory block 
2424              * allocated 
2425              */
2426             bkt->numAlloc++;
2427             if (bkt->numAlloc > bkt->maxAlloc)
2428             {
2429                bkt->maxAlloc = bkt->numAlloc;
2430             }
2431 #ifdef CM_MEM_OVERUSED
2432             {
2433                if (g_overused[bktIdx] == 0 && OVERUSED(bkt))
2434                {
2435                   g_overused[bktIdx] = 1;
2436                }
2437             }
2438 #endif
2439             /* cm_mem_c_001.main_12 - addition for header manipulation */
2440 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
2441             /* update the size for which this memory block has been allocated */
2442             alocBlk->requestedSize = *size;
2443             /* update the memory block header */
2444             CMM_RESET_FREE_FLAG(alocBlk->memFlags);
2445             if (memType == CMM_STATIC_MEM_FLAG)
2446             {
2447                CMM_SET_STATIC_FLAG(alocBlk->memFlags);
2448                /* add it to the static memory allocated */
2449                bkt->staticMemUsed += bkt->size;
2450             }
2451             else
2452             {
2453                CMM_SET_DYNAMIC_FLAG(alocBlk->memFlags);
2454                /* add it to the dynamic memory allocated */
2455                bkt->dynamicMemUsed += bkt->size;
2456             }
2457 #elif SS_LIGHT_MEM_LEAK_STS 
2458             alocBlk->requestedSize = *size;
2459             alocBlk->lineNo        = lineNo;
2460             alocBlk->currFuncName  = funcName;
2461             if(gmemLkCb.isStarted == TRUE)
2462             {
2463                alocBlk->allocQueueIndx = cmStorAllocBlk(alocBlk);
2464             }
2465 #endif /* SSI_DEBUG_LEVEL1 */
2466
2467             if (((bkt->size - (*size)) >> regCb->bktQnPwr) && flags)
2468             {
2469                bkt->bktNoFitCnt++;
2470 #ifdef MEMCAL_DEBUG
2471 #ifdef ALIGN_64BIT          
2472                sprintf(prntBuf,
2473                      "[MEM_CAL_CNTA] %u bytes request not fitted in bkt %d \
2474                      [size %u bytes] %u times\n", 
2475                      *size, regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktNoFitCnt++);
2476 #else   
2477                sprintf(prntBuf,
2478                      "[MEM_CAL_CNTA] %lu bytes request not fitted in bkt %d \
2479                      [size %lu bytes] %lu times\n", 
2480                      *size, regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktNoFitCnt++);
2481 #endif   
2482                SDisplay(0, prntBuf);
2483 #endif
2484             }
2485
2486 #ifdef MEMCAL_DEBUG
2487             if (flags)
2488             {
2489                sprintf(prntBuf,
2490                      "SGetSBuf:%08lu:Size Bucket Id:%03d Times:%05lu  Pointer: %8p\n",
2491                      *size, regCb->mapTbl[idx].bktIdx, num_times, *ptr);
2492                SDisplay(0, prntBuf);
2493             }
2494 #endif /* MEMCAL_DEBUG */
2495             /*  cm_mem_c_001.main_15 : Additions */
2496 #ifdef SS_HISTOGRAM_SUPPORT
2497             if(hstReg)
2498             {
2499                if (cmHstGrmAllocInsert(&(bkt->hstGrmHashListCp), 
2500                         bkt->size, size, line, fileName, entId) != ROK)
2501                {
2502                   sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
2503                   SPrint(hstGrmBuf);
2504                }
2505             }/* End of if */
2506
2507 #endif /* SS_HISTOGRAM_SUPPORT */
2508
2509             /* Update the size parameter */
2510             *size = bkt->size;
2511 #ifdef SS_MEM_LEAK_STS
2512             /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
2513             cmStorAllocBlk((PTR)*ptr, (Size) reqSz, (Size) *size,
2514                   regCb->mapTbl[idx].bktIdx);
2515 #elif BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
2516             cmStorAllocBlk(ptrHdr, (Size) reqSz, (Size) *size,
2517                         regCb->mapTbl[idx].bktIdx, regCb);
2518 #endif /* SS_MEM_LEAK_STS */
2519
2520             /* cm_mem_c_008.104 - Addition for memory calculator tool */
2521
2522             return ROK;
2523          }
2524          else if (flags)
2525          {
2526             bkt->bktFailCnt++;
2527 #ifdef MEMCAL_DEBUG
2528 #ifdef ALIGN_64BIT          
2529             sprintf(prntBuf,
2530                   "[MEM_CAL_CNTB] Allocation failed in bucket %d [ size %u bytes], \
2531                   %u times\n", regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktFailCnt);
2532 #else            
2533             sprintf(prntBuf,
2534                   "[MEM_CAL_CNTB] Allocation failed in bucket %d [ size %lu bytes], \
2535                   %lu times\n", regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktFailCnt);
2536 #endif            
2537             SDisplay(0, prntBuf);
2538 #endif
2539          }
2540
2541 #if (ERRCLASS & ERRCLS_DEBUG)
2542          regCb->mapTbl[idx].numFailure++;
2543 #endif /* (ERRCLASS & ERRCLS_DEBUG) */
2544
2545          cnt = cnt + 1;
2546       }
2547    }
2548    else
2549    {
2550       if (flags)
2551       {
2552          regCb->heapCb.heapAllocCnt++;
2553 #ifdef MEMCAL_DEBUG
2554 #ifdef ALIGN_64BIT          
2555          sprintf(prntBuf,
2556                "[MEM_CAL_CNTC]  No bucket block configured for %u bytes \n \
2557                Number of blocks allocated from heap = %u\n",*size,
2558                regCb->heapCb.heapAllocCnt);
2559 #else                 
2560          sprintf(prntBuf,
2561                "[MEM_CAL_CNTC]  No bucket block configured for %lu bytes \n \
2562                Number of blocks allocated from heap = %lu\n",*size,
2563                regCb->heapCb.heapAllocCnt);
2564 #endif                 
2565          SDisplay(0, prntBuf);
2566 #endif
2567       }
2568    }
2569
2570    /* Memory not available in the bucket pool */
2571    if (regCb->heapFlag &&  (*size < regCb->heapSize))
2572    {
2573 #ifdef MEMCAL_DEBUG
2574       if (flags) tryHeap = 1;
2575 #endif
2576       /* 
2577        * The heap memory block is available. Allocate the memory block from
2578        * heap pool.
2579        */ 
2580       /* cm_mem_c_001.main_15: Additions */
2581 #ifdef SS_HISTOGRAM_SUPPORT  
2582       /* cm_mem_c_001.main_12 - addition for passing an extra parameter */
2583 #ifdef SSI_DEBUG_LEVEL1
2584       return (cmHeapAlloc(&(regCb->heapCb), ptr, size, 
2585                memType, line, fileName, entId, hstReg));
2586 #else
2587       return (cmHeapAlloc(&(regCb->heapCb), ptr, size, 
2588                line, fileName, entId, hstReg));
2589 #endif /* SSI_DEBUG_LEVEL1 */
2590 #else
2591       /* cm_mem_c_001.main_12 - addition for passing an extra parameter */
2592 #ifdef SSI_DEBUG_LEVEL1
2593       return (cmHeapAlloc(&(regCb->heapCb), ptr, size, memType));
2594 #else
2595       return (cmHeapAlloc(&(regCb->heapCb), ptr, size));
2596 #endif /* SSI_DEBUG_LEVEL1 */
2597 #endif /* SS_HISTOGRAM_SUPPORT */
2598    }
2599
2600    /* No memory available */
2601    return RFAILED;
2602 #else /* use pure is on */
2603    /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/   
2604 #ifdef SS_4GMX_LCORE
2605    *ptr = (Data*) MxHeapAlloc(SsiHeap, *size);
2606    memset(ptr, 0, *size);
2607 #else
2608    *ptr = (Data*) malloc(*size);
2609 #endif
2610    if ( (*ptr) == NULLP)
2611       return RFAILED;
2612    avail_size -= *size;
2613    return ROK;
2614 #endif /* USE_PURE */
2615
2616 } /* end of cmAllocNL */
2617
2618 \f
2619 /*
2620 *
2621 *       Fun:   cmFreeNL
2622 *
2623 *       Desc:  Return the memory block for the memory region(No Lock).
2624 *
2625 *
2626 *       Ret:   ROK     - successful
2627 *              RFAILED - unsuccessful.
2628 *
2629 *       Notes: The user calls this function to return the previously allocated 
2630 *              memory block to the memory region. The memory manager does not 
2631 *              check the validity of the state of the memory block(like whether 
2632 *              it was allocated earlier). The caller must be sure that, the 
2633 *              address specified in the parameter 'ptr' is valid and was 
2634 *              allocated previously from same region.
2635 *
2636 *
2637 *       File:  cm_mem.c
2638 *
2639 */
2640
2641 /*  cm_mem_c_001.main_15 : Additions */
2642 #ifdef SS_LIGHT_MEM_LEAK_STS
2643 S16  cmFreeNL
2644 (
2645 Void   *regionCb,
2646 Data   *ptr, 
2647 Size    size,
2648 uint32_t     lineNo,
2649 uint8_t      *funcName
2650 )
2651 #else /*SS_LIGHT_MEM_LEAK_STS */
2652
2653 #ifdef SS_HISTOGRAM_SUPPORT
2654 S16  cmFreeNL
2655 (
2656 Void   *regionCb,
2657 Data   *ptr,
2658 Size    size,
2659 uint32_t     line,
2660 uint8_t     *fileName,
2661 uint8_t      entId,
2662 Bool    hstReg
2663 )
2664 #else
2665
2666 S16  cmFreeNL
2667 (
2668 Void   *regionCb,
2669 Data   *ptr, 
2670 Size    size
2671 )
2672    /*  cm_mem_c_001.main_15 : Additions */
2673 #endif /* SS_HISTOGRAM_SUPPORT */ 
2674 #endif /*SS_LIGHT_MEM_LEAK_STS */
2675
2676 {
2677 #ifndef USE_PURE
2678    uint16_t        idx;
2679    CmMmBkt   *bkt;
2680 #endif
2681    CmMmRegCb *regCb;
2682    /* cm_mem_c_001.main_12 - addition for holding the free pointer */
2683 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
2684    CmMmBlkHdr *ptrHdr;
2685 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2686    CmMmBlkHdr *lastHdr;
2687 #endif /* BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1 */
2688 #endif /* SSI_DEBUG_LEVEL1 */
2689    /*  cm_mem_c_001.main_15 : Additions */
2690 #ifdef SS_HISTOGRAM_SUPPORT 
2691    S8 hstGrmBuf[256];
2692 #endif /* SS_HISTOGRAM_SUPPORT */
2693
2694
2695    regCb = (CmMmRegCb *)regionCb;
2696
2697 #ifndef USE_PURE
2698 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2699    /* Check if the memory block is from the memory region */
2700    if (ptr >= ((CmMmRegCb *)regCb)->regInfo.start +
2701          ((CmMmRegCb *)regCb)->regInfo.size) 
2702    {
2703       return RFAILED;
2704    }
2705 #endif
2706 #if (ERRCLASS & ERRCLS_INT_PAR)
2707
2708    /* error check on parameters */
2709    if ((regCb == NULLP) || (!size) || (ptr == NULLP))
2710    {
2711       return RFAILED;
2712    }
2713 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2714    /* Check if the memory block is from the memory region */
2715    if (ptr >= ((CmMmRegCb *)regCb)->regInfo.start +
2716          ((CmMmRegCb *)regCb)->regInfo.size) 
2717    {
2718       return RFAILED;
2719    }
2720 #endif
2721    /* cm_mem_c_001.main_20 Addition */
2722    if (ptr < regCb->regInfo.start)
2723    {
2724       return RFAILED;
2725    }
2726
2727 #endif
2728
2729    /* 
2730     * Check if the memory block was allocated from the bucket pool. 
2731     */
2732
2733    if (ptr < (regCb->regInfo.start + regCb->bktSize))
2734    {
2735       /* The memory block was allocated from the bucket pool */
2736
2737       /* Get the map to the mapping table */
2738       idx = ((size - 1) >> regCb->bktQnPwr);
2739
2740 #if (ERRCLASS & ERRCLS_DEBUG)
2741       if (regCb->mapTbl[idx].bktIdx == 0xFF)
2742       { 
2743          /* Some fatal error in the map table initialization. */
2744          return RFAILED;
2745       }
2746 #endif
2747
2748       /* Enqueue the memory block and return it to the user */
2749       bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]); 
2750
2751       /*
2752        * Check if the size is not greater than the size available
2753        * in the bucket. If so, then the buffer must have been allocated
2754        * from next bucket.  We don't need to check the validity of the
2755        * next bucket, otherwise buffer must have been allocated from heap
2756        * pool.
2757        */
2758       if (size > bkt->size)
2759       {
2760          bkt = &(regCb->bktTbl[regCb->mapTbl[++idx].bktIdx]);
2761       }
2762
2763       /* cache_coherency_changes */
2764 #ifdef LOWERARM
2765       MxDMB();
2766 #endif
2767
2768       /* cm_mem_c_001.main_12 - addition for sanity check and free */
2769 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
2770       /* increment the dealloc attempt counter at bucket level */
2771       bkt->numDeallocAttempts++;
2772
2773       /* Check the memFlags to see whether this block was allocated */
2774       ptrHdr = (CmMmBlkHdr *) (ptr - sizeof(CmMmBlkHdr));
2775 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
2776       cmRlsAllocBlk(ptrHdr, regCb);
2777 #endif
2778 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2779       /* Check for ptr size */
2780       if(((ptrHdr->requestedSize - size) % size) != 0)
2781       {
2782 #ifdef DEBUGP
2783          sprintf(dbgPrntBuf, "Passed size (%d) does not match with allocated size(%d):%8p, Bucket Id:%03d\n",
2784                  size, ptrHdr->requestedSize, ptr, regCb->mapTbl[idx].bktIdx);
2785 #endif
2786          DU_LOG("\nERROR  --> Passed size (%d) does not match with allocated size(%d):%8p, Bucket Id:%03d\n",
2787                  size, ptrHdr->requestedSize, ptr, regCb->mapTbl[idx].bktIdx);
2788          abort();
2789       }
2790 #endif
2791
2792       /* validate the block to be freed for trampling */
2793 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2794       if(cmMmRegIsBlkSane(ptrHdr, bkt->size) != ROK)
2795 #else
2796       if (cmMmRegIsBlkSane(ptrHdr) != ROK)
2797 #endif
2798       {
2799          /* Handle error case of Memory trampling */
2800 #ifdef  DEBUGP
2801          /* display an error message here */
2802 #ifdef ALIGN_64BIT          
2803          sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, size %u bytes \n",
2804                ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
2805 #else                               
2806          sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, size %lu bytes \n",
2807                ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
2808 #endif                               
2809          SDisplay(0, dbgPrntBuf);
2810 #endif /* DEBUGP */
2811          abort();
2812          /* 
2813           * if sanity check returns RTRAMPLINGOK then there is nothing to do
2814           * as the memory blk is already invalidated in cmMmBktSanityChk
2815           */
2816 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2817          if (cmMmBktSanityChk(bkt) == RTRAMPLINGOK)
2818          {
2819             bkt->numAlloc--;
2820
2821             return ROK;
2822          }
2823          else
2824          {
2825             /* handle RTRAMPLINGNOK in SFree/SPutSBuf */
2826             return (RTRAMPLINGNOK);
2827          }
2828 #endif
2829 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2830          DU_LOG("\nERROR  --> Memory signature is invalid\n");
2831          abort();
2832 #endif
2833       }
2834       /* reset the size */
2835       ptrHdr->requestedSize = 0;
2836 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2837       /* Initialize the elements with 0XAB */
2838       memset(ptr, 0xAB, size);
2839 #endif
2840       /* check if the block to be freed is already having the state as FREE */
2841       if (CMM_IS_FREE(ptrHdr->memFlags))
2842       {
2843          /* Handle double deallocation error case */
2844 #ifdef DEBUGP
2845          /* display an error message here */
2846          /*cm_mem_c_001.main_23 Fix for specifier mismatch 
2847           * warnings in 64BIT compilation*/          
2848 #ifdef ALIGN_64BIT          
2849          sprintf(dbgPrntBuf, "Attempt to double deallocate memory at: %8p,"
2850                "Bucket Id:%03d, size %u bytes \n",
2851                ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
2852 #else                              
2853          sprintf(dbgPrntBuf, "Attempt to double deallocate memory at: %8p,"
2854                "Bucket Id:%03d, size %lu bytes \n",
2855                ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
2856 #endif                              
2857          SDisplay(0, dbgPrntBuf);
2858 #endif /* DEBUGP */
2859 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2860          DU_LOG("\nERROR  --> Attempt to double deallocate memory at: %8p, Bucket Id:%03d,\
2861                  size %u bytes \n", ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
2862           abort();
2863 #endif
2864
2865          /* handle RDBLFREE in SFree/SPutSBuf */
2866          return (RDBLFREE);
2867       }
2868       if (CMM_IS_STATIC(ptrHdr->memFlags))
2869       {
2870          CMM_SET_FREE_FLAG(ptrHdr->memFlags);
2871          CMM_RESET_STATIC_FLAG(ptrHdr->memFlags);
2872          /* deduct it from the static memory count */
2873          bkt->staticMemUsed -= bkt->size;
2874       }
2875       else if (CMM_IS_DYNAMIC(ptrHdr->memFlags))
2876       {
2877          CMM_SET_FREE_FLAG(ptrHdr->memFlags);
2878          CMM_RESET_DYNAMIC_FLAG(ptrHdr->memFlags);
2879          /* deduct it from the dynamic memory count */
2880          bkt->dynamicMemUsed -= bkt->size;
2881       }
2882       else
2883       {
2884          /* This is a case similar to trampled memory */
2885 #ifdef  DEBUGP
2886          /*cm_mem_c_001.main_23 Fix for specifier 
2887           * mismatch warnings in 64BIT compilation*/          
2888 #ifdef ALIGN_64BIT          
2889          sprintf(dbgPrntBuf, "Invalid memory flag: %u !!!\n", ptrHdr->memFlags);
2890 #else         
2891          sprintf(dbgPrntBuf, "Invalid memory flag: %lu !!!\n", ptrHdr->memFlags);
2892 #endif         
2893          SDisplay(0, dbgPrntBuf);
2894 #endif /* DEBUGP */
2895 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2896          if (cmMmBktSanityChk(bkt) == RTRAMPLINGOK)
2897          {
2898             /* do not add to the free list */
2899             bkt->numAlloc--;
2900             return ROK;
2901          }
2902          else
2903          {
2904             /* handle RTRAMPLINGNOK in SFree/SPutSBuf */
2905             return (RTRAMPLINGNOK);
2906          }
2907 #endif
2908       }
2909 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2910       /* Return the block to memory */
2911       ptrHdr->nextBlk = bkt->nextBlk;
2912       bkt->nextBlk = ptrHdr;
2913 #endif
2914 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2915       /* Move the ptr to end of the bucket */
2916       lastHdr = (CmMmBlkHdr *)bkt->lastBlk;
2917       lastHdr->nextBlk = ptrHdr;
2918       bkt->lastBlk = ptrHdr;
2919       ptrHdr->nextBlk = NULLP;
2920 #endif
2921 #elif SS_LIGHT_MEM_LEAK_STS
2922       ptrHdr = (CmMmBlkHdr *) (ptr - sizeof(CmMmBlkHdr));
2923       ptrHdr->lineNo        = lineNo;
2924       ptrHdr->currFuncName  =  funcName;
2925       if(gmemLkCb.isStarted == TRUE)
2926       {
2927          cmRlsAllocBlk(ptrHdr->allocQueueIndx);
2928       }
2929       ptrHdr->nextBlk = bkt->nextBlk;
2930       bkt->nextBlk = ptrHdr;
2931
2932 #else
2933 #ifdef SSI_MEM_CORR_PREVENTION
2934       if (*(((uint32_t *)(ptr)) + 2) == 0xdeaddead)
2935       {
2936          /* Do not free an already freed block to avoid corruption */
2937          cmDblFreeAttempts++;
2938          bkt->numAlloc++;
2939       }
2940       else
2941       {
2942          *((CmMmEntry **)bkt->last) = ptr;
2943          bkt->last = (CmMmEntry *)ptr;
2944          *((CmMmEntry **)ptr) = NULLP;
2945          *(((uint32_t *)(ptr)) + 2) = 0xdeaddead;
2946       }
2947 #else
2948       *((CmMmEntry **)ptr) =  bkt->next; 
2949       bkt->next = (CmMmEntry *)ptr;
2950 #endif
2951 #endif /* SSI_DEBUG_LEVEL1 */
2952
2953       /* cache_coherency_changes */
2954 #ifdef LOWERARM
2955       MxDMB();
2956 #endif
2957       /* 
2958        * Decrement the statistics variable of number of memory block 
2959        * allocated 
2960        */
2961       bkt->numAlloc--;
2962       /*  cm_mem_c_001.main_15 : Additions */
2963 #ifdef SS_HISTOGRAM_SUPPORT
2964       /* If Tapa task (entId)is registerd for histogram then insert Memrory Freed
2965        * information into the hash list */
2966       if(hstReg)
2967       {
2968          if (cmHstGrmFreeInsert(&bkt->hstGrmHashListCp, bkt->size, 
2969                   line, fileName, entId) != ROK)
2970          {
2971             sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
2972             SPrint(hstGrmBuf);
2973          }
2974       }/* End of if */
2975 #endif /* SS_HISTOGRAM_SUPPORT */
2976
2977 #ifdef SS_MEM_LEAK_STS
2978       /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
2979       cmRlsAllocBlk((PTR)ptr);
2980 #endif /* SS_MEM_LEAK_STS */
2981
2982       return ROK;
2983    }
2984
2985    /* The memory block was allocated from the heap pool */ 
2986    /*  cm_mem_c_001.main_15 : Additions */
2987 #ifdef SS_HISTOGRAM_SUPPORT 
2988    return (cmHeapFree (&(regCb->heapCb), ptr, size, line, fileName, entId, hstReg));
2989 #else
2990    return (cmHeapFree (&(regCb->heapCb), ptr, size));
2991 #endif /* SS_HISTOGRAM_SUPPORT */
2992 #else /* use pure is on */
2993    /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/   
2994 #ifdef SS_4GMX_LCORE
2995    (Void)MxHeapFree(SsiHeap, ptr);
2996 #else
2997    (Void)free(ptr);
2998 #endif
2999    avail_size += size;
3000    return ROK;
3001 #endif /* USE_PURE */
3002 } /* end of cmFreeNL */
3003
3004 \f
3005 /*
3006 *
3007 *       Fun:   cmAllocWL
3008 *
3009 *       Desc: alloc without lock 
3010 *
3011 *
3012 *       Ret:   ROK     - successful
3013 *              RFAILED - unsuccessful.
3014 *
3015 *
3016 *       File:  cm_mem.c
3017 *
3018 */
3019 /*cm_mem_c_001.main_21-added new function*/
3020 /*cm_mem_c_001.main_23 Removed support of SSI_DEBUG_LEVEL1 and SS_HISTOGRAM_SUPPORT for SS_FAP*/
3021 #ifdef SS_FAP
3022 static S16  cmAllocWL
3023 (
3024 Void   *regionCb,
3025 Size   *size,
3026 uint32_t flags,
3027 Data  **ptr 
3028 )
3029 {
3030    uint16_t  idx;
3031    CmMmBkt   *bkt;
3032    CmMmRegCb *regCb;
3033    /*cm_mem_c_001.main_23 Removed support of SSI_DEBUG_LEVEL1 and SS_HISTOGRAM_SUPPORT for SS_FAP*/
3034
3035
3036    /*cm_mem_c_001.main_23 Removed support of  USE_MEMCAL and MEMCAL_DEBUG support for  SS_FAP*/
3037
3038    regCb = (CmMmRegCb *)regionCb;
3039
3040 #if (ERRCLASS & ERRCLS_INT_PAR)
3041
3042    /* error check on parameters */
3043    if ((regCb == NULLP) || (size == NULLP) || !(*size) || (ptr == NULLP))
3044    {
3045       return RFAILED;
3046    }
3047 #endif
3048   
3049    /*cm_mem_c_001.main_23 Removed support of SSI_DEBUG_LEVEL1 and SS_HISTOGRAM_SUPPORT for SS_FAP*/
3050
3051 #ifndef USE_PURE
3052
3053    /* 
3054     * Check if the requested size is less than or equal to the maximum block 
3055     * size in the bucket. 
3056     */
3057    /* cm_mem_c_001.main_23 Adding check to compair size with Maximum block size*/
3058    if ( *size <= regCb->bktMaxBlkSize)
3059    {
3060       /* Get the map to the mapping table */
3061       idx = ((*size - 1) >> regCb->bktQnPwr);
3062
3063       /* Dequeue the memory block and return it to the user */
3064       bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]); 
3065
3066       {
3067          /*
3068           * Check if the size request is not greater than the size available
3069           * in the bucket
3070           */
3071          /* cm_mem_c_001.main_23 combined If(*size <= bkt->size) and if(*ptr = bkt->next)*/
3072          if ((*size <= bkt->size)&&(*ptr = bkt->next))
3073          {
3074             /* Try to go to the next bucket if available */
3075             bkt->next = *((CmMmEntry **)(bkt->next));
3076
3077             /* 
3078              * Increment the statistics variable of number of memory block 
3079              * allocated 
3080              */
3081             bkt->numAlloc++;
3082
3083             /* Update the size parameter */
3084             *size = bkt->size;
3085
3086             return ROK;
3087          }
3088       }
3089    }
3090
3091    /* Memory not available in the bucket pool */
3092    if (regCb->heapFlag &&  (*size < regCb->heapSize))
3093    {
3094       /*cm_mem_c_001.main_23 Removed support of  and MEMCAL_DEBUG support for  SS_FAP*/
3095       /* 
3096        * The heap memory block is available. Allocate the memory block from
3097        * heap pool.
3098        */ 
3099        /*cm_mem_c_001.main_23 Removed support of SSI_DEBUG_LEVEL1 and SS_HISTOGRAM_SUPPORT for SS_FAP*/
3100        return (cmHeapAlloc(&(regCb->heapCb), ptr, size));
3101    }
3102
3103    /* No memory available */
3104    return RFAILED;
3105 #else /* use pure is on */
3106 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/   
3107 #ifdef SS_4GMX_LCORE
3108    *ptr = (Data*) MxHeapAlloc(SsiHeap, *size);
3109    memset(ptr, 0, *size);
3110 #else
3111    *ptr = (Data*) malloc(*size);
3112 #endif
3113    if ( (*ptr) == NULLP)
3114        return RFAILED;
3115    avail_size -= *size;
3116    return ROK;
3117 #endif /* USE_PURE */
3118
3119 } /* end of cmAllocWL */
3120
3121 \f
3122 /*
3123 *
3124 *       Fun:   cmfree
3125 *
3126 *       Desc: free without lock 
3127 *
3128 *
3129 *       Ret:   ROK     - successful
3130 *              RFAILED - unsuccessful.
3131 *
3132 *
3133 *       File:  cm_mem.c
3134 *
3135 */
3136
3137 static S16  cmFreeWL(Void *regionCb,Data *ptr, Size size)
3138 {
3139    uint16_t  idx;
3140    CmMmBkt   *bkt;
3141    CmMmRegCb *regCb;
3142    /*cm_mem_c_001.main_23 Removed support of SSI_DEBUG_LEVEL1 and SS_HISTOGRAM_SUPPORT for SS_FAP*/
3143
3144
3145    regCb = (CmMmRegCb *)regionCb;
3146
3147 #ifndef USE_PURE
3148 #if (ERRCLASS & ERRCLS_INT_PAR)
3149
3150    /* error check on parameters */
3151    if ((regCb == NULLP) || (!size) || (ptr == NULLP))
3152    {
3153       return RFAILED;
3154    }
3155
3156    /* Check if the memory block is from the memory region */
3157    if (ptr >= ((CmMmRegCb *)regCb)->regInfo.start +
3158                ((CmMmRegCb *)regCb)->regInfo.size) 
3159    {
3160       return RFAILED;
3161    }
3162
3163 #endif
3164
3165    /* 
3166     * Check if the memory block was allocated from the bucket pool. 
3167     */
3168
3169    if (ptr < (regCb->regInfo.start + regCb->bktSize))
3170    {
3171       /* The memory block was allocated from the bucket pool */
3172
3173       /* Get the map to the mapping table */
3174       idx = ((size - 1) >> regCb->bktQnPwr);
3175
3176 #if (ERRCLASS & ERRCLS_DEBUG)
3177       if (regCb->mapTbl[idx].bktIdx == 0xFF)
3178       { 
3179          /* Some fatal error in the map table initialization. */
3180          return RFAILED;
3181       }
3182 #endif
3183
3184       /* Enqueue the memory block and return it to the user */
3185       bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]); 
3186
3187       /*
3188        * Check if the size is not greater than the size available
3189        * in the bucket. If so, then the buffer must have been allocated
3190        * from next bucket.  We don't need to check the validity of the
3191        * next bucket, otherwise buffer must have been allocated from heap
3192        * pool.
3193        */
3194        if (size > bkt->size)
3195        {
3196           bkt = &(regCb->bktTbl[regCb->mapTbl[++idx].bktIdx]);
3197        }
3198
3199       /*cm_mem_c_001.main_23 Removed support of SSI_DEBUG_LEVEL1 and SS_HISTOGRAM_SUPPORT for SS_FAP*/
3200       *((CmMmEntry **)ptr) =  bkt->next; 
3201       bkt->next = (CmMmEntry *)ptr;
3202
3203       /* 
3204       * Decrement the statistics variable of number of memory block 
3205       * allocated 
3206       */
3207       bkt->numAlloc--;
3208
3209       return ROK;
3210    }
3211
3212    /* The memory block was allocated from the heap pool */ 
3213    return (cmHeapFree (&(regCb->heapCb), ptr, size));
3214 #else /* use pure is on */
3215 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/   
3216 #ifdef SS_4GMX_LCORE
3217    (Void)MxHeapFree(SsiHeap, ptr);
3218 #else
3219    (Void)free(ptr);
3220 #endif
3221    avail_size += size;
3222    return ROK;
3223 #endif /* USE_PURE */
3224
3225
3226 } /* end of cmFreeWL */
3227 #endif
3228
3229 \f
3230 /*
3231 *
3232 *       Fun:   cmCtl
3233 *
3234 *       Desc:  Control request function. 
3235 *
3236 *
3237 *       Ret:   ROK     - successful
3238 *              RFAILED - unsuccessful.
3239 *
3240 *       Notes: The current semantics of the control function is defined for two 
3241 *              types of events: virtual address to physical address translation 
3242 *              and memory resource check. 
3243 *
3244 *              The physical address translation is valid only for the memory 
3245 *              region physically contiguous and non pagable.
3246 *
3247 *
3248 *
3249 *       File:  cm_mem.c
3250 *
3251 */
3252
3253 static S16  cmCtl(Void *regionCb,Event event, SMemCtl *memCtl)
3254 {
3255    CmMmRegCb *regCb;
3256
3257
3258    regCb = (CmMmRegCb *)regionCb;
3259
3260 #if (ERRCLASS & ERRCLS_INT_PAR)
3261
3262    /* error check on parameters */
3263    if ((regCb == NULLP) || (memCtl == NULLP))
3264    {
3265       return RFAILED;
3266    }
3267
3268 #endif
3269
3270    switch (event)
3271    {
3272       case SS_MEM_V_TO_P:
3273       {
3274          Size       offset;
3275   
3276 #if (ERRCLASS & ERRCLS_INT_PAR)
3277          if ((memCtl->u.vtop.vaddr == NULLP) || 
3278              (memCtl->u.vtop.paddr == NULLP))
3279          {
3280             return RFAILED;
3281          }
3282 #endif
3283    
3284          /* Check if the virtual to physical address translation is valid */
3285          if (regCb->chFlag & CMM_REG_PHY_VALID) 
3286          {
3287             offset = memCtl->u.vtop.vaddr - regCb->regInfo.start;
3288             *(memCtl->u.vtop.paddr) = regCb->pAddr + offset;
3289    
3290             return ROK;
3291          }
3292          break;
3293       }
3294
3295       case SS_MEM_CHK_RES:
3296       {
3297
3298 #if (ERRCLASS & ERRCLS_INT_PAR)
3299          if (!(memCtl->u.chkres.size) || 
3300             (memCtl->u.chkres.status == NULLP))
3301          {
3302             return RFAILED;
3303          }
3304 #endif
3305 #ifndef USE_PURE
3306          /* Check if the Bucket pool is configured */
3307          if (regCb->bktSize)
3308          {
3309             uint16_t        idx;
3310             CmMmBkt   *bkt;
3311             uint32_t        avlSize, totSize;
3312             /* 
3313              * The bucket pool is configured. The status value returned
3314              * does reflect on the memory availabilty in the bucket pool. 
3315              * The value does not consider the available memory in the
3316              * heap pool. 
3317              */
3318              idx = ((memCtl->u.chkres.size - 1) >> regCb->bktQnPwr);
3319              bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]); 
3320              avlSize = (bkt->numBlks - bkt->numAlloc) * bkt->size;
3321              avlSize += regCb->heapCb.avlSize;
3322              totSize = (bkt->numBlks * bkt->size) + regCb->heapSize;
3323              *(memCtl->u.chkres.status) = (avlSize/(totSize/10)); 
3324          }
3325          else
3326          {
3327             /* Bucket pool not configured */
3328
3329             /* 
3330              * Find the percentage memory available in the heap pool. The value
3331              * does not consider the fragmentation of the heap pool.
3332              */
3333             *(memCtl->u.chkres.status) = ((regCb->heapCb.avlSize) /
3334                                           (regCb->heapSize/10)); 
3335          }
3336
3337          return ROK;
3338 #else /* use pure is on */
3339             *(memCtl->u.chkres.status) = ((avail_size) /
3340                                           (regCb->regInfo.size/10));
3341          return ROK;
3342 #endif /* USE_PURE */
3343
3344       }
3345
3346       default:
3347       {
3348          /* No other event is supported currently */
3349          return RFAILED;
3350       }
3351    }
3352
3353    /* shouldn't reach here */
3354    return RFAILED;
3355 } /* end of cmCtl */
3356
3357 \f
3358 /*
3359 *
3360 *       Fun:   cmMmBktInit
3361 *
3362 *       Desc:  Initialize the bucket and the map table.
3363 *
3364 *
3365 *       Ret:   ROK     - successful, 
3366 *              RFAILED - unsuccessful.
3367 *
3368 *       Notes: This function is called by the cmMmRegInit. 
3369 *
3370 *       File:  cm_mem.c
3371 *
3372 */
3373 static Void cmMmBktInit
3374 (
3375 Data       **memAddr,
3376 CmMmRegCb  *regCb,
3377 CmMmRegCfg *cfg,
3378 uint16_t   bktIdx,
3379 uint16_t   *lstMapIdx
3380 )
3381 {
3382    uint32_t   cnt;
3383    uint16_t   idx;
3384    uint32_t   numBlks;
3385    Size  size;
3386 /* cm_mem_c_001.main_12 - addition for temporary variables */
3387 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
3388    CmMmBlkHdr **nextBlk;
3389 #ifdef SS_LIGHT_MEM_LEAK_STS
3390    CmMmBlkHdr *lastBlk;
3391 #endif /*SS_LIGHT_MEM_LEAK_STS */
3392    uint32_t sigCnt;
3393 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
3394    Data         *tmpMemAddr;
3395    CmMmBlkTail  *blkTail;
3396 #endif
3397 #else
3398    Data **next;
3399 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
3400    Data **last;
3401 #endif
3402 #endif /* SSI_DEBUG_LEVEL1 */
3403
3404
3405
3406    size = cfg->bktCfg[bktIdx].size; 
3407    numBlks = cfg->bktCfg[bktIdx].numBlks; 
3408
3409 /* cm_mem_c_001.main_12 - addition for header initialization */
3410 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
3411    /* Reset the next block pointer */
3412    regCb->bktTbl[bktIdx].nextBlk = NULLP;
3413 #if (defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
3414    regCb->bktTbl[bktIdx].lastBlk = NULLP;
3415 #endif
3416    /* Initialize the link list of the memory block */
3417    nextBlk = &(regCb->bktTbl[bktIdx].nextBlk);
3418
3419    for (cnt = 0; cnt < numBlks; cnt++)
3420    {
3421       *nextBlk = (CmMmBlkHdr *)*memAddr;
3422 #ifdef SS_LIGHT_MEM_LEAK_STS
3423       lastBlk = (CmMmBlkHdr *)*memAddr;
3424 #endif /*SS_LIGHT_MEM_LEAK_STS */
3425
3426       /* initialize the memory block header */
3427       for (sigCnt=0; sigCnt < CMM_TRAMPLING_SIGNATURE_LEN; sigCnt++)
3428       {
3429          (*nextBlk)->trSignature[sigCnt] = 0xAB;
3430       }
3431 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
3432       /* Initialize memory block tail */
3433       blkTail = (CmMmBlkTail *)(*memAddr + sizeof(CmMmBlkHdr) + size);
3434       for (sigCnt=0; sigCnt < CMM_TRAMPLING_SIGNATURE_LEN; sigCnt++)
3435       {
3436          blkTail->trSignature[sigCnt] = 0xFE;
3437       }
3438 #endif
3439       CMM_SET_FREE_FLAG((*nextBlk)->memFlags);
3440       (*nextBlk)->requestedSize = 0;
3441 #ifdef SS_LIGHT_MEM_LEAK_STS
3442       (*nextBlk)->timeStamp     = 0X7777;
3443       (*nextBlk)->lineNo        = 0;
3444       (*nextBlk)->allocQueueIndx = 1;
3445       (*nextBlk)->currFuncName  = NULL;
3446 #endif /*SS_LIGHT_MEM_LEAK_STS */
3447
3448 #if defined(SSI_DEBUG_LEVEL1) || defined(SS_LIGHT_MEM_LEAK_STS)
3449       *memAddr = (Data *)((*memAddr) + ((sizeof(CmMmBlkHdr)) + size));
3450 #elif BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
3451       *memAddr = (Data *)((*memAddr) + ((sizeof(CmMmBlkHdr)) + sizeof(CmMmBlkTail) + size));
3452 #endif
3453       nextBlk = &((*nextBlk)->nextBlk);
3454    }
3455
3456    *nextBlk = NULLP;
3457 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
3458     tmpMemAddr = (Data *)((*memAddr) - ((sizeof(CmMmBlkHdr)) + sizeof(uint32_t) + size));
3459     regCb->bktTbl[bktIdx].lastBlk = (CmMmBlkHdr *)tmpMemAddr;
3460 #endif
3461
3462 #else
3463    /* Reset the next pointer */
3464    regCb->bktTbl[bktIdx].next = NULLP; 
3465 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
3466    regCb->bktTbl[bktIdx].last = NULLP; 
3467 #endif
3468
3469    /* Initialize the link list of the memory block */
3470    next = &(regCb->bktTbl[bktIdx].next); 
3471 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
3472    last = &(regCb->bktTbl[bktIdx].last);    
3473    if(regCb->region == 0)
3474    {
3475       if(size == 128)
3476       {
3477          startPtr128 = *memAddr;
3478          regMemSize = regCb->regInfo.size;
3479          uart_printf("size of all pool=%u\n", regMemSize);
3480          uart_printf("startPtr128=%x\n", startPtr128);
3481       }
3482       if(size == 256)
3483       {
3484          startPtr256 = *memAddr;
3485          uart_printf("startPtr256=%x\n", startPtr256);
3486       }
3487       if(size == 512)
3488       {
3489          startPtr512 = *memAddr;
3490          uart_printf("startPtr512=%x\n", startPtr512);
3491       }
3492       if(size == 768)
3493       {
3494          startPtr768 = *memAddr;
3495          uart_printf("startPtr768=%x\n", startPtr768);
3496       }
3497       if(size == 1664)
3498       {
3499          startPtr1664 = *memAddr;
3500          uart_printf("startPtr1664=%x\n", startPtr1664);
3501       }
3502       if(size == 4800)
3503       {
3504          startPtr4800 = *memAddr;
3505          uart_printf("startPtr4800=%x\n", startPtr4800);
3506       }
3507       if(size == 9920)
3508       {
3509          startPtr9920 = *memAddr;
3510          uart_printf("startPtr9920=%x\n", startPtr9920);
3511       }
3512    }
3513 #endif
3514    for (cnt = 0; cnt < numBlks; cnt++)
3515    {
3516       *next     = *memAddr;
3517 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
3518          (*(uint32_t *)(*next + 4)) = 0xdeaddead;
3519          (*(uint32_t *)(*next + 124)) = 0;
3520          (*(uint32_t *)(*next + 116)) = 0xdeaddead;
3521          (*(uint32_t *)(*next + 24)) = 0xdeaddead;
3522          (*(uint32_t *)(*next + 44)) = 0xdeaddead;            
3523          (*(uint32_t *)(*next + 80)) = 0xdeaddead;
3524 #endif
3525 #ifdef SSI_MEM_CORR_PREVENTION
3526          *(((uint32_t *)(*next)) + 2) = 0xdeaddead;
3527 #endif
3528       next      = (CmMmEntry **)(*memAddr);
3529       *memAddr  = (*memAddr) + size;
3530    }
3531    *next = NULLP;
3532 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
3533    *last = (CmMmEntry *)next;
3534 #endif
3535 #ifdef SSI_MEM_CORR_PREVENTION
3536    regCb->bktTbl[bktIdx].last = (CmMmEntry *)next;
3537 #endif
3538
3539 #endif /* SSI_DEBUG_LEVEL1 */
3540
3541    /* Initialize the Map entry */
3542    idx = size / cfg->bktQnSize;
3543
3544    /* 
3545     * Check if the size is multiple of quantum size. If not we need to initialize
3546     * one more map table entry.
3547     */ 
3548    if(size % cfg->bktQnSize)
3549    {
3550       idx++;
3551    }
3552    
3553    while ( *lstMapIdx < idx)
3554    {
3555       regCb->mapTbl[*lstMapIdx].bktIdx = bktIdx;
3556
3557 #if (ERRCLASS & ERRCLS_DEBUG)
3558       regCb->mapTbl[*lstMapIdx].numReq     = 0;
3559       regCb->mapTbl[*lstMapIdx].numFailure = 0;
3560 #endif
3561
3562       (*lstMapIdx)++;
3563    } 
3564
3565    /* Initialize the bucket structure */
3566    regCb->bktTbl[bktIdx].size     = size; 
3567    regCb->bktTbl[bktIdx].numBlks  = numBlks; 
3568    regCb->bktTbl[bktIdx].numAlloc = 0;
3569
3570    /* Update the total bucket size */
3571 /* cm_mem_c_001.main_12 - addition for considering the header size */
3572 #if (defined(SSI_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
3573    regCb->bktSize += ((size + sizeof(CmMmBlkHdr)) * numBlks);
3574 /* Addition for considering the header size and tail */
3575 #elif BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
3576    regCb->bktSize += ((size + sizeof(CmMmBlkHdr) + sizeof(uint32_t)) * numBlks);
3577 #else
3578    regCb->bktSize += (size * numBlks); 
3579 #endif /* SSI_DEBUG_LEVEL1 */
3580
3581    regCb->bktTbl[bktIdx].bktFailCnt = 0;
3582    regCb->bktTbl[bktIdx].bktNoFitCnt = 0;
3583
3584 /* cm_mem_c_001.main_12 - addition for statistics related variable initialization */
3585 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
3586    /* Initialize other required pointers */
3587    regCb->bktTbl[bktIdx].bktStartPtr = (Data *)(regCb->bktTbl[bktIdx].nextBlk);
3588    regCb->bktTbl[bktIdx].numAllocAttempts = 0;
3589    regCb->bktTbl[bktIdx].numDeallocAttempts = 0;
3590    regCb->bktTbl[bktIdx].staticMemUsed = 0;
3591    regCb->bktTbl[bktIdx].dynamicMemUsed = 0;
3592    regCb->bktTbl[bktIdx].trampleCount = 0;
3593 #endif /*SSI_DEBUG_LEVEL1*/
3594 /*  cm_mem_c_001.main_15 : Additions */
3595 #ifdef SS_HISTOGRAM_SUPPORT 
3596    /* Initialise the memory histogram hash list */
3597    cmHstGrmHashListInit(&(regCb->bktTbl[bktIdx].hstGrmHashListCp));
3598 #endif /* SS_HISTOGRAM_SUPPORT */
3599
3600    return;
3601 } /* end of cmMmBktInit */
3602
3603 \f
3604 /*
3605 *
3606 *       Fun:   cmMmHeapInit
3607 *
3608 *       Desc:  Initialize the heap pool. 
3609 *
3610 *
3611 *       Ret:   ROK     - successful
3612 *              RFAILED - unsuccessful.
3613 *
3614 *       Notes: This function is called by the cmMmRegInit. 
3615 *
3616 *       File:  cm_mem.c
3617 *
3618 */
3619 static Void  cmMmHeapInit(Data *memAddr,CmMmHeapCb  *heapCb,Size size)
3620 {
3621 /* cm_mem_c_001.main_12 - addition for ssi enhancements */
3622 #ifdef SSI_DEBUG_LEVEL1
3623    uint16_t idx;
3624 #endif /* SSI_DEBUG_LEVEL1 */
3625
3626    /* Initialize the heap control block */
3627    heapCb->vStart      = memAddr;
3628    heapCb->vEnd        = memAddr + size;
3629    heapCb->avlSize    = size; 
3630    heapCb->minSize    = CMM_MINBUFSIZE; 
3631
3632    heapCb->next       = (CmHEntry *)memAddr;
3633    heapCb->next->next = NULLP;
3634 /* cm_mem_c_001.main_12 - addition for header initialization */
3635 #ifdef SSI_DEBUG_LEVEL1
3636    heapCb->next->size = size - sizeof(CmHEntry);
3637    heapCb->next->requestedSize = 0;
3638    for (idx=0; idx < CMM_TRAMPLING_SIGNATURE_LEN; idx++)
3639    {
3640       heapCb->next->trSignature[idx] = 0xAB;
3641    }
3642    CMM_SET_FREE_FLAG(heapCb->next->memFlags);
3643    heapCb->staticHeapMemUsed = 0;
3644    heapCb->dynamicHeapMemUsed = 0;
3645    heapCb->nextOffset = sizeof(heapCb->next->trSignature) +
3646                         sizeof(heapCb->next->memFlags) +
3647                         sizeof(heapCb->next->requestedSize);
3648    heapCb->numAllocAttempts = 0;
3649    heapCb->numDeallocAttempts = 0;
3650    heapCb->trampleCount = 0;
3651 #else
3652    heapCb->next->size = size; 
3653 #endif /* SSI_DEBUG_LEVEL1 */
3654
3655 #if (ERRCLASS & ERRCLS_DEBUG)
3656    heapCb->numFragBlk  = 0;
3657    heapCb->numReq      = 0;
3658    heapCb->numFailure  = 0;
3659 #endif
3660
3661    heapCb->heapAllocCnt = 0;
3662 /*  cm_mem_c_001.main_15 : Additions */
3663 #ifdef SS_HISTOGRAM_SUPPORT 
3664    /* Initialise the memory histogram hash list */
3665    cmHstGrmHashListInit(&(heapCb->heapHstGrmHashListCp));
3666 #endif /* SS_HISTOGRAM_SUPPORT */
3667    return;
3668
3669 } /* end of cmMmHeapInit */
3670
3671 #ifndef USE_PURE
3672 /*
3673 *
3674 *       Fun:   cmHeapAlloc
3675 *
3676 *       Desc:  Allocates the memory block from the heap pool. 
3677 *
3678 *
3679 *       Ret:   ROK     - successful
3680 *              RFAILED - unsuccessful.
3681 *
3682 *       Notes: This function is called by the cmAlloc. cmAlloc calls this
3683 *              function when there is no memory block available in the bucket 
3684 *              and the  heap pool is configured.
3685 *
3686 *
3687 *
3688 *       File:  cm_mem.c
3689 *
3690 */
3691 /* cm_mem_c_001.main_12 - addition for taking another parameter memType(static/dynamic) */
3692 /*  cm_mem_c_001.main_15 : Additions */
3693 #ifdef SS_4GMX_LCORE
3694 uint8_t ysCellConfigDone;
3695 #endif
3696 #ifdef SS_HISTOGRAM_SUPPORT 
3697 #ifdef SSI_DEBUG_LEVEL1
3698 static S16  cmHeapAlloc
3699 (
3700 CmMmHeapCb  *heapCb,
3701 Data       **ptr,
3702 Size        *size,
3703 uint32_t        memType,
3704 uint32_t     line,
3705 uint8_t     *fileName,
3706 uint8_t      entId,
3707 Bool    hstReg
3708 )
3709 #else
3710 static S16  cmHeapAlloc 
3711 (
3712 CmMmHeapCb  *heapCb,
3713 Data       **ptr,
3714 Size        *size,
3715 uint32_t     line,
3716 uint8_t     *fileName,
3717 uint8_t      entId,
3718 Bool    hstReg
3719 )
3720 #endif /* SSI_DEBUG_LEVEL1 */
3721 #else
3722 #ifdef SSI_DEBUG_LEVEL1
3723 static S16  cmHeapAlloc
3724 (
3725 CmMmHeapCb  *heapCb,
3726 Data       **ptr,
3727 Size        *size,
3728 uint32_t        memType
3729 )
3730 #else
3731 static S16  cmHeapAlloc 
3732 (
3733 CmMmHeapCb  *heapCb,
3734 Data       **ptr,
3735 Size        *size 
3736 )
3737 #endif /* SSI_DEBUG_LEVEL1 */
3738 /*  cm_mem_c_001.main_15 : Additions */
3739 #endif /* SS_HISTOGRAM_SUPPORT */ 
3740 {
3741    CmHEntry  *prvHBlk;    /* Previous heap block */
3742    CmHEntry  *curHBlk;    /* Current heap block */ 
3743    Size       tmpSize;
3744 /*  cm_mem_c_001.main_15 : Additions */
3745 #ifdef SS_MEM_LEAK_STS 
3746    Size       reqSz;
3747 #endif /* SS_MEM_LEAK_STS */
3748 /* cm_mem_c_001.main_12 - addition for ssi enhancements */
3749 #ifdef SSI_DEBUG_LEVEL1
3750    CmHEntry *alocHeapBlk;
3751    Size requestedSize;
3752    Size hdr;
3753    uint16_t idx;
3754 #endif /* SSI_DEBUG_LEVEL1 */
3755 /*  cm_mem_c_001.main_15 : Additions */
3756 #ifdef SS_HISTOGRAM_SUPPORT 
3757         S8 hstGrmBuf[256];
3758 #endif /* SS_HISTOGRAM_SUPPORT */
3759
3760 /*  cm_mem_c_001.main_15 : Additions */
3761    /* Acquire the heap lock */ 
3762    /* cm_mem_c_001.main_13 : Replaced SLock with WTLock for NT */
3763 #ifdef SS_4GMX_LCORE
3764    if(1 == ysCellConfigDone)
3765    {
3766 #ifdef SSI_DEBUG_LEVEL1
3767       /* display a message here */
3768       sprintf(dbgPrntBuf,"Allocating from heap size=%u\n", *size);
3769       SDisplay(0, dbgPrntBuf);
3770 #endif /* SSI_DEBUG_LEVEL1 */
3771    }
3772 #endif
3773 #ifdef SS_WIN
3774    (Void) WTLock (&(heapCb->heapLock));
3775 #else
3776    (Void) SLock (&(heapCb->heapLock));
3777 #endif
3778
3779 #ifdef SS_MEM_LEAK_STS
3780    reqSz = *size;
3781 #endif /* SS_MEM_LEAK_STS */
3782 /* cm_mem_c_001.main_12 - addition for manipulation of statistics related data */
3783 #ifdef SSI_DEBUG_LEVEL1
3784    heapCb->numAllocAttempts++;
3785    requestedSize = *size;
3786 #endif /* SSI_DEBUG_LEVEL1 */
3787
3788    /* Roundup the requested size */
3789    *size = CMM_DATALIGN(*size, (heapCb->minSize));
3790    
3791    /* Check if the available total size is adequate. */
3792    if ((*size) >= heapCb->avlSize)
3793    {
3794 /*  cm_mem_c_001.main_15 : Additions */
3795 #ifdef SS_WIN
3796                         (Void) WTUnlock (&(heapCb->heapLock));
3797 #else
3798                         (Void) SUnlock (&(heapCb->heapLock));
3799 #endif
3800       return (ROUTRES);
3801    }
3802
3803
3804 /* cm_mem_c_001.main_12 - addition for aligning the header size */
3805 #ifdef SSI_DEBUG_LEVEL1
3806    hdr = PTRALIGN(sizeof(CmHEntry));
3807 #endif /* SSI_DEBUG_LEVEL1 */
3808
3809    /* 
3810     * Search through the heap block list in the heap pool of size 
3811     * greater than or equal to the requested size.
3812     *
3813     */ 
3814 /* cm_mem_c_001.main_12 - addition for accessing the heapCb->next */
3815 #ifdef SSI_DEBUG_LEVEL1
3816    prvHBlk = (CmHEntry *)((Data *)&(heapCb->next) - heapCb->nextOffset);
3817 #else
3818    prvHBlk = (CmHEntry *)&(heapCb->next);
3819 #endif /* SSI_DEBUG_LEVEL1 */
3820    for (curHBlk = prvHBlk->next; curHBlk; curHBlk = curHBlk->next,
3821                                                    prvHBlk = prvHBlk->next)
3822    {
3823       /*
3824        * Since the size of the block is always multiple of CMM_MINBUFSIZE 
3825        * and the requested size is rounded to the size multiple of
3826        * CMM_MINBUFSIZE, the difference between the size of the heap block
3827        * and the size to allocate will be either zero or multiple of
3828        * CMM_MINBUFSIZE. 
3829        */
3830       if ((*size) <= curHBlk->size) 
3831       {
3832 /* cm_mem_c_001.main_12 - addition for block size calculation */
3833 #ifdef SSI_DEBUG_LEVEL1
3834       tmpSize = curHBlk->size - (*size);
3835       if (tmpSize != 0)
3836          tmpSize = tmpSize - hdr;
3837       if (tmpSize)
3838 #else
3839          /* cm_mem_c_001.main_28 : compilation warning fix */
3840          tmpSize = (curHBlk->size - (*size));
3841          if (tmpSize != 0)
3842 #endif /* SSI_DEBUG_LEVEL1 */
3843          {
3844             /* Heap block of bigger size */
3845 /* cm_mem_c_001.main_12 - addition for allocating memory */
3846 #ifdef SSI_DEBUG_LEVEL1
3847             *ptr = (Data *)curHBlk + hdr + tmpSize + hdr;
3848             alocHeapBlk = (CmHEntry *) ((Data *)curHBlk + hdr + tmpSize);
3849             /*
3850             * No need to look for memory trampling as this is a new block altogether
3851             * Update the header only for this case as it is new block formed 
3852             */
3853             for (idx=0; idx < CMM_TRAMPLING_SIGNATURE_LEN; idx++)
3854             {
3855                alocHeapBlk->trSignature[idx] = 0xAB;
3856             }
3857             alocHeapBlk->size = *size;
3858 #else
3859             *ptr = (Data *)curHBlk + tmpSize;             
3860 #endif /* SSI_DEBUG_LEVEL1 */
3861              curHBlk->size = tmpSize;
3862          } 
3863          else
3864          {
3865             /* Heap block is same size of the requested size */
3866 /* cm_mem_c_001.main_12 - addition for sanity check and allocation. This is a fresh block */
3867 #ifdef SSI_DEBUG_LEVEL1
3868             /* look for memory trampling as this is a pure block*/
3869             if (curHBlk)
3870             {
3871                if (cmMmRegIsBlkSane((CmMmBlkHdr *)curHBlk) != ROK)
3872                {
3873                      /* detected a trampled memory block in this bucket */
3874                   #ifdef DEBUGP
3875                      /* display an error message here */
3876 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/          
3877 #ifdef ALIGN_64BIT          
3878                      sprintf(dbgPrntBuf, "Memory Trampling in heap at: %8p, size %u bytes \n", (void *)curHBlk, requestedSize);
3879 #else                     
3880                      sprintf(dbgPrntBuf, "Memory Trampling in heap at: %8p, size %lu bytes \n", (void *)curHBlk, requestedSize);
3881 #endif                     
3882                      SDisplay(0, dbgPrntBuf);
3883                   #endif /* DEBUGP */
3884
3885                      if (cmMmHeapSanityChk(heapCb) == RTRAMPLINGNOK)
3886                      {
3887                         /* Release the lock */
3888                         /* cm_mem_c_001.main_13: Replaced SUnlock with
3889                            WTUnlock for NT */
3890 #ifdef SS_WIN
3891                         (Void) WTUnlock (&(heapCb->heapLock));
3892 #else
3893                         (Void) SUnlock (&(heapCb->heapLock));
3894 #endif
3895                         /* handle RTRAMPLINGNOK in SAlloc/SGetSBuf */
3896                         return (RTRAMPLINGNOK);
3897                      }
3898                      else
3899                      {
3900                         /* Release the lock */
3901                         /* cm_mem_c_001.main_13: Replaced SUnlock with
3902                            WTUnlock for NT */
3903 #ifdef SS_WIN
3904                         (Void) WTUnlock (&(heapCb->heapLock));
3905 #else
3906                         (Void) SUnlock (&(heapCb->heapLock));
3907 #endif
3908                         return RFAILED;
3909                      }
3910                }
3911             }
3912
3913             *ptr = (Data *)curHBlk + hdr;
3914             alocHeapBlk =  curHBlk;
3915             *size = curHBlk->size;
3916 #else
3917             *ptr = (Data *)curHBlk;
3918 #endif /* SSI_DEBUG_LEVEL1 */
3919              prvHBlk->next = curHBlk->next;
3920          }
3921
3922 /* cm_mem_c_001.main_12 - addition for header updation */
3923 #ifdef SSI_DEBUG_LEVEL1
3924          /* update the header fields */
3925          alocHeapBlk->requestedSize = requestedSize;
3926          alocHeapBlk->memFlags = 0;
3927          if (memType == CMM_STATIC_MEM_FLAG)
3928          {
3929             CMM_SET_STATIC_FLAG(alocHeapBlk->memFlags);
3930             heapCb->staticHeapMemUsed += (*size + hdr);
3931          }
3932          else
3933          {
3934             CMM_SET_DYNAMIC_FLAG(alocHeapBlk->memFlags);
3935             heapCb->dynamicHeapMemUsed += (*size + hdr);
3936          }
3937          heapCb->avlSize -= ((*size) + hdr);
3938 #else
3939          heapCb->avlSize -= (*size); 
3940 #endif /* SSI_DEBUG_LEVEL1 */
3941
3942 #ifdef MEMCAL_DEBUG
3943         if (tryHeap)
3944         {
3945             sprintf(prntBuf,
3946                  "SGetSBuf:%08lu:Size  Heap Alloc Times:%05lu  Pointer: %8p\n",
3947                  *size, num_times, *ptr);
3948             SDisplay(0, prntBuf);
3949             tryHeap = 0;
3950         }
3951 #endif
3952 /*  cm_mem_c_001.main_15 : Additions */
3953 #ifdef SS_MEM_LEAK_STS 
3954         /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
3955          cmStorAllocBlk((PTR)*ptr, (Size) reqSz, (Size) *size, MT_MAX_BKTS);
3956 #endif /* SS_MEM_LEAK_STS */
3957          /* Release the lock */
3958 /*  cm_mem_c_001.main_16 : cm_mem_c_001.main_18  Additions */
3959 #ifdef SS_WIN
3960    (Void) WTUnlock (&(heapCb->heapLock));
3961 #else
3962    (Void) SUnlock (&(heapCb->heapLock));
3963 #endif
3964
3965 #ifdef SS_HISTOGRAM_SUPPORT
3966             /* If If Tapa task (entId)is registerd for histogram then insert Memrory allocated
3967              * information into the hash list */
3968             if(hstReg)
3969             {
3970                if (cmHstGrmAllocInsert(&(heapCb->heapHstGrmHashListCp), *size, size, line, fileName, entId) != ROK)
3971                {
3972                  sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
3973                                           SPrint(hstGrmBuf);
3974                }
3975             }/* End of if */
3976
3977 #endif /* SS_HISTOGRAM_SUPPORT */
3978
3979          return ROK;
3980       }
3981    }
3982
3983 /* cm_mem_c_008.104 - Addition for memory calculator tool */
3984 #ifdef MEMCAL_DEBUG
3985         tryHeap = 0;
3986 #endif
3987    
3988
3989    /* Release the lock */
3990    /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
3991 #ifdef SS_WIN
3992    (Void) WTUnlock (&(heapCb->heapLock));
3993 #else
3994    (Void) SUnlock (&(heapCb->heapLock));
3995 #endif
3996
3997    return (ROUTRES);
3998
3999 } /* end of cmHeapAlloc */
4000
4001 \f
4002 /*
4003 *
4004 *       Fun:   cmHeapFree
4005 *
4006 *       Desc:  Return the memory block from the heap pool. 
4007 *
4008 *
4009 *       Ret:   ROK     - successful
4010 *              RFAILED - unsuccessful.
4011 *
4012 *       Notes: This function returns the memory block to the heap  pool. This 
4013 *              function is called by cmFree. The function does not check the 
4014 *              validity of the memory block. The caller must be sure that the 
4015 *              block was previously allocated and belongs to the heap pool. The 
4016 *              function maintain the sorting order of the memory block on the
4017 *              starting address of the block. This function also do compaction 
4018 *              if the neighbouring blocks are already in the heap. 
4019 *
4020 *
4021 *
4022 *       File:  cm_mem.c
4023 *
4024 */
4025 /*  cm_mem_c_001.main_15 : Additions */
4026 #ifdef SS_HISTOGRAM_SUPPORT  
4027 static S16  cmHeapFree 
4028 (
4029 CmMmHeapCb  *heapCb,
4030 Data        *ptr,
4031 Size         size,
4032 uint32_t     line,
4033 uint8_t     *fileName,
4034 uint8_t      entId,
4035 Bool    hstReg
4036 )
4037 #else
4038 static S16  cmHeapFree 
4039 (
4040 CmMmHeapCb  *heapCb,
4041 Data        *ptr,
4042 Size         size 
4043 )
4044 /*  cm_mem_c_001.main_15 : Additions */
4045 #endif /* SS_HISTOGRAM_SUPPORT */ 
4046 {
4047    CmHEntry  *p;    
4048    CmHEntry  *curHBlk;    /* Current heap block */ 
4049 /* cm_mem_c_001.main_12 - addition for ssi enhancements */
4050 #ifdef SSI_DEBUG_LEVEL1
4051    Size  hdr;
4052 #endif /* SSI_DEBUG_LEVEL1 */
4053 /*  cm_mem_c_001.main_15 : Additions */
4054 #ifdef SS_HISTOGRAM_SUPPORT 
4055    Size allocSize = size;
4056         S8 hstGrmBuf[256];
4057 #endif /* SS_HISTOGRAM_SUPPORT */
4058
4059
4060    /* Roundup the requested size */
4061    size = CMM_DATALIGN(size, (heapCb->minSize));
4062 /*  cm_mem_c_001.main_15: Additions */
4063 #ifdef SS_HISTOGRAM_SUPPORT  
4064    allocSize = size;
4065 #endif /* SS_HISTOGRAM_SUPPORT */
4066
4067    /* Acquire the heap lock */
4068    /* cm_mem_c_001.main_13 : Replaced SLock with WTLock for NT */
4069 #ifdef SS_WIN
4070    (Void) WTLock (&(heapCb->heapLock));
4071 #else
4072    (Void) SLock (&(heapCb->heapLock));
4073 #endif
4074
4075    /* increase the avlSize */
4076 /* cm_mem_c_001.main_12 - addition for manipulation of statistics related data */
4077 #ifdef SSI_DEBUG_LEVEL1
4078    hdr = PTRALIGN(sizeof(CmHEntry));
4079    heapCb->avlSize += (size + hdr);
4080    heapCb->numDeallocAttempts++;
4081 #else
4082    heapCb->avlSize += size;
4083 #endif /* SSI_DEBUG_LEVEL1 */
4084    
4085 /* cm_mem_c_001.main_12 - addition for pointing to the block */
4086 #ifdef SSI_DEBUG_LEVEL1
4087    p = (CmHEntry *)(ptr - hdr);
4088 #else
4089    p = (CmHEntry *)ptr; 
4090 /*  cm_mem_c_001.main_15 : Additions */
4091 #ifdef SS_MEM_LEAK_STS
4092    /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
4093    cmRlsAllocBlk((PTR)ptr);
4094 #endif /* SS_MEM_LEAK_STS */
4095 #endif /* SSI_DEBUG_LEVEL1 */
4096
4097
4098 /* cm_mem_c_001.main_12 - addition for sanity and double-free checks */
4099 #ifdef SSI_DEBUG_LEVEL1
4100    /* look for memory trampling */
4101    if (cmMmRegIsBlkSane((CmMmBlkHdr *)p) != ROK)
4102    {
4103       /* detected a trampled memory block in heap */
4104    #ifdef DEBUGP
4105       /* display an error message here */
4106 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/          
4107 #ifdef ALIGN_64BIT          
4108       sprintf(dbgPrntBuf, "Memory Trampling in heap at: %8p, size %u bytes \n", (void *)p, size);
4109 #else      
4110       sprintf(dbgPrntBuf, "Memory Trampling in heap at: %8p, size %lu bytes \n", (void *)p, size);
4111 #endif      
4112       SDisplay(0, dbgPrntBuf);
4113    #endif /* DEBUGP */
4114
4115       if (cmMmHeapSanityChk(heapCb) == RTRAMPLINGNOK)
4116       {
4117          /* Release the lock */
4118          /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
4119 #ifdef SS_WIN
4120          (Void) WTUnlock (&(heapCb->heapLock));
4121 #else
4122          (Void) SUnlock (&(heapCb->heapLock));
4123 #endif
4124          /* handle RTRAMPLINGNOK in SAlloc/SGetSBuf */
4125          return (RTRAMPLINGNOK);
4126       }
4127       else
4128       {
4129          /* do not add to the free heap */
4130          heapCb->avlSize -= (size + hdr);
4131          /* Release the heap lock */
4132          /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
4133 #ifdef SS_WIN
4134          (Void) WTUnlock (&(heapCb->heapLock));
4135 #else
4136          (Void) SUnlock (&(heapCb->heapLock));
4137 #endif
4138
4139          return ROK;
4140       }
4141    }
4142
4143    /* look for any double free */
4144    if (CMM_IS_FREE(p->memFlags))
4145    {
4146    #ifdef DEBUGP
4147 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/          
4148 #ifdef ALIGN_64BIT          
4149       sprintf(dbgPrntBuf, "DOUBLE FREE at %8p for size %u in HEAP \n", (void *)p, size);
4150 #else
4151       sprintf(dbgPrntBuf, "DOUBLE FREE at %8p for size %lu in HEAP \n", (void *)p, size);
4152 #endif      
4153       SDisplay(0, dbgPrntBuf);
4154    #endif /* DEBUGP */
4155
4156       heapCb->avlSize -= (size + hdr);
4157 /*  cm_mem_c_001.main_15 : Additions */
4158 #ifdef SS_WIN 
4159          (Void) WTUnlock (&(heapCb->heapLock));
4160 #else
4161          (Void) SUnlock (&(heapCb->heapLock));
4162 #endif
4163
4164       return (RDBLFREE);
4165    }
4166 #endif /* SSI_DEBUG_LEVEL1 */
4167
4168    for ( curHBlk = heapCb->next; curHBlk; curHBlk = curHBlk->next)
4169    {
4170       /* 
4171        * The block will be inserted to maintain the sorted order on the
4172        * starting address of the block.
4173        */
4174       if (p > curHBlk)
4175       {
4176          if (!(curHBlk->next) || 
4177              (p < (curHBlk->next)))
4178          {
4179             /* Heap block should be inserted here */
4180
4181             /* 
4182              * Check if the block to be returned can be merged with the
4183              * current block.
4184              */
4185 /* cm_mem_c_001.main_12 - addition for header consideration */
4186 #ifdef SSI_DEBUG_LEVEL1
4187              if (((Data *)curHBlk + hdr + curHBlk->size) == (Data *)p)
4188 #else
4189              if (((Data *)curHBlk + curHBlk->size) == (Data *)p)
4190 #endif /* SSI_DEBUG_LEVEL1 */
4191              {
4192                  /* Merge the block */
4193 /* cm_mem_c_001.main_12 - addition for updating statistics related data */
4194 #ifdef SSI_DEBUG_LEVEL1
4195                   /* update the flags */
4196                   if (CMM_IS_STATIC(p->memFlags))
4197                      heapCb->staticHeapMemUsed -= (size + hdr);
4198                   else if (CMM_IS_DYNAMIC(p->memFlags))
4199                      heapCb->dynamicHeapMemUsed -= (size + hdr);
4200                   size = (curHBlk->size += (size + hdr));
4201 #else
4202                   size = (curHBlk->size += size);
4203 #endif /*SSI_DEBUG_LEVEL1*/
4204                   p = curHBlk;
4205              }
4206              else
4207              {
4208 /* cm_mem_c_001.main_12 - addition for double-free check */
4209 #ifdef SSI_DEBUG_LEVEL1
4210                 /* Check for double deallocation in heap */
4211                 if ((Data *)p < ((Data *)curHBlk + curHBlk->size))
4212                 {
4213                    /* Release the lock */
4214                    /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
4215 #ifdef SS_WIN
4216                    (Void) WTUnlock (&(heapCb->heapLock));
4217 #else
4218                    (Void) SUnlock (&(heapCb->heapLock));
4219 #endif
4220
4221                    /* This block is already freed in the heap */
4222                    return (RDBLFREE);
4223                 }
4224                 /* update the flags as it is a new node */
4225                 if (CMM_IS_STATIC(p->memFlags))
4226                 {
4227                    heapCb->staticHeapMemUsed -= (size + hdr);
4228                    CMM_RESET_STATIC_FLAG(p->memFlags);
4229                 }
4230                 else if (CMM_IS_DYNAMIC(p->memFlags))
4231                 {
4232                    heapCb->dynamicHeapMemUsed -= (size + hdr);
4233                    CMM_RESET_DYNAMIC_FLAG(p->memFlags);
4234                 }
4235                 CMM_SET_FREE_FLAG(p->memFlags);
4236                 p->requestedSize = 0;
4237 #endif /*SSI_DEBUG_LEVEL1*/
4238                 /* insert the block */
4239                 p->next = curHBlk->next;
4240                 p->size = size; 
4241                 curHBlk->next = p;
4242              }
4243
4244             /* Try to merge with the next block in the chain */
4245 /* cm_mem_c_001.main_12 - addition for ssi enhancements */
4246 #ifdef SSI_DEBUG_LEVEL1
4247             if (((Data *)p + hdr + size) == (Data *)(p->next) && (p->next))
4248 #else
4249             if (((Data *)p + size) == (Data *)(p->next) && (p->next))
4250 #endif /*SSI_DEBUG_LEVEL1*/
4251             {
4252                /* p->next can not be NULL */
4253 /* cm_mem_c_001.main_12 - addition for header consideration */
4254 #ifdef SSI_DEBUG_LEVEL1
4255                p->size += (p->next->size + hdr);
4256 #else
4257                p->size += p->next->size; 
4258 #endif /*SSI_DEBUG_LEVEL1*/
4259                p->next  = p->next->next;
4260             }
4261
4262             /* Release the lock */
4263             /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
4264 #ifdef SS_WIN
4265             (Void) WTUnlock (&(heapCb->heapLock));
4266 #else
4267             (Void) SUnlock (&(heapCb->heapLock));
4268 #endif
4269 /*  cm_mem_c_001.main_15 : Additions */             
4270 #ifdef SS_HISTOGRAM_SUPPORT 
4271         /* If If Tapa task (entId)is registerd for histogram then insert 
4272                    Memrory Freed information into the hash list */
4273         if(hstReg)
4274         {
4275             if (cmHstGrmFreeInsert(&heapCb->heapHstGrmHashListCp, allocSize, line, 
4276                                            fileName, entId) != ROK)
4277             {
4278                sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
4279                                         SPrint(hstGrmBuf);
4280             }
4281          }/* End of if */
4282 #endif /* SS_HISTOGRAM_SUPPORT */
4283             return ROK;
4284          }
4285       }
4286       else if (p < curHBlk)
4287       {
4288          /*
4289          * Check if the block to be returned can be merged with the
4290          * current block.
4291          */
4292 /* cm_mem_c_001.main_12 - addition for header consideration */
4293 #ifdef SSI_DEBUG_LEVEL1
4294          if (((Data *)p + hdr + size) == (Data *)curHBlk)
4295 #else
4296          if (((Data *)p + size) == (Data *)curHBlk)
4297 #endif /* SSI_DEBUG_LEVEL1 */
4298          {
4299             /* Merge the block */
4300 /* cm_mem_c_001.main_12 - addition for header consideration */
4301 #ifdef SSI_DEBUG_LEVEL1
4302             p->size = size + (curHBlk->size + hdr);
4303 #else
4304             p->size = size + curHBlk->size;
4305 #endif /* SSI_DEBUG_LEVEL1 */
4306             p->next = curHBlk->next;
4307          }
4308          else
4309          {
4310             /* insert the block */
4311             p->next = curHBlk;
4312             p->size = size;
4313          }
4314 /* cm_mem_c_001.main_12 - addition for header updation */
4315 #ifdef SSI_DEBUG_LEVEL1
4316          /* update the flags in both cases as they are new start nodes*/
4317          if (CMM_IS_STATIC(p->memFlags))
4318          {
4319             heapCb->staticHeapMemUsed -= (size + hdr);
4320             CMM_RESET_STATIC_FLAG(p->memFlags);
4321          }
4322          else if (CMM_IS_DYNAMIC(p->memFlags))
4323          {
4324             heapCb->dynamicHeapMemUsed -= (size + hdr);
4325             CMM_RESET_DYNAMIC_FLAG(p->memFlags);
4326          }
4327          CMM_SET_FREE_FLAG(p->memFlags);
4328          p->requestedSize = 0;
4329 #endif /* SSI_DEBUG_LEVEL1 */
4330
4331          heapCb->next = p;
4332
4333          /* Release the lock */
4334          /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
4335 #ifdef SS_WIN
4336          (Void) WTUnlock (&(heapCb->heapLock));
4337 #else
4338          (Void) SUnlock (&(heapCb->heapLock));
4339 #endif
4340 /*  cm_mem_c_001.main_15 : Additions */
4341 #ifdef SS_HISTOGRAM_SUPPORT  
4342         /* If If Tapa task (entId)is registerd for histogram then insert 
4343                    Memrory Freed information into the hash list */
4344         if(hstReg)
4345         {
4346             if (cmHstGrmFreeInsert(&heapCb->heapHstGrmHashListCp, allocSize, line, 
4347                                            fileName, entId) != ROK)
4348             {
4349                sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
4350                                         SPrint(hstGrmBuf);
4351             }
4352          }/* End of if */
4353 #endif /* SS_HISTOGRAM_SUPPORT */
4354          return ROK;
4355       }
4356
4357    }
4358
4359    if (heapCb->next == NULLP)
4360    {
4361       /* Heap block is empty. Insert the block in the head. */
4362       heapCb->next = p;
4363       p->next = NULLP;
4364       p->size = size;
4365
4366 /* cm_mem_c_001.main_12 - addition for header updation */
4367 #ifdef SSI_DEBUG_LEVEL1
4368       if (CMM_IS_STATIC(p->memFlags))
4369       {
4370          heapCb->staticHeapMemUsed -= (size + hdr);
4371          CMM_RESET_STATIC_FLAG(p->memFlags);
4372       }
4373       else if (CMM_IS_DYNAMIC(p->memFlags))
4374       {
4375          heapCb->dynamicHeapMemUsed -= (size + hdr);
4376          CMM_RESET_DYNAMIC_FLAG(p->memFlags);
4377       }
4378       CMM_SET_FREE_FLAG(p->memFlags);
4379       p->requestedSize = 0;
4380 #endif /* SSI_DEBUG_LEVEL1 */
4381
4382       /* Release the heap lock */
4383       /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
4384 #ifdef SS_WIN
4385       (Void) WTUnlock (&(heapCb->heapLock));
4386 #else
4387       (Void) SUnlock (&(heapCb->heapLock));
4388 #endif
4389 /*  cm_mem_c_001.main_15 : Additions */
4390 #ifdef SS_HISTOGRAM_SUPPORT 
4391         /* If If Tapa task (entId)is registerd for histogram then insert 
4392                    Memrory Freed information into the hash list */
4393         if(hstReg)
4394         {
4395             if (cmHstGrmFreeInsert(&heapCb->heapHstGrmHashListCp, allocSize, line, 
4396                                            fileName, entId) != ROK)
4397             {
4398                sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
4399                                         SPrint(hstGrmBuf);
4400             }
4401          }/* End of if */
4402 #endif /* SS_HISTOGRAM_SUPPORT */
4403       return ROK;
4404    }
4405
4406    /* Release the lock */
4407    /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
4408 #ifdef SS_WIN
4409    (Void) WTUnlock (&(heapCb->heapLock));
4410 #else
4411    (Void) SUnlock (&(heapCb->heapLock));
4412 #endif
4413
4414    return RFAILED;
4415 } /* end of cmHeapFree */
4416 #endif/*USE_PURE*/
4417 #ifdef SS_LIGHT_MEM_LEAK_STS
4418 uint32_t cmGetFreeIndx(Void)
4419 {
4420
4421     if(gmemLkCb.head == gmemLkCb.tail)
4422     {
4423        allocQueueEmptyCnt++;
4424        return (CM_MAX_ALLOC_ENTERIES);
4425     }
4426     else
4427     {
4428        uint32_t allocQIndx = gmemLkCb.queue[gmemLkCb.head];
4429        gmemLkCb.head = (gmemLkCb.head +1) % CM_MAX_ALLOC_ENTERIES;
4430        return (allocQIndx);
4431     }
4432  
4433 }
4434 uint32_t cmPutFreeIndx(uint32_t allocQIndx)
4435 {
4436     uint32_t tmpTail = (gmemLkCb.tail+1)% CM_MAX_ALLOC_ENTERIES;
4437     if(tmpTail == gmemLkCb.head)
4438     {
4439        allocQueueFullCnt++;
4440        return RFAILED;
4441     }
4442     else
4443     {
4444        gmemLkCb.queue[gmemLkCb.tail]  = allocQIndx;
4445        gmemLkCb.tail = tmpTail;
4446        return ROK;
4447     }
4448 }
4449 /*
4450 *
4451 *       Fun:   cmInitMemLeakMdl
4452 *
4453 *       Desc:  Initializes the memory leak detection module
4454 *
4455 *
4456 *       Ret:   void
4457 *
4458 *       Notes: This function initializes the memory leak detection module.
4459 *
4460 *
4461 *       File:  cm_mem.c
4462 *
4463 */
4464 Void cmInitMemLeak (Void)
4465 {
4466    uint32_t   indx;  
4467
4468
4469    gmemLkCb.isStarted = FALSE;
4470    gmemLkCb.head      = 0;
4471    gmemLkCb.tail      = 0;
4472    SInitLock(&gmemLkCb.memLock, 1);
4473    for(indx = 0; indx < CM_MAX_ALLOC_ENTERIES; indx++)
4474    {
4475       gmemLkCb.allocInfo[indx].used  = FALSE;
4476       cmPutFreeIndx(indx);
4477    }
4478
4479    return;
4480 } /* cmInitMemLeak */
4481 /*
4482  *
4483  * Fun:   cmDeinitMemLeak
4484  * 
4485  * Desc:  De-initializes the memory leak detection
4486  * 
4487  * 
4488  * Ret:   void
4489  * 
4490  * Notes: This function de-initializes the memory leak detection module.
4491  * 
4492  * 
4493  * File:  cm_mem.c
4494  * 
4495  **/
4496 Void cmDeinitMemLeak(Void)
4497 {
4498    uint32_t   indx;  
4499
4500  
4501    for(indx = 0; indx < CM_MAX_ALLOC_ENTERIES; indx++)
4502    {
4503       gmemLkCb.allocInfo[indx].used  = FALSE;
4504    }
4505    SDestroyLock(&gmemLkCb.memLock);
4506    gmemLkCb.isStarted = FALSE;
4507    gmemLkCb.head      = 0;
4508    gmemLkCb.tail      = 0;
4509    return;
4510 }
4511 /*
4512 *
4513 *       Fun:   cmStorAllocBlk
4514 *
4515 *       Desc:  Initializes the memory leak detection module
4516 *
4517 *
4518 *       Ret:   void
4519 *
4520 *       Notes: This function initializes the memory leak detection module.
4521 *
4522 *
4523 *       File:  cm_mem.c
4524 *
4525 */
4526 uint32_t cmStorAllocBlk(Void  *addr)
4527 {
4528    uint32_t allocQIndx;
4529
4530   (Void) SLock(&gmemLkCb.memLock);
4531    allocQIndx = cmGetFreeIndx();
4532    if(allocQIndx < CM_MAX_ALLOC_ENTERIES)
4533    { 
4534       queueIndxAllocCnt++;
4535       gmemLkCb.allocInfo[allocQIndx].memAddr    = addr;
4536       gmemLkCb.allocInfo[allocQIndx].used       = TRUE;
4537    }
4538   (Void) SUnlock(&(gmemLkCb.memLock));
4539    
4540    return (allocQIndx);
4541 } /* cmStorAllocBlk */
4542
4543
4544 /*
4545 *
4546 *       Fun:   cmRlsAllocBlk
4547 *
4548 *       Desc:  Initializes the memory leak detection module
4549 *
4550 *
4551 *       Ret:   void
4552 *
4553 *       Notes: This function initializes the memory leak detection module.
4554 *
4555 *
4556 *       File:  cm_mem.c
4557 *
4558 */
4559 Void cmRlsAllocBlk(uint32_t allocQIndx)
4560 {
4561    
4562    if(allocQIndx < CM_MAX_ALLOC_ENTERIES)
4563    {
4564       if(gmemLkCb.allocInfo[allocQIndx].used == TRUE)
4565       {
4566          (Void) SLock(&gmemLkCb.memLock);
4567          gmemLkCb.allocInfo[allocQIndx].used = FALSE;
4568          cmPutFreeIndx(allocQIndx);
4569          queueIndxFreeCnt++;
4570          (Void) SUnlock(&(gmemLkCb.memLock));
4571       }
4572    }
4573    return;
4574 } /* cmRlsAllocBlk */
4575
4576 /*
4577 *
4578 *       Fun:  cmStartStopLeakLog
4579 *
4580 *
4581 *       Ret:   Void
4582 *
4583 *
4584 *       File:  cm_mem.c
4585 *
4586 */
4587 Void cmStartStopLeakLog(Void)
4588 {
4589    if (FALSE == gmemLkCb.isStarted)
4590    {
4591       DU_LOG("\nINFO  --> leak capturing started\n");
4592       gmemLkCb.isStarted = TRUE;
4593    }
4594    else
4595    {
4596       gmemLkCb.isStarted = FALSE;
4597       DU_LOG("\nINFO  --> leak capturing stopped\n");
4598       cmPrintLeakLog();
4599    }
4600    return;
4601 }
4602
4603 /*
4604 *
4605 *       Fun:  cmPrintLeakLog
4606 *
4607 *       Desc:  Prints leak log
4608 *
4609 *       Ret:   Void
4610 *
4611 *       Notes: None
4612 *
4613 *       File:  cm_mem.c
4614 *
4615 */
4616 Void cmPrintLeakLog(Void)
4617 {
4618  
4619    uint32_t   indx;  
4620    CmMmBlkHdr *aBkt;
4621    static uint32_t leakCount =0; 
4622
4623
4624    DU_LOG("\nINFO  --> START OF LEAK LOG ");
4625    SLock(&gmemLkCb.memLock);
4626    DU_LOG("\nINFO  -->  Lock Acquired");
4627    for(indx = 0; indx < CM_MAX_ALLOC_ENTERIES; indx++)
4628    {
4629       if(gmemLkCb.allocInfo[indx].used == TRUE)
4630       {
4631          leakCount++;
4632          aBkt =(CmMmBlkHdr *)gmemLkCb.allocInfo[indx].memAddr;
4633          DU_LOG("LeakCnt(%ld)Addr(0x%p)RqSz(%ld)",
4634             leakCount,gmemLkCb.allocInfo[indx].memAddr, aBkt->requestedSize);
4635          DU_LOG("LineNo(%ld)funcName(%s)\n",
4636             aBkt->lineNo, aBkt->currFuncName);
4637          gmemLkCb.allocInfo[indx].used = FALSE;
4638          cmPutFreeIndx(indx);
4639       }
4640       //if(leakCount % 10 == 0)
4641         // sleep(1);
4642    }
4643    DU_LOG("\nINFO  -->  END OF LEAK LOG ");
4644    SUnlock(&gmemLkCb.memLock);
4645    DU_LOG("\nINFO  -->  Lock Released");
4646    leakCount =0; 
4647    return;
4648 }
4649 #endif
4650
4651
4652 #if (defined(SS_MEM_LEAK_STS) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2))
4653 /*
4654 *
4655 *       Fun:   cmRlsAllocBlk
4656 *
4657 *       Desc:  Initializes the memory leak detection module
4658 *
4659 *
4660 *       Ret:   void
4661 *
4662 *       Notes: This function initializes the memory leak detection module.
4663 *
4664 *
4665 *       File:  cm_mem.c
4666 *
4667 */
4668 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4669 Void cmRlsAllocBlk
4670 (
4671 CmMmBlkHdr  *ptrHdr,
4672 CmMmRegCb *regCb
4673 )
4674 #else
4675 Void cmRlsAllocBlk
4676 (
4677 #ifdef BIT_64
4678 uint64_t    addr
4679 #else
4680 uint32_t    addr
4681 #endif
4682 )
4683 #endif
4684 {
4685     Ptr           trace[CM_MAX_STACK_TRACE];
4686     S8            **funcNm;
4687     uint8_t       idx;
4688     uint8_t       i;
4689     S16           retVal;
4690     S32           traceSize;
4691 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4692     MemAllocInfo  *memAllocInfo;
4693 #endif
4694
4695
4696 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4697 #ifdef BIT_64
4698     retVal = cmHashListFind(&regCb->brdcmSsiLstCp,(uint8_t *)&ptrHdr,
4699                    (uint16_t)sizeof(uint64_t), 0, (PTR *)&ptrHdr);                              
4700 #else
4701     retVal = cmHashListFind(&regCb->brdcmSsiLstCp,(uint8_t *)&ptrHdr,
4702                    (uint16_t)sizeof(uint32_t), 0, (PTR *)&ptrHdr);                              
4703 #endif
4704     if(retVal == ROK)
4705     {
4706        cmHashListDelete(&regCb->brdcmSsiLstCp, (PTR)ptrHdr);
4707     }
4708 #else
4709     if( memLkCb.memLkMdlInit == FALSE)
4710     {
4711       return;
4712     }
4713     for(idx = 0; idx < CM_MEM_USR_MDL; idx++)
4714     {
4715        SLock(&memLkCb.memUsrMdl[idx][addr & 0x3].memLck);
4716 #ifdef BIT_64
4717        retVal = cmHashListFind(&memLkCb.memUsrMdl[idx][addr & 0x3].memHashCp,
4718                                (uint8_t *)&addr, sizeof(uint64_t), 0,
4719                                (PTR *)&memAllocInfo);      
4720 #else
4721        retVal = cmHashListFind(&memLkCb.memUsrMdl[idx][addr & 0x3].memHashCp,
4722                                (uint8_t *)&addr, sizeof(uint32_t), 0,
4723                                (PTR *)&memAllocInfo);                              
4724 #endif                 
4725        if(retVal == ROK)
4726        {
4727          cmHashListDelete(&memLkCb.memUsrMdl[idx][addr & 0x3].memHashCp,
4728                           (PTR)memAllocInfo);
4729          SUnlock(&memLkCb.memUsrMdl[idx][addr & 0x3].memLck);
4730          funcNm = (S8 **) memAllocInfo->backTrace;
4731 #ifdef SS_MEM_LEAK_SOL
4732          for(i = 0; i < memAllocInfo->bTrcSz; i++)
4733          {
4734 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/   
4735 #ifdef SS_4GMX_LCORE
4736             MxHeapFree(SsiHeap, funcNm[i]);
4737 #else
4738             free(funcNm[i]);
4739 #endif
4740          }
4741 #endif /* SS_MEM_LEAK_SOL */
4742 #ifdef SS_MEM_LEAK_FREE_TRACE
4743          {
4744
4745             Txt   prntBuf[255];
4746             sprintf( prntBuf, "\n==============================\n");
4747             SPrint(prntBuf);
4748             /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
4749 #ifdef ALIGN_64BIT
4750             sprintf( prntBuf, "Address: [%x]\n", addr);
4751 #else
4752             sprintf( prntBuf, "Address: [%lx]\n", addr);
4753 #endif
4754             SPrint(prntBuf);
4755             traceSize = backtrace(trace, CM_MAX_STACK_TRACE);
4756             funcNm    = backtrace_symbols(trace, traceSize);
4757             sprintf( prntBuf, "[bt] Execution path:\n");
4758             SPrint(prntBuf);
4759             for (i=0; i < traceSize; ++i)
4760             {
4761               sprintf( prntBuf, "[bt] %s\n", funcNm[i]);
4762               SPrint(prntBuf);
4763             }
4764             sprintf( prntBuf, "\n==============================\n");
4765             SPrint(prntBuf);
4766          }
4767 #endif   /* SS_MEM_LEAK_FREE_TRACE */
4768 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/   
4769 #ifdef SS_4GMX_LCORE
4770          MxHeapFree(SsiHeap, funcNm);
4771          MxHeapFree(SsiHeap, memAllocInfo);
4772 #else
4773          free(funcNm);
4774          free(memAllocInfo);
4775 #endif
4776          break;
4777        }
4778        SUnlock(&memLkCb.memUsrMdl[idx][addr & 0x3].memLck);
4779     }
4780
4781 #ifndef SS_MEM_LEAK_SOL
4782      if(idx == CM_MEM_USR_MDL)
4783      {
4784        Txt   prntBuf[255];
4785        sprintf( prntBuf,"\nPossible Double-Deallocation.\n");
4786        SPrint(prntBuf);
4787 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/          
4788 #ifdef ALIGN_64BIT          
4789        sprintf( prntBuf, "Address: [%u]\n", addr);
4790 #else       
4791        sprintf( prntBuf, "Address: [%lu]\n", addr);
4792 #endif       
4793        SPrint(prntBuf);
4794        traceSize = backtrace(trace, CM_MAX_STACK_TRACE);
4795        funcNm    = backtrace_symbols(trace, traceSize);
4796        sprintf( prntBuf,"[bt] Execution path:\n");
4797        SPrint(prntBuf);
4798        for (i=0; i < traceSize; ++i)
4799        {
4800              sprintf( prntBuf,"[bt] %s\n", funcNm[i]);
4801              SPrint(prntBuf);
4802        }
4803        printf("\n==============================\n");
4804 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/   
4805 #ifdef SS_4GMX_LCORE
4806        MxHeapFree(SsiHeap, funcNm);
4807 #else
4808        free(funcNm);
4809 #endif
4810      }
4811 #endif /* SS_MEM_LEAK_SOL */
4812 #endif/* BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1 */
4813    /*cm_mem_c_001.main_25 : */
4814    return;
4815 } /* cmRlsAllocBlk */
4816
4817
4818 /*
4819 *
4820 *       Fun:   cmStorAllocBlk
4821 *
4822 *       Desc:  Initializes the memory leak detection module
4823 *
4824 *
4825 *       Ret:   void
4826 *
4827 *       Notes: This function initializes the memory leak detection module.
4828 *
4829 *
4830 *       File:  cm_mem.c
4831 *
4832 */
4833 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4834 Void cmStorAllocBlk
4835 (
4836 CmMmBlkHdr    *ptrHdr, 
4837 Size          reqSz,
4838 Size          allocSz,
4839 uint16_t           bktIdx,
4840 CmMmRegCb     *regCb
4841 )
4842 #else
4843 Void cmStorAllocBlk
4844 (
4845 #ifdef BIT_64
4846 uint64_t    addr,
4847 #else
4848 uint32_t    addr,
4849 #endif
4850 Size   reqSz,
4851 Size   allocSz,
4852 uint16_t    bktIdx
4853 )
4854 #endif /* BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1 */
4855 {
4856 #ifndef SS_MEM_LEAK_SOL
4857   void           *trace[CM_MAX_STACK_TRACE];
4858 #endif  /* SS_MEM_LEAK_SOL */
4859 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4860    uint8_t            btrIdx;
4861 #else
4862    MemAllocInfo  *allocInfo;
4863    uint8_t            moduleId;
4864    S8            **funcNm;
4865    S32           traceSize;
4866 #endif
4867
4868 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4869    if( memLkCb.memLkMdlInit == FALSE)
4870    {
4871      return;
4872    }
4873 #endif
4874 #ifdef SS_MEM_LEAK_SOL
4875    /* I need to do this for solaris, because it does not implement 
4876     * backtrace. Here backtrace is my function. See below for the 
4877     * implementation. */
4878 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/   
4879 #ifdef SS_4GMX_LCORE
4880    funcNm = (S8 **)MxHeapAlloc(SsiHeap, (sizeof(uint32_t) * CM_MAX_STACK_TRACE));
4881    memset(funcNm, 0, (sizeof(uint32_t) * CM_MAX_STACK_TRACE));
4882 #else
4883    funcNm = (S8 **)calloc(1, (sizeof(uint32_t) * CM_MAX_STACK_TRACE));
4884 #endif
4885         /* SGetSBufNewForDebug(__FILE__,__FUNCTION__,__LINE__,DFLT_REGION, DFLT_POOL, &funcNm, sizeof(uint32_t) * CM_MAX_STACK_TRACE); */
4886    traceSize = backtrace((Void **)funcNm, CM_MAX_STACK_TRACE);
4887 #else /* SS_MEM_LEAK_SOL */
4888 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4889    traceSize = backtrace(trace, CM_MAX_STACK_TRACE);
4890    funcNm = backtrace_symbols(trace, traceSize);  
4891 #endif
4892 #endif /* SS_MEM_LEAK_SOL */
4893
4894 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4895    moduleId = cmMemGetModuleId(funcNm, traceSize);
4896    (Void)SLock(&(memLkCb.memUsrMdl[moduleId][addr & 0x3].memLck)); 
4897 #endif
4898
4899 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/   
4900 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4901 #ifdef SS_4GMX_LCORE
4902    allocInfo = (MemAllocInfo *)MxHeapAlloc(SsiHeap, sizeof(MemAllocInfo)); 
4903    memset(allocInfo, 0, sizeof(MemAllocInfo));
4904 #else
4905    allocInfo = (MemAllocInfo *)calloc(1, sizeof(MemAllocInfo));  
4906 #endif
4907 #endif
4908         /* SGetSBufNewForDebug(__FILE__,__FUNCTION__,__LINE__,DFLT_REGION, DFLT_POOL, &allocInfo,  sizeof(MemAllocInfo)); */
4909 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4910    /* check if hashListCp is initialised yet */
4911    if ( regCb->brdcmSsiLstCp.nmbBins == 0)
4912    {
4913       return ROK;
4914    }
4915    ptrHdr->reqSz      = reqSz;
4916    ptrHdr->allocSz    = allocSz;
4917    ptrHdr->bktIdx     = bktIdx;
4918    cmHashListInsert(&regCb->brdcmSsiLstCp, (PTR)ptrHdr,
4919          (uint8_t *)&(ptrHdr), sizeof(PTR));
4920 #else
4921    allocInfo->memAddr    = addr;
4922    allocInfo->reqSz      = reqSz;
4923    allocInfo->allocSz    = allocSz;
4924    allocInfo->bktIdx     = bktIdx;
4925    allocInfo->backTrace  = (PTR) funcNm;
4926    allocInfo->moduleId   = moduleId;
4927    allocInfo->bTrcSz     = traceSize;
4928    cmHashListInsert(&memLkCb.memUsrMdl[moduleId][addr & 0x3].memHashCp, 
4929                     (PTR)allocInfo, (uint8_t *)&(allocInfo->memAddr),
4930                     sizeof(allocInfo->memAddr));
4931    memLkCb.memUsrMdl[moduleId][addr & 0x3].used = TRUE;
4932
4933    (Void) SUnlock(&(memLkCb.memUsrMdl[moduleId][addr & 0x3].memLck));
4934 #endif
4935    return;
4936 } /* cmStorAllocBlk */
4937
4938 /*
4939 *
4940    return;
4941 } /* cmStorAllocBlk */
4942
4943 /*
4944 *
4945 *       Fun:   SLogLkInfo
4946 *
4947 *       Desc:  Initializes the memory leak detection module
4948 *
4949 *
4950 *       Ret:   void
4951 *
4952 *       Notes: This function initializes the memory leak detection module.
4953 *
4954 *
4955 *       File:  cm_mem.c
4956 *
4957 */
4958 Void SLogLkInfo (Void)
4959 {
4960 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4961    uint8_t                 idx;
4962    uint8_t                 regionIdx;
4963    Txt                prntBuf[255];
4964    S8                 **funcNm;
4965    CmMmBlkHdr         *newBlkHdr;
4966    CmMmBlkHdr         *oldBlkHdr;
4967    CmMmRegCb          *tmpRegCb;
4968    FILE               *fp;
4969
4970    fp = fopen("meLeakLog.txt", "w");
4971    if(fp == NULL)
4972    {
4973       memLk.fileLkLog = (FILE *)stdout;
4974    }
4975    else
4976    {
4977       memLk.fileLkLog = fp;
4978    }
4979    sprintf(prntBuf, "\n------- START OF LEAK LOG -------\n");
4980    fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
4981    for(regionIdx = 0; regionIdx <  SS_MAX_REGS; regionIdx++)
4982    {
4983 //      tmpRegCb = mtCMMRegCb[regionIdx];
4984       while(cmHashListGetNext(&tmpRegCb->brdcmSsiLstCp,
4985                (PTR)oldBlkHdr, (PTR *)&newBlkHdr) == ROK)
4986       {
4987          sprintf(prntBuf, "[LBIS]\n");
4988          fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
4989 #ifdef ALIGN_64BIT
4990          sprintf(prntBuf, "Address: 0x%u\n", newBlkHdr);
4991 #else
4992          sprintf(prntBuf, "Address: 0x%lu\n", newBlkHdr);
4993 #endif
4994          fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
4995          sprintf(prntBuf, "Requested Size: %d\n", (S16)newBlkHdr->reqSz);
4996          fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
4997          sprintf(prntBuf, "Allocated Size: %d\n", (S16)newBlkHdr->allocSz);
4998          fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
4999          sprintf(prntBuf, "Bucket Idx: %d\n", newBlkHdr->bktIdx);
5000          fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
5001          sprintf(prntBuf,"Memory Allocation Path:\n");
5002          fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
5003          //funcNm = (S8 **)newBlkHdr->backTrace;
5004          for(idx = 0; idx < BRDCM_MEM_LEAK_BTRACE; idx ++)
5005          {
5006           //  sprintf(prntBuf,"==> %p\n", newBlkHdr->backTrace[idx]);
5007             fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
5008          }
5009          sprintf(prntBuf, "[LBIE]\n\n");
5010          fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
5011          fflush(memLk.fileLkLog);
5012          oldBlkHdr = newBlkHdr;
5013          newBlkHdr = NULLP;
5014       } 
5015    }
5016    sprintf(prntBuf, "\n------- END OF LEAK LOG -------\n");
5017    fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
5018    fclose(fp);
5019 #else
5020    MemAllocInfo      *oldMemInfo;
5021    MemAllocInfo      *newMemInfo;
5022    uint8_t                 memMdl;  
5023    uint8_t                 hashIdx;  
5024    uint8_t                 idx;
5025    Txt                prntBuf[255];
5026    S8                 **funcNm;
5027    if( memLkCb.memLkMdlInit == FALSE)
5028    {
5029      return;
5030    }
5031    sprintf(prntBuf, "\n------- START OF LEAK LOG -------\n");
5032    fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5033
5034    for(memMdl = 0; memMdl < CM_MEM_USR_MDL; memMdl++)
5035    {
5036            for (hashIdx = 0; hashIdx < CM_MAX_HASH_PER_TSK; hashIdx++)
5037                 {
5038          if(memLkCb.memUsrMdl[memMdl][hashIdx].used == FALSE)
5039          {
5040             continue;
5041          }
5042          oldMemInfo = NULLP;
5043          newMemInfo = NULLP;
5044          SLock(&memLkCb.memUsrMdl[memMdl][hashIdx].memLck);
5045          while(cmHashListGetNext(&memLkCb.memUsrMdl[memMdl][hashIdx].memHashCp,
5046                                  (PTR)oldMemInfo, (PTR *)&newMemInfo) == ROK)
5047          {
5048              sprintf(prntBuf, "[LBIS]\n");
5049              fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5050              /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
5051 #ifdef ALIGN_64BIT
5052              sprintf(prntBuf, "Address: 0x%u\n", newMemInfo->memAddr);
5053 #else
5054              sprintf(prntBuf, "Address: 0x%lu\n", newMemInfo->memAddr);
5055 #endif
5056              fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5057              sprintf(prntBuf, "Module Name: %s\n", 
5058                      memUsrMdlStr[newMemInfo->moduleId].mdlStr);
5059              fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5060              sprintf(prntBuf, "Requested Size: %d\n", (S16)newMemInfo->reqSz);
5061              fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5062              sprintf(prntBuf, "Allocated Size: %d\n", (S16)newMemInfo->allocSz);
5063              fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5064              sprintf(prntBuf, "Bucket Idx: %d\n", newMemInfo->bktIdx);
5065              fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5066              sprintf(prntBuf,"Memory Allocation Path:\n");
5067              fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5068              funcNm = (S8 **)newMemInfo->backTrace;
5069              for(idx = 0; idx < newMemInfo->bTrcSz; idx ++)
5070              {
5071                 sprintf(prntBuf,"==> %s\n", funcNm[idx]);
5072                 fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5073              }
5074              sprintf(prntBuf, "[LBIE]\n\n");
5075              fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5076              fflush(memLkCb.fileLkLog);
5077              oldMemInfo = newMemInfo;
5078              newMemInfo = NULLP;
5079          } 
5080          SUnlock(&memLkCb.memUsrMdl[memMdl][hashIdx].memLck);
5081                 }
5082    }
5083    sprintf(prntBuf, "\n------- END OF LEAK LOG -------\n");
5084    fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5085 #endif
5086    return;
5087 }
5088
5089 #endif
5090
5091 /*  cm_mem_c_001.main_15 : Additions */
5092 #ifdef SS_MEM_LEAK_STS 
5093 /*
5094 *
5095 *       Fun:   cmInitMemLeakMdl
5096 *
5097 *       Desc:  Initializes the memory leak detection module
5098 *
5099 *
5100 *       Ret:   void
5101 *
5102 *       Notes: This function initializes the memory leak detection module.
5103 *
5104 *
5105 *       File:  cm_mem.c
5106 *
5107 */
5108 Void cmInitMemLeakMdl (Void)
5109 {
5110    uint8_t   memMdl;
5111    uint8_t   hashIdx;
5112
5113
5114    memLkCb.memLkMdlInit = FALSE;
5115    for(memMdl = 0; memMdl < CM_MEM_USR_MDL; memMdl++)
5116    {
5117            for (hashIdx = 0; hashIdx < CM_MAX_HASH_PER_TSK; hashIdx++)
5118                 {
5119        SInitLock(&memLkCb.memUsrMdl[memMdl][hashIdx].memLck, 1);
5120        cmHashListInit(&memLkCb.memUsrMdl[memMdl][hashIdx].memHashCp,
5121                       500, 0, FALSE, CM_HASH_KEYTYPE_UINT32_MOD, 0, 0);
5122        memLkCb.memUsrMdl[memMdl][hashIdx].used = FALSE;
5123                 }
5124    }
5125    if(memLkCb.fileLkLog == NULLP)
5126    {
5127       memLkCb.fileLkLog = (FILE *) stdout;
5128    }
5129    memLkCb.memLkMdlInit = TRUE;
5130
5131    return;
5132 } /* cmInitMemLeakMdl */
5133 /* cm_mem_c_002.main_21 Added for shutdown procedure */
5134 /*
5135  *
5136  * Fun:   cmDeinitMemLeakMdl
5137  * 
5138  * Desc:  De-initializes the memory leak detection module
5139  * 
5140  * 
5141  * Ret:   void
5142  * 
5143  * Notes: This function de-initializes the memory leak detection module.
5144  * 
5145  * 
5146  * File:  cm_mem.c
5147  * 
5148  **/
5149 Void cmDeinitMemLeakMdl (Void)
5150 {
5151   uint8_t   memMdl;
5152   uint8_t   hashIdx;
5153
5154
5155   memLkCb.memLkMdlInit = FALSE;
5156   for(memMdl = 0; memMdl < CM_MEM_USR_MDL; memMdl++)
5157   {
5158          for (hashIdx = 0; hashIdx < CM_MAX_HASH_PER_TSK; hashIdx++)
5159          {
5160                 SDestroyLock(&memLkCb.memUsrMdl[memMdl][hashIdx].memLck);
5161                 cmHashListDeinit(&memLkCb.memUsrMdl[memMdl][hashIdx].memHashCp);
5162                 memLkCb.memUsrMdl[memMdl][hashIdx].used = FALSE;
5163          }
5164   }
5165   return;
5166 }
5167 /*
5168 *
5169 *       Fun:   cmMemOpenMemLkFile
5170 *
5171 *       Desc:  Initializes the memory leak detection module
5172 *
5173 *
5174 *       Ret:   void
5175 *
5176 *       Notes: This function initializes the memory leak detection module.
5177 *
5178 *
5179 *       File:  cm_mem.c
5180 *
5181 */
5182 Void cmMemOpenMemLkFile(S8 *arg)
5183 {
5184    memLkCb.fileLkLog = NULLP;
5185    memLkCb.fileLkLog = fopen(arg, "w");
5186    return;
5187 }
5188 /*
5189 *
5190 *       Fun:   SFlushLkInfo
5191 *
5192 *       Desc:  Initializes the memory leak detection module
5193 *
5194 *
5195 *       Ret:   void
5196 *
5197 *       Notes: This function initializes the memory leak detection module.
5198 *
5199 *
5200 *       File:  cm_mem.c
5201 *
5202 */
5203 Void SFlushLkInfo (Void)
5204 {
5205    MemAllocInfo *newMemInfo;
5206    uint8_t      memMdl;
5207    uint8_t      hashIdx;
5208    S8           **funcNm;
5209 #ifdef SS_MEM_LEAK_SOL
5210    uint8_t                 i;
5211 #endif /* SS_MEM_LEAK_SOL */
5212
5213    if( memLkCb.memLkMdlInit == FALSE)
5214    {
5215      return;
5216    }
5217
5218    for(memMdl = 0; memMdl < CM_MEM_USR_MDL; memMdl++)
5219    {
5220            for(hashIdx = 0; hashIdx < CM_MAX_HASH_PER_TSK; hashIdx++)
5221                 {
5222          if(memLkCb.memUsrMdl[memMdl][hashIdx].used == FALSE)
5223          {
5224             continue;
5225          }
5226          newMemInfo = NULLP;
5227          SLock(&memLkCb.memUsrMdl[memMdl][hashIdx].memLck);
5228          while(cmHashListGetNext(&memLkCb.memUsrMdl[memMdl][hashIdx].memHashCp,
5229                                  (PTR)NULLP, (PTR *)&newMemInfo) == ROK)
5230          {
5231              funcNm = (S8 **)newMemInfo->backTrace;
5232 #ifdef SS_MEM_LEAK_SOL
5233              for(i = 0; i < newMemInfo->bTrcSz; i++)
5234              {
5235 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/   
5236 #ifdef SS_4GMX_LCORE
5237                 MxHeapFree(SsiHeap, funcNm[i]); 
5238 #else
5239                 free(funcNm[i]); 
5240 #endif
5241                                     /* SPutSBufNewForDebug(__FILE__,__FUNCTION__,__LINE__,DFLT_REGION, DFLT_POOL, funcNm[i], sizeof(uint32_t) * CM_MAX_STACK_TRACE); */
5242              }
5243 #endif /* SS_MEM_LEAK_SOl */
5244 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/   
5245 #ifdef SS_4GMX_LCORE
5246              MxHeapFree(SsiHeap, funcNm);
5247              MxHeapFree(SsiHeap, newMemInfo);
5248 #else
5249              free(funcNm);
5250              free(newMemInfo);
5251 #endif
5252          }
5253          SUnlock(&memLkCb.memUsrMdl[memMdl][hashIdx].memLck);
5254                 }
5255     }
5256     return;
5257 }
5258 /*
5259 *
5260 *       Fun:   cmMemGetModuleId
5261 *
5262 *       Desc:  Initializes the memory leak detection module
5263 *
5264 *
5265 *       Ret:   void
5266 *
5267 *       Notes: This function initializes the memory leak detection module.
5268 *
5269 *
5270 *       File:  cm_mem.c
5271 *
5272 */
5273 uint8_t cmMemGetModuleId(S8 **funNm,S32 traceSize)
5274 {
5275    uint8_t  idx;
5276    uint8_t  memStrIdx;
5277    uint32_t len;
5278    S32   retVal;
5279    S16   memReqIdx;
5280    S16   mdlFunStrIdx;
5281
5282    Txt   *memFn[]={"SGetMsg", "SGetSBuf", "SGetDBuf", NULLP};
5283                  
5284    /*cm_mem_c_001.main_25 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
5285    for(idx = 0; idx < traceSize; idx++)
5286    {
5287       memReqIdx = -1;
5288       memStrIdx = 0;
5289       while((memReqIdx < 0) && (memFn[memStrIdx] != NULLP))
5290       {
5291          memReqIdx = cmMemGetStrMtchIdx(0, traceSize, memFn[memStrIdx], 
5292                                         funNm);
5293          memStrIdx++;
5294       }
5295       mdlFunStrIdx = 0;
5296       while(memUsrMdlStr[mdlFunStrIdx].fPStr != NULLP)
5297       {
5298          len = strlen((const S8 *)memUsrMdlStr[mdlFunStrIdx].fPStr);
5299          memReqIdx = cmMemGetStrMtchIdx((memReqIdx + 1), traceSize, 
5300                                         memUsrMdlStr[mdlFunStrIdx].fPStr,
5301                                         funNm);
5302          if(memReqIdx >= 0)
5303          {
5304             return (mdlFunStrIdx);
5305          }
5306          mdlFunStrIdx++;
5307       }
5308       mdlFunStrIdx = 0;
5309       while(memUsrMdlStr[mdlFunStrIdx].fPStr != NULLP)
5310       {
5311           retVal = strcmp((const S8 *)"DEFAULT", 
5312                                (const S8 *)memUsrMdlStr[mdlFunStrIdx].fPStr);
5313          if(retVal == NULLD)
5314          {
5315             return (mdlFunStrIdx);
5316          } 
5317          mdlFunStrIdx++;
5318       }
5319    }
5320
5321    return (0);
5322 } /* cmMemGetModuleId */
5323
5324 /*
5325 *
5326 *       Fun:   cmMemGetStrMtchIdx
5327 *
5328 *       Desc:  Initializes the memory leak detection module
5329 *
5330 *
5331 *       Ret:   void
5332 *
5333 *       Notes: This function initializes the memory leak detection module.
5334 *
5335 *
5336 *       File:  cm_mem.c
5337 *
5338 */
5339 S16 cmMemGetStrMtchIdx  
5340 (
5341 uint8_t strtIdx, 
5342 uint8_t endIdx,
5343 S8 *str, 
5344 S8 **strLst
5345 )
5346 {
5347
5348    S8   cmpStr[255];
5349    uint32_t  len;
5350    Bool found;
5351    uint32_t  tempLen;
5352    uint8_t   idx;
5353    S32  retVal;
5354
5355    len = strlen((const S8 *)str);
5356    cmpStr[0] = '(';
5357    strncpy((S8 *)&cmpStr[1], (const S8 *)str, len);
5358    cmpStr[len + 1] = '\0';
5359    len++;
5360    found = FALSE;
5361    for(;strtIdx < endIdx && !found; strtIdx++)
5362    {
5363        idx = 0;
5364        tempLen = strlen((const S8 *)strLst[strtIdx]);
5365        if(tempLen < len)
5366          continue;
5367
5368        while(*(strLst[strtIdx] + idx + len) != '\0')
5369        {
5370          retVal = strncmp((const S8 *)cmpStr, 
5371                          ((const S8 *)strLst[strtIdx] + idx), len);
5372          if(0 == retVal)
5373          {
5374            found = TRUE;
5375            break;
5376          }
5377          idx++;
5378        }
5379    }
5380
5381    if(!found)
5382    {
5383      return (-1); 
5384    }
5385    return (strtIdx);
5386
5387 }  /* cmMemGetStrMtchIdx */
5388 #ifdef SS_MEM_LEAK_SOL
5389 /*
5390 *
5391 *       Fun:   cmAddrToSymStr
5392 *
5393 *       Desc:  Initializes the memory leak detection module
5394 *
5395 *
5396 *       Ret:   void
5397 *
5398 *       Notes: This function initializes the memory leak detection module.
5399 *
5400 *
5401 *       File:  cm_mem.c
5402 *
5403 */
5404 S32 cmAddrToSymStr(Void *pc, S8 *buffer, S32 size)
5405 {
5406
5407    Dl_info info;
5408    Sym *sym;
5409
5410
5411    if (dladdr1(pc, &info, (Void **)&sym, RTLD_DL_SYMENT) == 0)
5412    {
5413        return (snprintf(buffer, size, "[0x%p]", pc));
5414    }
5415
5416    if ((info.dli_fname != NULLP && info.dli_sname != NULLP) &&
5417        ((uintptr_t)pc - (uintptr_t)info.dli_saddr < sym->st_size))
5418    {
5419       return (snprintf(buffer, size, "%s(%s+0x%x) [0x%p]",
5420                        info.dli_fname,
5421                        info.dli_sname,
5422                        (unsigned long)pc - (unsigned long)info.dli_saddr, pc));
5423    }
5424    else
5425    {
5426       return (snprintf(buffer, size, "%s(0x%p [0x%p]",
5427                       info.dli_fname,
5428                       (unsigned long)pc - (unsigned long)info.dli_fbase, pc));
5429    }
5430
5431 } /* cmAddrToSymStr */
5432
5433 /*
5434 *
5435 *       Fun:   cmLeakCallBack
5436 *
5437 *       Desc:  Initializes the memory leak detection module
5438 *
5439 *
5440 *       Ret:   void
5441 *
5442 *       Notes: This function initializes the memory leak detection module.
5443 *
5444 *
5445 *       File:  cm_mem.c
5446 *
5447 */
5448 S32 cmLeakCallBack(uintptr_t pc,S32  sigNo, Void *arg)
5449 {
5450     S8   *buffer;
5451
5452     Backtrace_t *bt = (Backtrace_t *)arg;
5453     if (bt->bt_actcount >= bt->bt_maxcount)
5454          return (-1);
5455 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/   
5456 #ifdef SS_4GMX_LCORE
5457     buffer = (S8 *)MxHeapAlloc(SsiHeap, 510); 
5458     memset(buffer, 0, 510);
5459 #else
5460     buffer = (S8 *)calloc(1, 510); 
5461 #endif
5462          /* SGetSBufNewForDebug(__FILE__,__FUNCTION__,__LINE__,DFLT_REGION, DFLT_POOL, &buffer, 510); */
5463     (void) cmAddrToSymStr((void *)pc, buffer, 505);
5464     bt->bt_buffer[bt->bt_actcount++] = (S8 *)buffer;
5465
5466     return (0);
5467 } /* cmLeakCallBack */
5468 #endif /* SS_MEM_LEAK_SOL */
5469
5470 #endif /* SS_MEM_LEAK_STS */
5471 /* cm_mem_c_001.main_12 - addition related to SSI enhancemens
5472 * These include sanity check functions for bucket and heap,
5473 * for printing several memory related statistics 
5474 */
5475 #if (defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined(SSI_DEBUG_LEVEL1))
5476 /*
5477 *
5478 *       Fun:   cmMmRegIsBlkSane
5479 *
5480 *       Desc:  Performs the sanity check for the memory block by checking its header.
5481 *
5482 *       Ret:   ROK - If no trampling is detected in the block
5483 *              RFAILED  - If trampling is detected in the block
5484 *
5485 *       Notes: This function performs the memory block sanity in a block.
5486 *
5487 *       File:  cm_mem.c
5488 *
5489 */
5490 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
5491 S16 cmMmRegIsBlkSane(CmMmBlkHdr *blkPtr,Size  size)
5492 #else
5493 S16 cmMmRegIsBlkSane(CmMmBlkHdr *blkPtr)
5494 #endif
5495 {
5496    uint32_t sigCnt;
5497 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
5498    CmMmBlkTail  *tailPtr;
5499 #endif
5500
5501    for ( sigCnt=0; sigCnt < CMM_TRAMPLING_SIGNATURE_LEN; sigCnt++)
5502    {
5503       if (blkPtr->trSignature[sigCnt] != 0xAB)
5504       {
5505          return RFAILED;
5506       }
5507    }
5508 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
5509    tailPtr = (CmMmBlkTail *)((Data *)blkPtr + (sizeof(CmMmBlkHdr) + size));
5510    for ( sigCnt=0; sigCnt < CMM_TRAMPLING_SIGNATURE_LEN; sigCnt++)
5511    {
5512       if (tailPtr->trSignature[sigCnt] != 0xFE)
5513       {
5514          return RFAILED;
5515       }
5516    }
5517 #endif
5518    return ROK;
5519 }
5520 #endif
5521 #ifdef SSI_DEBUG_LEVEL1
5522 /*
5523 *
5524 *       Fun:   cmMmBktSanityChk
5525 *
5526 *       Desc:  Performs the sanity check for the memory blocks in a memory bucket.
5527 *              This API gets called when trampling is detected in a memory block.
5528 *
5529 *       Ret:   RTRAMPLINGNOK - Trampling, serious error
5530 *              RTRAMPLINGOK  - Trampling, but OK to proceed
5531 *
5532 *       Notes: This function performs the memory block sanity in a bucket. This
5533 *              function is called by cmAlloc and cmFree as part of error handling mechanism.
5534 *              Performs sanity check for the whole bucket by traversing each
5535 *              of the memory blocks using the pointer bktStartPtr.
5536 *              Keeps track of number of tramplings happened. If the count
5537 *              exceeds the threshold decided, then invalidates this bucket.
5538 *
5539 *       File:  cm_mem.c
5540 *
5541 */
5542 static S16  cmMmBktSanityChk(CmMmBkt *bkt)
5543 {
5544    CmMmBlkHdr *ptrBlk;
5545    uint32_t blkCnt;
5546
5547    bkt->trampleCount = 0;
5548
5549    /* scan the entire memory list of the bucket */
5550    for (blkCnt = 0, ptrBlk = (CmMmBlkHdr *)bkt->bktStartPtr;
5551          blkCnt < (bkt->numBlks); blkCnt++)
5552    {
5553       if (cmMmRegIsBlkSane(ptrBlk) != ROK)
5554       {
5555          bkt->trampleCount++;
5556          if (bkt->trampleCount > CMM_TRAMPLING_THRESHOLD)
5557          {
5558             /* Take action to invalidate the entire bucket */
5559             return (RTRAMPLINGNOK);
5560          }
5561       }
5562       /* reach next memory block in this bucket manually */
5563       ptrBlk = (CmMmBlkHdr *)((Data *)ptrBlk + ((bkt->size) + (sizeof(CmMmBlkHdr))));
5564    }
5565
5566  #ifdef  DEBUGP
5567    /* display an error message here */
5568    sprintf(dbgPrntBuf, " %d Memory tramplings detected in the bucket!\n", bkt->trampleCount);
5569    SDisplay(0, dbgPrntBuf);
5570  #endif /* DEBUGP */
5571
5572    return (RTRAMPLINGOK);
5573 }
5574
5575 /*
5576 *
5577 *       Fun:   cmMmHeapSanityChk
5578 *
5579 *       Desc:  Performs the sanity check for the memory blocks in the memory heap.
5580 *              This API gets called when trampling is detected in heap(Alloc/Free).
5581 *
5582 *       Ret:   RTRAMPLINGNOK - Trampling, serious error
5583 *              RTRAMPLINGOK  - Trampling, but OK to proceed
5584 *
5585 *       Notes: This function performs the memory block sanity in the heap. This
5586 *              function is called by cmHeapAlloc and cmHeapFree as part of error
5587 *              handling mechanism. Keeps track of number of tramplings happened.
5588 *              If the count exceeds the threshold then return RTRAMPLINGNOK. If the
5589 *              count is less than threshold, then return RTRAMPLINGOK.
5590 *
5591 *       File:  cm_mem.c
5592 *
5593 */
5594 static S16  cmMmHeapSanityChk(CmMmHeapCb *heapCb)
5595 {
5596
5597    /* increment the trample count */
5598    heapCb->trampleCount++;
5599
5600    if (heapCb->trampleCount > CMM_TRAMPLING_THRESHOLD)
5601    {
5602       return (RTRAMPLINGNOK);
5603    }
5604
5605    return (RTRAMPLINGOK);
5606 }
5607 /*
5608 *
5609 *       Fun:   cmMmHashFunc
5610 *
5611 *       Desc:  Computes the hash list index (bin number) for a specified
5612 *              key of type (x % 101).
5613 *
5614 *       return (idx % hash_table_size);
5615 *
5616 *       Ret:   ROK     - successful, *idx contains computed index
5617 *
5618 *       Notes: None.
5619 *
5620 *       File:  cm_mem.c
5621 *
5622 */
5623 static S16 cmMmHashFunc(CmMmHashListCp *hashListCp,uint32_t key,uint16_t *idx)
5624 {
5625
5626    *idx = (uint16_t)(key % hashListCp->numOfbins);
5627
5628    return ROK;
5629
5630 } /* end of cmMmHashFunc () */
5631
5632 /*
5633 *
5634 *       Fun:   cmMmHashListInit
5635 *
5636 *       Desc:  Initializes a hash list. Parameters are:
5637 *
5638 *              hashListCp   control point for hash list
5639 *              nmbBins      number of bins in the hash list. Storage will
5640 *                           be allocated for them from the indicated memory
5641 *                           region and pool.
5642 *              region
5643 *              pool         for allocating storage for bins.
5644 *
5645 *       Ret:   ROK      - initialization successful
5646 *              RFAILED  - initialization failed, lack of memory
5647 *
5648 *       Notes: None
5649 *
5650 *       File:  cm_mem.c
5651 *
5652 */
5653 static S16 cmMmHashListInit
5654 (
5655 CmMmHashListCp *hashListCp,  /* hash list to initialize */
5656 uint16_t     nmbBins,      /* number of hash list bins */
5657 Region       region,       /* memory region to allocate bins */
5658 Pool         pool          /* memory pool to allocate bins */
5659 )
5660 {
5661    uint16_t i;
5662    CmMmHashListEnt *hl;
5663
5664
5665    /* initialize control point fields */
5666    hashListCp->hashList = NULLP;
5667    hashListCp->numOfbins = 0;
5668    hashListCp->numOfEntries  = 0;
5669
5670    /* allocate memory for bins */
5671    if (nmbBins)
5672    {
5673       if (SGetSBufNewForDebug(__FILE__,__FUNCTION__,__LINE__,region, pool, (Data **) &hashListCp->hashList,
5674                (Size)(nmbBins * sizeof(CmMmHashListEnt))) != ROK)
5675       return RFAILED;
5676
5677       /* initialize bin pointers */
5678       hl = hashListCp->hashList;
5679       for(i = 0; i < nmbBins; i++)
5680       {
5681          hl[i].size = hl[i].numAttempts = 0;
5682       }
5683
5684       /* initialize bin size */
5685       hashListCp->numOfbins = nmbBins;
5686    }
5687
5688    return ROK;
5689 }
5690
5691 /*
5692 *
5693 *       Fun:   cmMmHashListDeinit
5694 *
5695 *       Desc:  Deinitializes a hash list. Deallocates memory for bins
5696 *              and resets header fields. Parameters are:
5697 *
5698 *              hashListCp   control point for hash list
5699 *              region
5700 *              pool         for allocating storage for bins.
5701 *
5702 *       Ret:   ROK      - successful
5703 *              RFAILED - failure, invalid parameters
5704 *
5705 *       Notes: None
5706 *
5707 *       File:  cm_mem.c
5708 *
5709 */
5710 static S16 cmMmHashListDeinit
5711 (
5712 CmMmHashListCp *hashListCp,   /* hash list to deinitialize */
5713 Region region,       /* memory region to allocate bins */
5714 Pool   pool          /* memory pool to allocate bins */
5715 )
5716 {
5717
5718    /* deallocate memory for bins */
5719    if (hashListCp->numOfbins)
5720       (Void) SPutSBufNewForDebug(__FILE__,__FUNCTION__,__LINE__,region, pool,
5721                       (Data *) hashListCp->hashList,
5722                       (Size) (hashListCp->numOfbins * sizeof(CmMmHashListEnt)));
5723
5724    /* deinitialize control point fields */
5725    hashListCp->hashList = NULLP;
5726    hashListCp->numOfbins = 0;
5727    hashListCp->numOfEntries = 0;
5728
5729    return ROK;
5730 } /* end of cmMmHashListDeinit */
5731
5732 /*
5733 *
5734 *       Fun:   cmMmHashListInsert
5735 *
5736 *       Desc:  Inserts a new entry in the hash list. Parameters are:
5737 *
5738 *              hashListCp   control point for hash list
5739 *              key          pointer to key string in the new entry
5740 *
5741 *       Ret:   ROK      - insertion successful
5742 *              RFAILED  - insertion failed (incorrect parameter values)
5743 *
5744 *       Notes: None
5745 *
5746 *       File:  cm_mem.c
5747 *
5748 */
5749 static S16 cmMmHashListInsert
5750 (
5751 CmMmHashListCp *hashListCp,  /* hash list to add to */
5752 uint32_t key         /* pointer to key */
5753 )
5754 {
5755    CmMmHashListEnt *hashListEnt;    /* pointer to hash list entry header */
5756    uint16_t idx;                       /* index for insertion into hash list */
5757    uint16_t i;
5758
5759
5760    /* check if hashListCp is initialised yet */
5761    if ( hashListCp->numOfbins == 0)
5762       return ROK;
5763
5764    /* compute index for insertion */
5765    if (cmMmHashFunc(hashListCp, key, &idx) != ROK)
5766       return RFAILED;
5767
5768    hashListEnt = hashListCp->hashList;
5769
5770    if (hashListEnt[idx].numAttempts == 0)
5771    {
5772       /* new entry, insert here */
5773       hashListEnt[idx].size = key;
5774       hashListEnt[idx].numAttempts++;
5775       /* increment count of entries in hash list */
5776       hashListCp->numOfEntries++;
5777    }
5778    else
5779    {
5780       /* this hash is occupied, re-hash it using linear probing */
5781       for (i=idx; i < CMM_STAT_HASH_TBL_LEN; i++)
5782       {
5783          if (hashListEnt[i].size == key)
5784          {
5785             hashListEnt[i].numAttempts++;
5786             break;
5787          }
5788
5789          if (hashListEnt[i].numAttempts == 0)
5790          {
5791             hashListEnt[i].size = key;
5792             hashListEnt[i].numAttempts++;
5793             /* increment count of entries in hash list */
5794             hashListCp->numOfEntries++;
5795             break;
5796          }
5797    }
5798
5799    if (i == CMM_STAT_HASH_TBL_LEN)
5800    {
5801       /* there is no free slot for this key */
5802       return RFAILED;
5803    }
5804    }
5805
5806    return ROK;
5807 } /* end of cmMmHashListInsert */
5808
5809 #endif /* SSI_DEBUG_LEVEL1 */
5810 /*  cm_mem_c_001.main_15 : Additions */
5811 #ifdef SS_HISTOGRAM_SUPPORT  
5812 /*
5813 *
5814 *       Fun:   cmHstGrmHashListInit
5815 *
5816 *       Desc:  Initializes a hash list. Parameters are:
5817 *
5818 *              hashListCp   control point for hash list
5819 *       Ret:   ROK      - initialization successful
5820 *              RFAILED  - initialization failed, lack of memory
5821 *
5822 *       Notes: None
5823 *
5824 *       File:  cm_mem.c
5825 *
5826 */
5827 static S16 cmHstGrmHashListInit
5828 (
5829 CmHstGrmHashListCp *hashListCp  /* hash list to initialize */
5830 )
5831 {
5832    /*cm_mem_c_001.main_25 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
5833 #ifdef  DEBUGP
5834    /* display an error message here */
5835    /*cm_mem_c_001.main_25: Fixed Warnings for 32/64 bit compilation*/ 
5836 #ifdef ALIGN_64BIT
5837     sprintf(dbgPrntBuf, " %lu size of memory histogram hash List\n", sizeof(CmHstGrmHashListCp));
5838 #else
5839     sprintf(dbgPrntBuf, " %d size of memory histogram hash List\n", sizeof(CmHstGrmHashListCp));
5840 #endif
5841     SDisplay(0, dbgPrntBuf);
5842 #endif /* DEBUGP */
5843     memset(hashListCp, 0, sizeof(CmHstGrmHashListCp));
5844     return ROK;
5845 }
5846
5847 /*
5848 *
5849 *       Fun:   cmHstGrmHashListDeInit
5850 *
5851 *       Desc:  De-initializes a hash list. Parameters are:
5852 *
5853 *              hashListCp   control point for hash list
5854 *       Ret:   ROK      - initialization successful
5855 *              RFAILED  - initialization failed, lack of memory
5856 *
5857 *       Notes: None
5858 *
5859 *       File:  cm_mem.c
5860 *
5861 */
5862 static S16 cmHstGrmHashListDeInit
5863 (
5864 CmHstGrmHashListCp *hashListCp  /* hash list to initialize */
5865 )
5866 {
5867    /*cm_mem_c_001.main_25 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
5868 #ifdef  DEBUGP
5869    /* display an error message here */
5870    /*cm_mem_c_001.main_25: Fixed Warnings for 32/64 bit compilation*/ 
5871 #ifdef ALIGN_64BIT
5872     sprintf(dbgPrntBuf, " %lu size of memory histogram hash List\n", sizeof(CmHstGrmHashListCp));
5873 #else
5874     sprintf(dbgPrntBuf, " %d size of memory histogram hash List\n", sizeof(CmHstGrmHashListCp));
5875 #endif
5876     SDisplay(0, dbgPrntBuf);
5877 #endif /* DEBUGP */
5878     memset(hashListCp, 0, sizeof(CmHstGrmHashListCp));
5879     return ROK;
5880 }
5881
5882 /*
5883 *
5884 *       Fun:   cmHstGrmFreeInsert
5885 *
5886 *       Desc:  Inserts a Freed information in into the hash list. Parameters are:
5887 *
5888 *              bkt : pointer to bucket for which memory is freed.
5889 *              line : Line where memory is freed.
5890 *              file : file where memory is freed.
5891 *              entId : Tapa task which releases the memory.
5892 *
5893 *       Ret:   ROK      - insertion successful
5894 *              RFAILED  - insertion failed (incorrect parameter values)
5895 *
5896 *       Notes: None
5897 *
5898 *       File:  cm_mem.c
5899 *
5900 */
5901 static S16 cmHstGrmFreeInsert
5902 (
5903 CmHstGrmHashListCp* hashListCp, /* hash list cp */
5904 uint32_t  blkSz, /* size of the block freed */
5905 uint32_t  line, /* Line number */
5906 uint8_t   *fileName, /* file name */
5907 uint8_t   entId    /* Tapa task which free the memory */
5908 )
5909 {
5910    uint32_t      binIdx = 0; /* Bin index to insert the entry into the hash list */
5911    uint32_t      key = 0; /* Key to fine the bin index */
5912    uint32_t      ret = 0; /* Return value */
5913    CmMemEntries  *entry = NULLP; /* Entry which contains the information */
5914
5915
5916
5917    /* check for the total number of entries in the hash list. *
5918     * If there is no place for new entry return failure */
5919     cmHstGrmGetHashIdxAndKey(fileName, line, &binIdx, &key);
5920
5921    /* After comuting the hash bind and key, check the entity already *
5922       existing or not. if we found the entry then update the information */
5923    ret = cmHstGrmFindEntry(hashListCp, key, &binIdx, &entry);
5924    if(ret == ROK)
5925    {
5926                 entry->freedBytes += blkSz;
5927       entry->bucketFreeReq++;
5928       return ROK;
5929    } /* End of if */
5930
5931    /* If hash list is full then print the error tna continue */
5932    if(hashListCp->totalNumEntries == (CMM_HIST_MAX_MEM_BIN * CMM_HIST_MAX_MEM_ENTRY_PER_BIN))
5933    {
5934         DU_LOG("\nERROR  --> No place in the hash list. Increase the value of macro CMM_HIST_MAX_MEM_BIN and CMM_HIST_MAX_MEM_ENTRY_PER_BIN \n");
5935         return RFAILED;
5936    } /* End of if */
5937
5938    /* Take the address of next free entry in the hash bin */
5939    entry = &(hashListCp->hashList[binIdx].entries[hashListCp->hashList[binIdx].numOfEntries]);
5940
5941    /* Increase the number of time frees called */
5942    entry->bucketFreeReq++;
5943         entry->freedBytes += blkSz;
5944
5945    /* Fill the information into the entry structure */
5946    cmHstGrmFillEntry(entry, key, line, fileName, entId);
5947    /* Increase the total numbet of entries in the bin */
5948    hashListCp->hashList[binIdx].numOfEntries++;
5949
5950    /* Increase the total number of entries in the hash list */
5951    hashListCp->totalNumEntries++;
5952
5953    return ROK;
5954 } /* end of cmHstGrmFreeInsert */
5955
5956
5957 /*
5958 *
5959 *       Fun:  ret = cmHstGrmAllocInsert
5960 *
5961 *       Desc:  Inserts a memory allocated information in the hash list. Parameters are:
5962 *
5963 *              hashListCp   control point for hash list
5964 *              key          pointer to key string in the new entry
5965 *
5966 *       Ret:   ROK      - insertion successful
5967 *              RFAILED  - insertion failed (incorrect parameter values)
5968 *
5969 *       Notes: None
5970 *
5971 *       File:  cm_mem.c
5972 *
5973 */
5974 static S16 cmHstGrmAllocInsert
5975 (
5976 CmHstGrmHashListCp     *hashListCp,
5977 uint32_t   blkSz,
5978 uint32_t   *reqSz,
5979 uint32_t   line,
5980 uint8_t    *fileName,
5981 uint8_t    entId
5982 )
5983 {
5984    uint32_t    binIdx = 0;
5985    uint32_t    key = 0;
5986    uint32_t    ret = 0;
5987    CmMemEntries *entry = NULLP;
5988
5989
5990    /* check for the total number of entries in the hash list. *
5991     * If there is no place for new entry return failure */
5992    cmHstGrmGetHashIdxAndKey(fileName, line, &binIdx, &key);
5993
5994    /* After comuting the hash bind and key, check the entity already *
5995       existing or not. if we found the entry then update the information */
5996    ret = cmHstGrmFindEntry(hashListCp, key, &binIdx, &entry);
5997
5998    if(ret == ROK)
5999    {
6000            entry->allocBytes += blkSz;
6001       entry->bucketAllocReq++;
6002       entry->wastedBytes += (blkSz - *reqSz);
6003       return ROK;
6004    } /* End of if */
6005
6006    if(hashListCp->totalNumEntries == (CMM_HIST_MAX_MEM_BIN * CMM_HIST_MAX_MEM_ENTRY_PER_BIN))
6007    {
6008         DU_LOG("\nERROR  --> No place in the hash list. Increase the value of macro CMM_HIST_MAX_MEM_BIN and CMM_HIST_MAX_MEM_ENTRY_PER_BIN\n");
6009         return RFAILED;
6010    } /* End of if */
6011
6012    /* Take the address of next free entry in the hash bin */
6013    entry = &(hashListCp->hashList[binIdx].entries[hashListCp->hashList[binIdx].numOfEntries]);
6014
6015    /* Clauculae the wasted bytes */
6016    /* Here wasted byte is differnce between the byte user
6017     * has requested and the byte the ssi allocated */
6018    entry->wastedBytes += (blkSz - *reqSz);
6019    entry->bucketAllocReq++;
6020         entry->allocBytes += blkSz;
6021
6022    /* Fill the information into the entry structure */
6023    cmHstGrmFillEntry(entry, key, line, fileName, entId);
6024    /* Increase the total numbet of entries in the bin */
6025    hashListCp->hashList[binIdx].numOfEntries++;
6026
6027    /* Increase the total number of entries in the hash list */
6028    hashListCp->totalNumEntries++;
6029
6030    return ROK;
6031 } /* end of cmHstGrmAllocInsert */
6032
6033
6034 /*
6035 *
6036 *       Fun:   cmHstGrmGetHashIdxAndKey
6037 *
6038 *       Desc:  Finds an entry in the hash list. Parameters are:
6039 *
6040 *              hashListCp   control point for hash list
6041 *              key          pointer to key string in the new entry
6042 *
6043 *       Ret:   ROK      - insertion successful
6044 *              RFAILED  - insertion failed (incorrect parameter values)
6045 *
6046 *       Notes: None
6047 *
6048 *       File:  cm_mem.c
6049 *
6050 */
6051 static S16 cmHstGrmGetHashIdxAndKey
6052 (
6053 uint8_t   *fileName,
6054 uint32_t  line,
6055 uint32_t  *binIdx,
6056 uint32_t  *key
6057 )
6058 {
6059
6060    uint32_t  i = 0;
6061
6062    /* Calculate the key using file name and line number */
6063    for(i = 0 ; fileName[i] != '\0'; i++)
6064    {
6065        *key += fileName[i];
6066    }/* End of for */
6067        *key += line;
6068    *binIdx = ( *key % CMM_HIST_MAX_MEM_BIN);
6069    return ROK;
6070 } /* end of cmHstGrmFillEntry */
6071
6072 /*
6073 *
6074 *       Fun:   cmHstGrmFillEntry
6075 *
6076 *       Desc:  Insert the entry into the hash list.
6077 *
6078 *              entry : Infornation which will be inserted into the hash list
6079 *              key   : Which will be used ti find the entry.
6080 *              line  : Line number
6081 *              fileName : File name
6082 *              entId   : Tapa task Id
6083 *
6084 *       Ret:   ROK      - insertion successful
6085 *              RFAILED  - insertion failed (incorrect parameter values)
6086 *
6087 *       Notes: None
6088 *
6089 *       File:  cm_mem.c
6090 *
6091 */
6092 static S16 cmHstGrmFillEntry
6093 (
6094 CmMemEntries *entry,
6095 uint32_t     key,
6096 uint32_t     line,
6097 uint8_t      *fileName,
6098 uint8_t      entId
6099 )
6100 {
6101
6102    uint32_t       idx = 0;
6103    entry->key = key;
6104    entry->line = line;
6105    entry->entId = entId;
6106    for(idx = 0; fileName[idx] != '\0'; idx++)
6107    {
6108       entry->fileName[idx] = fileName[idx];
6109    }
6110    entry->fileName[idx] = '\0';
6111    return ROK;
6112 } /* end of cmHstGrmFillEntry */
6113
6114 /*
6115 *
6116 *       Fun:  cmHstGrmFindEntry
6117 *
6118 *       Desc:  Finds an entry in the hash list. Parameters are:
6119 *
6120 *              hashListCp   control point for hash list
6121 *              key          pointer to key string in the new entry
6122 *
6123 *       Ret:   ROK      - insertion successful
6124 *              RFAILED  - insertion failed (incorrect parameter values)
6125 *
6126 *       Notes: None
6127 *
6128 *       File:  cm_mem.c
6129 *
6130 */
6131 static S16 cmHstGrmFindEntry
6132 (
6133 CmHstGrmHashListCp  *hashListCp,
6134 uint32_t      key,
6135 uint32_t      *binIdx,
6136 CmMemEntries  **entry
6137 )
6138 {
6139
6140    uint32_t   numEnt = 0;
6141    uint32_t   numBin = 0;
6142    CmHstGrmHashListEnt  *tmpBin = NULLP;
6143
6144    for(numBin = 0; numBin < CMM_HIST_MAX_MEM_BIN; numBin++)
6145    {
6146       /* find for the entry in the bin */
6147       tmpBin = &(hashListCp->hashList[*binIdx]);
6148       for(numEnt = 0; numEnt < tmpBin->numOfEntries; numEnt++)
6149       {
6150          /* If key supplied is matched with the stored key then
6151           * return that entity */
6152          if(tmpBin->entries[numEnt].key == key)
6153          {
6154             *entry = &(tmpBin->entries[numEnt]);
6155             return ROK;
6156          }/* End of if (tmpBin->entries[numEnt].key) */
6157       }/* end of for (numEnt = 0) */
6158
6159       /* Here we are checking for any room left in the bin. If the room *
6160          exists its mean that there is no entry with the Key. so return *
6161          failure.
6162          If there is no room in the bin, then check the other bins to find *
6163          the entry */
6164       if(numEnt == CMM_HIST_MAX_MEM_ENTRY_PER_BIN)
6165       {
6166         if(*binIdx == CMM_HIST_MAX_MEM_BIN)
6167         {
6168             *binIdx = 0;
6169         }/* End of if (binIdx) */
6170         else
6171         {
6172             *binIdx++;
6173         }/* End of else (binIdx) */
6174       } /* End of if (numEnt) */
6175       else
6176       {
6177          DU_LOG("\nERROR  --> Unable to find the entry in hash list\n");
6178          return RFAILED;
6179       }/* End of else (numEnt) */
6180    }/* end of for (numBin = 0) */
6181
6182    DU_LOG("\nERROR  --> Unable to find the entry in the hash list\n");
6183    return RFAILED;
6184 } /* end of cmHstGrmFindEntry */
6185
6186 #endif /* SS_HISTOGRAM_SUPPORT */
6187
6188 /**********************************************************************
6189          End of file
6190  **********************************************************************/