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