-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathframe_mem_mgt.cpp
772 lines (656 loc) · 16.7 KB
/
frame_mem_mgt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/*
* frame_mem_mgt.cpp
*
* Created on: 2012-12-18
* Author: jimm
*/
#include "common/common_errordef.h"
#include "frame_mem_mgt.h"
#include <string.h>
FRAME_NAMESPACE_BEGIN
CFrameMemMgt::CFrameMemMgt()
{
m_nMaxBlockSize = 0;
m_nMemLeakCount = 0;
}
CFrameMemMgt::~CFrameMemMgt()
{
}
int32_t CFrameMemMgt::Initialize()
{
int32_t nWantCount = 0;
//从MAM_BLOCK_SIZE_STEP字节到n * MAM_BLOCK_SIZE_STEP字节各申请0个内存块,主要是为了生成内存块索引
for(int32_t i = 1; i <= 1024 * 1024; ++i)
{
//注意,初始化内存一定要保证单线程操作!
int32_t nMallocCount = MallocMemBlock(i * MAM_BLOCK_SIZE_STEP, nWantCount);
if(nMallocCount < nWantCount)
{
return -1;
}
}
return S_OK;
}
int32_t CFrameMemMgt::Resume()
{
return S_OK;
}
int32_t CFrameMemMgt::Uinitialize()
{
return S_OK;
}
uint8_t *CFrameMemMgt::AllocBlock(int32_t nWantSize)
{
if(nWantSize <= 0)
{
return NULL;
}
int32_t nIndex = GetTableIndexByBytes(nWantSize);
if(nIndex <= 0)
{
return NULL;
}
MemBlockInfo *pMemBlockInfo = GetMemBlockInfo(nIndex);
if(pMemBlockInfo == NULL)
{
return NULL;
}
MUTEX_GUARD(MemBlockLock, pMemBlockInfo->m_stMemBlockLock);
if(pMemBlockInfo->m_stMemBlockList.empty())
{
MallocMemBlock(pMemBlockInfo->m_nBlockSize, ALLOC_MEM_STEP);
}
if(pMemBlockInfo->m_stMemBlockList.empty())
{
return NULL;
}
uint8_t *pMem = NULL;
MemBlockHead *pHead = NULL;
while(!pMemBlockInfo->m_stMemBlockList.empty())
{
uint8_t *pMemTemp = pMemBlockInfo->m_stMemBlockList.front();
pMemBlockInfo->m_stMemBlockList.pop_front();
//增加引用计数
pHead = (MemBlockHead *)(pMemTemp - sizeof(MemBlockHead));
//此内存是否正在使用
if(pHead->m_nReferCount > 0)
{
RecordMemLeakInfo(pMemTemp);
continue;
}
pMem = pMemTemp;
++pHead->m_nReferCount;
break;
}
return pMem;
}
void CFrameMemMgt::RecycleBlock(uint8_t *pMemBlock)
{
if(pMemBlock == NULL)
{
return;
}
MemBlockHead *pHead = (MemBlockHead *)(pMemBlock - sizeof(MemBlockHead));
int32_t nIndex = pHead->m_nIndex;
if(m_stMemInfoTable.count(nIndex) <= 0)
{
RecordMemLeakInfo(pMemBlock);
return;
}
//此内存块大小
uint32_t nBlockSize = pHead->m_nBlockSize;
if(nBlockSize == 0 || nBlockSize > m_nMaxBlockSize)
{
RecordMemLeakInfo(pMemBlock);
return;
}
// int32_t nIndex = GetTableIndexByBytes(nBlockSize);
// if(nIndex <= 0)
// {
// RecordMemLeakInfo(pMemBlock);
// return;
// }
MemBlockInfo *pMemBlockInfo = GetMemBlockInfo(nIndex);
if(pMemBlockInfo == NULL)
{
RecordMemLeakInfo(pMemBlock);
return;
}
if(pMemBlockInfo->m_nBlockSize != nBlockSize)
{
RecordMemLeakInfo(pMemBlock);
return;
}
//引用计数减一
if(pHead->m_nReferCount > 0)
{
--pHead->m_nReferCount;
MUTEX_GUARD(MemBlockLock, pMemBlockInfo->m_stMemBlockLock);
pMemBlockInfo->m_stMemBlockList.push_front(pMemBlock);
}
else
{
RecordMemLeakInfo(pMemBlock);
}
}
//添加块内存正在使用记录
uint8_t* CFrameMemMgt::AddBlkAddrRcd(uint8_t *pBlock)
{
MUTEX_GUARD(lock, m_stBlkAddrLock);
if(pBlock != NULL)
{
if(m_stBlkAddrRcd.count(pBlock) > 0)
{
//todo : trouble
}
else
{
m_stBlkAddrRcd.insert(pBlock);
}
}
return pBlock;
}
//删除块内存正在使用记录
void CFrameMemMgt::DelBlkAddrRcd(uint8_t *pBlock)
{
MUTEX_GUARD(lock, m_stBlkAddrLock);
if(pBlock != NULL)
{
if(m_stBlkAddrRcd.count(pBlock) > 0)
{
m_stBlkAddrRcd.erase(pBlock);
}
}
}
//是否块内存有正在使用记录
bool CFrameMemMgt::HasBlkAddrRcd(uint8_t *pBlock)
{
MUTEX_GUARD(lock, m_stBlkAddrLock);
return m_stBlkAddrRcd.count(pBlock) > 0;
}
//添加堆内存正在使用记录
uint8_t* CFrameMemMgt::AddHeapAddrRcd(uint8_t *pBlock)
{
MUTEX_GUARD(lock, m_stHeapAddrLock);
if(pBlock != NULL)
{
if(m_stHeapAddrRcd.count(pBlock) > 0)
{
//todo : trouble
}
else
{
m_stHeapAddrRcd.insert(pBlock);
}
}
return pBlock;
}
//删除堆内存正在使用记录
void CFrameMemMgt::DelHeapAddrRcd(uint8_t *pBlock)
{
MUTEX_GUARD(lock, m_stHeapAddrLock);
if(pBlock != NULL)
{
if(m_stHeapAddrRcd.count(pBlock) > 0)
{
m_stHeapAddrRcd.erase(pBlock);
}
delete pBlock;
}
}
//是否有堆内存正在使用记录
bool CFrameMemMgt::HasHeapAddrRcd(uint8_t *pBlock)
{
MUTEX_GUARD(lock, m_stHeapAddrLock);
return m_stHeapAddrRcd.count(pBlock) > 0;
}
//记录内存泄露信息
void CFrameMemMgt::RecordMemLeakInfo(uint8_t *pMemBlock)
{
if(pMemBlock != NULL)
{
++m_nMemLeakCount;
}
}
#include <stdio.h>
//统计目前各个内存块的数量
void CFrameMemMgt::PrintMemBlockInfo()
{
int32_t nOffset = 0;
static char strMemBlockInfo[1024 * 1024];
memset(strMemBlockInfo, 0, sizeof(strMemBlockInfo));
sprintf(strMemBlockInfo + nOffset, "frame mem block info : {");
nOffset = strlen(strMemBlockInfo);
//个内存结点信息
for(MemInfoTable::iterator it = m_stMemInfoTable.begin(); it != m_stMemInfoTable.end(); it++)
{
MemBlockInfo *pMemBlockInfo = it->second;
if(pMemBlockInfo == NULL)
{
continue;
}
if(pMemBlockInfo->m_nBlockCount <= ALLOC_MEM_STEP)
{
continue;
}
MUTEX_GUARD(MemBlockLock, pMemBlockInfo->m_stMemBlockLock);
sprintf(strMemBlockInfo + nOffset, "{BlockSize(%d) : BlockCount(%d) : LeftCount(%d)}",
pMemBlockInfo->m_nBlockSize, pMemBlockInfo->m_nBlockCount,
(int32_t)pMemBlockInfo->m_stMemBlockList.size());
nOffset = strlen(strMemBlockInfo);
}
sprintf(strMemBlockInfo + nOffset, ", m_nMemLeakCount = %d}", m_nMemLeakCount);
nOffset = strlen(strMemBlockInfo);
//g_FrameLogEngine.WriteMainLog(enmLogLevel_Notice, "%s\n", strMemBlockInfo);
printf("%s\n", strMemBlockInfo);
//内存申请记录
nOffset = 0;
memset(strMemBlockInfo, 0, sizeof(strMemBlockInfo));
sprintf(strMemBlockInfo + nOffset, "frame mem malloc record : {");
nOffset = strlen(strMemBlockInfo);
m_stAllocMemRecordLock.enter();
for(MemRecordMap::iterator it = m_stAllocMemRecordMap.begin(); it != m_stAllocMemRecordMap.end(); it++)
{
LineNoMap *pLineNoMap = it->second;
for(LineNoMap::iterator st = pLineNoMap->begin(); st != pLineNoMap->end(); st++)
{
BlockSizeMap *pBlockSizeMap = st->second;
for(BlockSizeMap::iterator pt = pBlockSizeMap->begin(); pt != pBlockSizeMap->end(); pt++)
{
MemOperationRecord *pRecord = pt->second;
if(pRecord == NULL)
{
continue;
}
sprintf(strMemBlockInfo + nOffset, "{FileName=%s, LineNo=%d, BlockSize=%u, MallocCount=%u}",
it->first.c_str(), pRecord->m_nLineNo, pRecord->m_nBlockSize, pRecord->m_nOperationCount);
nOffset = strlen(strMemBlockInfo);
}
}
}
m_stAllocMemRecordLock.leave();
sprintf(strMemBlockInfo + nOffset, "}");
nOffset = strlen(strMemBlockInfo);
//g_FrameLogEngine.WriteMainLog(enmLogLevel_Notice, "%s\n", strMemBlockInfo);
printf("%s\n", strMemBlockInfo);
//内存释放记录
nOffset = 0;
memset(strMemBlockInfo, 0, sizeof(strMemBlockInfo));
sprintf(strMemBlockInfo + nOffset, "frame mem free record : {");
nOffset = strlen(strMemBlockInfo);
m_stRecycleMemRecordLock.enter();
for(MemRecordMap::iterator it = m_stRecycleMemRecordMap.begin(); it != m_stRecycleMemRecordMap.end(); it++)
{
LineNoMap *pLineNoMap = it->second;
for(LineNoMap::iterator st = pLineNoMap->begin(); st != pLineNoMap->end(); st++)
{
BlockSizeMap *pBlockSizeMap = st->second;
for(BlockSizeMap::iterator pt = pBlockSizeMap->begin(); pt != pBlockSizeMap->end(); pt++)
{
MemOperationRecord *pRecord = pt->second;
if(pRecord == NULL)
{
continue;
}
sprintf(strMemBlockInfo + nOffset, "{FileName=%s, LineNo=%d, BlockSize=%u, FreeCount=%u}",
it->first.c_str(), pRecord->m_nLineNo, pRecord->m_nBlockSize, pRecord->m_nOperationCount);
nOffset = strlen(strMemBlockInfo);
}
}
}
m_stRecycleMemRecordLock.leave();
sprintf(strMemBlockInfo + nOffset, "}");
nOffset = strlen(strMemBlockInfo);
//g_FrameLogEngine.WriteMainLog(enmLogLevel_Notice, "%s\n", strMemBlockInfo);
printf("%s\n", strMemBlockInfo);
}
int32_t CFrameMemMgt::GetBlockSize(int32_t nWantSize)
{
int32_t nBlockSize = 0;
if(nWantSize % MAM_BLOCK_SIZE_STEP == 0)
{
nBlockSize = nWantSize;
}
else
{
nBlockSize = ((nWantSize / MAM_BLOCK_SIZE_STEP) + 1) * MAM_BLOCK_SIZE_STEP;
}
return nBlockSize;
}
//统计申请信息
void CFrameMemMgt::RecordAllocInfo(const char*pFileName, int32_t nLineNo, uint32_t nBlockSize)
{
if(pFileName == NULL)
{
return;
}
MUTEX_GUARD(AllocMemRecordLock, m_stAllocMemRecordLock);
//已经有文件记录
if(m_stAllocMemRecordMap.count(string(pFileName)) > 0)
{
LineNoMap *pLineNoMap = m_stAllocMemRecordMap[string(pFileName)];
BlockSizeMap *pBlockSizeMap = NULL;
MemOperationRecord *pRecord = NULL;
//获取行号和blocksize的映射
LineNoMap::iterator st = pLineNoMap->find(nLineNo);
//没有找到的话就创建一个映射
if(st == pLineNoMap->end())
{
pBlockSizeMap = new(nothrow) BlockSizeMap();
if(pBlockSizeMap == NULL)
{
return;
}
pLineNoMap->insert(make_pair(nLineNo, pBlockSizeMap));
}
//找到了还需要查看有没有blocksize和相应数据记录对象的映射
else
{
pBlockSizeMap = st->second;
//找到了blocksize对应的MemOperationRecord
if(pBlockSizeMap->count(nBlockSize) > 0)
{
pRecord = (*pBlockSizeMap)[nBlockSize];
}
}
//没有找到相应的record对象
if(pRecord == NULL)
{
pRecord = new(nothrow) MemOperationRecord();
if(pRecord == NULL)
{
return;
}
pRecord->m_nLineNo = nLineNo;
pRecord->m_nBlockSize = nBlockSize;
pBlockSizeMap->insert(make_pair(nBlockSize, pRecord));
}
++pRecord->m_nOperationCount;
}
//没有文件名到行号的映射就创建映射
else
{
LineNoMap *pLineNoMap = new(nothrow) LineNoMap;
if(pLineNoMap == NULL)
{
return;
}
BlockSizeMap *pBlockSizeMap = new(nothrow) BlockSizeMap();
if(pBlockSizeMap == NULL)
{
delete pLineNoMap;
return;
}
MemOperationRecord *pRecord = new(nothrow) MemOperationRecord();
if(pRecord == NULL)
{
delete pBlockSizeMap;
delete pLineNoMap;
return;
}
pRecord->m_nLineNo = nLineNo;
pRecord->m_nBlockSize = nBlockSize;
pRecord->m_nOperationCount = 1;
pBlockSizeMap->insert(make_pair(nBlockSize, pRecord));
pLineNoMap->insert(make_pair(nLineNo, pBlockSizeMap));
m_stAllocMemRecordMap[string(pFileName)] = pLineNoMap;
}
}
//统计释放信息
void CFrameMemMgt::RecordRecycleInfo(const char*pFileName, int32_t nLineNo, uint32_t nBlockSize)
{
if(pFileName == NULL)
{
return;
}
MUTEX_GUARD(RecycleMemRecordLock, m_stRecycleMemRecordLock);
//已经有文件记录
if(m_stRecycleMemRecordMap.count(string(pFileName)) > 0)
{
LineNoMap *pLineNoMap = m_stRecycleMemRecordMap[string(pFileName)];
BlockSizeMap *pBlockSizeMap = NULL;
MemOperationRecord *pRecord = NULL;
//获取行号和blocksize的映射
LineNoMap::iterator st = pLineNoMap->find(nLineNo);
//没有找到的话就创建一个映射
if(st == pLineNoMap->end())
{
pBlockSizeMap = new(nothrow) BlockSizeMap();
if(pBlockSizeMap == NULL)
{
return;
}
pLineNoMap->insert(make_pair(nLineNo, pBlockSizeMap));
}
//找到了还需要查看有没有blocksize和相应数据记录对象的映射
else
{
pBlockSizeMap = st->second;
//找到了blocksize对应的MemOperationRecord
if(pBlockSizeMap->count(nBlockSize) > 0)
{
pRecord = (*pBlockSizeMap)[nBlockSize];
}
}
//没有找到相应的record对象
if(pRecord == NULL)
{
pRecord = new(nothrow) MemOperationRecord();
if(pRecord == NULL)
{
return;
}
pRecord->m_nLineNo = nLineNo;
pRecord->m_nBlockSize = nBlockSize;
pBlockSizeMap->insert(make_pair(nBlockSize, pRecord));
}
++pRecord->m_nOperationCount;
}
//没有文件名到行号的映射就创建映射
else
{
LineNoMap *pLineNoMap = new(nothrow) LineNoMap;
if(pLineNoMap == NULL)
{
return;
}
BlockSizeMap *pBlockSizeMap = new(nothrow) BlockSizeMap();
if(pBlockSizeMap == NULL)
{
delete pLineNoMap;
return;
}
MemOperationRecord *pRecord = new(nothrow) MemOperationRecord();
if(pRecord == NULL)
{
delete pBlockSizeMap;
delete pLineNoMap;
return;
}
pRecord->m_nLineNo = nLineNo;
pRecord->m_nBlockSize = nBlockSize;
pRecord->m_nOperationCount = 1;
pBlockSizeMap->insert(make_pair(nBlockSize, pRecord));
pLineNoMap->insert(make_pair(nLineNo, pBlockSizeMap));
m_stRecycleMemRecordMap[string(pFileName)] = pLineNoMap;
}
}
//获取最大内存块大小
uint32_t CFrameMemMgt::GetMaxBlockSize()
{
return m_nMaxBlockSize;
}
int32_t CFrameMemMgt::MallocMemBlock(int32_t nBytes, int32_t nWantCount)
{
int32_t nIndex = GetTableIndexByBytes(nBytes);
if(nIndex <= 0)
{
return 0;
}
MemBlockInfo *pMemBlockInfo = GetMemBlockInfo(nIndex);
if(pMemBlockInfo == NULL)
{
pMemBlockInfo = CreateMemBlockInfo(nBytes);
if(pMemBlockInfo == NULL)
{
return 0;
}
}
// MUTEX_GUARD(MemBlockLock, pMemBlockInfo->m_stMemBlockLock);
int32_t nMallocCount = 0;
for(int32_t i = 0; i < nWantCount; ++i)
{
//前面8字节存放内存块信息
uint8_t *pMem = new(nothrow) uint8_t[pMemBlockInfo->m_nBlockSize + sizeof(MemBlockHead)];
if(pMem == NULL)
{
break;
}
MemBlockHead *pHead = new(pMem)MemBlockHead();
pHead->m_nIndex = nIndex;
pHead->m_nBlockSize = pMemBlockInfo->m_nBlockSize;
pHead->m_nReferCount = 0;
pMemBlockInfo->m_stMemBlockList.push_back(&pMem[sizeof(MemBlockHead)]);
++pMemBlockInfo->m_nBlockCount;
++nMallocCount;
}
return nMallocCount;
}
int32_t CFrameMemMgt::GetTableIndexByBytes(int32_t nBytes)
{
int32_t nMemTableIndex = 0;
int32_t nIndex = nBytes / MAM_BLOCK_SIZE_STEP;
if(nIndex < 0)
{
nMemTableIndex = -1;
}
else
{
if(nBytes % MAM_BLOCK_SIZE_STEP > 0)
{
nMemTableIndex = nIndex + 1;
}
else
{
nMemTableIndex = nIndex;
}
}
return nMemTableIndex;
}
MemBlockInfo *CFrameMemMgt::CreateMemBlockInfo(int32_t nBytes)
{
int32_t nIndex = GetTableIndexByBytes(nBytes);
if(nIndex <= 0)
{
return NULL;
}
MemBlockInfo *pMemBlockInfo = new(nothrow) MemBlockInfo();
if(pMemBlockInfo == NULL)
{
return NULL;
}
m_stMemInfoTable[nIndex] = pMemBlockInfo;
pMemBlockInfo->m_nBlockSize = nBytes;
//更新最大内存块大小
if((uint32_t)nBytes > m_nMaxBlockSize)
{
m_nMaxBlockSize = (uint32_t)nBytes;
}
return pMemBlockInfo;
}
MemBlockInfo *CFrameMemMgt::GetMemBlockInfo(int32_t nIndex)
{
if(m_stMemInfoTable.count(nIndex) <= 0)
{
return NULL;
}
return m_stMemInfoTable[nIndex];
}
//增加引用计数
int32_t IncReferCount(uint8_t *pMem)
{
if(pMem == NULL)
{
return -1;
}
if(!g_FrameMemMgt.HasBlkAddrRcd(pMem))
{
return -1;
}
MemBlockHead *pHead = (MemBlockHead *)(pMem - sizeof(MemBlockHead));
return ++pHead->m_nReferCount;
}
//减少引用计数
int32_t DecReferCount(uint8_t *pMem)
{
if(pMem == NULL)
{
return -1;
}
if(!g_FrameMemMgt.HasBlkAddrRcd(pMem))
{
return -1;
}
MemBlockHead *pHead = (MemBlockHead *)(pMem - sizeof(MemBlockHead));
return --pHead->m_nReferCount;
}
//获取引用计数
int32_t GetReferCount(uint8_t *pMem)
{
if(pMem == NULL)
{
return -1;
}
if(!g_FrameMemMgt.HasBlkAddrRcd(pMem))
{
return -1;
}
MemBlockHead *pHead = (MemBlockHead *)(pMem - sizeof(MemBlockHead));
return pHead->m_nReferCount;
}
#include <iostream>
using namespace std;
uint8_t* frame_malloc(uint32_t size, char *pFileName, int32_t nLineNo)
{
//cout << "malloc:" << pFileName << " line: " << nLineNo << endl << flush;
uint8_t *pMem = NULL;
if(size > MaxBlockSize)
{
pMem = new(nothrow) uint8_t[size];
g_FrameMemMgt.AddHeapAddrRcd(pMem);
}
else
{
int32_t nBlockSize = g_FrameMemMgt.GetBlockSize(size);
pMem = g_FrameMemMgt.AllocBlock(nBlockSize);
g_FrameMemMgt.AddBlkAddrRcd(pMem);
g_FrameMemMgt.RecordAllocInfo(pFileName, nLineNo, nBlockSize);
}
//cout << "malloc : " << hex << (unsigned)pMem << dec << " --- size=" << size << endl << flush;
return pMem;
}
void frame_free(void *addr, char *pFileName, int32_t nLineNo)
{
// cout << "@@@@free:" << pFileName << " line: " << nLineNo << endl << flush;
uint8_t *ptr = (uint8_t *)(addr);
if((ptr) != NULL)
{
//cout << "free : " << hex << (unsigned)addr << endl << flush;
if(g_FrameMemMgt.HasHeapAddrRcd(ptr))
{
g_FrameMemMgt.DelHeapAddrRcd(ptr);
}
else if(g_FrameMemMgt.HasBlkAddrRcd(ptr))
{
MemBlockHead *pHead = (MemBlockHead *)(ptr - sizeof(MemBlockHead));
g_FrameMemMgt.RecordRecycleInfo(pFileName, nLineNo, pHead->m_nBlockSize);
g_FrameMemMgt.DelBlkAddrRcd(ptr);
g_FrameMemMgt.RecycleBlock(ptr);
}
else
{
cout << "######: " << pFileName << nLineNo << endl << flush;
}
}
}
FRAME_NAMESPACE_END