/********************************************************************************************************* ** 函数名称: memLeaks ** 功能描述: 测试内存泄露 ** 输 入 : NONE ** 输 出 : NONE ** 全局变量: ** 调用模块: *********************************************************************************************************/ static int memLeaks(int argc, char *argv[]) { int fd; if (argc < 2) { bstats(STDOUT_FILENO, printMemStats); return 0; } else if ((fd = gopen(T(argv[1]), O_CREAT | O_TRUNC | O_WRONLY, 0644)) >= 0) { bstats(fd, printMemStats); gclose(fd); } return 0; }
static void memLeaks() { int fd; if ((fd = gopen(T("leak.txt"), O_CREAT | O_TRUNC | O_WRONLY, 0666)) >= 0) { bstats(fd, printMemStats); close(fd); } }
static void memLeaks() { int fd=1; /* if ((fd = gopen(T("leak.txt"), O_CREAT | O_TRUNC | O_WRONLY)) >= 0) { */ bstats(fd, printMemStats); /* close(fd); } */ }
//***************************************************************************** // //! Reports information on the current heap usage. //! //! \param pulTotalFree points to storage which will be written with the total //! number of bytes unallocated in the heap. //! //! This function reports the total amount of memory free in the SDRAM heap and //! the size of the largest available block. It is included in the build only //! if label INCLUDE_BGET_STATS is defined. //! //! \return Returns the size of the largest available free block in the SDRAM //! heap. // //***************************************************************************** unsigned long ExtRAMMaxFree(unsigned long *pulTotalFree) { bufsize sizeTotalFree, sizeTotalAlloc, sizeMaxFree; long lGet, lRel; if(g_bSDRAMPresent) { bstats(&sizeTotalAlloc, &sizeTotalFree, &sizeMaxFree, &lGet, &lRel); *pulTotalFree = (unsigned long)sizeTotalFree; return(sizeMaxFree); } else { *pulTotalFree = 0; return(0); } }
void *balloc(B_ARGS_DEC, int size) { bType *bp; int q, memSize; /* * Call bopen with default values if the application has not yet done so */ if (bFreeBuf == NULL) { if (bopen(NULL, B_DEFAULT_MEM, 0) < 0) { return NULL; } } #ifdef B_VERIFY_CAUSES_SEVERE_OVERHEAD verifyBallocSpace(); #endif if (size < 0) { return NULL; } #ifdef BASTARD_TESTING if (rand() == 0x7fff) { return NULL; } #endif /* BASTARD_TESTING */ memSize = ballocGetSize(size, &q); if (q >= B_MAX_CLASS) { /* * Size if bigger than the maximum class. Malloc if use has been okayed */ if (bFlags & B_USE_MALLOC) { #ifdef B_STATS bstats(0, NULL); #endif #ifdef IRIX memSize = ROUNDUP4(memSize); #endif bp = (bType*) malloc(memSize); if (bp == NULL) { traceRaw(T("B: malloc failed\n")); return NULL; } #ifdef B_STATS bStatsMemMalloc += memSize; #endif #if (defined (B_FILL) || defined (B_VERIFY_CAUSES_SEVERE_OVERHEAD)) bFillBlock(bp, memSize); #endif } else { traceRaw(T("B: malloc failed\n")); return NULL; } /* * the u.size is the actual size allocated for data */ bp->u.size = memSize - sizeof(bType); bp->flags = B_MALLOCED; } else if ((bp = bQhead[q]) != NULL) { /* * Take first block off the relevant q if non-empty */ bQhead[q] = bp->u.next; #ifdef B_VERIFY_CAUSES_SEVERE_OVERHEAD verifyFreeBlock(bp, q); #endif #if (defined (B_FILL) || defined (B_VERIFY_CAUSES_SEVERE_OVERHEAD)) bFillBlock(bp, memSize); #endif bp->u.size = memSize - sizeof(bType); bp->flags = 0; } else { if (bFreeLeft > memSize) { /* * The q was empty, and the free list has spare memory so * create a new block out of the primary free block */ bp = (bType*) bFreeNext; #ifdef B_VERIFY_CAUSES_SEVERE_OVERHEAD verifyFreeBlock(bp, q); #endif bFreeNext += memSize; bFreeLeft -= memSize; #if (defined (B_FILL) || defined (B_VERIFY_CAUSES_SEVERE_OVERHEAD)) bFillBlock(bp, memSize); #endif bp->u.size = memSize - sizeof(bType); bp->flags = 0; } else if (bFlags & B_USE_MALLOC) { #ifdef B_STATS static int once = 0; if (once++ == 0) { bstats(0, NULL); } #endif /* * Nothing left on the primary free list, so malloc a new block */ #ifdef IRIX memSize = ROUNDUP4(memSize); #endif if ((bp = (bType*) malloc(memSize)) == NULL) { traceRaw(T("B: malloc failed\n")); return NULL; } #ifdef B_STATS bStatsMemMalloc += memSize; #endif #if (defined (B_FILL) || defined (B_VERIFY_CAUSES_SEVERE_OVERHEAD)) bFillBlock(bp, memSize); #endif bp->u.size = memSize - sizeof(bType); bp->flags = B_MALLOCED; } else { traceRaw(T("B: malloc failed\n")); return NULL; } } #ifdef B_STATS bStatsAlloc(B_ARGS, bp, q, memSize); #endif bp->flags |= B_INTEGRITY; /* * The following is a good place to put a breakpoint when trying to reduce * determine and reduce maximum memory use. */ #if 0 #ifdef B_STATS if (bStatsBallocInUse == bStatsBallocMax) { bstats(0, NULL); } #endif #endif return (void*) ((char*) bp + sizeof(bType)); }