| 94 | } |
| 95 | |
| 96 | XN_C_API void* xnOSLogMemAlloc(void* pMemBlock, XnAllocationType nAllocType, XnUInt32 nBytes, const XnChar* csFunction, const XnChar* csFile, XnUInt32 nLine, const XnChar* csAdditional) |
| 97 | { |
| 98 | static XnBool bFirstTime = TRUE; |
| 99 | static XnBool bReentrent = FALSE; |
| 100 | |
| 101 | if (bFirstTime) |
| 102 | { |
| 103 | bFirstTime = FALSE; |
| 104 | printf("************************************************************\n"); |
| 105 | printf("** WARNING: Memory Profiling is on! **\n"); |
| 106 | printf("************************************************************\n"); |
| 107 | |
| 108 | bReentrent = TRUE; |
| 109 | xnOSCreateCriticalSection(&g_hCS); |
| 110 | |
| 111 | #ifdef XN_MEMORY_PROFILING_DUMP |
| 112 | xnDumpSetMaskState("MemProf", TRUE); |
| 113 | #endif |
| 114 | g_dump = xnDumpFileOpen("MemProf", "MemProfiling.log"); |
| 115 | xnDumpFileWriteString(g_dump, "Entry,Address,AllocType,Bytes,Function,File,Line,AdditionalInfo\n"); |
| 116 | bReentrent = FALSE; |
| 117 | } |
| 118 | |
| 119 | // ignore stuff that is being allocated during "first time" |
| 120 | if (bReentrent) |
| 121 | { |
| 122 | return pMemBlock; |
| 123 | } |
| 124 | |
| 125 | XnMemBlockDataNode* pNode; |
| 126 | pNode = (XnMemBlockDataNode*)xnOSMalloc(sizeof(XnMemBlockDataNode)); |
| 127 | pNode->Data.pMemBlock = pMemBlock; |
| 128 | pNode->Data.nAllocType = nAllocType; |
| 129 | pNode->Data.nBytes = nBytes; |
| 130 | pNode->Data.csFunction = csFunction; |
| 131 | pNode->Data.csFile = csFile; |
| 132 | pNode->Data.nLine = nLine; |
| 133 | pNode->Data.csAdditional = csAdditional; |
| 134 | pNode->Data.nFrames = XN_MEM_PROF_MAX_FRAMES; |
| 135 | xnDumpFileWriteString(g_dump, "Alloc,0x%x,%s,%u,%s,%s,%u,%s\n", pMemBlock, XnGetAllocTypeString(nAllocType), nBytes, csFunction, csFile, nLine, csAdditional); |
| 136 | |
| 137 | // try to get call stack (skip 2 frames - this one and the alloc func) |
| 138 | XnChar* pstrFrames[XN_MEM_PROF_MAX_FRAMES]; |
| 139 | for (XnUInt32 i = 0; i < XN_MEM_PROF_MAX_FRAMES; ++i) |
| 140 | { |
| 141 | pstrFrames[i] = pNode->Data.aFrames[i]; |
| 142 | } |
| 143 | if (XN_STATUS_OK != xnOSGetCurrentCallStack(2, pstrFrames, XN_MEM_PROF_MAX_FRAME_LEN, &pNode->Data.nFrames)) |
| 144 | { |
| 145 | pNode->Data.nFrames = 0; |
| 146 | } |
| 147 | |
| 148 | pNode->pNext = NULL; |
| 149 | |
| 150 | XnAutoCSLocker lock(g_hCS); |
| 151 | if (g_allocatedMemory.pLast == NULL) |
| 152 | { |
| 153 | g_allocatedMemory.pFirst = g_allocatedMemory.pLast = pNode; |
no test coverage detected