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