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