1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2017-2019] [Radisys] #
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 #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
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 *******************************************************************************/
19 /********************************************************************20**
21 Name: Common Memory Manager
25 Desc: C source code for the Commom Memory Manager module.
29 Sid: cm_mem.c@@/main/28 - Fri Aug 26 13:52:41 2011
33 *********************************************************************21*/
36 /************************************************************************
38 The following functions are provided in this file.
40 cmMmRegInit Memory Region Initialization.
41 cmMmRegDeInit Memory Region Deinitialization.
43 ************************************************************************/
46 /* header include files (.h) */
47 #include "envopt.h" /* environment options */
48 #include "envdep.h" /* environment dependent */
49 #include "envind.h" /* environment independent */
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
58 #ifdef SS_MEM_LEAK_SOL
61 #include <sys/machelf.h>
62 #else /* SS_MEM_LEAK_SOL */
64 #endif /* SS_MEM_LEAK_SOL */
65 #include <sys/types.h>
67 #endif /* SS_MEM_LEAK_STS */
69 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
70 #include "cm_hash.h" /* common hash functions */
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 */
77 #ifdef SS_MULTICORE_SUPPORT
78 #include "ss_dep.h" /* implementation-specific */
79 #include "ss_queue.h" /* queues */
80 #include "ss_task.h" /* tasking */
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 */
91 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
92 #include "cm_hash.x" /* common hash functions */
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 */
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 */
113 #endif /* USE_PURE */
115 #ifdef SS_LIGHT_MEM_LEAK_STS
117 uint32_t queueIndxAllocCnt =0;
118 uint32_t queueIndxFreeCnt =0;
119 uint32_t allocQueueFullCnt =0;
120 uint32_t allocQueueEmptyCnt =0;
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*/
137 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
138 uint32_t memFreeCount=0;
139 uint32_t cmFreeCaller[4]={0};
140 uint32_t cmAllocCaller[4]={0};
141 Data *startPtr256=NULLP;
142 Data *startPtr128=NULLP;
143 uint32_t cmMemInfo128[100000][2]={0, 0};
144 uint32_t cmMemInfo256[100000][2]={0, 0};
145 Data *startPtr512=NULLP;
146 Data *startPtr768=NULLP;
147 Data *startPtr1664=NULLP;
148 Data *startPtr4800=NULLP;
149 Data *startPtr9920=NULLP;
152 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
153 //extern CmMmRegCb *mtCMMRegCb[SS_MAX_REGS];
155 #ifdef SS_HISTOGRAM_SUPPORT
156 #ifdef SSI_DEBUG_LEVEL1
157 static S16 cmAlloc ARGS((Void *regionCb, Size *size, uint32_t flags, Data **ptr,
158 uint32_t memType, uint32_t line, uint8_t *fileName, uint8_t entId, Bool hstReg));
159 static S16 cmHeapAlloc ARGS((CmMmHeapCb *heapCb, Data **ptr, Size *size, uint32_t memType, uint32_t line, uint8_t *fileName, uint8_t entId, Bool hstReg));
160 /*cm_mem_c_001.main_20-added new functionto allocate memory from new region*/
162 static S16 cmHeapAlloc ARGS((CmMmHeapCb *heapCb, Data **ptr, Size *size,
163 uint32_t line, uint8_t *fileName, uint8_t entId, Bool hstReg));
164 static S16 cmAlloc ARGS((Void *regionCb, Size *size, uint32_t flags, Data **ptr,
165 uint32_t line, uint8_t *fileName, uint8_t entId, Bool hstReg));
166 #endif /* SSI_DEBUG_LEVEL1 */
168 static S16 cmFree ARGS((Void *regionCb, Data *ptr, Size size, uint32_t line,
169 uint8_t *fileName, uint8_t entId, Bool hstReg));
171 static S16 cmHeapFree ARGS((CmMmHeapCb *heapCb, Data *ptr, Size size,
172 uint32_t line, uint8_t *fileName, uint8_t entId, Bool hstReg));
173 #else /* no histogram support */
174 /* cm_mem_c_001.main_12 - prototype is changed to accept memType(static/dynamic) */
175 #ifdef SSI_DEBUG_LEVEL1
176 static S16 cmHeapAlloc ARGS((CmMmHeapCb *heapCb, Data **ptr, Size *size, uint32_t memType));
179 static S16 cmHeapAlloc ARGS((CmMmHeapCb *heapCb, Data **ptr, Size *size));
181 #endif /* SSI_DEBUG_LEVEL1 */
183 static S16 cmHeapFree ARGS((CmMmHeapCb *heapCb, Data *ptr, Size size));
185 /* cm_mem_c_001.main_15 :Additions */
186 #ifdef SS_LIGHT_MEM_LEAK_STS
187 static S16 cmAlloc ARGS((Void *regionCb, Size *size, uint32_t flags, Data **ptr, uint32_t memType, uint32_t lineNo,uint8_t *funcName ));
188 static S16 cmFree ARGS((Void *regionCb, Data *ptr, Size size, uint32_t lineNo, uint8_t* funcName));
189 #else /*SS_LIGHT_MEM_LEAK_STS */
190 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
191 static S16 cmAlloc ARGS((Void *regionCb, Size *size, uint32_t flags, Data **ptr, uint32_t memType));
193 #ifdef T2K_MEM_LEAK_DBG
194 static S16 cmAlloc ARGS((Void *regionCb, Size *size, uint32_t flags, Data **ptr, char* file, uint32_t line));
196 static S16 cmAlloc ARGS((Void *regionCb, Size *size, uint32_t flags, Data **ptr));
198 #endif /* SSI_DEBUG_LEVEL1 */
199 #ifdef T2K_MEM_LEAK_DBG
200 static S16 cmFree ARGS((Void *regionCb, Data *ptr, Size size, char* file, uint32_t line));
202 static S16 cmFree ARGS((Void *regionCb, Data *ptr, Size size));
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*/
208 static S16 cmAllocWL ARGS((Void *regionCb, Size *size, uint32_t flags, Data **ptr));
209 static S16 cmFreeWL ARGS((Void *regionCb, Data *ptr, Size size));
212 static S16 cmCtl ARGS((Void *regionCb, Event event, SMemCtl *memCtl));
214 static Void cmMmHeapInit ARGS((Data *memAddr, CmMmHeapCb *heapCb, Size size));
215 static Void cmMmBktInit ARGS((Data **memAddr, CmMmRegCb *regCb,
216 CmMmRegCfg *cfg, uint16_t bktIdx, uint16_t *lstMapIdx));
218 /* cm_mem_c_001.main_12 - addition of protoypes for sanity check and hash list functions */
219 #ifdef SSI_DEBUG_LEVEL1
220 static S16 cmMmBktSanityChk ARGS((CmMmBkt *bkt));
221 static S16 cmMmHeapSanityChk ARGS((CmMmHeapCb *heapCb));
222 static S16 cmMmHashFunc ARGS((CmMmHashListCp *hashListCp, uint32_t key, uint16_t *idx ));
223 static S16 cmMmHashListInit ARGS((CmMmHashListCp *hashListCp, uint16_t nmbBins,
224 Region region, Pool pool));
225 static S16 cmMmHashListDeinit ARGS((CmMmHashListCp *hashListCp, Region region, Pool pool));
226 static S16 cmMmHashListInsert ARGS((CmMmHashListCp *hashListCp, uint32_t key));
227 #endif /* SSI_DEBUG_LEVEL1 */
228 /* cm_mem_c_001.main_15 : Addtions */
229 #ifdef SS_HISTOGRAM_SUPPORT
230 static S16 cmHstGrmAllocInsert ARGS((CmHstGrmHashListCp *hashListCp, uint32_t blkSz, uint32_t *reqSz, uint32_t line, uint8_t *fileName, uint8_t entId));
231 static S16 cmHstGrmFreeInsert ARGS((CmHstGrmHashListCp* hashListCp, uint32_t blkSz, uint32_t line, uint8_t *fileName, uint8_t entId));
232 static S16 cmHstGrmHashListInit ARGS((CmHstGrmHashListCp *hashListCp));
233 static S16 cmHstGrmHashListDeInit ARGS((CmHstGrmHashListCp *hashListCp));
234 static S16 cmHstGrmGetHashIdxAndKey ARGS((uint8_t *fileName, uint32_t line, uint32_t *binIdx, uint32_t *key));
235 static S16 cmHstGrmFindEntry ARGS((CmHstGrmHashListCp *hashListCp, uint32_t key, uint32_t *binIdx, CmMemEntries **entry));
236 static S16 cmHstGrmFillEntry ARGS((CmMemEntries *entry, uint32_t key, uint32_t line, uint8_t *fileName, uint8_t entId));
237 #endif /* SS_HISTOGRAM_SUPPORT */
239 /* cm_mem_c_001.main_22: Fixing warnings on GCC compiler */
244 /* public variable declarations */
247 #endif /* USE_PURE */
248 /* cm_mem_c_001.main_15:Additions */
249 #ifdef SS_MEM_LEAK_STS
250 MemUsrMdlStr memUsrMdlStr[]=
252 MEMRAW2STR(DEFAULT, STACK),
253 MEMRAW2STR(tc, PDCP_LAYER),
254 MEMRAW2STR(Tc, PDCP_LAYER),
255 MEMRAW2STR(mg, GCP_LAYER),
256 MEMRAW2STR(Mg, GCP_LAYER),
261 #endif /* SS_MEM_LEAK_STS */
262 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
268 #ifdef T2K_MEM_LEAK_DBG
270 uint8_t minBktSzBitMask; /* minimum allocation size in Log(x)base 2, where X is minimum bucket size in region */
271 uint16_t minBktSzMins1; /* X-1 */
273 static RegMinBktSzInfo regMinBktSzInfo[SS_MAX_REGS] = {{8,0xFF},{7,0x7F},{7,0x7F},{7,0x7F}};
274 RegionMemLeakInfo regMemLeakInfo;
276 uint32_t getT2kMemLeakIndex(uint64_t address, Region region)
278 return ((address - regMemLeakInfo.regStartAddr[region]) >> regMinBktSzInfo[region].minBktSzBitMask);
281 static uint32_t t2kMemAllocTick;
282 static uint32_t smallTick;
284 void InsertToT2kMemLeakInfo(uint64_t address, uint32_t size, uint32_t lineNo, char* fileName, Region region)
287 T2kMeamLeakInfo *leakInfo;
293 uint32_t index1 = getT2kMemLeakIndex(address,region);
295 if(((uint64_t)(address - regMemLeakInfo.regStartAddr[region]) & regMinBktSzInfo[region].minBktSzMins1) !=0)
297 DU_LOG("\nINFO --> address in InsertToT2kMemLeakInfo is %ld size = %d file is %s line is %d \n", address, size, fileName, lineNo);
300 leakInfo = (((T2kMeamLeakInfo*)(regMemLeakInfo.gMemLeakInfo[region])) + index1);
301 if(leakInfo->address == 0)
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';
311 //DU_LOG("\nINFO --> InsertToT2kMemLeakInfo the adress from List Address = %x, index1 = %d \
312 from File=%s, line=%d \n",address,index1,fileName,lineNo);
313 if(smallTick++ == 4096)
316 leakInfo->age = (++t2kMemAllocTick);
321 DU_LOG("\nERROR --> Something is wrong, trying to insert %ld index1 = %d file is %s line is %d \n",address, index1, fileName, lineNo);
322 DU_LOG("\nINFO --> Address present :%ld, from File:%s, Line:%d, Size:%d, Age:%d",
323 leakInfo->address, leakInfo->fileName,
324 leakInfo->lineNo, leakInfo->size,
330 void RemoveFromT2kMemLeakInfo(uint64_t address, char *file, uint32_t line,Region region)
332 T2kMeamLeakInfo *leakInfo;
339 uint32_t index1 = getT2kMemLeakIndex(address, region);
341 if(index1 >= T2K_MEM_LEAK_INFO_TABLE_SIZE)
343 DU_LOG("\nERROR --> index1 out of range = %d address is %ld file = %s line = %d. We are going to crash!!!\n",
349 leakInfo = (((T2kMeamLeakInfo*)(regMemLeakInfo.gMemLeakInfo[region])) + index1);
350 if(leakInfo->address == address)
353 leakInfo->address = 0;
355 leakInfo->prevRemLineNo = leakInfo->lineNo;
356 leakInfo->prevRemFileName = leakInfo->fileName;
357 leakInfo->lastDelLineNum = line;
358 leakInfo->lastDelFileName = file;
362 DU_LOG("\nERROR --> Something is wrong, trying to remove %ld index1 = %d from File=%s, line=%d address present is %ld region%d \n",address, index1, file,line,leakInfo->address,region);
364 DU_LOG("\nINFO --> Last Del file %s line %d\n",leakInfo->lastDelFileName,
365 leakInfo->lastDelLineNum);
367 if(leakInfo->prevRemFileName != NULLP)
369 DU_LOG("\nINFO --> Previous File:%s, Previous Line:%d\n",
370 leakInfo->prevRemFileName, leakInfo->prevRemLineNo);
375 void DumpT2kMemLeakInfoToFile()
378 T2kMeamLeakInfo *leakInfo;
380 FILE *fp = fopen("memLeakInfo_reg.txt","wb");
384 DU_LOG("\nERROR --> Could not open file for dumping mem leak info\n");
387 for(reg=0; reg <regMemLeakInfo.numActvRegions; reg++)
389 fprintf(fp, "REGION %d LEAKS START\n",reg);
392 for(i = 0; i< T2K_MEM_LEAK_INFO_TABLE_SIZE; i++)
394 leakInfo = (((T2kMeamLeakInfo*)(regMemLeakInfo.gMemLeakInfo[reg])) + i);
395 if(leakInfo->address != 0)
397 char* onlyFileName = rindex(leakInfo->fileName,'/');
398 if(onlyFileName == NULL)
400 onlyFileName = leakInfo->fileName;
403 fprintf(fp, "%ld s=%d a=%d l=%d f=%s\n",leakInfo->address,
410 fprintf(fp, "REGION %d LEAKS END\n",reg);
412 fprintf(fp,"Current t2kMemAllocTick = %d\n",t2kMemAllocTick);
416 #endif /* T2K_MEM_LEAK_DBG */
418 /* cm_mem_c_008.104 - Addition for memory calculator tool */
420 static Txt prntBuf[200]; /* print buffer */
421 static uint8_t tryHeap=0;
424 /* cm_mem_c_001.main_12 - addition for ssi enhancements prints */
425 /* cm_mem_c_001.main_20 Additions */
426 #if (defined(SSI_DEBUG_LEVEL1) || defined(SS_HISTOGRAM_SUPPORT) || \
427 defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
429 static Txt dbgPrntBuf[200]; /* print buffer */
431 #endif /*SSI_DEBUG_LEVEL1 || SS_HISTOGRAM_SUPPORT */
433 uint32_t num_times = 0;
434 #ifdef SSI_MEM_CORR_PREVENTION
435 uint32_t cmDblFreeAttempts = 0;
437 /* private variable declarations */
444 * Desc: Configure the memory region for allocation. The function
445 * registers the memory region with System Service by calling
449 * Ret: ROK - successful,
450 * RFAILED - unsuccessful.
452 * Notes: The memory owner calls this function to initialize the memory
453 * manager with the information of the memory region. Before
454 * calling this function, the memory owner should allocate memory
455 * for the memory region. The memory owner should also provide the
456 * memory for the control block needed by the memory manager. The
457 * memory owner should allocate the memory for the region control
458 * block as cachable memory. This may increase the average
459 * throughput in allocation and deallocation as the region control
460 * block is mostly accessed by the CMM.
465 S16 cmMmRegInit(Region region,CmMmRegCb *regCb,CmMmRegCfg *cfg)
471 #if (ERRCLASS & ERRCLS_INT_PAR)
474 Txt errMsg[256] = {'\0'};
476 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
482 #if (ERRCLASS & ERRCLS_INT_PAR)
484 /* error check on parameters */
485 if ((regCb == NULLP) || (cfg == NULLP))
490 /* Error check on the configuration fields */
491 if ((!cfg->size) || (cfg->vAddr == NULLP) ||
492 (cfg->numBkts > CMM_MAX_BKT_ENT))
496 /* Check if the quantum size is power of 2 */
497 if ((cfg->numBkts) &&
498 ((cfg->bktQnSize - 1) & (cfg->bktQnSize)))
500 /* cm_mem_c_001.main_20 Addition */
501 sprintf(errMsg,"\n cmMmRegInit() failed, check if BktQuantum size might not be power of 2 \n");
507 * Check if the size of the memory region is enough, whether bucket sizes
508 * are multiples of quantumn size, and also whether two consecutive buckets
509 * falls within same quanta.
511 lstQnSize = cfg->bktQnSize;
514 for ( bktIdx =0; bktIdx < cfg->numBkts; bktIdx++)
516 /* check if bucket size is mutiple of quantum size */
517 if (cfg->bktCfg[bktIdx].size % cfg->bktQnSize)
519 /* cm_mem_c_001.main_20 Addition */
520 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/
522 sprintf(errMsg,"\n cmMmRegInit() failed, Bkt:%d size:%u not multiple of quantum size:%u\
523 \n",bktIdx,cfg->bktCfg[bktIdx].size,cfg->bktQnSize);
525 sprintf(errMsg,"\n cmMmRegInit() failed, Bkt:%d size:%lu not multiple of quantum size:%lu\
526 \n",bktIdx,cfg->bktCfg[bktIdx].size,cfg->bktQnSize);
532 if ((bktBlkSize = cfg->bktCfg[bktIdx].size) < lstQnSize)
535 * Two consecutive buckets are not separated by quantum size.
537 /* cm_mem_c_001.main_20 Addition */
538 sprintf(errMsg,"\n cmMmRegInit() failed, Two consecutive buckets are not separated by quantum size \n");
542 /* cm_mem_c_001.main_20 Addition */
543 if (((cfg->bktCfg[bktIdx].size) /\
544 cfg->bktQnSize) > CMM_MAX_MAP_ENT)
546 /* Error check whether the size of the mapping table is sufficient */
547 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/
549 sprintf(errMsg,"\n cmMmRegInit() failed, check maxBucketSize/BktQuantumSize(%u)\
550 \n should be less than CMM_MAX_MAP_ENT:%d \n",cfg->bktQnSize,CMM_MAX_MAP_ENT);
552 sprintf(errMsg,"\n cmMmRegInit() failed, check maxBucketSize/BktQuantumSize(%lu)\
553 \n should be less than CMM_MAX_MAP_ENT:%d \n",cfg->bktQnSize,CMM_MAX_MAP_ENT);
560 regCb->bktSize += (cfg->bktCfg[bktIdx].size *
561 cfg->bktCfg[bktIdx].numBlks);
563 if (regCb->bktSize > cfg->size)
565 /* Size of the memory region is less than the required size */
567 sprintf(errMsg,"\n cmMmRegInit() failed, Size of the memory region is less than the required size \n");
572 lstQnSize = ((bktBlkSize / cfg->bktQnSize) + 1) * cfg->bktQnSize;
577 /* Initialize the region control block */
578 regCb->region = region;
579 regCb->regInfo.regCb = regCb;
580 regCb->regInfo.start = cfg->vAddr;
581 regCb->regInfo.size = cfg->size;
584 avail_size = cfg->size;
585 #endif /* USE_PURE */
587 if ( cfg->chFlag & CMM_REG_OUTBOARD)
589 /* Out_of_board memory */
590 regCb->regInfo.flags = CMM_REG_OUTBOARD;
594 regCb->regInfo.flags = 0;
598 /* Initialize the memory manager function handlers */
599 /*cm_mem_c_001.main_21-registering new alloc function for new region*/
601 if(region == SS_WL_REGION)
603 regCb->regInfo.alloc = cmAllocWL;
604 regCb->regInfo.free = cmFreeWL;
609 /* Initialize the memory manager function handlers */
610 regCb->regInfo.alloc = cmAlloc;
611 regCb->regInfo.free = cmFree;
613 regCb->regInfo.ctl = cmCtl;
615 /* Initialize the physical address */
616 if ((regCb->chFlag = cfg->chFlag) & CMM_REG_PHY_VALID)
618 regCb->pAddr = cfg->pAddr;
621 /* Initial address of the memory region block */
622 memAddr = cfg->vAddr;
624 /* Initialize the fields related to the bucket pool */
625 regCb->bktMaxBlkSize = 0;
628 if (cfg->numBkts > 0 && cfg->numBkts < CMM_MAX_BKT_ENT)
630 /* Last bucket has the maximum size */
631 regCb->bktMaxBlkSize = cfg->bktCfg[cfg->numBkts - 1].size;
633 /* Get the power of the bktQnSize */
635 while( !((cfg->bktQnSize >> regCb->bktQnPwr) & 0x01))
640 /* Initilaize the bktIndex of the map entries to FF */
641 for ( lstMapIdx = 0; lstMapIdx < CMM_MAX_MAP_ENT; lstMapIdx++)
643 regCb->mapTbl[lstMapIdx].bktIdx = 0xFF;
647 for ( bktIdx = 0; bktIdx < cfg->numBkts; bktIdx++)
649 /* Allocate the lock for the bucket pool */
650 /* cm_mem_c_001.main_13 : Replaced SInitLock with WTInitLock for NT */
652 if (WTInitLock (&(regCb->bktTbl[bktIdx].bktLock), cfg->lType) != ROK)
654 if (SInitLock (&(regCb->bktTbl[bktIdx].bktLock), cfg->lType) != ROK)
657 /* Free the initialzed lock for the earlier buckets. */
660 /* cm_mem_c_001.main_13: Replaced SDestroyLock with
661 WTDestroyLock for NT */
662 /* cm_mem_c_001.main_24 fix for memory corruption*/
665 WTDestroyLock(&(regCb->bktTbl[bktIdx].bktLock));
667 SDestroyLock(&(regCb->bktTbl[bktIdx].bktLock));
674 cmMmBktInit( &memAddr, regCb, cfg, bktIdx, &lstMapIdx);
677 /* Used while freeing the bktLock in cmMmRegDeInit */
678 regCb->numBkts = cfg->numBkts;
682 * Initialize the heap pool if size the memory region region is more
683 * than the size of the bucket pool
686 regCb->heapFlag = FALSE;
688 /* Align the memory address */
689 memAddr = (Data *)(PTRALIGN(memAddr));
691 regCb->heapSize = cfg->vAddr + cfg->size - memAddr;
694 * Round the heap size so that the heap size is multiple
697 regCb->heapSize -= (regCb->heapSize % CMM_MINBUFSIZE);
701 /* Allocate the lock for the heap pool */
702 /* cm_mem_c_001.main_13 : Replaced SInitLock with WTInitLock for NT */
704 if (WTInitLock (®Cb->heapCb.heapLock, cfg->lType) != ROK)
706 if (SInitLock (®Cb->heapCb.heapLock, cfg->lType) != ROK)
709 bktIdx = cfg->numBkts;/* ccpu00125353: warning fix */
712 /* Free the initialzed locks of the buckets */
715 /* cm_mem_c_001.main_13: Replaced SDestroyLock with
716 WTDestroyLock for NT */
717 /* cm_mem_c_001.main_24 fix for memory corruption*/
720 WTDestroyLock(&(regCb->bktTbl[bktIdx].bktLock));
722 SDestroyLock(&(regCb->bktTbl[bktIdx].bktLock));
730 regCb->heapFlag = TRUE;
731 cmMmHeapInit(memAddr, &(regCb->heapCb), regCb->heapSize);
734 /* Call SRegRegion to register the memory region with SSI */
735 if (SRegRegion(region, ®Cb->regInfo) != ROK)
740 /* cm_mem_c_001.main_12 - addition for initializing the hash table */
741 #ifdef SSI_DEBUG_LEVEL1
742 /* Initialize the region level hash table for debug info storage */
743 if (cmMmHashListInit(®Cb->hashListCp, CMM_STAT_HASH_TBL_LEN, region, 0) != ROK)
747 #endif /* SSI_DEBUG_LEVEL1 */
748 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
749 /* Initialize the hast list to maintain the SSI memory information for Broadcom */
750 offset = (uint16_t)((PTR)(&ptrHdr.ent) - (PTR) &ptrHdr);
751 DU_LOG("\nINFO --> offset is %d region %d\n", offset, region);
752 if(cmHashListInit(®Cb->brdcmSsiLstCp, 1000, offset, FALSE,
753 CM_HASH_KEYTYPE_UINT32_MOD, region, 0) != ROK)
757 #endif /* SSI_DEBUG_LEVEL1 */
760 } /* end of cmMmRegInit*/
768 * Desc: Deinitialize the memory region. The function call SDeregRegion
769 * to deregister the memory region with System Service.
772 * Ret: ROK - successful
773 * RFAILED - unsuccessful.
775 * Notes: The memory owner calls this function to deinitialize the region.
776 * The memory manager does not return the memory to the system.
777 * Before calling this function, the memory owner must be sure that
778 * no layer is using any memory block from this region. On
779 * successful return from the function, any request to the memory
780 * manager to allocate/deallocate memory will fail. The memory owner
781 * can reuse the memory for other region or return the memory to the
782 * system memory pool.
789 S16 cmMmRegDeInit(CmMmRegCb *regCb)
794 #if (ERRCLASS & ERRCLS_INT_PAR)
796 /* error check on parameters */
804 /* cm_mem_c_001.main_12 - addition for deinitializing the hash table */
805 #ifdef SSI_DEBUG_LEVEL1
806 /* Deinitialize the hash table used for debug info storage at region level */
807 if (cmMmHashListDeinit(®Cb->hashListCp, regCb->region, 0) != ROK)
811 #endif /* SSI_DEBUG_LEVEL1 */
812 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
813 /* Deinitialize the hash table used for broadcom ssi instrumentation */
814 if (cmHashListDeinit(®Cb->brdcmSsiLstCp) != ROK)
820 /* Call SDeregRegion first to deregister the memory region with SSI */
821 (Void) SDeregRegion (regCb->region);
825 /* Bucket pool is configured */
827 /* Free the initialzed locks of the buckets */
828 for ( bktIdx = regCb->numBkts; bktIdx > 0;)
830 /* cm_mem_c_001.main_13: Replaced SDestroyLock with
831 WTDestroyLock for NT */
832 /* cm_mem_c_001.main_24 fix for memory corruption*/
835 WTDestroyLock(&(regCb->bktTbl[bktIdx].bktLock));
837 SDestroyLock(&(regCb->bktTbl[bktIdx].bktLock));
839 /* cm_mem_c_001.main_15:Additions */
840 #ifdef SS_HISTOGRAM_SUPPORT
841 /* De-initialise the memory histogram hash list */
842 cmHstGrmHashListDeInit(&(regCb->bktTbl[bktIdx].hstGrmHashListCp));
843 #endif /* SS_HISTOGRAM_SUPPORT */
849 /* Heap pool is configured */
851 /* cm_mem_c_001.main_13: Replaced SDestroyLock with
852 WTDestroyLock for NT */
854 WTDestroyLock(®Cb->heapCb.heapLock);
856 SDestroyLock(®Cb->heapCb.heapLock);
862 } /* end of cmMmRegDeInit */
864 #ifdef CM_MEM_OVERUSED
865 #define OVERUSED(_bkt) (((_bkt)->numAlloc * 100) / (_bkt)->numBlks > 80)
867 int g_overused[5] = {0};
870 #ifdef CM_MEM_PRINT_DEFINED
871 volatile int gSubIndex = 0;
872 int regAllocBktSts[10][2][6]={{0}};
873 int regFreeBktSts[10][2][6]={{0}};
880 * Desc: Allocate a memory block for the memory region.
883 * Ret: ROK - successful
884 * RFAILED - unsuccessful.
887 * The function allocates a memory block of size atleast equal to
888 * the requested size. The size parameter will be updated with the
889 * actual size of the memory block allocated for the request. The
890 * CMM tries to allocate the memory block form the bucket pool. If
891 * there is no memory in the bucket the CMM allocates the memory
892 * block form the heap pool. This function is always called by the
893 * System Service module.
895 * The caller of the function should try to use the out value of
896 * the size while returning the memory block to the region. However
897 * the current design of the memory manager does not enforce to pass
898 * the actual size of the memory block. (Due to the SGetSBuf
899 * semantics the layer will not able to pass the correct size of the
900 * memory block while calling SPutSBuf).
906 /* cm_mem_c_001.main_12 - addition to accept new parameter memType(static/dynamic) */
908 /* cm_mem_c_001.main_15 : Additions */
909 #ifdef SS_HISTOGRAM_SUPPORT
910 #ifdef SSI_DEBUG_LEVEL1
935 #endif /* SSI_DEBUG_LEVEL1 */
937 #ifdef SS_LIGHT_MEM_LEAK_STS
948 #else /*SS_LIGHT_MEM_LEAK_STS */
949 #ifdef SSI_DEBUG_LEVEL1
959 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
969 #ifdef T2K_MEM_LEAK_DBG
988 #endif /* BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1 */
989 #endif /* SSI_DEBUG_LEVEL1 */
990 #endif /*SS_LIGHT_MEM_LEAK_STS */
991 /* cm_mem_c_001.main_15: Additions */
992 #endif /* SS_HISTOGRAM_SUPPORT */
995 /* cm_mem_c_001.main_26 : Fixes for Compilation Warnings */
1001 /* cm_mem_c_001.main_26 : Fixes for Compilation Warnings */
1005 /* cm_mem_c_001.main_15 : Additions */
1006 #if (defined(SS_MEM_LEAK_STS) || defined( BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
1008 #endif /* SS_MEM_LEAK_STS */
1009 /* cm_mem_c_001.main_12 - addition to hold the allocated block */
1010 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
1011 CmMmBlkHdr *alocBlk;
1012 #endif /* SSI_DEBUG_LEVEL1 */
1013 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
1016 /* cm_mem_c_001.main_15 : Additions */
1017 #ifdef SS_HISTOGRAM_SUPPORT
1019 #endif /* SS_HISTOGRAM_SUPPORT */
1020 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
1021 uint16_t memIndex=0;
1034 /* cm_mem_c_001.main_15 : Additions */
1035 #if (defined(SS_MEM_LEAK_STS) || defined( BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
1037 #endif /* SS_MEM_LEAK_STS */
1039 regCb = (CmMmRegCb *)regionCb;
1041 #if (ERRCLASS & ERRCLS_INT_PAR)
1043 /* error check on parameters */
1044 if ((regCb == NULLP) || (size == NULLP) || !(*size) || (ptr == NULLP))
1050 /* cm_mem_c_001.main_12 - addition for checking memType parameter */
1051 #ifdef SSI_DEBUG_LEVEL1
1052 #if (ERRCLASS & ERRCLS_INT_PAR)
1053 if ((memType != CMM_STATIC_MEM_FLAG) && (memType != CMM_DYNAMIC_MEM_FLAG))
1057 #endif /* (ERRCLASS & ERRCLS_INT_PAR) */
1058 #endif /* SSI_DEBUG_LEVEL1 */
1065 /* cm_mem_c_001.main_12 - addition to insert the size into hash list */
1066 #ifdef SSI_DEBUG_LEVEL1
1067 /* Update the hash list */
1068 if (cmMmHashListInsert(&(regCb->hashListCp), *size) != ROK)
1070 /* display that, this entry could not be made in the hash list */
1072 /* display an error message here */
1073 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/
1075 sprintf(dbgPrntBuf, "\n Could not make an entry for size %u in hash table of region %d \n",
1076 *size, regCb->region);
1078 sprintf(dbgPrntBuf, "\n Could not make an entry for size %lu in hash table of region %d \n",
1079 *size, regCb->region);
1081 SDisplay(0, dbgPrntBuf);
1084 #endif /* SSI_DEBUG_LEVEL1 */
1087 * Check if the requested size is less than or equal to the maximum block
1088 * size in the bucket.
1090 if ( *size <= regCb->bktMaxBlkSize)
1092 /* Get the map to the mapping table */
1093 idx = ((*size - 1) >> regCb->bktQnPwr);
1095 #if (ERRCLASS & ERRCLS_DEBUG)
1096 if (regCb->mapTbl[idx].bktIdx == 0xFF)
1098 /* Some fatal error in the map table initialization. */
1103 /* Dequeue the memory block and return it to the user */
1104 bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]);
1107 /* While loop is introduced to use the "break statement inside */
1111 * Check if the size request is not greater than the size available
1114 if (*size > bkt->size)
1116 /* Try to go to the next bucket if available */
1117 if((idx < (CMM_MAX_MAP_ENT - 1)) &&
1118 (regCb->mapTbl[++idx].bktIdx != 0xFF))
1120 bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]);
1124 /* This is the last bucket, try to allocate from heap */
1129 /* Acquire the bucket lock */
1130 /* cm_mem_c_001.main_13 : Replaced SLock with WTLock for NT */
1132 (Void) WTLock(&(bkt->bktLock));
1134 (Void) SLock(&(bkt->bktLock));
1136 #ifdef XEON_SPECIFIC_CHANGES
1137 CM_MEM_ALLOC_STS(regCb->region, idx);
1140 #if (ERRCLASS & ERRCLS_DEBUG)
1141 regCb->mapTbl[idx].numReq++;
1142 #endif /* (ERRCLASS & ERRCLS_DEBUG) */
1144 /* cm_mem_c_001.main_12 - addition for sanity check before allocation */
1145 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
1146 /* increment the allocation attempt counter at bucket level */
1147 bkt->numAllocAttempts++;
1149 /* detect trampling if any and call sanity check. This is done for (bkt->nextBlk) as
1150 the allocation is always from (bkt->nextBlk) */
1153 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1154 if(cmMmRegIsBlkSane(bkt->nextBlk, bkt->size) != ROK)
1156 if (cmMmRegIsBlkSane(bkt->nextBlk) != ROK)
1159 /* detected a trampled memory block in this bucket */
1161 /* display an error message here */
1162 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/
1164 sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, size %u bytes \n",
1165 (void *)bkt->nextBlk, regCb->mapTbl[idx].bktIdx, *size);
1167 sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, size %lu bytes \n",
1168 (void *)bkt->nextBlk, regCb->mapTbl[idx].bktIdx, *size);
1170 SDisplay(0, dbgPrntBuf);
1173 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1174 if (cmMmBktSanityChk(bkt) == RTRAMPLINGNOK)
1176 /* Release the lock */
1177 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1179 (Void) WTUnlock(&(bkt->bktLock));
1181 (Void) SUnlock(&(bkt->bktLock));
1183 /* handle RTRAMPLINGNOK in SAlloc/SGetSBuf */
1184 return (RTRAMPLINGNOK);
1189 /* Release the lock */
1190 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1192 (Void) WTUnlock(&(bkt->bktLock));
1194 (Void) SUnlock(&(bkt->bktLock));
1196 /* return RFAILED */
1201 *ptr = (Data *)(bkt->nextBlk) + (sizeof(CmMmBlkHdr)); /* ccpu00125353: warning fix */
1202 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
1203 ptrHdr = (CmMmBlkHdr *)bkt->nextBlk;
1205 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1206 /* Initialize the elements with 0xAB */
1207 memset(*ptr, 0xAB, *size);
1209 // DU_LOG("\nINFO --> Pointer allocated %8p size %d\n", *ptr, *size);
1210 /* Store this pointer in hash list */
1211 if ((bkt->nextBlk) && *ptr)
1212 #elif SS_LIGHT_MEM_LEAK_STS
1213 *ptr = (Data *)(bkt->nextBlk) + (sizeof(CmMmBlkHdr)); /* ccpu00125353: warning fix */
1214 if ((bkt->nextBlk) && *ptr)
1216 *ptr = bkt->next;/* ccpu00125353: warning fix */
1218 #endif /* SSI_DEBUG_LEVEL1 */
1220 /* cm_mem_c_001.main_12 - addition for header */
1221 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
1222 /* point to next block header */
1223 alocBlk = bkt->nextBlk;
1224 bkt->nextBlk = (CmMmBlkHdr *)(bkt->nextBlk->nextBlk);
1226 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
1227 if(bkt->size == 128)
1229 memIndex = (*ptr - startPtr128) / 128;
1231 if(bkt->size == 256)
1233 memIndex = (*ptr - startPtr256) / 256;
1236 if (*((uint32_t *)(*ptr + 4)) != 0xDEADDEAD && *((uint32_t *)(*ptr + 80)) != 0xDEADDEAD && *((uint32_t *)(*ptr + 24)) != 0xDEADDEAD)
1239 if(bkt->size == 256)
1244 #endif /* MS_MBUF_CORRUPTION */
1245 #ifdef SSI_MEM_CORR_PREVENTION
1246 *(((uint32_t *)(*ptr)) + 2) = 0;
1249 #ifdef T2K_MEM_LEAK_DBG
1252 /* Lock before the transaction-start */
1253 pthread_mutex_lock(&(regMemLeakInfo.memLock[regCb->region]));
1254 InsertToT2kMemLeakInfo( (uint64_t)*ptr,*size,line,file,regCb->region);
1255 /* UnLock after the transaction */
1256 pthread_mutex_unlock(&(regMemLeakInfo.memLock[regCb->region]));
1258 #endif /* T2K_MEM_LEAK_DBG */
1260 bkt->next = *((CmMmEntry **)(bkt->next));
1261 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
1262 *nextptr = bkt->next;
1263 if (*((uint32_t *)(*nextptr + 4)) != 0xDEADDEAD && *((uint32_t *)(*nextptr + 80)) != 0xDEADDEAD && *((uint32_t *)(*nextptr + 24)) != 0xDEADDEAD)
1265 if(bkt->size == 128)
1266 *prevptr = startPtr128 + ((memIndex-1)*128);
1267 if(bkt->size == 256)
1268 *prevptr = startPtr256 + ((memIndex-1)*256);
1269 if(bkt->size == 128)
1270 memIndex = (*nextptr - startPtr128) / 128;
1271 if(bkt->size == 256)
1272 memIndex = (*nextptr - startPtr256) / 256;
1276 if(bkt->size == 128)
1278 memIndex = (*ptr - startPtr128) / 128;
1279 cmMemInfo128[memIndex][0] = cmAllocCaller[MxGetCpuID()];
1281 if(bkt->size == 256)
1283 memIndex = (*ptr - startPtr256) / 256;
1284 cmMemInfo256[memIndex][0] = cmAllocCaller[MxGetCpuID()];
1286 cmAllocCaller[MxGetCpuID()] = NULLP;
1287 *((uint32_t *)(*ptr + 4)) = 0x00000000;
1288 *((uint32_t *)(*ptr + 124)) = 0;
1289 *((uint32_t *)(*ptr + 24)) = 0x00000000;
1290 *((uint32_t *)(*ptr + 44)) = 0x00000000;
1291 *((uint32_t *)(*ptr + 80)) = 0x00000000;
1292 *((uint32_t *)(*ptr + 116)) = 0x00000000;
1294 #endif /* SSI_DEBUG_LEVEL1 */
1296 /* cache_coherency_changes */
1301 * Increment the statistics variable of number of memory block
1305 if (bkt->numAlloc > bkt->maxAlloc)
1307 bkt->maxAlloc = bkt->numAlloc;
1309 #ifdef CM_MEM_OVERUSED
1311 if (g_overused[bktIdx] == 0 && OVERUSED(bkt))
1313 g_overused[bktIdx] = 1;
1314 /*DU_LOG("\nINFO --> cmAlloc: bktIdx %u overused %u numAlloc %u\n", bktIdx, g_overused[bktIdx], bkt->numAlloc); */
1318 /* cm_mem_c_001.main_12 - addition for header manipulation */
1319 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
1320 /* update the size for which this memory block has been allocated */
1321 alocBlk->requestedSize = *size;
1322 /* update the memory block header */
1323 CMM_RESET_FREE_FLAG(alocBlk->memFlags);
1324 if (memType == CMM_STATIC_MEM_FLAG)
1326 CMM_SET_STATIC_FLAG(alocBlk->memFlags);
1327 /* add it to the static memory allocated */
1328 bkt->staticMemUsed += bkt->size;
1332 CMM_SET_DYNAMIC_FLAG(alocBlk->memFlags);
1333 /* add it to the dynamic memory allocated */
1334 bkt->dynamicMemUsed += bkt->size;
1336 #elif SS_LIGHT_MEM_LEAK_STS
1337 alocBlk->requestedSize = *size;
1338 alocBlk->lineNo = lineNo;
1339 alocBlk->currFuncName = funcName;
1340 if(gmemLkCb.isStarted == TRUE)
1342 alocBlk->allocQueueIndx = cmStorAllocBlk(alocBlk);
1344 #endif /* SSI_DEBUG_LEVEL1 */
1346 if (((bkt->size - (*size)) >> regCb->bktQnPwr) && flags)
1350 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/
1353 "[MEM_CAL_CNTA] %u bytes request not fitted in bkt %d [size %u bytes] %u times\n", *size, regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktNoFitCnt++);
1356 "[MEM_CAL_CNTA] %lu bytes request not fitted in bkt %d [size %lu bytes] %lu times\n", *size, regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktNoFitCnt++);
1358 SDisplay(0, prntBuf);
1366 "SGetSBuf:%08lu:Size Bucket Id:%03d Times:%05lu Pointer: %8p\n",
1367 *size, regCb->mapTbl[idx].bktIdx, num_times, *ptr);
1368 SDisplay(0, prntBuf);
1370 #endif /* MEMCAL_DEBUG */
1371 /* cm_mem_c_001.main_15 : Additions */
1372 #ifdef SS_HISTOGRAM_SUPPORT
1373 /* If If Tapa task (entId)is registerd for histogram then insert Memrory allocated
1374 * information into the hash list */
1377 if (cmHstGrmAllocInsert(&(bkt->hstGrmHashListCp), bkt->size, size, line, fileName, entId) != ROK)
1379 sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
1384 #endif /* SS_HISTOGRAM_SUPPORT */
1386 /* Update the size parameter */
1388 #ifdef SS_MEM_LEAK_STS
1389 /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
1390 cmStorAllocBlk((PTR)*ptr, (Size) reqSz, (Size) *size,
1391 regCb->mapTbl[idx].bktIdx);
1392 #elif BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
1393 cmStorAllocBlk(ptrHdr, (Size)reqSz, (Size) *size,
1394 regCb->mapTbl[idx].bktIdx, regCb);
1395 #endif /* SS_MEM_LEAK_STS */
1397 /* cm_mem_c_008.104 - Addition for memory calculator tool */
1399 /* Release the lock */
1400 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1402 (Void) WTUnlock(&(bkt->bktLock));
1404 (Void) SUnlock(&(bkt->bktLock));
1413 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/
1416 "[MEM_CAL_CNTB] Allocation failed in bucket %d [ size %u bytes], %u times\n", regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktFailCnt);
1419 "[MEM_CAL_CNTB] Allocation failed in bucket %d [ size %lu bytes], %lu times\n", regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktFailCnt);
1421 SDisplay(0, prntBuf);
1425 #if (ERRCLASS & ERRCLS_DEBUG)
1426 regCb->mapTbl[idx].numFailure++;
1427 #endif /* (ERRCLASS & ERRCLS_DEBUG) */
1429 /* Release the lock */
1430 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1432 (Void) WTUnlock(&(bkt->bktLock));
1434 (Void) SUnlock(&(bkt->bktLock));
1443 regCb->heapCb.heapAllocCnt++;
1445 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/
1448 "[MEM_CAL_CNTC] No bucket block configured for %u bytes \n Number of blocks allocated from heap = %u\n",*size,
1449 regCb->heapCb.heapAllocCnt);
1452 "[MEM_CAL_CNTC] No bucket block configured for %lu bytes \n Number of blocks allocated from heap = %lu\n",*size,
1453 regCb->heapCb.heapAllocCnt);
1455 SDisplay(0, prntBuf);
1460 /* Memory not available in the bucket pool */
1461 if (regCb->heapFlag && (*size < regCb->heapSize))
1464 if (flags) tryHeap = 1;
1467 * The heap memory block is available. Allocate the memory block from
1470 /* cm_mem_c_001.main_15: Additions */
1471 #ifdef SS_HISTOGRAM_SUPPORT
1472 /* cm_mem_c_001.main_12 - addition for passing an extra parameter */
1473 #ifdef SSI_DEBUG_LEVEL1
1474 return (cmHeapAlloc(&(regCb->heapCb), ptr, size, memType, line, fileName, entId, hstReg));
1476 return (cmHeapAlloc(&(regCb->heapCb), ptr, size, line, fileName, entId, hstReg));
1477 #endif /* SSI_DEBUG_LEVEL1 */
1479 /* cm_mem_c_001.main_12 - addition for passing an extra parameter */
1480 #ifdef SSI_DEBUG_LEVEL1
1481 return (cmHeapAlloc(&(regCb->heapCb), ptr, size, memType));
1483 return (cmHeapAlloc(&(regCb->heapCb), ptr, size));
1484 #endif /* SSI_DEBUG_LEVEL1 */
1485 #endif /* SS_HISTOGRAM_SUPPORT */
1488 /* No memory available */
1490 #else /* use pure is on */
1491 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/
1492 #ifdef SS_4GMX_LCORE
1493 *ptr = (Data*) MxHeapAlloc(SsiHeap, *size);
1494 memset(ptr, 0, *size);
1496 *ptr = (Data*) malloc(*size);
1498 if ( (*ptr) == NULLP)
1500 avail_size -= *size;
1502 #endif /* USE_PURE */
1504 } /* end of cmAlloc */
1511 * Desc: Return the memory block for the memory region.
1514 * Ret: ROK - successful
1515 * RFAILED - unsuccessful.
1517 * Notes: The user calls this function to return the previously allocated
1518 * memory block to the memory region. The memory manager does not
1519 * check the validity of the state of the memory block(like whether
1520 * it was allocated earlier). The caller must be sure that, the
1521 * address specified in the parameter 'ptr' is valid and was
1522 * allocated previously from same region.
1529 /* cm_mem_c_001.main_15 : Additions */
1530 #ifdef SS_LIGHT_MEM_LEAK_STS
1539 #else /*SS_LIGHT_MEM_LEAK_STS */
1540 #ifdef SS_HISTOGRAM_SUPPORT
1553 #ifdef T2K_MEM_LEAK_DBG
1562 #else /* T2K_MEM_LEAK_DBG */
1570 /* cm_mem_c_001.main_15 : Additions */
1571 #endif /* SS_HISTOGRAM_SUPPORT */
1572 #endif /*SS_LIGHT_MEM_LEAK_STS */
1575 /* cm_mem_c_001.main_26 : Fixes for Compilation Warnings */
1581 /* cm_mem_c_001.main_12 - addition for holding the free pointer */
1582 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
1584 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1585 CmMmBlkHdr *lastHdr;
1587 #endif /* SSI_DEBUG_LEVEL1 */
1588 /* cm_mem_c_001.main_15 : Additions */
1589 #ifdef SS_HISTOGRAM_SUPPORT
1591 #endif /* SS_HISTOGRAM_SUPPORT */
1592 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption*/
1593 uint16_t memIndex=0;
1597 regCb = (CmMmRegCb *)regionCb;
1600 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1601 /* Check if the memory block is from the memory region */
1602 if (ptr >= ((CmMmRegCb *)regCb)->regInfo.start +
1603 ((CmMmRegCb *)regCb)->regInfo.size)
1608 #if (ERRCLASS & ERRCLS_INT_PAR)
1610 /* error check on parameters */
1611 if ((regCb == NULLP) || (!size) || (ptr == NULLP))
1616 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1617 /* Check if the memory block is from the memory region */
1618 if (ptr >= ((CmMmRegCb *)regCb)->regInfo.start +
1619 ((CmMmRegCb *)regCb)->regInfo.size)
1624 /* cm_mem_c_001.main_20 Addition */
1625 if (ptr < regCb->regInfo.start)
1633 * Check if the memory block was allocated from the bucket pool.
1636 if (ptr < (regCb->regInfo.start + regCb->bktSize))
1638 #ifdef T2K_MEM_LEAK_DBG
1640 pthread_mutex_lock(&(regMemLeakInfo.memLock[regCb->region]));
1641 RemoveFromT2kMemLeakInfo((uint64_t)ptr , file,line,regCb->region);
1642 pthread_mutex_unlock(&(regMemLeakInfo.memLock[regCb->region]));
1645 /* The memory block was allocated from the bucket pool */
1647 /* Get the map to the mapping table */
1648 idx = ((size - 1) >> regCb->bktQnPwr);
1650 #if (ERRCLASS & ERRCLS_DEBUG)
1651 if (regCb->mapTbl[idx].bktIdx == 0xFF)
1653 /* Some fatal error in the map table initialization. */
1658 /* Enqueue the memory block and return it to the user */
1659 bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]);
1662 * Check if the size is not greater than the size available
1663 * in the bucket. If so, then the buffer must have been allocated
1664 * from next bucket. We don't need to check the validity of the
1665 * next bucket, otherwise buffer must have been allocated from heap
1668 if (size > bkt->size)
1670 bkt = &(regCb->bktTbl[regCb->mapTbl[++idx].bktIdx]);
1673 /* Acquire the bucket lock */
1674 /* cm_mem_c_001.main_13 : Replaced SLock with WTLock for NT */
1676 (Void) WTLock(&(bkt->bktLock));
1678 (Void) SLock(&(bkt->bktLock));
1680 #ifdef XEON_SPECIFIC_CHANGES
1681 CM_MEM_FREE_STS(regCb->region, idx);
1683 /* cache_coherency_changes */
1688 /* cm_mem_c_001.main_12 - addition for sanity check and free */
1689 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
1690 /* increment the dealloc attempt counter at bucket level */
1691 bkt->numDeallocAttempts++;
1693 /* Check the memFlags to see whether this block was allocated */
1694 ptrHdr = (CmMmBlkHdr *) (ptr - sizeof(CmMmBlkHdr));
1695 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
1696 cmRlsAllocBlk(ptrHdr, regCb);
1698 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1699 /* Check for ptr size */
1700 if(((ptrHdr->requestedSize - size) % size) != 0)
1703 sprintf(dbgPrntBuf, "Passed size (%d) does not match with allocated \
1704 size(%d) %8p, Bucket Id:%03d\n",
1705 size, ptrHdr->requestedSize, ptr, regCb->mapTbl[idx].bktIdx);
1707 DU_LOG("\nERROR --> Passed size (%d) does not match with allocated \
1708 size(%d) %8p, Bucket Id:%03d\n",
1709 size, ptrHdr->requestedSize, ptr, regCb->mapTbl[idx].bktIdx);
1712 /* Validate the tail part to see if there is any over run */
1713 // DU_LOG("\nINFO --> Pointer free request %8p, size %d\n", ptr, size);
1716 /* validate the block to be freed for trampling */
1717 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1718 if(cmMmRegIsBlkSane(ptrHdr, bkt->size) != ROK)
1720 if (cmMmRegIsBlkSane(ptrHdr) != ROK)
1723 /* Handle error case of Memory trampling */
1725 /* display an error message here */
1726 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/
1728 sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, size %u bytes \n",
1729 ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
1731 sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, size %lu bytes \n",
1732 ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
1734 SDisplay(0, dbgPrntBuf);
1738 * if sanity check returns RTRAMPLINGOK then there is nothing to do
1739 * as the memory blk is already invalidated in cmMmBktSanityChk
1741 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1742 if (cmMmBktSanityChk(bkt) == RTRAMPLINGOK)
1746 /* Release the lock */
1747 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1749 (Void) WTUnlock(&(bkt->bktLock));
1751 (Void) SUnlock(&(bkt->bktLock));
1760 * this is the case where in the entire bucket has been made unusable
1763 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1765 (Void) WTUnlock(&(bkt->bktLock));
1767 (Void) SUnlock(&(bkt->bktLock));
1770 /* handle RTRAMPLINGNOK in SFree/SPutSBuf */
1771 return (RTRAMPLINGNOK);
1773 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1774 DU_LOG("\nERROR --> Memory signature is invalid\n");
1779 /* reset the size */
1780 ptrHdr->requestedSize = 0;
1781 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1782 /* Initialize the elements with 0xAB */
1783 memset(ptr, 0xAB, size);
1785 /* check if the block to be freed is already having the state as FREE */
1786 if (CMM_IS_FREE(ptrHdr->memFlags))
1788 /* Handle double deallocation error case */
1790 /* display an error message here */
1791 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/
1793 sprintf(dbgPrntBuf, "Attempt to double deallocate memory at: %8p, Bucket Id:%03d, size %u bytes \n",
1794 ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
1796 sprintf(dbgPrntBuf, "Attempt to double deallocate memory at: %8p, Bucket Id:%03d, size %lu bytes \n",
1797 ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
1799 SDisplay(0, dbgPrntBuf);
1802 /* Release the lock */
1803 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1805 (Void) WTUnlock(&(bkt->bktLock));
1807 (Void) SUnlock(&(bkt->bktLock));
1809 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1810 DU_LOG("\nERROR --> Attempt to double deallocate memory at: %8p, Bucket Id:%03d,\
1811 size %u bytes \n", ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
1814 /* handle RDBLFREE in SFree/SPutSBuf */
1817 if (CMM_IS_STATIC(ptrHdr->memFlags))
1819 CMM_SET_FREE_FLAG(ptrHdr->memFlags);
1820 CMM_RESET_STATIC_FLAG(ptrHdr->memFlags);
1821 /* deduct it from the static memory count */
1822 bkt->staticMemUsed -= bkt->size;
1824 else if (CMM_IS_DYNAMIC(ptrHdr->memFlags))
1826 CMM_SET_FREE_FLAG(ptrHdr->memFlags);
1827 CMM_RESET_DYNAMIC_FLAG(ptrHdr->memFlags);
1828 /* deduct it from the dynamic memory count */
1829 bkt->dynamicMemUsed -= bkt->size;
1833 /* This is a case similar to trampled memory */
1835 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/
1837 sprintf(dbgPrntBuf, "Invalid memory flag: %u !!!\n", ptrHdr->memFlags);
1839 sprintf(dbgPrntBuf, "Invalid memory flag: %lu !!!\n", ptrHdr->memFlags);
1841 SDisplay(0, dbgPrntBuf);
1843 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1844 if (cmMmBktSanityChk(bkt) == RTRAMPLINGOK)
1846 /* do not add to the free list */
1849 /* Release the lock */
1850 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1852 (Void) WTUnlock(&(bkt->bktLock));
1854 (Void) SUnlock(&(bkt->bktLock));
1862 * this is the case where in the entire bucket has been made unusable
1865 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
1867 (Void) WTUnlock(&(bkt->bktLock));
1869 (Void) SUnlock(&(bkt->bktLock));
1872 /* handle RTRAMPLINGNOK in SFree/SPutSBuf */
1873 return (RTRAMPLINGNOK);
1876 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1877 /* Return the block to memory */
1878 ptrHdr->nextBlk = bkt->nextBlk;
1879 bkt->nextBlk = ptrHdr;
1881 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
1882 /* Move the ptr to end of the bucket */
1883 lastHdr = (CmMmBlkHdr *)bkt->lastBlk;
1884 lastHdr->nextBlk = ptrHdr;
1885 bkt->lastBlk = ptrHdr;
1886 ptrHdr->nextBlk = NULLP;
1888 #elif SS_LIGHT_MEM_LEAK_STS
1889 ptrHdr = (CmMmBlkHdr *) (ptr - sizeof(CmMmBlkHdr));
1890 ptrHdr->lineNo = lineNo;
1891 ptrHdr->currFuncName = funcName;
1892 if(gmemLkCb.isStarted == TRUE)
1894 cmRlsAllocBlk(ptrHdr->allocQueueIndx);
1896 ptrHdr->nextBlk = bkt->nextBlk;
1897 bkt->nextBlk = ptrHdr;
1900 #ifndef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
1901 #ifdef SSI_MEM_CORR_PREVENTION
1902 if (*(((uint32_t *)(ptr)) + 2) == 0xdeaddead)
1904 /* Do not free an already freed block to avoid corruption */
1905 cmDblFreeAttempts++;
1910 *((CmMmEntry **)bkt->last) = ptr;
1911 bkt->last = (CmMmEntry *)ptr;
1912 *((CmMmEntry **)ptr) = NULLP;
1913 *(((uint32_t *)(ptr)) + 2) = 0xdeaddead;
1916 *((CmMmEntry **)ptr) = bkt->next;
1917 bkt->next = (CmMmEntry *)ptr;
1920 if(memFreeCount >= 125000)
1922 if(bkt->size == 128)
1924 Data *crashPtr=NULLP;
1925 if(((ptr - startPtr128) % 128) != 0)
1929 memIndex = (ptr - startPtr128) / 128;
1931 if(bkt->size == 256)
1933 Data *crashPtr=NULLP;
1934 if(((ptr - startPtr256) % 256) != 0)
1938 memIndex = (ptr - startPtr256) / 256;
1940 if(bkt->size == 512)
1942 Data *crashPtr=NULLP;
1943 if(((ptr - startPtr512) % 512) != 0)
1948 if(bkt->size == 768)
1950 Data *crashPtr=NULLP;
1951 if(((ptr - startPtr768) % 768) != 0)
1956 if(bkt->size == 1664)
1958 Data *crashPtr=NULLP;
1959 if(((ptr - startPtr1664) % 1664) != 0)
1964 if(bkt->size == 4800)
1966 Data *crashPtr=NULLP;
1967 if(((ptr - startPtr4800) % 4800) != 0)
1972 if(bkt->size == 9920)
1974 Data *crashPtr=NULLP;
1975 if(((ptr - startPtr9920) % 9920) != 0)
1980 if (*((uint32_t *)(ptr + 4)) != 0xDEADDEAD)
1982 *(uint32_t *)(ptr + 4) = 0xDEADDEAD;
1986 Data *crashPtr=NULLP;
1989 if (*((uint32_t *)(ptr + 24)) != 0xDEADDEAD)
1991 *(uint32_t *)(ptr + 24) = 0xDEADDEAD;
1995 Data *crashPtr=NULLP;
1998 if (*((uint32_t *)(ptr + 44)) != 0xDEADDEAD)
2000 *(uint32_t *)(ptr + 44) = 0xDEADDEAD;
2004 Data *crashPtr=NULLP;
2007 if (*((uint32_t *)(ptr + 80)) != 0xDEADDEAD)
2009 *(uint32_t *)(ptr + 80) = 0xDEADDEAD;
2013 Data *crashPtr=NULLP;
2015 /* Cause a crash to identify the caller */
2017 *(uint32_t *)(ptr + 124) = memFreeCount++;
2018 (*(uint32_t *)(ptr + 116)) = cmFreeCaller[MxGetCpuID()];
2019 if(bkt->size == 128)
2021 memIndex = (ptr - startPtr128) / 128;
2022 cmMemInfo128[memIndex][1] = cmFreeCaller[MxGetCpuID()];
2024 if(bkt->size == 256)
2026 memIndex = (ptr - startPtr256) / 256;
2027 cmMemInfo256[memIndex][1] = cmFreeCaller[MxGetCpuID()];
2029 cmFreeCaller[MxGetCpuID()] = NULLP;
2032 Reverted: Removed functionality to move freed buffer to end of free List in bucket.
2033 This is impacting throughput.
2036 *((CmMmEntry **)bkt->last) = ptr;
2037 bkt->last = (CmMmEntry *)ptr;
2038 *((CmMmEntry **)ptr) = NULLP;
2040 *((CmMmEntry **)ptr) = bkt->next;
2041 bkt->next = (CmMmEntry *)ptr;
2044 #endif /* SSI_DEBUG_LEVEL1 */
2046 /* cache_coherency_changes */
2051 * Decrement the statistics variable of number of memory block
2055 /* cm_mem_c_001.main_15 : Additions */
2056 #ifdef SS_HISTOGRAM_SUPPORT
2057 /* If If Tapa task (entId)is registerd for histogram then insert Memrory Freed
2058 * information into the hash list */
2061 if (cmHstGrmFreeInsert(&bkt->hstGrmHashListCp, bkt->size, line, fileName, entId) != ROK)
2063 sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
2067 #endif /* SS_HISTOGRAM_SUPPORT */
2069 #ifdef SS_MEM_LEAK_STS
2070 /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
2071 cmRlsAllocBlk((PTR)ptr);
2072 #endif /* SS_MEM_LEAK_STS */
2073 /* Release the lock */
2074 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
2076 (Void) WTUnlock(&(bkt->bktLock));
2078 (Void) SUnlock(&(bkt->bktLock));
2084 /* The memory block was allocated from the heap pool */
2085 /* cm_mem_c_001.main_15 : Additions */
2086 #ifdef SS_HISTOGRAM_SUPPORT
2087 return (cmHeapFree (&(regCb->heapCb), ptr, size, line, fileName, entId, hstReg));
2089 return (cmHeapFree (&(regCb->heapCb), ptr, size));
2090 #endif /* SS_HISTOGRAM_SUPPORT */
2091 #else /* use pure is on */
2092 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/
2093 #ifdef SS_4GMX_LCORE
2094 (Void)MxHeapFree(SsiHeap, ptr);
2100 #endif /* USE_PURE */
2103 } /* end of cmFree */
2109 * Desc: Allocate a memory block for the memory region(No Lock).
2112 * Ret: ROK - successful
2113 * RFAILED - unsuccessful.
2116 * The function allocates a memory block of size atleast equal to
2117 * the requested size. The size parameter will be updated with the
2118 * actual size of the memory block allocated for the request. The
2119 * CMM tries to allocate the memory block form the bucket pool. If
2120 * there is no memory in the bucket the CMM allocates the memory
2121 * block form the heap pool. This function is always called by the
2122 * System Service module.
2124 * The caller of the function should try to use the out value of
2125 * the size while returning the memory block to the region. However
2126 * the current design of the memory manager does not enforce to pass
2127 * the actual size of the memory block. (Due to the SGetSBuf
2128 * semantics the layer will not able to pass the correct size of the
2129 * memory block while calling SPutSBuf).
2135 /* cm_mem_c_001.main_12 - addition to accept new parameter memType(static/dynamic) */
2137 /* cm_mem_c_001.main_15 : Additions */
2138 #ifdef SS_HISTOGRAM_SUPPORT
2139 #ifdef SSI_DEBUG_LEVEL1
2164 #endif /* SSI_DEBUG_LEVEL1 */
2167 #ifdef SS_LIGHT_MEM_LEAK_STS
2178 #else /*SS_LIGHT_MEM_LEAK_STS */
2179 #ifdef SSI_DEBUG_LEVEL1
2189 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2206 #endif /* BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1 */
2207 #endif /* SSI_DEBUG_LEVEL1 */
2208 /* cm_mem_c_001.main_15: Additions */
2209 #endif /*SS_LIGHT_MEM_LEAK_STS */
2210 #endif /* SS_HISTOGRAM_SUPPORT */
2213 /* cm_mem_c_001.main_26 : Fixes for Compilation Warnings */
2219 /* cm_mem_c_001.main_26 : Fixes for Compilation Warnings */
2223 /* cm_mem_c_001.main_15 : Additions */
2224 #if (defined(SS_MEM_LEAK_STS) || defined( BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
2226 #endif /* SS_MEM_LEAK_STS */
2227 /* cm_mem_c_001.main_12 - addition to hold the allocated block */
2228 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
2229 CmMmBlkHdr *alocBlk;
2230 #endif /* SSI_DEBUG_LEVEL1 */
2231 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
2234 /* cm_mem_c_001.main_15 : Additions */
2235 #ifdef SS_HISTOGRAM_SUPPORT
2237 #endif /* SS_HISTOGRAM_SUPPORT */
2243 /* cm_mem_c_001.main_15 : Additions */
2244 #ifdef SS_MEM_LEAK_STS
2246 #endif /* SS_MEM_LEAK_STS */
2248 regCb = (CmMmRegCb *)regionCb;
2250 #if (ERRCLASS & ERRCLS_INT_PAR)
2252 /* error check on parameters */
2253 if ((regCb == NULLP) || (size == NULLP) || !(*size) || (ptr == NULLP))
2259 /* cm_mem_c_001.main_12 - addition for checking memType parameter */
2260 #ifdef SSI_DEBUG_LEVEL1
2261 #if (ERRCLASS & ERRCLS_INT_PAR)
2262 if ((memType != CMM_STATIC_MEM_FLAG) && (memType != CMM_DYNAMIC_MEM_FLAG))
2266 #endif /* (ERRCLASS & ERRCLS_INT_PAR) */
2267 #endif /* SSI_DEBUG_LEVEL1 */
2274 /* cm_mem_c_001.main_12 - addition to insert the size into hash list */
2275 #ifdef SSI_DEBUG_LEVEL1
2276 /* Update the hash list */
2277 if (cmMmHashListInsert(&(regCb->hashListCp), *size) != ROK)
2279 /* display that, this entry could not be made in the hash list */
2281 /* display an error message here */
2283 sprintf(dbgPrntBuf, "\n Could not make an entry for size %u \
2284 in hash table of region %d \n",
2285 *size, regCb->region);
2287 sprintf(dbgPrntBuf, "\n Could not make an entry for size %lu \
2288 in hash table of region %d \n",
2289 *size, regCb->region);
2291 SDisplay(0, dbgPrntBuf);
2294 #endif /* SSI_DEBUG_LEVEL1 */
2297 * Check if the requested size is less than or equal to the maximum block
2298 * size in the bucket.
2300 if ( *size <= regCb->bktMaxBlkSize)
2302 /* Get the map to the mapping table */
2303 idx = ((*size - 1) >> regCb->bktQnPwr);
2305 #if (ERRCLASS & ERRCLS_DEBUG)
2306 if (regCb->mapTbl[idx].bktIdx == 0xFF)
2308 /* Some fatal error in the map table initialization. */
2313 /* Dequeue the memory block and return it to the user */
2314 bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]);
2317 /* While loop is introduced to use the "break statement inside */
2321 * Check if the size request is not greater than the size available
2324 if (*size > bkt->size)
2326 /* Try to go to the next bucket if available */
2327 if((idx < (CMM_MAX_MAP_ENT - 1)) &&
2328 (regCb->mapTbl[++idx].bktIdx != 0xFF))
2330 bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]);
2334 /* This is the last bucket, try to allocate from heap */
2339 #if (ERRCLASS & ERRCLS_DEBUG)
2340 regCb->mapTbl[idx].numReq++;
2341 #endif /* (ERRCLASS & ERRCLS_DEBUG) */
2343 /* cm_mem_c_001.main_12 - addition for sanity check before allocation */
2344 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
2345 /* increment the allocation attempt counter at bucket level */
2346 bkt->numAllocAttempts++;
2348 /* detect trampling if any and call sanity check. This is done for (bkt->nextBlk) as
2349 the allocation is always from (bkt->nextBlk) */
2352 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2353 if(cmMmRegIsBlkSane(bkt->nextBlk, bkt->size) != ROK)
2355 if (cmMmRegIsBlkSane(bkt->nextBlk) != ROK)
2358 /* detected a trampled memory block in this bucket */
2360 /* display an error message here */
2361 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/
2363 sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, \
2365 (void *)bkt->nextBlk, regCb->mapTbl[idx].bktIdx, *size);
2367 sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, \
2369 (void *)bkt->nextBlk, regCb->mapTbl[idx].bktIdx, *size);
2371 SDisplay(0, dbgPrntBuf);
2375 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2376 if (cmMmBktSanityChk(bkt) == RTRAMPLINGNOK)
2378 /* handle RTRAMPLINGNOK in SAlloc/SGetSBuf */
2379 return (RTRAMPLINGNOK);
2383 /* return RFAILED */
2389 *ptr = (Data *)(bkt->nextBlk) + (sizeof(CmMmBlkHdr));
2390 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
2391 ptrHdr = (CmMmBlkHdr *)bkt->nextBlk;
2393 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2394 /* Initialize the elements with 0xAB */
2395 memset(*ptr, 0xAB, *size);
2397 if ((bkt->nextBlk) && *ptr)
2398 #elif SS_LIGHT_MEM_LEAK_STS
2399 *ptr = (Data *)(bkt->nextBlk) + (sizeof(CmMmBlkHdr)); /* ccpu00125353: warning fix */
2400 if ((bkt->nextBlk) && *ptr)
2402 *ptr = bkt->next;/* ccpu00125353: warning fix */
2404 #endif /* SSI_DEBUG_LEVEL1 */
2406 /* cm_mem_c_001.main_12 - addition for header */
2407 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
2408 /* point to next block header */
2409 alocBlk = bkt->nextBlk;
2410 bkt->nextBlk = (CmMmBlkHdr *)(bkt->nextBlk->nextBlk);
2412 #ifdef SSI_MEM_CORR_PREVENTION
2413 *(((uint32_t *)(*ptr)) + 2) = 0;
2415 bkt->next = *((CmMmEntry **)(bkt->next));
2416 #endif /* SSI_DEBUG_LEVEL1 */
2418 /* cache_coherency_changes */
2423 * Increment the statistics variable of number of memory block
2427 if (bkt->numAlloc > bkt->maxAlloc)
2429 bkt->maxAlloc = bkt->numAlloc;
2431 #ifdef CM_MEM_OVERUSED
2433 if (g_overused[bktIdx] == 0 && OVERUSED(bkt))
2435 g_overused[bktIdx] = 1;
2439 /* cm_mem_c_001.main_12 - addition for header manipulation */
2440 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
2441 /* update the size for which this memory block has been allocated */
2442 alocBlk->requestedSize = *size;
2443 /* update the memory block header */
2444 CMM_RESET_FREE_FLAG(alocBlk->memFlags);
2445 if (memType == CMM_STATIC_MEM_FLAG)
2447 CMM_SET_STATIC_FLAG(alocBlk->memFlags);
2448 /* add it to the static memory allocated */
2449 bkt->staticMemUsed += bkt->size;
2453 CMM_SET_DYNAMIC_FLAG(alocBlk->memFlags);
2454 /* add it to the dynamic memory allocated */
2455 bkt->dynamicMemUsed += bkt->size;
2457 #elif SS_LIGHT_MEM_LEAK_STS
2458 alocBlk->requestedSize = *size;
2459 alocBlk->lineNo = lineNo;
2460 alocBlk->currFuncName = funcName;
2461 if(gmemLkCb.isStarted == TRUE)
2463 alocBlk->allocQueueIndx = cmStorAllocBlk(alocBlk);
2465 #endif /* SSI_DEBUG_LEVEL1 */
2467 if (((bkt->size - (*size)) >> regCb->bktQnPwr) && flags)
2473 "[MEM_CAL_CNTA] %u bytes request not fitted in bkt %d \
2474 [size %u bytes] %u times\n",
2475 *size, regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktNoFitCnt++);
2478 "[MEM_CAL_CNTA] %lu bytes request not fitted in bkt %d \
2479 [size %lu bytes] %lu times\n",
2480 *size, regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktNoFitCnt++);
2482 SDisplay(0, prntBuf);
2490 "SGetSBuf:%08lu:Size Bucket Id:%03d Times:%05lu Pointer: %8p\n",
2491 *size, regCb->mapTbl[idx].bktIdx, num_times, *ptr);
2492 SDisplay(0, prntBuf);
2494 #endif /* MEMCAL_DEBUG */
2495 /* cm_mem_c_001.main_15 : Additions */
2496 #ifdef SS_HISTOGRAM_SUPPORT
2499 if (cmHstGrmAllocInsert(&(bkt->hstGrmHashListCp),
2500 bkt->size, size, line, fileName, entId) != ROK)
2502 sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
2507 #endif /* SS_HISTOGRAM_SUPPORT */
2509 /* Update the size parameter */
2511 #ifdef SS_MEM_LEAK_STS
2512 /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
2513 cmStorAllocBlk((PTR)*ptr, (Size) reqSz, (Size) *size,
2514 regCb->mapTbl[idx].bktIdx);
2515 #elif BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
2516 cmStorAllocBlk(ptrHdr, (Size) reqSz, (Size) *size,
2517 regCb->mapTbl[idx].bktIdx, regCb);
2518 #endif /* SS_MEM_LEAK_STS */
2520 /* cm_mem_c_008.104 - Addition for memory calculator tool */
2530 "[MEM_CAL_CNTB] Allocation failed in bucket %d [ size %u bytes], \
2531 %u times\n", regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktFailCnt);
2534 "[MEM_CAL_CNTB] Allocation failed in bucket %d [ size %lu bytes], \
2535 %lu times\n", regCb->mapTbl[idx].bktIdx, bkt->size, bkt->bktFailCnt);
2537 SDisplay(0, prntBuf);
2541 #if (ERRCLASS & ERRCLS_DEBUG)
2542 regCb->mapTbl[idx].numFailure++;
2543 #endif /* (ERRCLASS & ERRCLS_DEBUG) */
2552 regCb->heapCb.heapAllocCnt++;
2556 "[MEM_CAL_CNTC] No bucket block configured for %u bytes \n \
2557 Number of blocks allocated from heap = %u\n",*size,
2558 regCb->heapCb.heapAllocCnt);
2561 "[MEM_CAL_CNTC] No bucket block configured for %lu bytes \n \
2562 Number of blocks allocated from heap = %lu\n",*size,
2563 regCb->heapCb.heapAllocCnt);
2565 SDisplay(0, prntBuf);
2570 /* Memory not available in the bucket pool */
2571 if (regCb->heapFlag && (*size < regCb->heapSize))
2574 if (flags) tryHeap = 1;
2577 * The heap memory block is available. Allocate the memory block from
2580 /* cm_mem_c_001.main_15: Additions */
2581 #ifdef SS_HISTOGRAM_SUPPORT
2582 /* cm_mem_c_001.main_12 - addition for passing an extra parameter */
2583 #ifdef SSI_DEBUG_LEVEL1
2584 return (cmHeapAlloc(&(regCb->heapCb), ptr, size,
2585 memType, line, fileName, entId, hstReg));
2587 return (cmHeapAlloc(&(regCb->heapCb), ptr, size,
2588 line, fileName, entId, hstReg));
2589 #endif /* SSI_DEBUG_LEVEL1 */
2591 /* cm_mem_c_001.main_12 - addition for passing an extra parameter */
2592 #ifdef SSI_DEBUG_LEVEL1
2593 return (cmHeapAlloc(&(regCb->heapCb), ptr, size, memType));
2595 return (cmHeapAlloc(&(regCb->heapCb), ptr, size));
2596 #endif /* SSI_DEBUG_LEVEL1 */
2597 #endif /* SS_HISTOGRAM_SUPPORT */
2600 /* No memory available */
2602 #else /* use pure is on */
2603 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/
2604 #ifdef SS_4GMX_LCORE
2605 *ptr = (Data*) MxHeapAlloc(SsiHeap, *size);
2606 memset(ptr, 0, *size);
2608 *ptr = (Data*) malloc(*size);
2610 if ( (*ptr) == NULLP)
2612 avail_size -= *size;
2614 #endif /* USE_PURE */
2616 } /* end of cmAllocNL */
2623 * Desc: Return the memory block for the memory region(No Lock).
2626 * Ret: ROK - successful
2627 * RFAILED - unsuccessful.
2629 * Notes: The user calls this function to return the previously allocated
2630 * memory block to the memory region. The memory manager does not
2631 * check the validity of the state of the memory block(like whether
2632 * it was allocated earlier). The caller must be sure that, the
2633 * address specified in the parameter 'ptr' is valid and was
2634 * allocated previously from same region.
2641 /* cm_mem_c_001.main_15 : Additions */
2642 #ifdef SS_LIGHT_MEM_LEAK_STS
2651 #else /*SS_LIGHT_MEM_LEAK_STS */
2653 #ifdef SS_HISTOGRAM_SUPPORT
2672 /* cm_mem_c_001.main_15 : Additions */
2673 #endif /* SS_HISTOGRAM_SUPPORT */
2674 #endif /*SS_LIGHT_MEM_LEAK_STS */
2682 /* cm_mem_c_001.main_12 - addition for holding the free pointer */
2683 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
2685 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2686 CmMmBlkHdr *lastHdr;
2687 #endif /* BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1 */
2688 #endif /* SSI_DEBUG_LEVEL1 */
2689 /* cm_mem_c_001.main_15 : Additions */
2690 #ifdef SS_HISTOGRAM_SUPPORT
2692 #endif /* SS_HISTOGRAM_SUPPORT */
2695 regCb = (CmMmRegCb *)regionCb;
2698 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2699 /* Check if the memory block is from the memory region */
2700 if (ptr >= ((CmMmRegCb *)regCb)->regInfo.start +
2701 ((CmMmRegCb *)regCb)->regInfo.size)
2706 #if (ERRCLASS & ERRCLS_INT_PAR)
2708 /* error check on parameters */
2709 if ((regCb == NULLP) || (!size) || (ptr == NULLP))
2713 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2714 /* Check if the memory block is from the memory region */
2715 if (ptr >= ((CmMmRegCb *)regCb)->regInfo.start +
2716 ((CmMmRegCb *)regCb)->regInfo.size)
2721 /* cm_mem_c_001.main_20 Addition */
2722 if (ptr < regCb->regInfo.start)
2730 * Check if the memory block was allocated from the bucket pool.
2733 if (ptr < (regCb->regInfo.start + regCb->bktSize))
2735 /* The memory block was allocated from the bucket pool */
2737 /* Get the map to the mapping table */
2738 idx = ((size - 1) >> regCb->bktQnPwr);
2740 #if (ERRCLASS & ERRCLS_DEBUG)
2741 if (regCb->mapTbl[idx].bktIdx == 0xFF)
2743 /* Some fatal error in the map table initialization. */
2748 /* Enqueue the memory block and return it to the user */
2749 bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]);
2752 * Check if the size is not greater than the size available
2753 * in the bucket. If so, then the buffer must have been allocated
2754 * from next bucket. We don't need to check the validity of the
2755 * next bucket, otherwise buffer must have been allocated from heap
2758 if (size > bkt->size)
2760 bkt = &(regCb->bktTbl[regCb->mapTbl[++idx].bktIdx]);
2763 /* cache_coherency_changes */
2768 /* cm_mem_c_001.main_12 - addition for sanity check and free */
2769 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
2770 /* increment the dealloc attempt counter at bucket level */
2771 bkt->numDeallocAttempts++;
2773 /* Check the memFlags to see whether this block was allocated */
2774 ptrHdr = (CmMmBlkHdr *) (ptr - sizeof(CmMmBlkHdr));
2775 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
2776 cmRlsAllocBlk(ptrHdr, regCb);
2778 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2779 /* Check for ptr size */
2780 if(((ptrHdr->requestedSize - size) % size) != 0)
2783 sprintf(dbgPrntBuf, "Passed size (%d) does not match with allocated size(%d):%8p, Bucket Id:%03d\n",
2784 size, ptrHdr->requestedSize, ptr, regCb->mapTbl[idx].bktIdx);
2786 DU_LOG("\nERROR --> Passed size (%d) does not match with allocated size(%d):%8p, Bucket Id:%03d\n",
2787 size, ptrHdr->requestedSize, ptr, regCb->mapTbl[idx].bktIdx);
2792 /* validate the block to be freed for trampling */
2793 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2794 if(cmMmRegIsBlkSane(ptrHdr, bkt->size) != ROK)
2796 if (cmMmRegIsBlkSane(ptrHdr) != ROK)
2799 /* Handle error case of Memory trampling */
2801 /* display an error message here */
2803 sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, size %u bytes \n",
2804 ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
2806 sprintf(dbgPrntBuf, "Memory Trampling at: %8p, Bucket Id:%03d, size %lu bytes \n",
2807 ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
2809 SDisplay(0, dbgPrntBuf);
2813 * if sanity check returns RTRAMPLINGOK then there is nothing to do
2814 * as the memory blk is already invalidated in cmMmBktSanityChk
2816 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2817 if (cmMmBktSanityChk(bkt) == RTRAMPLINGOK)
2825 /* handle RTRAMPLINGNOK in SFree/SPutSBuf */
2826 return (RTRAMPLINGNOK);
2829 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2830 DU_LOG("\nERROR --> Memory signature is invalid\n");
2834 /* reset the size */
2835 ptrHdr->requestedSize = 0;
2836 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2837 /* Initialize the elements with 0XAB */
2838 memset(ptr, 0xAB, size);
2840 /* check if the block to be freed is already having the state as FREE */
2841 if (CMM_IS_FREE(ptrHdr->memFlags))
2843 /* Handle double deallocation error case */
2845 /* display an error message here */
2846 /*cm_mem_c_001.main_23 Fix for specifier mismatch
2847 * warnings in 64BIT compilation*/
2849 sprintf(dbgPrntBuf, "Attempt to double deallocate memory at: %8p,"
2850 "Bucket Id:%03d, size %u bytes \n",
2851 ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
2853 sprintf(dbgPrntBuf, "Attempt to double deallocate memory at: %8p,"
2854 "Bucket Id:%03d, size %lu bytes \n",
2855 ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
2857 SDisplay(0, dbgPrntBuf);
2859 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2860 DU_LOG("\nERROR --> Attempt to double deallocate memory at: %8p, Bucket Id:%03d,\
2861 size %u bytes \n", ptr, regCb->mapTbl[idx].bktIdx, bkt->size);
2865 /* handle RDBLFREE in SFree/SPutSBuf */
2868 if (CMM_IS_STATIC(ptrHdr->memFlags))
2870 CMM_SET_FREE_FLAG(ptrHdr->memFlags);
2871 CMM_RESET_STATIC_FLAG(ptrHdr->memFlags);
2872 /* deduct it from the static memory count */
2873 bkt->staticMemUsed -= bkt->size;
2875 else if (CMM_IS_DYNAMIC(ptrHdr->memFlags))
2877 CMM_SET_FREE_FLAG(ptrHdr->memFlags);
2878 CMM_RESET_DYNAMIC_FLAG(ptrHdr->memFlags);
2879 /* deduct it from the dynamic memory count */
2880 bkt->dynamicMemUsed -= bkt->size;
2884 /* This is a case similar to trampled memory */
2886 /*cm_mem_c_001.main_23 Fix for specifier
2887 * mismatch warnings in 64BIT compilation*/
2889 sprintf(dbgPrntBuf, "Invalid memory flag: %u !!!\n", ptrHdr->memFlags);
2891 sprintf(dbgPrntBuf, "Invalid memory flag: %lu !!!\n", ptrHdr->memFlags);
2893 SDisplay(0, dbgPrntBuf);
2895 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2896 if (cmMmBktSanityChk(bkt) == RTRAMPLINGOK)
2898 /* do not add to the free list */
2904 /* handle RTRAMPLINGNOK in SFree/SPutSBuf */
2905 return (RTRAMPLINGNOK);
2909 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2910 /* Return the block to memory */
2911 ptrHdr->nextBlk = bkt->nextBlk;
2912 bkt->nextBlk = ptrHdr;
2914 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
2915 /* Move the ptr to end of the bucket */
2916 lastHdr = (CmMmBlkHdr *)bkt->lastBlk;
2917 lastHdr->nextBlk = ptrHdr;
2918 bkt->lastBlk = ptrHdr;
2919 ptrHdr->nextBlk = NULLP;
2921 #elif SS_LIGHT_MEM_LEAK_STS
2922 ptrHdr = (CmMmBlkHdr *) (ptr - sizeof(CmMmBlkHdr));
2923 ptrHdr->lineNo = lineNo;
2924 ptrHdr->currFuncName = funcName;
2925 if(gmemLkCb.isStarted == TRUE)
2927 cmRlsAllocBlk(ptrHdr->allocQueueIndx);
2929 ptrHdr->nextBlk = bkt->nextBlk;
2930 bkt->nextBlk = ptrHdr;
2933 #ifdef SSI_MEM_CORR_PREVENTION
2934 if (*(((uint32_t *)(ptr)) + 2) == 0xdeaddead)
2936 /* Do not free an already freed block to avoid corruption */
2937 cmDblFreeAttempts++;
2942 *((CmMmEntry **)bkt->last) = ptr;
2943 bkt->last = (CmMmEntry *)ptr;
2944 *((CmMmEntry **)ptr) = NULLP;
2945 *(((uint32_t *)(ptr)) + 2) = 0xdeaddead;
2948 *((CmMmEntry **)ptr) = bkt->next;
2949 bkt->next = (CmMmEntry *)ptr;
2951 #endif /* SSI_DEBUG_LEVEL1 */
2953 /* cache_coherency_changes */
2958 * Decrement the statistics variable of number of memory block
2962 /* cm_mem_c_001.main_15 : Additions */
2963 #ifdef SS_HISTOGRAM_SUPPORT
2964 /* If Tapa task (entId)is registerd for histogram then insert Memrory Freed
2965 * information into the hash list */
2968 if (cmHstGrmFreeInsert(&bkt->hstGrmHashListCp, bkt->size,
2969 line, fileName, entId) != ROK)
2971 sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
2975 #endif /* SS_HISTOGRAM_SUPPORT */
2977 #ifdef SS_MEM_LEAK_STS
2978 /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
2979 cmRlsAllocBlk((PTR)ptr);
2980 #endif /* SS_MEM_LEAK_STS */
2985 /* The memory block was allocated from the heap pool */
2986 /* cm_mem_c_001.main_15 : Additions */
2987 #ifdef SS_HISTOGRAM_SUPPORT
2988 return (cmHeapFree (&(regCb->heapCb), ptr, size, line, fileName, entId, hstReg));
2990 return (cmHeapFree (&(regCb->heapCb), ptr, size));
2991 #endif /* SS_HISTOGRAM_SUPPORT */
2992 #else /* use pure is on */
2993 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/
2994 #ifdef SS_4GMX_LCORE
2995 (Void)MxHeapFree(SsiHeap, ptr);
3001 #endif /* USE_PURE */
3002 } /* end of cmFreeNL */
3009 * Desc: alloc without lock
3012 * Ret: ROK - successful
3013 * RFAILED - unsuccessful.
3019 /*cm_mem_c_001.main_21-added new function*/
3020 /*cm_mem_c_001.main_23 Removed support of SSI_DEBUG_LEVEL1 and SS_HISTOGRAM_SUPPORT for SS_FAP*/
3022 static S16 cmAllocWL
3033 /*cm_mem_c_001.main_23 Removed support of SSI_DEBUG_LEVEL1 and SS_HISTOGRAM_SUPPORT for SS_FAP*/
3036 /*cm_mem_c_001.main_23 Removed support of USE_MEMCAL and MEMCAL_DEBUG support for SS_FAP*/
3038 regCb = (CmMmRegCb *)regionCb;
3040 #if (ERRCLASS & ERRCLS_INT_PAR)
3042 /* error check on parameters */
3043 if ((regCb == NULLP) || (size == NULLP) || !(*size) || (ptr == NULLP))
3049 /*cm_mem_c_001.main_23 Removed support of SSI_DEBUG_LEVEL1 and SS_HISTOGRAM_SUPPORT for SS_FAP*/
3054 * Check if the requested size is less than or equal to the maximum block
3055 * size in the bucket.
3057 /* cm_mem_c_001.main_23 Adding check to compair size with Maximum block size*/
3058 if ( *size <= regCb->bktMaxBlkSize)
3060 /* Get the map to the mapping table */
3061 idx = ((*size - 1) >> regCb->bktQnPwr);
3063 /* Dequeue the memory block and return it to the user */
3064 bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]);
3068 * Check if the size request is not greater than the size available
3071 /* cm_mem_c_001.main_23 combined If(*size <= bkt->size) and if(*ptr = bkt->next)*/
3072 if ((*size <= bkt->size)&&(*ptr = bkt->next))
3074 /* Try to go to the next bucket if available */
3075 bkt->next = *((CmMmEntry **)(bkt->next));
3078 * Increment the statistics variable of number of memory block
3083 /* Update the size parameter */
3091 /* Memory not available in the bucket pool */
3092 if (regCb->heapFlag && (*size < regCb->heapSize))
3094 /*cm_mem_c_001.main_23 Removed support of and MEMCAL_DEBUG support for SS_FAP*/
3096 * The heap memory block is available. Allocate the memory block from
3099 /*cm_mem_c_001.main_23 Removed support of SSI_DEBUG_LEVEL1 and SS_HISTOGRAM_SUPPORT for SS_FAP*/
3100 return (cmHeapAlloc(&(regCb->heapCb), ptr, size));
3103 /* No memory available */
3105 #else /* use pure is on */
3106 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/
3107 #ifdef SS_4GMX_LCORE
3108 *ptr = (Data*) MxHeapAlloc(SsiHeap, *size);
3109 memset(ptr, 0, *size);
3111 *ptr = (Data*) malloc(*size);
3113 if ( (*ptr) == NULLP)
3115 avail_size -= *size;
3117 #endif /* USE_PURE */
3119 } /* end of cmAllocWL */
3126 * Desc: free without lock
3129 * Ret: ROK - successful
3130 * RFAILED - unsuccessful.
3137 static S16 cmFreeWL(Void *regionCb,Data *ptr, Size size)
3142 /*cm_mem_c_001.main_23 Removed support of SSI_DEBUG_LEVEL1 and SS_HISTOGRAM_SUPPORT for SS_FAP*/
3145 regCb = (CmMmRegCb *)regionCb;
3148 #if (ERRCLASS & ERRCLS_INT_PAR)
3150 /* error check on parameters */
3151 if ((regCb == NULLP) || (!size) || (ptr == NULLP))
3156 /* Check if the memory block is from the memory region */
3157 if (ptr >= ((CmMmRegCb *)regCb)->regInfo.start +
3158 ((CmMmRegCb *)regCb)->regInfo.size)
3166 * Check if the memory block was allocated from the bucket pool.
3169 if (ptr < (regCb->regInfo.start + regCb->bktSize))
3171 /* The memory block was allocated from the bucket pool */
3173 /* Get the map to the mapping table */
3174 idx = ((size - 1) >> regCb->bktQnPwr);
3176 #if (ERRCLASS & ERRCLS_DEBUG)
3177 if (regCb->mapTbl[idx].bktIdx == 0xFF)
3179 /* Some fatal error in the map table initialization. */
3184 /* Enqueue the memory block and return it to the user */
3185 bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]);
3188 * Check if the size is not greater than the size available
3189 * in the bucket. If so, then the buffer must have been allocated
3190 * from next bucket. We don't need to check the validity of the
3191 * next bucket, otherwise buffer must have been allocated from heap
3194 if (size > bkt->size)
3196 bkt = &(regCb->bktTbl[regCb->mapTbl[++idx].bktIdx]);
3199 /*cm_mem_c_001.main_23 Removed support of SSI_DEBUG_LEVEL1 and SS_HISTOGRAM_SUPPORT for SS_FAP*/
3200 *((CmMmEntry **)ptr) = bkt->next;
3201 bkt->next = (CmMmEntry *)ptr;
3204 * Decrement the statistics variable of number of memory block
3212 /* The memory block was allocated from the heap pool */
3213 return (cmHeapFree (&(regCb->heapCb), ptr, size));
3214 #else /* use pure is on */
3215 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/
3216 #ifdef SS_4GMX_LCORE
3217 (Void)MxHeapFree(SsiHeap, ptr);
3223 #endif /* USE_PURE */
3226 } /* end of cmFreeWL */
3234 * Desc: Control request function.
3237 * Ret: ROK - successful
3238 * RFAILED - unsuccessful.
3240 * Notes: The current semantics of the control function is defined for two
3241 * types of events: virtual address to physical address translation
3242 * and memory resource check.
3244 * The physical address translation is valid only for the memory
3245 * region physically contiguous and non pagable.
3253 static S16 cmCtl(Void *regionCb,Event event, SMemCtl *memCtl)
3258 regCb = (CmMmRegCb *)regionCb;
3260 #if (ERRCLASS & ERRCLS_INT_PAR)
3262 /* error check on parameters */
3263 if ((regCb == NULLP) || (memCtl == NULLP))
3276 #if (ERRCLASS & ERRCLS_INT_PAR)
3277 if ((memCtl->u.vtop.vaddr == NULLP) ||
3278 (memCtl->u.vtop.paddr == NULLP))
3284 /* Check if the virtual to physical address translation is valid */
3285 if (regCb->chFlag & CMM_REG_PHY_VALID)
3287 offset = memCtl->u.vtop.vaddr - regCb->regInfo.start;
3288 *(memCtl->u.vtop.paddr) = regCb->pAddr + offset;
3295 case SS_MEM_CHK_RES:
3298 #if (ERRCLASS & ERRCLS_INT_PAR)
3299 if (!(memCtl->u.chkres.size) ||
3300 (memCtl->u.chkres.status == NULLP))
3306 /* Check if the Bucket pool is configured */
3311 uint32_t avlSize, totSize;
3313 * The bucket pool is configured. The status value returned
3314 * does reflect on the memory availabilty in the bucket pool.
3315 * The value does not consider the available memory in the
3318 idx = ((memCtl->u.chkres.size - 1) >> regCb->bktQnPwr);
3319 bkt = &(regCb->bktTbl[regCb->mapTbl[idx].bktIdx]);
3320 avlSize = (bkt->numBlks - bkt->numAlloc) * bkt->size;
3321 avlSize += regCb->heapCb.avlSize;
3322 totSize = (bkt->numBlks * bkt->size) + regCb->heapSize;
3323 *(memCtl->u.chkres.status) = (avlSize/(totSize/10));
3327 /* Bucket pool not configured */
3330 * Find the percentage memory available in the heap pool. The value
3331 * does not consider the fragmentation of the heap pool.
3333 *(memCtl->u.chkres.status) = ((regCb->heapCb.avlSize) /
3334 (regCb->heapSize/10));
3338 #else /* use pure is on */
3339 *(memCtl->u.chkres.status) = ((avail_size) /
3340 (regCb->regInfo.size/10));
3342 #endif /* USE_PURE */
3348 /* No other event is supported currently */
3353 /* shouldn't reach here */
3355 } /* end of cmCtl */
3362 * Desc: Initialize the bucket and the map table.
3365 * Ret: ROK - successful,
3366 * RFAILED - unsuccessful.
3368 * Notes: This function is called by the cmMmRegInit.
3373 static Void cmMmBktInit
3386 /* cm_mem_c_001.main_12 - addition for temporary variables */
3387 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
3388 CmMmBlkHdr **nextBlk;
3389 #ifdef SS_LIGHT_MEM_LEAK_STS
3390 CmMmBlkHdr *lastBlk;
3391 #endif /*SS_LIGHT_MEM_LEAK_STS */
3393 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
3395 CmMmBlkTail *blkTail;
3399 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
3402 #endif /* SSI_DEBUG_LEVEL1 */
3406 size = cfg->bktCfg[bktIdx].size;
3407 numBlks = cfg->bktCfg[bktIdx].numBlks;
3409 /* cm_mem_c_001.main_12 - addition for header initialization */
3410 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
3411 /* Reset the next block pointer */
3412 regCb->bktTbl[bktIdx].nextBlk = NULLP;
3413 #if (defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
3414 regCb->bktTbl[bktIdx].lastBlk = NULLP;
3416 /* Initialize the link list of the memory block */
3417 nextBlk = &(regCb->bktTbl[bktIdx].nextBlk);
3419 for (cnt = 0; cnt < numBlks; cnt++)
3421 *nextBlk = (CmMmBlkHdr *)*memAddr;
3422 #ifdef SS_LIGHT_MEM_LEAK_STS
3423 lastBlk = (CmMmBlkHdr *)*memAddr;
3424 #endif /*SS_LIGHT_MEM_LEAK_STS */
3426 /* initialize the memory block header */
3427 for (sigCnt=0; sigCnt < CMM_TRAMPLING_SIGNATURE_LEN; sigCnt++)
3429 (*nextBlk)->trSignature[sigCnt] = 0xAB;
3431 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
3432 /* Initialize memory block tail */
3433 blkTail = (CmMmBlkTail *)(*memAddr + sizeof(CmMmBlkHdr) + size);
3434 for (sigCnt=0; sigCnt < CMM_TRAMPLING_SIGNATURE_LEN; sigCnt++)
3436 blkTail->trSignature[sigCnt] = 0xFE;
3439 CMM_SET_FREE_FLAG((*nextBlk)->memFlags);
3440 (*nextBlk)->requestedSize = 0;
3441 #ifdef SS_LIGHT_MEM_LEAK_STS
3442 (*nextBlk)->timeStamp = 0X7777;
3443 (*nextBlk)->lineNo = 0;
3444 (*nextBlk)->allocQueueIndx = 1;
3445 (*nextBlk)->currFuncName = NULL;
3446 #endif /*SS_LIGHT_MEM_LEAK_STS */
3448 #if defined(SSI_DEBUG_LEVEL1) || defined(SS_LIGHT_MEM_LEAK_STS)
3449 *memAddr = (Data *)((*memAddr) + ((sizeof(CmMmBlkHdr)) + size));
3450 #elif BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
3451 *memAddr = (Data *)((*memAddr) + ((sizeof(CmMmBlkHdr)) + sizeof(CmMmBlkTail) + size));
3453 nextBlk = &((*nextBlk)->nextBlk);
3457 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
3458 tmpMemAddr = (Data *)((*memAddr) - ((sizeof(CmMmBlkHdr)) + sizeof(uint32_t) + size));
3459 regCb->bktTbl[bktIdx].lastBlk = (CmMmBlkHdr *)tmpMemAddr;
3463 /* Reset the next pointer */
3464 regCb->bktTbl[bktIdx].next = NULLP;
3465 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
3466 regCb->bktTbl[bktIdx].last = NULLP;
3469 /* Initialize the link list of the memory block */
3470 next = &(regCb->bktTbl[bktIdx].next);
3471 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
3472 last = &(regCb->bktTbl[bktIdx].last);
3473 if(regCb->region == 0)
3477 startPtr128 = *memAddr;
3478 regMemSize = regCb->regInfo.size;
3479 uart_printf("size of all pool=%u\n", regMemSize);
3480 uart_printf("startPtr128=%x\n", startPtr128);
3484 startPtr256 = *memAddr;
3485 uart_printf("startPtr256=%x\n", startPtr256);
3489 startPtr512 = *memAddr;
3490 uart_printf("startPtr512=%x\n", startPtr512);
3494 startPtr768 = *memAddr;
3495 uart_printf("startPtr768=%x\n", startPtr768);
3499 startPtr1664 = *memAddr;
3500 uart_printf("startPtr1664=%x\n", startPtr1664);
3504 startPtr4800 = *memAddr;
3505 uart_printf("startPtr4800=%x\n", startPtr4800);
3509 startPtr9920 = *memAddr;
3510 uart_printf("startPtr9920=%x\n", startPtr9920);
3514 for (cnt = 0; cnt < numBlks; cnt++)
3517 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
3518 (*(uint32_t *)(*next + 4)) = 0xdeaddead;
3519 (*(uint32_t *)(*next + 124)) = 0;
3520 (*(uint32_t *)(*next + 116)) = 0xdeaddead;
3521 (*(uint32_t *)(*next + 24)) = 0xdeaddead;
3522 (*(uint32_t *)(*next + 44)) = 0xdeaddead;
3523 (*(uint32_t *)(*next + 80)) = 0xdeaddead;
3525 #ifdef SSI_MEM_CORR_PREVENTION
3526 *(((uint32_t *)(*next)) + 2) = 0xdeaddead;
3528 next = (CmMmEntry **)(*memAddr);
3529 *memAddr = (*memAddr) + size;
3532 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
3533 *last = (CmMmEntry *)next;
3535 #ifdef SSI_MEM_CORR_PREVENTION
3536 regCb->bktTbl[bktIdx].last = (CmMmEntry *)next;
3539 #endif /* SSI_DEBUG_LEVEL1 */
3541 /* Initialize the Map entry */
3542 idx = size / cfg->bktQnSize;
3545 * Check if the size is multiple of quantum size. If not we need to initialize
3546 * one more map table entry.
3548 if(size % cfg->bktQnSize)
3553 while ( *lstMapIdx < idx)
3555 regCb->mapTbl[*lstMapIdx].bktIdx = bktIdx;
3557 #if (ERRCLASS & ERRCLS_DEBUG)
3558 regCb->mapTbl[*lstMapIdx].numReq = 0;
3559 regCb->mapTbl[*lstMapIdx].numFailure = 0;
3565 /* Initialize the bucket structure */
3566 regCb->bktTbl[bktIdx].size = size;
3567 regCb->bktTbl[bktIdx].numBlks = numBlks;
3568 regCb->bktTbl[bktIdx].numAlloc = 0;
3570 /* Update the total bucket size */
3571 /* cm_mem_c_001.main_12 - addition for considering the header size */
3572 #if (defined(SSI_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
3573 regCb->bktSize += ((size + sizeof(CmMmBlkHdr)) * numBlks);
3574 /* Addition for considering the header size and tail */
3575 #elif BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
3576 regCb->bktSize += ((size + sizeof(CmMmBlkHdr) + sizeof(uint32_t)) * numBlks);
3578 regCb->bktSize += (size * numBlks);
3579 #endif /* SSI_DEBUG_LEVEL1 */
3581 regCb->bktTbl[bktIdx].bktFailCnt = 0;
3582 regCb->bktTbl[bktIdx].bktNoFitCnt = 0;
3584 /* cm_mem_c_001.main_12 - addition for statistics related variable initialization */
3585 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
3586 /* Initialize other required pointers */
3587 regCb->bktTbl[bktIdx].bktStartPtr = (Data *)(regCb->bktTbl[bktIdx].nextBlk);
3588 regCb->bktTbl[bktIdx].numAllocAttempts = 0;
3589 regCb->bktTbl[bktIdx].numDeallocAttempts = 0;
3590 regCb->bktTbl[bktIdx].staticMemUsed = 0;
3591 regCb->bktTbl[bktIdx].dynamicMemUsed = 0;
3592 regCb->bktTbl[bktIdx].trampleCount = 0;
3593 #endif /*SSI_DEBUG_LEVEL1*/
3594 /* cm_mem_c_001.main_15 : Additions */
3595 #ifdef SS_HISTOGRAM_SUPPORT
3596 /* Initialise the memory histogram hash list */
3597 cmHstGrmHashListInit(&(regCb->bktTbl[bktIdx].hstGrmHashListCp));
3598 #endif /* SS_HISTOGRAM_SUPPORT */
3601 } /* end of cmMmBktInit */
3608 * Desc: Initialize the heap pool.
3611 * Ret: ROK - successful
3612 * RFAILED - unsuccessful.
3614 * Notes: This function is called by the cmMmRegInit.
3619 static Void cmMmHeapInit(Data *memAddr,CmMmHeapCb *heapCb,Size size)
3621 /* cm_mem_c_001.main_12 - addition for ssi enhancements */
3622 #ifdef SSI_DEBUG_LEVEL1
3624 #endif /* SSI_DEBUG_LEVEL1 */
3626 /* Initialize the heap control block */
3627 heapCb->vStart = memAddr;
3628 heapCb->vEnd = memAddr + size;
3629 heapCb->avlSize = size;
3630 heapCb->minSize = CMM_MINBUFSIZE;
3632 heapCb->next = (CmHEntry *)memAddr;
3633 heapCb->next->next = NULLP;
3634 /* cm_mem_c_001.main_12 - addition for header initialization */
3635 #ifdef SSI_DEBUG_LEVEL1
3636 heapCb->next->size = size - sizeof(CmHEntry);
3637 heapCb->next->requestedSize = 0;
3638 for (idx=0; idx < CMM_TRAMPLING_SIGNATURE_LEN; idx++)
3640 heapCb->next->trSignature[idx] = 0xAB;
3642 CMM_SET_FREE_FLAG(heapCb->next->memFlags);
3643 heapCb->staticHeapMemUsed = 0;
3644 heapCb->dynamicHeapMemUsed = 0;
3645 heapCb->nextOffset = sizeof(heapCb->next->trSignature) +
3646 sizeof(heapCb->next->memFlags) +
3647 sizeof(heapCb->next->requestedSize);
3648 heapCb->numAllocAttempts = 0;
3649 heapCb->numDeallocAttempts = 0;
3650 heapCb->trampleCount = 0;
3652 heapCb->next->size = size;
3653 #endif /* SSI_DEBUG_LEVEL1 */
3655 #if (ERRCLASS & ERRCLS_DEBUG)
3656 heapCb->numFragBlk = 0;
3658 heapCb->numFailure = 0;
3661 heapCb->heapAllocCnt = 0;
3662 /* cm_mem_c_001.main_15 : Additions */
3663 #ifdef SS_HISTOGRAM_SUPPORT
3664 /* Initialise the memory histogram hash list */
3665 cmHstGrmHashListInit(&(heapCb->heapHstGrmHashListCp));
3666 #endif /* SS_HISTOGRAM_SUPPORT */
3669 } /* end of cmMmHeapInit */
3676 * Desc: Allocates the memory block from the heap pool.
3679 * Ret: ROK - successful
3680 * RFAILED - unsuccessful.
3682 * Notes: This function is called by the cmAlloc. cmAlloc calls this
3683 * function when there is no memory block available in the bucket
3684 * and the heap pool is configured.
3691 /* cm_mem_c_001.main_12 - addition for taking another parameter memType(static/dynamic) */
3692 /* cm_mem_c_001.main_15 : Additions */
3693 #ifdef SS_4GMX_LCORE
3694 uint8_t ysCellConfigDone;
3696 #ifdef SS_HISTOGRAM_SUPPORT
3697 #ifdef SSI_DEBUG_LEVEL1
3698 static S16 cmHeapAlloc
3710 static S16 cmHeapAlloc
3720 #endif /* SSI_DEBUG_LEVEL1 */
3722 #ifdef SSI_DEBUG_LEVEL1
3723 static S16 cmHeapAlloc
3731 static S16 cmHeapAlloc
3737 #endif /* SSI_DEBUG_LEVEL1 */
3738 /* cm_mem_c_001.main_15 : Additions */
3739 #endif /* SS_HISTOGRAM_SUPPORT */
3741 CmHEntry *prvHBlk; /* Previous heap block */
3742 CmHEntry *curHBlk; /* Current heap block */
3744 /* cm_mem_c_001.main_15 : Additions */
3745 #ifdef SS_MEM_LEAK_STS
3747 #endif /* SS_MEM_LEAK_STS */
3748 /* cm_mem_c_001.main_12 - addition for ssi enhancements */
3749 #ifdef SSI_DEBUG_LEVEL1
3750 CmHEntry *alocHeapBlk;
3754 #endif /* SSI_DEBUG_LEVEL1 */
3755 /* cm_mem_c_001.main_15 : Additions */
3756 #ifdef SS_HISTOGRAM_SUPPORT
3758 #endif /* SS_HISTOGRAM_SUPPORT */
3760 /* cm_mem_c_001.main_15 : Additions */
3761 /* Acquire the heap lock */
3762 /* cm_mem_c_001.main_13 : Replaced SLock with WTLock for NT */
3763 #ifdef SS_4GMX_LCORE
3764 if(1 == ysCellConfigDone)
3766 #ifdef SSI_DEBUG_LEVEL1
3767 /* display a message here */
3768 sprintf(dbgPrntBuf,"Allocating from heap size=%u\n", *size);
3769 SDisplay(0, dbgPrntBuf);
3770 #endif /* SSI_DEBUG_LEVEL1 */
3774 (Void) WTLock (&(heapCb->heapLock));
3776 (Void) SLock (&(heapCb->heapLock));
3779 #ifdef SS_MEM_LEAK_STS
3781 #endif /* SS_MEM_LEAK_STS */
3782 /* cm_mem_c_001.main_12 - addition for manipulation of statistics related data */
3783 #ifdef SSI_DEBUG_LEVEL1
3784 heapCb->numAllocAttempts++;
3785 requestedSize = *size;
3786 #endif /* SSI_DEBUG_LEVEL1 */
3788 /* Roundup the requested size */
3789 *size = CMM_DATALIGN(*size, (heapCb->minSize));
3791 /* Check if the available total size is adequate. */
3792 if ((*size) >= heapCb->avlSize)
3794 /* cm_mem_c_001.main_15 : Additions */
3796 (Void) WTUnlock (&(heapCb->heapLock));
3798 (Void) SUnlock (&(heapCb->heapLock));
3804 /* cm_mem_c_001.main_12 - addition for aligning the header size */
3805 #ifdef SSI_DEBUG_LEVEL1
3806 hdr = PTRALIGN(sizeof(CmHEntry));
3807 #endif /* SSI_DEBUG_LEVEL1 */
3810 * Search through the heap block list in the heap pool of size
3811 * greater than or equal to the requested size.
3814 /* cm_mem_c_001.main_12 - addition for accessing the heapCb->next */
3815 #ifdef SSI_DEBUG_LEVEL1
3816 prvHBlk = (CmHEntry *)((Data *)&(heapCb->next) - heapCb->nextOffset);
3818 prvHBlk = (CmHEntry *)&(heapCb->next);
3819 #endif /* SSI_DEBUG_LEVEL1 */
3820 for (curHBlk = prvHBlk->next; curHBlk; curHBlk = curHBlk->next,
3821 prvHBlk = prvHBlk->next)
3824 * Since the size of the block is always multiple of CMM_MINBUFSIZE
3825 * and the requested size is rounded to the size multiple of
3826 * CMM_MINBUFSIZE, the difference between the size of the heap block
3827 * and the size to allocate will be either zero or multiple of
3830 if ((*size) <= curHBlk->size)
3832 /* cm_mem_c_001.main_12 - addition for block size calculation */
3833 #ifdef SSI_DEBUG_LEVEL1
3834 tmpSize = curHBlk->size - (*size);
3836 tmpSize = tmpSize - hdr;
3839 /* cm_mem_c_001.main_28 : compilation warning fix */
3840 tmpSize = (curHBlk->size - (*size));
3842 #endif /* SSI_DEBUG_LEVEL1 */
3844 /* Heap block of bigger size */
3845 /* cm_mem_c_001.main_12 - addition for allocating memory */
3846 #ifdef SSI_DEBUG_LEVEL1
3847 *ptr = (Data *)curHBlk + hdr + tmpSize + hdr;
3848 alocHeapBlk = (CmHEntry *) ((Data *)curHBlk + hdr + tmpSize);
3850 * No need to look for memory trampling as this is a new block altogether
3851 * Update the header only for this case as it is new block formed
3853 for (idx=0; idx < CMM_TRAMPLING_SIGNATURE_LEN; idx++)
3855 alocHeapBlk->trSignature[idx] = 0xAB;
3857 alocHeapBlk->size = *size;
3859 *ptr = (Data *)curHBlk + tmpSize;
3860 #endif /* SSI_DEBUG_LEVEL1 */
3861 curHBlk->size = tmpSize;
3865 /* Heap block is same size of the requested size */
3866 /* cm_mem_c_001.main_12 - addition for sanity check and allocation. This is a fresh block */
3867 #ifdef SSI_DEBUG_LEVEL1
3868 /* look for memory trampling as this is a pure block*/
3871 if (cmMmRegIsBlkSane((CmMmBlkHdr *)curHBlk) != ROK)
3873 /* detected a trampled memory block in this bucket */
3875 /* display an error message here */
3876 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/
3878 sprintf(dbgPrntBuf, "Memory Trampling in heap at: %8p, size %u bytes \n", (void *)curHBlk, requestedSize);
3880 sprintf(dbgPrntBuf, "Memory Trampling in heap at: %8p, size %lu bytes \n", (void *)curHBlk, requestedSize);
3882 SDisplay(0, dbgPrntBuf);
3885 if (cmMmHeapSanityChk(heapCb) == RTRAMPLINGNOK)
3887 /* Release the lock */
3888 /* cm_mem_c_001.main_13: Replaced SUnlock with
3891 (Void) WTUnlock (&(heapCb->heapLock));
3893 (Void) SUnlock (&(heapCb->heapLock));
3895 /* handle RTRAMPLINGNOK in SAlloc/SGetSBuf */
3896 return (RTRAMPLINGNOK);
3900 /* Release the lock */
3901 /* cm_mem_c_001.main_13: Replaced SUnlock with
3904 (Void) WTUnlock (&(heapCb->heapLock));
3906 (Void) SUnlock (&(heapCb->heapLock));
3913 *ptr = (Data *)curHBlk + hdr;
3914 alocHeapBlk = curHBlk;
3915 *size = curHBlk->size;
3917 *ptr = (Data *)curHBlk;
3918 #endif /* SSI_DEBUG_LEVEL1 */
3919 prvHBlk->next = curHBlk->next;
3922 /* cm_mem_c_001.main_12 - addition for header updation */
3923 #ifdef SSI_DEBUG_LEVEL1
3924 /* update the header fields */
3925 alocHeapBlk->requestedSize = requestedSize;
3926 alocHeapBlk->memFlags = 0;
3927 if (memType == CMM_STATIC_MEM_FLAG)
3929 CMM_SET_STATIC_FLAG(alocHeapBlk->memFlags);
3930 heapCb->staticHeapMemUsed += (*size + hdr);
3934 CMM_SET_DYNAMIC_FLAG(alocHeapBlk->memFlags);
3935 heapCb->dynamicHeapMemUsed += (*size + hdr);
3937 heapCb->avlSize -= ((*size) + hdr);
3939 heapCb->avlSize -= (*size);
3940 #endif /* SSI_DEBUG_LEVEL1 */
3946 "SGetSBuf:%08lu:Size Heap Alloc Times:%05lu Pointer: %8p\n",
3947 *size, num_times, *ptr);
3948 SDisplay(0, prntBuf);
3952 /* cm_mem_c_001.main_15 : Additions */
3953 #ifdef SS_MEM_LEAK_STS
3954 /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
3955 cmStorAllocBlk((PTR)*ptr, (Size) reqSz, (Size) *size, MT_MAX_BKTS);
3956 #endif /* SS_MEM_LEAK_STS */
3957 /* Release the lock */
3958 /* cm_mem_c_001.main_16 : cm_mem_c_001.main_18 Additions */
3960 (Void) WTUnlock (&(heapCb->heapLock));
3962 (Void) SUnlock (&(heapCb->heapLock));
3965 #ifdef SS_HISTOGRAM_SUPPORT
3966 /* If If Tapa task (entId)is registerd for histogram then insert Memrory allocated
3967 * information into the hash list */
3970 if (cmHstGrmAllocInsert(&(heapCb->heapHstGrmHashListCp), *size, size, line, fileName, entId) != ROK)
3972 sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
3977 #endif /* SS_HISTOGRAM_SUPPORT */
3983 /* cm_mem_c_008.104 - Addition for memory calculator tool */
3989 /* Release the lock */
3990 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
3992 (Void) WTUnlock (&(heapCb->heapLock));
3994 (Void) SUnlock (&(heapCb->heapLock));
3999 } /* end of cmHeapAlloc */
4006 * Desc: Return the memory block from the heap pool.
4009 * Ret: ROK - successful
4010 * RFAILED - unsuccessful.
4012 * Notes: This function returns the memory block to the heap pool. This
4013 * function is called by cmFree. The function does not check the
4014 * validity of the memory block. The caller must be sure that the
4015 * block was previously allocated and belongs to the heap pool. The
4016 * function maintain the sorting order of the memory block on the
4017 * starting address of the block. This function also do compaction
4018 * if the neighbouring blocks are already in the heap.
4025 /* cm_mem_c_001.main_15 : Additions */
4026 #ifdef SS_HISTOGRAM_SUPPORT
4027 static S16 cmHeapFree
4038 static S16 cmHeapFree
4044 /* cm_mem_c_001.main_15 : Additions */
4045 #endif /* SS_HISTOGRAM_SUPPORT */
4048 CmHEntry *curHBlk; /* Current heap block */
4049 /* cm_mem_c_001.main_12 - addition for ssi enhancements */
4050 #ifdef SSI_DEBUG_LEVEL1
4052 #endif /* SSI_DEBUG_LEVEL1 */
4053 /* cm_mem_c_001.main_15 : Additions */
4054 #ifdef SS_HISTOGRAM_SUPPORT
4055 Size allocSize = size;
4057 #endif /* SS_HISTOGRAM_SUPPORT */
4060 /* Roundup the requested size */
4061 size = CMM_DATALIGN(size, (heapCb->minSize));
4062 /* cm_mem_c_001.main_15: Additions */
4063 #ifdef SS_HISTOGRAM_SUPPORT
4065 #endif /* SS_HISTOGRAM_SUPPORT */
4067 /* Acquire the heap lock */
4068 /* cm_mem_c_001.main_13 : Replaced SLock with WTLock for NT */
4070 (Void) WTLock (&(heapCb->heapLock));
4072 (Void) SLock (&(heapCb->heapLock));
4075 /* increase the avlSize */
4076 /* cm_mem_c_001.main_12 - addition for manipulation of statistics related data */
4077 #ifdef SSI_DEBUG_LEVEL1
4078 hdr = PTRALIGN(sizeof(CmHEntry));
4079 heapCb->avlSize += (size + hdr);
4080 heapCb->numDeallocAttempts++;
4082 heapCb->avlSize += size;
4083 #endif /* SSI_DEBUG_LEVEL1 */
4085 /* cm_mem_c_001.main_12 - addition for pointing to the block */
4086 #ifdef SSI_DEBUG_LEVEL1
4087 p = (CmHEntry *)(ptr - hdr);
4089 p = (CmHEntry *)ptr;
4090 /* cm_mem_c_001.main_15 : Additions */
4091 #ifdef SS_MEM_LEAK_STS
4092 /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
4093 cmRlsAllocBlk((PTR)ptr);
4094 #endif /* SS_MEM_LEAK_STS */
4095 #endif /* SSI_DEBUG_LEVEL1 */
4098 /* cm_mem_c_001.main_12 - addition for sanity and double-free checks */
4099 #ifdef SSI_DEBUG_LEVEL1
4100 /* look for memory trampling */
4101 if (cmMmRegIsBlkSane((CmMmBlkHdr *)p) != ROK)
4103 /* detected a trampled memory block in heap */
4105 /* display an error message here */
4106 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/
4108 sprintf(dbgPrntBuf, "Memory Trampling in heap at: %8p, size %u bytes \n", (void *)p, size);
4110 sprintf(dbgPrntBuf, "Memory Trampling in heap at: %8p, size %lu bytes \n", (void *)p, size);
4112 SDisplay(0, dbgPrntBuf);
4115 if (cmMmHeapSanityChk(heapCb) == RTRAMPLINGNOK)
4117 /* Release the lock */
4118 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
4120 (Void) WTUnlock (&(heapCb->heapLock));
4122 (Void) SUnlock (&(heapCb->heapLock));
4124 /* handle RTRAMPLINGNOK in SAlloc/SGetSBuf */
4125 return (RTRAMPLINGNOK);
4129 /* do not add to the free heap */
4130 heapCb->avlSize -= (size + hdr);
4131 /* Release the heap lock */
4132 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
4134 (Void) WTUnlock (&(heapCb->heapLock));
4136 (Void) SUnlock (&(heapCb->heapLock));
4143 /* look for any double free */
4144 if (CMM_IS_FREE(p->memFlags))
4147 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/
4149 sprintf(dbgPrntBuf, "DOUBLE FREE at %8p for size %u in HEAP \n", (void *)p, size);
4151 sprintf(dbgPrntBuf, "DOUBLE FREE at %8p for size %lu in HEAP \n", (void *)p, size);
4153 SDisplay(0, dbgPrntBuf);
4156 heapCb->avlSize -= (size + hdr);
4157 /* cm_mem_c_001.main_15 : Additions */
4159 (Void) WTUnlock (&(heapCb->heapLock));
4161 (Void) SUnlock (&(heapCb->heapLock));
4166 #endif /* SSI_DEBUG_LEVEL1 */
4168 for ( curHBlk = heapCb->next; curHBlk; curHBlk = curHBlk->next)
4171 * The block will be inserted to maintain the sorted order on the
4172 * starting address of the block.
4176 if (!(curHBlk->next) ||
4177 (p < (curHBlk->next)))
4179 /* Heap block should be inserted here */
4182 * Check if the block to be returned can be merged with the
4185 /* cm_mem_c_001.main_12 - addition for header consideration */
4186 #ifdef SSI_DEBUG_LEVEL1
4187 if (((Data *)curHBlk + hdr + curHBlk->size) == (Data *)p)
4189 if (((Data *)curHBlk + curHBlk->size) == (Data *)p)
4190 #endif /* SSI_DEBUG_LEVEL1 */
4192 /* Merge the block */
4193 /* cm_mem_c_001.main_12 - addition for updating statistics related data */
4194 #ifdef SSI_DEBUG_LEVEL1
4195 /* update the flags */
4196 if (CMM_IS_STATIC(p->memFlags))
4197 heapCb->staticHeapMemUsed -= (size + hdr);
4198 else if (CMM_IS_DYNAMIC(p->memFlags))
4199 heapCb->dynamicHeapMemUsed -= (size + hdr);
4200 size = (curHBlk->size += (size + hdr));
4202 size = (curHBlk->size += size);
4203 #endif /*SSI_DEBUG_LEVEL1*/
4208 /* cm_mem_c_001.main_12 - addition for double-free check */
4209 #ifdef SSI_DEBUG_LEVEL1
4210 /* Check for double deallocation in heap */
4211 if ((Data *)p < ((Data *)curHBlk + curHBlk->size))
4213 /* Release the lock */
4214 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
4216 (Void) WTUnlock (&(heapCb->heapLock));
4218 (Void) SUnlock (&(heapCb->heapLock));
4221 /* This block is already freed in the heap */
4224 /* update the flags as it is a new node */
4225 if (CMM_IS_STATIC(p->memFlags))
4227 heapCb->staticHeapMemUsed -= (size + hdr);
4228 CMM_RESET_STATIC_FLAG(p->memFlags);
4230 else if (CMM_IS_DYNAMIC(p->memFlags))
4232 heapCb->dynamicHeapMemUsed -= (size + hdr);
4233 CMM_RESET_DYNAMIC_FLAG(p->memFlags);
4235 CMM_SET_FREE_FLAG(p->memFlags);
4236 p->requestedSize = 0;
4237 #endif /*SSI_DEBUG_LEVEL1*/
4238 /* insert the block */
4239 p->next = curHBlk->next;
4244 /* Try to merge with the next block in the chain */
4245 /* cm_mem_c_001.main_12 - addition for ssi enhancements */
4246 #ifdef SSI_DEBUG_LEVEL1
4247 if (((Data *)p + hdr + size) == (Data *)(p->next) && (p->next))
4249 if (((Data *)p + size) == (Data *)(p->next) && (p->next))
4250 #endif /*SSI_DEBUG_LEVEL1*/
4252 /* p->next can not be NULL */
4253 /* cm_mem_c_001.main_12 - addition for header consideration */
4254 #ifdef SSI_DEBUG_LEVEL1
4255 p->size += (p->next->size + hdr);
4257 p->size += p->next->size;
4258 #endif /*SSI_DEBUG_LEVEL1*/
4259 p->next = p->next->next;
4262 /* Release the lock */
4263 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
4265 (Void) WTUnlock (&(heapCb->heapLock));
4267 (Void) SUnlock (&(heapCb->heapLock));
4269 /* cm_mem_c_001.main_15 : Additions */
4270 #ifdef SS_HISTOGRAM_SUPPORT
4271 /* If If Tapa task (entId)is registerd for histogram then insert
4272 Memrory Freed information into the hash list */
4275 if (cmHstGrmFreeInsert(&heapCb->heapHstGrmHashListCp, allocSize, line,
4276 fileName, entId) != ROK)
4278 sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
4282 #endif /* SS_HISTOGRAM_SUPPORT */
4286 else if (p < curHBlk)
4289 * Check if the block to be returned can be merged with the
4292 /* cm_mem_c_001.main_12 - addition for header consideration */
4293 #ifdef SSI_DEBUG_LEVEL1
4294 if (((Data *)p + hdr + size) == (Data *)curHBlk)
4296 if (((Data *)p + size) == (Data *)curHBlk)
4297 #endif /* SSI_DEBUG_LEVEL1 */
4299 /* Merge the block */
4300 /* cm_mem_c_001.main_12 - addition for header consideration */
4301 #ifdef SSI_DEBUG_LEVEL1
4302 p->size = size + (curHBlk->size + hdr);
4304 p->size = size + curHBlk->size;
4305 #endif /* SSI_DEBUG_LEVEL1 */
4306 p->next = curHBlk->next;
4310 /* insert the block */
4314 /* cm_mem_c_001.main_12 - addition for header updation */
4315 #ifdef SSI_DEBUG_LEVEL1
4316 /* update the flags in both cases as they are new start nodes*/
4317 if (CMM_IS_STATIC(p->memFlags))
4319 heapCb->staticHeapMemUsed -= (size + hdr);
4320 CMM_RESET_STATIC_FLAG(p->memFlags);
4322 else if (CMM_IS_DYNAMIC(p->memFlags))
4324 heapCb->dynamicHeapMemUsed -= (size + hdr);
4325 CMM_RESET_DYNAMIC_FLAG(p->memFlags);
4327 CMM_SET_FREE_FLAG(p->memFlags);
4328 p->requestedSize = 0;
4329 #endif /* SSI_DEBUG_LEVEL1 */
4333 /* Release the lock */
4334 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
4336 (Void) WTUnlock (&(heapCb->heapLock));
4338 (Void) SUnlock (&(heapCb->heapLock));
4340 /* cm_mem_c_001.main_15 : Additions */
4341 #ifdef SS_HISTOGRAM_SUPPORT
4342 /* If If Tapa task (entId)is registerd for histogram then insert
4343 Memrory Freed information into the hash list */
4346 if (cmHstGrmFreeInsert(&heapCb->heapHstGrmHashListCp, allocSize, line,
4347 fileName, entId) != ROK)
4349 sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
4353 #endif /* SS_HISTOGRAM_SUPPORT */
4359 if (heapCb->next == NULLP)
4361 /* Heap block is empty. Insert the block in the head. */
4366 /* cm_mem_c_001.main_12 - addition for header updation */
4367 #ifdef SSI_DEBUG_LEVEL1
4368 if (CMM_IS_STATIC(p->memFlags))
4370 heapCb->staticHeapMemUsed -= (size + hdr);
4371 CMM_RESET_STATIC_FLAG(p->memFlags);
4373 else if (CMM_IS_DYNAMIC(p->memFlags))
4375 heapCb->dynamicHeapMemUsed -= (size + hdr);
4376 CMM_RESET_DYNAMIC_FLAG(p->memFlags);
4378 CMM_SET_FREE_FLAG(p->memFlags);
4379 p->requestedSize = 0;
4380 #endif /* SSI_DEBUG_LEVEL1 */
4382 /* Release the heap lock */
4383 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
4385 (Void) WTUnlock (&(heapCb->heapLock));
4387 (Void) SUnlock (&(heapCb->heapLock));
4389 /* cm_mem_c_001.main_15 : Additions */
4390 #ifdef SS_HISTOGRAM_SUPPORT
4391 /* If If Tapa task (entId)is registerd for histogram then insert
4392 Memrory Freed information into the hash list */
4395 if (cmHstGrmFreeInsert(&heapCb->heapHstGrmHashListCp, allocSize, line,
4396 fileName, entId) != ROK)
4398 sprintf(hstGrmBuf, "Unable to Insert into the histgram hash list\n");
4402 #endif /* SS_HISTOGRAM_SUPPORT */
4406 /* Release the lock */
4407 /* cm_mem_c_001.main_13: Replaced SUnlock with WTUnlock for NT */
4409 (Void) WTUnlock (&(heapCb->heapLock));
4411 (Void) SUnlock (&(heapCb->heapLock));
4415 } /* end of cmHeapFree */
4417 #ifdef SS_LIGHT_MEM_LEAK_STS
4418 uint32_t cmGetFreeIndx(Void)
4421 if(gmemLkCb.head == gmemLkCb.tail)
4423 allocQueueEmptyCnt++;
4424 return (CM_MAX_ALLOC_ENTERIES);
4428 uint32_t allocQIndx = gmemLkCb.queue[gmemLkCb.head];
4429 gmemLkCb.head = (gmemLkCb.head +1) % CM_MAX_ALLOC_ENTERIES;
4430 return (allocQIndx);
4434 uint32_t cmPutFreeIndx(uint32_t allocQIndx)
4436 uint32_t tmpTail = (gmemLkCb.tail+1)% CM_MAX_ALLOC_ENTERIES;
4437 if(tmpTail == gmemLkCb.head)
4439 allocQueueFullCnt++;
4444 gmemLkCb.queue[gmemLkCb.tail] = allocQIndx;
4445 gmemLkCb.tail = tmpTail;
4451 * Fun: cmInitMemLeakMdl
4453 * Desc: Initializes the memory leak detection module
4458 * Notes: This function initializes the memory leak detection module.
4464 Void cmInitMemLeak (Void)
4469 gmemLkCb.isStarted = FALSE;
4472 SInitLock(&gmemLkCb.memLock, 1);
4473 for(indx = 0; indx < CM_MAX_ALLOC_ENTERIES; indx++)
4475 gmemLkCb.allocInfo[indx].used = FALSE;
4476 cmPutFreeIndx(indx);
4480 } /* cmInitMemLeak */
4483 * Fun: cmDeinitMemLeak
4485 * Desc: De-initializes the memory leak detection
4490 * Notes: This function de-initializes the memory leak detection module.
4496 Void cmDeinitMemLeak(Void)
4501 for(indx = 0; indx < CM_MAX_ALLOC_ENTERIES; indx++)
4503 gmemLkCb.allocInfo[indx].used = FALSE;
4505 SDestroyLock(&gmemLkCb.memLock);
4506 gmemLkCb.isStarted = FALSE;
4513 * Fun: cmStorAllocBlk
4515 * Desc: Initializes the memory leak detection module
4520 * Notes: This function initializes the memory leak detection module.
4526 uint32_t cmStorAllocBlk(Void *addr)
4528 uint32_t allocQIndx;
4530 (Void) SLock(&gmemLkCb.memLock);
4531 allocQIndx = cmGetFreeIndx();
4532 if(allocQIndx < CM_MAX_ALLOC_ENTERIES)
4534 queueIndxAllocCnt++;
4535 gmemLkCb.allocInfo[allocQIndx].memAddr = addr;
4536 gmemLkCb.allocInfo[allocQIndx].used = TRUE;
4538 (Void) SUnlock(&(gmemLkCb.memLock));
4540 return (allocQIndx);
4541 } /* cmStorAllocBlk */
4546 * Fun: cmRlsAllocBlk
4548 * Desc: Initializes the memory leak detection module
4553 * Notes: This function initializes the memory leak detection module.
4559 Void cmRlsAllocBlk(uint32_t allocQIndx)
4562 if(allocQIndx < CM_MAX_ALLOC_ENTERIES)
4564 if(gmemLkCb.allocInfo[allocQIndx].used == TRUE)
4566 (Void) SLock(&gmemLkCb.memLock);
4567 gmemLkCb.allocInfo[allocQIndx].used = FALSE;
4568 cmPutFreeIndx(allocQIndx);
4570 (Void) SUnlock(&(gmemLkCb.memLock));
4574 } /* cmRlsAllocBlk */
4578 * Fun: cmStartStopLeakLog
4587 Void cmStartStopLeakLog(Void)
4589 if (FALSE == gmemLkCb.isStarted)
4591 DU_LOG("\nINFO --> leak capturing started\n");
4592 gmemLkCb.isStarted = TRUE;
4596 gmemLkCb.isStarted = FALSE;
4597 DU_LOG("\nINFO --> leak capturing stopped\n");
4605 * Fun: cmPrintLeakLog
4607 * Desc: Prints leak log
4616 Void cmPrintLeakLog(Void)
4621 static uint32_t leakCount =0;
4624 DU_LOG("\nINFO --> START OF LEAK LOG ");
4625 SLock(&gmemLkCb.memLock);
4626 DU_LOG("\nINFO --> Lock Acquired");
4627 for(indx = 0; indx < CM_MAX_ALLOC_ENTERIES; indx++)
4629 if(gmemLkCb.allocInfo[indx].used == TRUE)
4632 aBkt =(CmMmBlkHdr *)gmemLkCb.allocInfo[indx].memAddr;
4633 DU_LOG("LeakCnt(%ld)Addr(0x%p)RqSz(%ld)",
4634 leakCount,gmemLkCb.allocInfo[indx].memAddr, aBkt->requestedSize);
4635 DU_LOG("LineNo(%ld)funcName(%s)\n",
4636 aBkt->lineNo, aBkt->currFuncName);
4637 gmemLkCb.allocInfo[indx].used = FALSE;
4638 cmPutFreeIndx(indx);
4640 //if(leakCount % 10 == 0)
4643 DU_LOG("\nINFO --> END OF LEAK LOG ");
4644 SUnlock(&gmemLkCb.memLock);
4645 DU_LOG("\nINFO --> Lock Released");
4652 #if (defined(SS_MEM_LEAK_STS) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2))
4655 * Fun: cmRlsAllocBlk
4657 * Desc: Initializes the memory leak detection module
4662 * Notes: This function initializes the memory leak detection module.
4668 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4685 Ptr trace[CM_MAX_STACK_TRACE];
4691 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4692 MemAllocInfo *memAllocInfo;
4696 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4698 retVal = cmHashListFind(®Cb->brdcmSsiLstCp,(uint8_t *)&ptrHdr,
4699 (uint16_t)sizeof(uint64_t), 0, (PTR *)&ptrHdr);
4701 retVal = cmHashListFind(®Cb->brdcmSsiLstCp,(uint8_t *)&ptrHdr,
4702 (uint16_t)sizeof(uint32_t), 0, (PTR *)&ptrHdr);
4706 cmHashListDelete(®Cb->brdcmSsiLstCp, (PTR)ptrHdr);
4709 if( memLkCb.memLkMdlInit == FALSE)
4713 for(idx = 0; idx < CM_MEM_USR_MDL; idx++)
4715 SLock(&memLkCb.memUsrMdl[idx][addr & 0x3].memLck);
4717 retVal = cmHashListFind(&memLkCb.memUsrMdl[idx][addr & 0x3].memHashCp,
4718 (uint8_t *)&addr, sizeof(uint64_t), 0,
4719 (PTR *)&memAllocInfo);
4721 retVal = cmHashListFind(&memLkCb.memUsrMdl[idx][addr & 0x3].memHashCp,
4722 (uint8_t *)&addr, sizeof(uint32_t), 0,
4723 (PTR *)&memAllocInfo);
4727 cmHashListDelete(&memLkCb.memUsrMdl[idx][addr & 0x3].memHashCp,
4729 SUnlock(&memLkCb.memUsrMdl[idx][addr & 0x3].memLck);
4730 funcNm = (S8 **) memAllocInfo->backTrace;
4731 #ifdef SS_MEM_LEAK_SOL
4732 for(i = 0; i < memAllocInfo->bTrcSz; i++)
4734 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/
4735 #ifdef SS_4GMX_LCORE
4736 MxHeapFree(SsiHeap, funcNm[i]);
4741 #endif /* SS_MEM_LEAK_SOL */
4742 #ifdef SS_MEM_LEAK_FREE_TRACE
4746 sprintf( prntBuf, "\n==============================\n");
4748 /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
4750 sprintf( prntBuf, "Address: [%x]\n", addr);
4752 sprintf( prntBuf, "Address: [%lx]\n", addr);
4755 traceSize = backtrace(trace, CM_MAX_STACK_TRACE);
4756 funcNm = backtrace_symbols(trace, traceSize);
4757 sprintf( prntBuf, "[bt] Execution path:\n");
4759 for (i=0; i < traceSize; ++i)
4761 sprintf( prntBuf, "[bt] %s\n", funcNm[i]);
4764 sprintf( prntBuf, "\n==============================\n");
4767 #endif /* SS_MEM_LEAK_FREE_TRACE */
4768 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/
4769 #ifdef SS_4GMX_LCORE
4770 MxHeapFree(SsiHeap, funcNm);
4771 MxHeapFree(SsiHeap, memAllocInfo);
4778 SUnlock(&memLkCb.memUsrMdl[idx][addr & 0x3].memLck);
4781 #ifndef SS_MEM_LEAK_SOL
4782 if(idx == CM_MEM_USR_MDL)
4785 sprintf( prntBuf,"\nPossible Double-Deallocation.\n");
4787 /*cm_mem_c_001.main_23 Fix for specifier mismatch warnings in 64BIT compilation*/
4789 sprintf( prntBuf, "Address: [%u]\n", addr);
4791 sprintf( prntBuf, "Address: [%lu]\n", addr);
4794 traceSize = backtrace(trace, CM_MAX_STACK_TRACE);
4795 funcNm = backtrace_symbols(trace, traceSize);
4796 sprintf( prntBuf,"[bt] Execution path:\n");
4798 for (i=0; i < traceSize; ++i)
4800 sprintf( prntBuf,"[bt] %s\n", funcNm[i]);
4803 printf("\n==============================\n");
4804 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/
4805 #ifdef SS_4GMX_LCORE
4806 MxHeapFree(SsiHeap, funcNm);
4811 #endif /* SS_MEM_LEAK_SOL */
4812 #endif/* BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1 */
4813 /*cm_mem_c_001.main_25 : */
4815 } /* cmRlsAllocBlk */
4820 * Fun: cmStorAllocBlk
4822 * Desc: Initializes the memory leak detection module
4827 * Notes: This function initializes the memory leak detection module.
4833 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4854 #endif /* BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1 */
4856 #ifndef SS_MEM_LEAK_SOL
4857 void *trace[CM_MAX_STACK_TRACE];
4858 #endif /* SS_MEM_LEAK_SOL */
4859 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4862 MemAllocInfo *allocInfo;
4868 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4869 if( memLkCb.memLkMdlInit == FALSE)
4874 #ifdef SS_MEM_LEAK_SOL
4875 /* I need to do this for solaris, because it does not implement
4876 * backtrace. Here backtrace is my function. See below for the
4877 * implementation. */
4878 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/
4879 #ifdef SS_4GMX_LCORE
4880 funcNm = (S8 **)MxHeapAlloc(SsiHeap, (sizeof(uint32_t) * CM_MAX_STACK_TRACE));
4881 memset(funcNm, 0, (sizeof(uint32_t) * CM_MAX_STACK_TRACE));
4883 funcNm = (S8 **)calloc(1, (sizeof(uint32_t) * CM_MAX_STACK_TRACE));
4885 /* SGetSBufNewForDebug(__FILE__,__FUNCTION__,__LINE__,DFLT_REGION, DFLT_POOL, &funcNm, sizeof(uint32_t) * CM_MAX_STACK_TRACE); */
4886 traceSize = backtrace((Void **)funcNm, CM_MAX_STACK_TRACE);
4887 #else /* SS_MEM_LEAK_SOL */
4888 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4889 traceSize = backtrace(trace, CM_MAX_STACK_TRACE);
4890 funcNm = backtrace_symbols(trace, traceSize);
4892 #endif /* SS_MEM_LEAK_SOL */
4894 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4895 moduleId = cmMemGetModuleId(funcNm, traceSize);
4896 (Void)SLock(&(memLkCb.memUsrMdl[moduleId][addr & 0x3].memLck));
4899 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/
4900 #ifndef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4901 #ifdef SS_4GMX_LCORE
4902 allocInfo = (MemAllocInfo *)MxHeapAlloc(SsiHeap, sizeof(MemAllocInfo));
4903 memset(allocInfo, 0, sizeof(MemAllocInfo));
4905 allocInfo = (MemAllocInfo *)calloc(1, sizeof(MemAllocInfo));
4908 /* SGetSBufNewForDebug(__FILE__,__FUNCTION__,__LINE__,DFLT_REGION, DFLT_POOL, &allocInfo, sizeof(MemAllocInfo)); */
4909 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4910 /* check if hashListCp is initialised yet */
4911 if ( regCb->brdcmSsiLstCp.nmbBins == 0)
4915 ptrHdr->reqSz = reqSz;
4916 ptrHdr->allocSz = allocSz;
4917 ptrHdr->bktIdx = bktIdx;
4918 cmHashListInsert(®Cb->brdcmSsiLstCp, (PTR)ptrHdr,
4919 (uint8_t *)&(ptrHdr), sizeof(PTR));
4921 allocInfo->memAddr = addr;
4922 allocInfo->reqSz = reqSz;
4923 allocInfo->allocSz = allocSz;
4924 allocInfo->bktIdx = bktIdx;
4925 allocInfo->backTrace = (PTR) funcNm;
4926 allocInfo->moduleId = moduleId;
4927 allocInfo->bTrcSz = traceSize;
4928 cmHashListInsert(&memLkCb.memUsrMdl[moduleId][addr & 0x3].memHashCp,
4929 (PTR)allocInfo, (uint8_t *)&(allocInfo->memAddr),
4930 sizeof(allocInfo->memAddr));
4931 memLkCb.memUsrMdl[moduleId][addr & 0x3].used = TRUE;
4933 (Void) SUnlock(&(memLkCb.memUsrMdl[moduleId][addr & 0x3].memLck));
4936 } /* cmStorAllocBlk */
4941 } /* cmStorAllocBlk */
4947 * Desc: Initializes the memory leak detection module
4952 * Notes: This function initializes the memory leak detection module.
4958 Void SLogLkInfo (Void)
4960 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
4965 CmMmBlkHdr *newBlkHdr;
4966 CmMmBlkHdr *oldBlkHdr;
4967 CmMmRegCb *tmpRegCb;
4970 fp = fopen("meLeakLog.txt", "w");
4973 memLk.fileLkLog = (FILE *)stdout;
4977 memLk.fileLkLog = fp;
4979 sprintf(prntBuf, "\n------- START OF LEAK LOG -------\n");
4980 fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
4981 for(regionIdx = 0; regionIdx < SS_MAX_REGS; regionIdx++)
4983 // tmpRegCb = mtCMMRegCb[regionIdx];
4984 while(cmHashListGetNext(&tmpRegCb->brdcmSsiLstCp,
4985 (PTR)oldBlkHdr, (PTR *)&newBlkHdr) == ROK)
4987 sprintf(prntBuf, "[LBIS]\n");
4988 fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
4990 sprintf(prntBuf, "Address: 0x%u\n", newBlkHdr);
4992 sprintf(prntBuf, "Address: 0x%lu\n", newBlkHdr);
4994 fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
4995 sprintf(prntBuf, "Requested Size: %d\n", (S16)newBlkHdr->reqSz);
4996 fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
4997 sprintf(prntBuf, "Allocated Size: %d\n", (S16)newBlkHdr->allocSz);
4998 fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
4999 sprintf(prntBuf, "Bucket Idx: %d\n", newBlkHdr->bktIdx);
5000 fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
5001 sprintf(prntBuf,"Memory Allocation Path:\n");
5002 fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
5003 //funcNm = (S8 **)newBlkHdr->backTrace;
5004 for(idx = 0; idx < BRDCM_MEM_LEAK_BTRACE; idx ++)
5006 // sprintf(prntBuf,"==> %p\n", newBlkHdr->backTrace[idx]);
5007 fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
5009 sprintf(prntBuf, "[LBIE]\n\n");
5010 fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
5011 fflush(memLk.fileLkLog);
5012 oldBlkHdr = newBlkHdr;
5016 sprintf(prntBuf, "\n------- END OF LEAK LOG -------\n");
5017 fwrite(prntBuf, strlen(prntBuf), 1, memLk.fileLkLog);
5020 MemAllocInfo *oldMemInfo;
5021 MemAllocInfo *newMemInfo;
5027 if( memLkCb.memLkMdlInit == FALSE)
5031 sprintf(prntBuf, "\n------- START OF LEAK LOG -------\n");
5032 fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5034 for(memMdl = 0; memMdl < CM_MEM_USR_MDL; memMdl++)
5036 for (hashIdx = 0; hashIdx < CM_MAX_HASH_PER_TSK; hashIdx++)
5038 if(memLkCb.memUsrMdl[memMdl][hashIdx].used == FALSE)
5044 SLock(&memLkCb.memUsrMdl[memMdl][hashIdx].memLck);
5045 while(cmHashListGetNext(&memLkCb.memUsrMdl[memMdl][hashIdx].memHashCp,
5046 (PTR)oldMemInfo, (PTR *)&newMemInfo) == ROK)
5048 sprintf(prntBuf, "[LBIS]\n");
5049 fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5050 /* cm_mem_c_001.main_25 - Fixed compilation warnings 32/64 bit */
5052 sprintf(prntBuf, "Address: 0x%u\n", newMemInfo->memAddr);
5054 sprintf(prntBuf, "Address: 0x%lu\n", newMemInfo->memAddr);
5056 fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5057 sprintf(prntBuf, "Module Name: %s\n",
5058 memUsrMdlStr[newMemInfo->moduleId].mdlStr);
5059 fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5060 sprintf(prntBuf, "Requested Size: %d\n", (S16)newMemInfo->reqSz);
5061 fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5062 sprintf(prntBuf, "Allocated Size: %d\n", (S16)newMemInfo->allocSz);
5063 fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5064 sprintf(prntBuf, "Bucket Idx: %d\n", newMemInfo->bktIdx);
5065 fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5066 sprintf(prntBuf,"Memory Allocation Path:\n");
5067 fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5068 funcNm = (S8 **)newMemInfo->backTrace;
5069 for(idx = 0; idx < newMemInfo->bTrcSz; idx ++)
5071 sprintf(prntBuf,"==> %s\n", funcNm[idx]);
5072 fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5074 sprintf(prntBuf, "[LBIE]\n\n");
5075 fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5076 fflush(memLkCb.fileLkLog);
5077 oldMemInfo = newMemInfo;
5080 SUnlock(&memLkCb.memUsrMdl[memMdl][hashIdx].memLck);
5083 sprintf(prntBuf, "\n------- END OF LEAK LOG -------\n");
5084 fwrite(prntBuf, strlen(prntBuf), 1, memLkCb.fileLkLog);
5091 /* cm_mem_c_001.main_15 : Additions */
5092 #ifdef SS_MEM_LEAK_STS
5095 * Fun: cmInitMemLeakMdl
5097 * Desc: Initializes the memory leak detection module
5102 * Notes: This function initializes the memory leak detection module.
5108 Void cmInitMemLeakMdl (Void)
5114 memLkCb.memLkMdlInit = FALSE;
5115 for(memMdl = 0; memMdl < CM_MEM_USR_MDL; memMdl++)
5117 for (hashIdx = 0; hashIdx < CM_MAX_HASH_PER_TSK; hashIdx++)
5119 SInitLock(&memLkCb.memUsrMdl[memMdl][hashIdx].memLck, 1);
5120 cmHashListInit(&memLkCb.memUsrMdl[memMdl][hashIdx].memHashCp,
5121 500, 0, FALSE, CM_HASH_KEYTYPE_UINT32_MOD, 0, 0);
5122 memLkCb.memUsrMdl[memMdl][hashIdx].used = FALSE;
5125 if(memLkCb.fileLkLog == NULLP)
5127 memLkCb.fileLkLog = (FILE *) stdout;
5129 memLkCb.memLkMdlInit = TRUE;
5132 } /* cmInitMemLeakMdl */
5133 /* cm_mem_c_002.main_21 Added for shutdown procedure */
5136 * Fun: cmDeinitMemLeakMdl
5138 * Desc: De-initializes the memory leak detection module
5143 * Notes: This function de-initializes the memory leak detection module.
5149 Void cmDeinitMemLeakMdl (Void)
5155 memLkCb.memLkMdlInit = FALSE;
5156 for(memMdl = 0; memMdl < CM_MEM_USR_MDL; memMdl++)
5158 for (hashIdx = 0; hashIdx < CM_MAX_HASH_PER_TSK; hashIdx++)
5160 SDestroyLock(&memLkCb.memUsrMdl[memMdl][hashIdx].memLck);
5161 cmHashListDeinit(&memLkCb.memUsrMdl[memMdl][hashIdx].memHashCp);
5162 memLkCb.memUsrMdl[memMdl][hashIdx].used = FALSE;
5169 * Fun: cmMemOpenMemLkFile
5171 * Desc: Initializes the memory leak detection module
5176 * Notes: This function initializes the memory leak detection module.
5182 Void cmMemOpenMemLkFile(S8 *arg)
5184 memLkCb.fileLkLog = NULLP;
5185 memLkCb.fileLkLog = fopen(arg, "w");
5192 * Desc: Initializes the memory leak detection module
5197 * Notes: This function initializes the memory leak detection module.
5203 Void SFlushLkInfo (Void)
5205 MemAllocInfo *newMemInfo;
5209 #ifdef SS_MEM_LEAK_SOL
5211 #endif /* SS_MEM_LEAK_SOL */
5213 if( memLkCb.memLkMdlInit == FALSE)
5218 for(memMdl = 0; memMdl < CM_MEM_USR_MDL; memMdl++)
5220 for(hashIdx = 0; hashIdx < CM_MAX_HASH_PER_TSK; hashIdx++)
5222 if(memLkCb.memUsrMdl[memMdl][hashIdx].used == FALSE)
5227 SLock(&memLkCb.memUsrMdl[memMdl][hashIdx].memLck);
5228 while(cmHashListGetNext(&memLkCb.memUsrMdl[memMdl][hashIdx].memHashCp,
5229 (PTR)NULLP, (PTR *)&newMemInfo) == ROK)
5231 funcNm = (S8 **)newMemInfo->backTrace;
5232 #ifdef SS_MEM_LEAK_SOL
5233 for(i = 0; i < newMemInfo->bTrcSz; i++)
5235 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/
5236 #ifdef SS_4GMX_LCORE
5237 MxHeapFree(SsiHeap, funcNm[i]);
5241 /* SPutSBufNewForDebug(__FILE__,__FUNCTION__,__LINE__,DFLT_REGION, DFLT_POOL, funcNm[i], sizeof(uint32_t) * CM_MAX_STACK_TRACE); */
5243 #endif /* SS_MEM_LEAK_SOl */
5244 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/
5245 #ifdef SS_4GMX_LCORE
5246 MxHeapFree(SsiHeap, funcNm);
5247 MxHeapFree(SsiHeap, newMemInfo);
5253 SUnlock(&memLkCb.memUsrMdl[memMdl][hashIdx].memLck);
5260 * Fun: cmMemGetModuleId
5262 * Desc: Initializes the memory leak detection module
5267 * Notes: This function initializes the memory leak detection module.
5273 uint8_t cmMemGetModuleId(S8 **funNm,S32 traceSize)
5282 Txt *memFn[]={"SGetMsg", "SGetSBuf", "SGetDBuf", NULLP};
5284 /*cm_mem_c_001.main_25 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
5285 for(idx = 0; idx < traceSize; idx++)
5289 while((memReqIdx < 0) && (memFn[memStrIdx] != NULLP))
5291 memReqIdx = cmMemGetStrMtchIdx(0, traceSize, memFn[memStrIdx],
5296 while(memUsrMdlStr[mdlFunStrIdx].fPStr != NULLP)
5298 len = strlen((const S8 *)memUsrMdlStr[mdlFunStrIdx].fPStr);
5299 memReqIdx = cmMemGetStrMtchIdx((memReqIdx + 1), traceSize,
5300 memUsrMdlStr[mdlFunStrIdx].fPStr,
5304 return (mdlFunStrIdx);
5309 while(memUsrMdlStr[mdlFunStrIdx].fPStr != NULLP)
5311 retVal = strcmp((const S8 *)"DEFAULT",
5312 (const S8 *)memUsrMdlStr[mdlFunStrIdx].fPStr);
5315 return (mdlFunStrIdx);
5322 } /* cmMemGetModuleId */
5326 * Fun: cmMemGetStrMtchIdx
5328 * Desc: Initializes the memory leak detection module
5333 * Notes: This function initializes the memory leak detection module.
5339 S16 cmMemGetStrMtchIdx
5355 len = strlen((const S8 *)str);
5357 strncpy((S8 *)&cmpStr[1], (const S8 *)str, len);
5358 cmpStr[len + 1] = '\0';
5361 for(;strtIdx < endIdx && !found; strtIdx++)
5364 tempLen = strlen((const S8 *)strLst[strtIdx]);
5368 while(*(strLst[strtIdx] + idx + len) != '\0')
5370 retVal = strncmp((const S8 *)cmpStr,
5371 ((const S8 *)strLst[strtIdx] + idx), len);
5387 } /* cmMemGetStrMtchIdx */
5388 #ifdef SS_MEM_LEAK_SOL
5391 * Fun: cmAddrToSymStr
5393 * Desc: Initializes the memory leak detection module
5398 * Notes: This function initializes the memory leak detection module.
5404 S32 cmAddrToSymStr(Void *pc, S8 *buffer, S32 size)
5411 if (dladdr1(pc, &info, (Void **)&sym, RTLD_DL_SYMENT) == 0)
5413 return (snprintf(buffer, size, "[0x%p]", pc));
5416 if ((info.dli_fname != NULLP && info.dli_sname != NULLP) &&
5417 ((uintptr_t)pc - (uintptr_t)info.dli_saddr < sym->st_size))
5419 return (snprintf(buffer, size, "%s(%s+0x%x) [0x%p]",
5422 (unsigned long)pc - (unsigned long)info.dli_saddr, pc));
5426 return (snprintf(buffer, size, "%s(0x%p [0x%p]",
5428 (unsigned long)pc - (unsigned long)info.dli_fbase, pc));
5431 } /* cmAddrToSymStr */
5435 * Fun: cmLeakCallBack
5437 * Desc: Initializes the memory leak detection module
5442 * Notes: This function initializes the memory leak detection module.
5448 S32 cmLeakCallBack(uintptr_t pc,S32 sigNo, Void *arg)
5452 Backtrace_t *bt = (Backtrace_t *)arg;
5453 if (bt->bt_actcount >= bt->bt_maxcount)
5455 /*cm_mem_c_001.main_27 SSI-4GMX specfic changes*/
5456 #ifdef SS_4GMX_LCORE
5457 buffer = (S8 *)MxHeapAlloc(SsiHeap, 510);
5458 memset(buffer, 0, 510);
5460 buffer = (S8 *)calloc(1, 510);
5462 /* SGetSBufNewForDebug(__FILE__,__FUNCTION__,__LINE__,DFLT_REGION, DFLT_POOL, &buffer, 510); */
5463 (void) cmAddrToSymStr((void *)pc, buffer, 505);
5464 bt->bt_buffer[bt->bt_actcount++] = (S8 *)buffer;
5467 } /* cmLeakCallBack */
5468 #endif /* SS_MEM_LEAK_SOL */
5470 #endif /* SS_MEM_LEAK_STS */
5471 /* cm_mem_c_001.main_12 - addition related to SSI enhancemens
5472 * These include sanity check functions for bucket and heap,
5473 * for printing several memory related statistics
5475 #if (defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined(SSI_DEBUG_LEVEL1))
5478 * Fun: cmMmRegIsBlkSane
5480 * Desc: Performs the sanity check for the memory block by checking its header.
5482 * Ret: ROK - If no trampling is detected in the block
5483 * RFAILED - If trampling is detected in the block
5485 * Notes: This function performs the memory block sanity in a block.
5490 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
5491 S16 cmMmRegIsBlkSane(CmMmBlkHdr *blkPtr,Size size)
5493 S16 cmMmRegIsBlkSane(CmMmBlkHdr *blkPtr)
5497 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
5498 CmMmBlkTail *tailPtr;
5501 for ( sigCnt=0; sigCnt < CMM_TRAMPLING_SIGNATURE_LEN; sigCnt++)
5503 if (blkPtr->trSignature[sigCnt] != 0xAB)
5508 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
5509 tailPtr = (CmMmBlkTail *)((Data *)blkPtr + (sizeof(CmMmBlkHdr) + size));
5510 for ( sigCnt=0; sigCnt < CMM_TRAMPLING_SIGNATURE_LEN; sigCnt++)
5512 if (tailPtr->trSignature[sigCnt] != 0xFE)
5521 #ifdef SSI_DEBUG_LEVEL1
5524 * Fun: cmMmBktSanityChk
5526 * Desc: Performs the sanity check for the memory blocks in a memory bucket.
5527 * This API gets called when trampling is detected in a memory block.
5529 * Ret: RTRAMPLINGNOK - Trampling, serious error
5530 * RTRAMPLINGOK - Trampling, but OK to proceed
5532 * Notes: This function performs the memory block sanity in a bucket. This
5533 * function is called by cmAlloc and cmFree as part of error handling mechanism.
5534 * Performs sanity check for the whole bucket by traversing each
5535 * of the memory blocks using the pointer bktStartPtr.
5536 * Keeps track of number of tramplings happened. If the count
5537 * exceeds the threshold decided, then invalidates this bucket.
5542 static S16 cmMmBktSanityChk(CmMmBkt *bkt)
5547 bkt->trampleCount = 0;
5549 /* scan the entire memory list of the bucket */
5550 for (blkCnt = 0, ptrBlk = (CmMmBlkHdr *)bkt->bktStartPtr;
5551 blkCnt < (bkt->numBlks); blkCnt++)
5553 if (cmMmRegIsBlkSane(ptrBlk) != ROK)
5555 bkt->trampleCount++;
5556 if (bkt->trampleCount > CMM_TRAMPLING_THRESHOLD)
5558 /* Take action to invalidate the entire bucket */
5559 return (RTRAMPLINGNOK);
5562 /* reach next memory block in this bucket manually */
5563 ptrBlk = (CmMmBlkHdr *)((Data *)ptrBlk + ((bkt->size) + (sizeof(CmMmBlkHdr))));
5567 /* display an error message here */
5568 sprintf(dbgPrntBuf, " %d Memory tramplings detected in the bucket!\n", bkt->trampleCount);
5569 SDisplay(0, dbgPrntBuf);
5572 return (RTRAMPLINGOK);
5577 * Fun: cmMmHeapSanityChk
5579 * Desc: Performs the sanity check for the memory blocks in the memory heap.
5580 * This API gets called when trampling is detected in heap(Alloc/Free).
5582 * Ret: RTRAMPLINGNOK - Trampling, serious error
5583 * RTRAMPLINGOK - Trampling, but OK to proceed
5585 * Notes: This function performs the memory block sanity in the heap. This
5586 * function is called by cmHeapAlloc and cmHeapFree as part of error
5587 * handling mechanism. Keeps track of number of tramplings happened.
5588 * If the count exceeds the threshold then return RTRAMPLINGNOK. If the
5589 * count is less than threshold, then return RTRAMPLINGOK.
5594 static S16 cmMmHeapSanityChk(CmMmHeapCb *heapCb)
5597 /* increment the trample count */
5598 heapCb->trampleCount++;
5600 if (heapCb->trampleCount > CMM_TRAMPLING_THRESHOLD)
5602 return (RTRAMPLINGNOK);
5605 return (RTRAMPLINGOK);
5611 * Desc: Computes the hash list index (bin number) for a specified
5612 * key of type (x % 101).
5614 * return (idx % hash_table_size);
5616 * Ret: ROK - successful, *idx contains computed index
5623 static S16 cmMmHashFunc(CmMmHashListCp *hashListCp,uint32_t key,uint16_t *idx)
5626 *idx = (uint16_t)(key % hashListCp->numOfbins);
5630 } /* end of cmMmHashFunc () */
5634 * Fun: cmMmHashListInit
5636 * Desc: Initializes a hash list. Parameters are:
5638 * hashListCp control point for hash list
5639 * nmbBins number of bins in the hash list. Storage will
5640 * be allocated for them from the indicated memory
5643 * pool for allocating storage for bins.
5645 * Ret: ROK - initialization successful
5646 * RFAILED - initialization failed, lack of memory
5653 static S16 cmMmHashListInit
5655 CmMmHashListCp *hashListCp, /* hash list to initialize */
5656 uint16_t nmbBins, /* number of hash list bins */
5657 Region region, /* memory region to allocate bins */
5658 Pool pool /* memory pool to allocate bins */
5662 CmMmHashListEnt *hl;
5665 /* initialize control point fields */
5666 hashListCp->hashList = NULLP;
5667 hashListCp->numOfbins = 0;
5668 hashListCp->numOfEntries = 0;
5670 /* allocate memory for bins */
5673 if (SGetSBufNewForDebug(__FILE__,__FUNCTION__,__LINE__,region, pool, (Data **) &hashListCp->hashList,
5674 (Size)(nmbBins * sizeof(CmMmHashListEnt))) != ROK)
5677 /* initialize bin pointers */
5678 hl = hashListCp->hashList;
5679 for(i = 0; i < nmbBins; i++)
5681 hl[i].size = hl[i].numAttempts = 0;
5684 /* initialize bin size */
5685 hashListCp->numOfbins = nmbBins;
5693 * Fun: cmMmHashListDeinit
5695 * Desc: Deinitializes a hash list. Deallocates memory for bins
5696 * and resets header fields. Parameters are:
5698 * hashListCp control point for hash list
5700 * pool for allocating storage for bins.
5702 * Ret: ROK - successful
5703 * RFAILED - failure, invalid parameters
5710 static S16 cmMmHashListDeinit
5712 CmMmHashListCp *hashListCp, /* hash list to deinitialize */
5713 Region region, /* memory region to allocate bins */
5714 Pool pool /* memory pool to allocate bins */
5718 /* deallocate memory for bins */
5719 if (hashListCp->numOfbins)
5720 (Void) SPutSBufNewForDebug(__FILE__,__FUNCTION__,__LINE__,region, pool,
5721 (Data *) hashListCp->hashList,
5722 (Size) (hashListCp->numOfbins * sizeof(CmMmHashListEnt)));
5724 /* deinitialize control point fields */
5725 hashListCp->hashList = NULLP;
5726 hashListCp->numOfbins = 0;
5727 hashListCp->numOfEntries = 0;
5730 } /* end of cmMmHashListDeinit */
5734 * Fun: cmMmHashListInsert
5736 * Desc: Inserts a new entry in the hash list. Parameters are:
5738 * hashListCp control point for hash list
5739 * key pointer to key string in the new entry
5741 * Ret: ROK - insertion successful
5742 * RFAILED - insertion failed (incorrect parameter values)
5749 static S16 cmMmHashListInsert
5751 CmMmHashListCp *hashListCp, /* hash list to add to */
5752 uint32_t key /* pointer to key */
5755 CmMmHashListEnt *hashListEnt; /* pointer to hash list entry header */
5756 uint16_t idx; /* index for insertion into hash list */
5760 /* check if hashListCp is initialised yet */
5761 if ( hashListCp->numOfbins == 0)
5764 /* compute index for insertion */
5765 if (cmMmHashFunc(hashListCp, key, &idx) != ROK)
5768 hashListEnt = hashListCp->hashList;
5770 if (hashListEnt[idx].numAttempts == 0)
5772 /* new entry, insert here */
5773 hashListEnt[idx].size = key;
5774 hashListEnt[idx].numAttempts++;
5775 /* increment count of entries in hash list */
5776 hashListCp->numOfEntries++;
5780 /* this hash is occupied, re-hash it using linear probing */
5781 for (i=idx; i < CMM_STAT_HASH_TBL_LEN; i++)
5783 if (hashListEnt[i].size == key)
5785 hashListEnt[i].numAttempts++;
5789 if (hashListEnt[i].numAttempts == 0)
5791 hashListEnt[i].size = key;
5792 hashListEnt[i].numAttempts++;
5793 /* increment count of entries in hash list */
5794 hashListCp->numOfEntries++;
5799 if (i == CMM_STAT_HASH_TBL_LEN)
5801 /* there is no free slot for this key */
5807 } /* end of cmMmHashListInsert */
5809 #endif /* SSI_DEBUG_LEVEL1 */
5810 /* cm_mem_c_001.main_15 : Additions */
5811 #ifdef SS_HISTOGRAM_SUPPORT
5814 * Fun: cmHstGrmHashListInit
5816 * Desc: Initializes a hash list. Parameters are:
5818 * hashListCp control point for hash list
5819 * Ret: ROK - initialization successful
5820 * RFAILED - initialization failed, lack of memory
5827 static S16 cmHstGrmHashListInit
5829 CmHstGrmHashListCp *hashListCp /* hash list to initialize */
5832 /*cm_mem_c_001.main_25 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
5834 /* display an error message here */
5835 /*cm_mem_c_001.main_25: Fixed Warnings for 32/64 bit compilation*/
5837 sprintf(dbgPrntBuf, " %lu size of memory histogram hash List\n", sizeof(CmHstGrmHashListCp));
5839 sprintf(dbgPrntBuf, " %d size of memory histogram hash List\n", sizeof(CmHstGrmHashListCp));
5841 SDisplay(0, dbgPrntBuf);
5843 memset(hashListCp, 0, sizeof(CmHstGrmHashListCp));
5849 * Fun: cmHstGrmHashListDeInit
5851 * Desc: De-initializes a hash list. Parameters are:
5853 * hashListCp control point for hash list
5854 * Ret: ROK - initialization successful
5855 * RFAILED - initialization failed, lack of memory
5862 static S16 cmHstGrmHashListDeInit
5864 CmHstGrmHashListCp *hashListCp /* hash list to initialize */
5867 /*cm_mem_c_001.main_25 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
5869 /* display an error message here */
5870 /*cm_mem_c_001.main_25: Fixed Warnings for 32/64 bit compilation*/
5872 sprintf(dbgPrntBuf, " %lu size of memory histogram hash List\n", sizeof(CmHstGrmHashListCp));
5874 sprintf(dbgPrntBuf, " %d size of memory histogram hash List\n", sizeof(CmHstGrmHashListCp));
5876 SDisplay(0, dbgPrntBuf);
5878 memset(hashListCp, 0, sizeof(CmHstGrmHashListCp));
5884 * Fun: cmHstGrmFreeInsert
5886 * Desc: Inserts a Freed information in into the hash list. Parameters are:
5888 * bkt : pointer to bucket for which memory is freed.
5889 * line : Line where memory is freed.
5890 * file : file where memory is freed.
5891 * entId : Tapa task which releases the memory.
5893 * Ret: ROK - insertion successful
5894 * RFAILED - insertion failed (incorrect parameter values)
5901 static S16 cmHstGrmFreeInsert
5903 CmHstGrmHashListCp* hashListCp, /* hash list cp */
5904 uint32_t blkSz, /* size of the block freed */
5905 uint32_t line, /* Line number */
5906 uint8_t *fileName, /* file name */
5907 uint8_t entId /* Tapa task which free the memory */
5910 uint32_t binIdx = 0; /* Bin index to insert the entry into the hash list */
5911 uint32_t key = 0; /* Key to fine the bin index */
5912 uint32_t ret = 0; /* Return value */
5913 CmMemEntries *entry = NULLP; /* Entry which contains the information */
5917 /* check for the total number of entries in the hash list. *
5918 * If there is no place for new entry return failure */
5919 cmHstGrmGetHashIdxAndKey(fileName, line, &binIdx, &key);
5921 /* After comuting the hash bind and key, check the entity already *
5922 existing or not. if we found the entry then update the information */
5923 ret = cmHstGrmFindEntry(hashListCp, key, &binIdx, &entry);
5926 entry->freedBytes += blkSz;
5927 entry->bucketFreeReq++;
5931 /* If hash list is full then print the error tna continue */
5932 if(hashListCp->totalNumEntries == (CMM_HIST_MAX_MEM_BIN * CMM_HIST_MAX_MEM_ENTRY_PER_BIN))
5934 DU_LOG("\nERROR --> No place in the hash list. Increase the value of macro CMM_HIST_MAX_MEM_BIN and CMM_HIST_MAX_MEM_ENTRY_PER_BIN \n");
5938 /* Take the address of next free entry in the hash bin */
5939 entry = &(hashListCp->hashList[binIdx].entries[hashListCp->hashList[binIdx].numOfEntries]);
5941 /* Increase the number of time frees called */
5942 entry->bucketFreeReq++;
5943 entry->freedBytes += blkSz;
5945 /* Fill the information into the entry structure */
5946 cmHstGrmFillEntry(entry, key, line, fileName, entId);
5947 /* Increase the total numbet of entries in the bin */
5948 hashListCp->hashList[binIdx].numOfEntries++;
5950 /* Increase the total number of entries in the hash list */
5951 hashListCp->totalNumEntries++;
5954 } /* end of cmHstGrmFreeInsert */
5959 * Fun: ret = cmHstGrmAllocInsert
5961 * Desc: Inserts a memory allocated information in the hash list. Parameters are:
5963 * hashListCp control point for hash list
5964 * key pointer to key string in the new entry
5966 * Ret: ROK - insertion successful
5967 * RFAILED - insertion failed (incorrect parameter values)
5974 static S16 cmHstGrmAllocInsert
5976 CmHstGrmHashListCp *hashListCp,
5984 uint32_t binIdx = 0;
5987 CmMemEntries *entry = NULLP;
5990 /* check for the total number of entries in the hash list. *
5991 * If there is no place for new entry return failure */
5992 cmHstGrmGetHashIdxAndKey(fileName, line, &binIdx, &key);
5994 /* After comuting the hash bind and key, check the entity already *
5995 existing or not. if we found the entry then update the information */
5996 ret = cmHstGrmFindEntry(hashListCp, key, &binIdx, &entry);
6000 entry->allocBytes += blkSz;
6001 entry->bucketAllocReq++;
6002 entry->wastedBytes += (blkSz - *reqSz);
6006 if(hashListCp->totalNumEntries == (CMM_HIST_MAX_MEM_BIN * CMM_HIST_MAX_MEM_ENTRY_PER_BIN))
6008 DU_LOG("\nERROR --> No place in the hash list. Increase the value of macro CMM_HIST_MAX_MEM_BIN and CMM_HIST_MAX_MEM_ENTRY_PER_BIN\n");
6012 /* Take the address of next free entry in the hash bin */
6013 entry = &(hashListCp->hashList[binIdx].entries[hashListCp->hashList[binIdx].numOfEntries]);
6015 /* Clauculae the wasted bytes */
6016 /* Here wasted byte is differnce between the byte user
6017 * has requested and the byte the ssi allocated */
6018 entry->wastedBytes += (blkSz - *reqSz);
6019 entry->bucketAllocReq++;
6020 entry->allocBytes += blkSz;
6022 /* Fill the information into the entry structure */
6023 cmHstGrmFillEntry(entry, key, line, fileName, entId);
6024 /* Increase the total numbet of entries in the bin */
6025 hashListCp->hashList[binIdx].numOfEntries++;
6027 /* Increase the total number of entries in the hash list */
6028 hashListCp->totalNumEntries++;
6031 } /* end of cmHstGrmAllocInsert */
6036 * Fun: cmHstGrmGetHashIdxAndKey
6038 * Desc: Finds an entry in the hash list. Parameters are:
6040 * hashListCp control point for hash list
6041 * key pointer to key string in the new entry
6043 * Ret: ROK - insertion successful
6044 * RFAILED - insertion failed (incorrect parameter values)
6051 static S16 cmHstGrmGetHashIdxAndKey
6062 /* Calculate the key using file name and line number */
6063 for(i = 0 ; fileName[i] != '\0'; i++)
6065 *key += fileName[i];
6068 *binIdx = ( *key % CMM_HIST_MAX_MEM_BIN);
6070 } /* end of cmHstGrmFillEntry */
6074 * Fun: cmHstGrmFillEntry
6076 * Desc: Insert the entry into the hash list.
6078 * entry : Infornation which will be inserted into the hash list
6079 * key : Which will be used ti find the entry.
6080 * line : Line number
6081 * fileName : File name
6082 * entId : Tapa task Id
6084 * Ret: ROK - insertion successful
6085 * RFAILED - insertion failed (incorrect parameter values)
6092 static S16 cmHstGrmFillEntry
6094 CmMemEntries *entry,
6105 entry->entId = entId;
6106 for(idx = 0; fileName[idx] != '\0'; idx++)
6108 entry->fileName[idx] = fileName[idx];
6110 entry->fileName[idx] = '\0';
6112 } /* end of cmHstGrmFillEntry */
6116 * Fun: cmHstGrmFindEntry
6118 * Desc: Finds an entry in the hash list. Parameters are:
6120 * hashListCp control point for hash list
6121 * key pointer to key string in the new entry
6123 * Ret: ROK - insertion successful
6124 * RFAILED - insertion failed (incorrect parameter values)
6131 static S16 cmHstGrmFindEntry
6133 CmHstGrmHashListCp *hashListCp,
6136 CmMemEntries **entry
6140 uint32_t numEnt = 0;
6141 uint32_t numBin = 0;
6142 CmHstGrmHashListEnt *tmpBin = NULLP;
6144 for(numBin = 0; numBin < CMM_HIST_MAX_MEM_BIN; numBin++)
6146 /* find for the entry in the bin */
6147 tmpBin = &(hashListCp->hashList[*binIdx]);
6148 for(numEnt = 0; numEnt < tmpBin->numOfEntries; numEnt++)
6150 /* If key supplied is matched with the stored key then
6151 * return that entity */
6152 if(tmpBin->entries[numEnt].key == key)
6154 *entry = &(tmpBin->entries[numEnt]);
6156 }/* End of if (tmpBin->entries[numEnt].key) */
6157 }/* end of for (numEnt = 0) */
6159 /* Here we are checking for any room left in the bin. If the room *
6160 exists its mean that there is no entry with the Key. so return *
6162 If there is no room in the bin, then check the other bins to find *
6164 if(numEnt == CMM_HIST_MAX_MEM_ENTRY_PER_BIN)
6166 if(*binIdx == CMM_HIST_MAX_MEM_BIN)
6169 }/* End of if (binIdx) */
6173 }/* End of else (binIdx) */
6174 } /* End of if (numEnt) */
6177 DU_LOG("\nERROR --> Unable to find the entry in hash list\n");
6179 }/* End of else (numEnt) */
6180 }/* end of for (numBin = 0) */
6182 DU_LOG("\nERROR --> Unable to find the entry in the hash list\n");
6184 } /* end of cmHstGrmFindEntry */
6186 #endif /* SS_HISTOGRAM_SUPPORT */
6188 /**********************************************************************
6190 **********************************************************************/