13 * |
13 * |
14 * Description: |
14 * Description: |
15 * |
15 * |
16 * |
16 * |
17 */ |
17 */ |
|
18 |
18 #include "FastAllocator.h" |
19 #include "FastAllocator.h" |
19 #include "MemoryManager.h" |
20 #include "MemoryManager.h" |
20 #include "MemoryPool.h" |
21 #include "MemoryPool.h" |
21 |
22 #include <e32std.h> |
|
23 #include <e32debug.h> |
|
24 |
|
25 |
|
26 #ifdef TRACK_ALLOCATIONS |
|
27 TAny* GetClientPtrFromRealPtr(TAny* aRealPtr) |
|
28 { |
|
29 return (TAny*)(((TUint8*)aRealPtr) + sizeof(THeapTrace)); |
|
30 } |
|
31 TAny* GetRealPtrFromClientPtr(TAny* aClientPtr) |
|
32 { |
|
33 return (TAny*)(((TUint8*)aClientPtr) - sizeof(THeapTrace)); |
|
34 } |
|
35 |
|
36 void IncreaseSizeInPlace(TInt& aSize) |
|
37 { |
|
38 aSize += sizeof(THeapTrace); |
|
39 } |
|
40 void DecreaseSizeInPlace(TInt& aSize) |
|
41 { |
|
42 aSize -= sizeof(THeapTrace); |
|
43 } |
|
44 |
|
45 TInt GetSizeFromRealPtr(TAny* aRealPtr) |
|
46 { |
|
47 return ((THeapTrace*)aRealPtr)->requestedSize; |
|
48 } |
|
49 |
|
50 void SetSizeWithRealPtr(TAny *aRealPtr, TInt aSize) |
|
51 { |
|
52 ((THeapTrace*)aRealPtr)->requestedSize = aSize; |
|
53 } |
|
54 |
|
55 TUint32 GetCellIdWithRealPtr(TAny* aRealPtr) |
|
56 { |
|
57 return((THeapTrace*)aRealPtr)->cellId; |
|
58 } |
|
59 |
|
60 void SetCellIdWithRealPtr(TAny* aRealPtr, TUint32 aCellId) |
|
61 { |
|
62 ((THeapTrace*)aRealPtr)->cellId = aCellId; |
|
63 } |
|
64 void IncrementAndSetCellIdWithRealPtr(TAny* aRealPtr, TUint32* aCellId) |
|
65 { |
|
66 ((THeapTrace*)aRealPtr)->cellId = (*aCellId)++; |
|
67 } |
|
68 |
|
69 |
|
70 TAny* TraceAlloc(CMemoryPool* aPool, TInt* aSize, TUint32* aCellId) |
|
71 { |
|
72 // add space at the start of the data area for a TInt tracking requested size |
|
73 TInt requestedSize = *aSize; |
|
74 IncreaseSizeInPlace(*aSize); |
|
75 TAny *ptr = aPool->Allocate( *aSize ); |
|
76 SetSizeWithRealPtr(ptr, requestedSize); |
|
77 IncrementAndSetCellIdWithRealPtr(ptr, aCellId); |
|
78 |
|
79 RDebug::Print(_L("MEM: a,%d,,%d"), GetCellIdWithRealPtr(ptr), requestedSize); |
|
80 |
|
81 // somewhere to add a debugger to catch every 1024 allocations |
|
82 if((*aCellId & 0x0FFF) == 0) |
|
83 { |
|
84 aCellId = aCellId; |
|
85 } |
|
86 |
|
87 return GetClientPtrFromRealPtr(ptr); |
|
88 } |
|
89 |
|
90 void TraceFree(TAny **aPtr) |
|
91 { |
|
92 if(aPtr && *aPtr) |
|
93 { |
|
94 *aPtr = GetRealPtrFromClientPtr(*aPtr); |
|
95 RDebug::Print(_L("MEM: f,%d,%d,"), GetCellIdWithRealPtr(*aPtr), GetSizeFromRealPtr(*aPtr)); |
|
96 } |
|
97 } |
|
98 |
|
99 TBool TracePreRealloc(TAny** aPtr, TInt *aSize, TUint32& aOriginalCellId) |
|
100 { |
|
101 TInt requestedSize = *aSize; |
|
102 TBool issueNewCellId = EFalse; |
|
103 aOriginalCellId=0; |
|
104 if(aPtr && *aPtr) |
|
105 { |
|
106 *aPtr = GetRealPtrFromClientPtr(*aPtr); |
|
107 aOriginalCellId = GetCellIdWithRealPtr(*aPtr); |
|
108 RDebug::Print(_L("MEM: r,%d,%d,%d"), GetCellIdWithRealPtr(*aPtr), GetSizeFromRealPtr(*aPtr), requestedSize); |
|
109 } |
|
110 else |
|
111 { |
|
112 issueNewCellId = ETrue; |
|
113 } |
|
114 IncreaseSizeInPlace(*aSize); |
|
115 |
|
116 return issueNewCellId; |
|
117 } |
|
118 |
|
119 void TracePostRealloc(TAny** p, TInt aRequestedSize, TUint32* aNewCellId, TUint32 aOriginalCellId, TBool aIssueNewCellId) |
|
120 { |
|
121 if(p && *p) |
|
122 { |
|
123 SetSizeWithRealPtr(*p, aRequestedSize); |
|
124 if(aIssueNewCellId) |
|
125 IncrementAndSetCellIdWithRealPtr(*p, aNewCellId); |
|
126 else |
|
127 SetCellIdWithRealPtr(*p, aOriginalCellId); |
|
128 *p = GetClientPtrFromRealPtr(*p); |
|
129 } |
|
130 } |
|
131 #endif |
22 EXPORT_C RFastAllocator::RFastAllocator(CFastMemoryPool* aPool) : iHeap( User::Heap() ), iPool( aPool ) |
132 EXPORT_C RFastAllocator::RFastAllocator(CFastMemoryPool* aPool) : iHeap( User::Heap() ), iPool( aPool ) |
23 { |
133 { |
24 iHeapBase = (TUint32)iHeap.Base() & 0xffff0000; |
134 iHeapBase = (TUint32)iHeap.Base() & 0xffff0000; |
|
135 #ifdef TRACK_ALLOCATIONS |
|
136 iNextCellId = 0; |
|
137 #endif |
25 } |
138 } |
26 |
139 |
27 EXPORT_C RFastAllocator::~RFastAllocator() |
140 EXPORT_C RFastAllocator::~RFastAllocator() |
28 { |
141 { |
29 } |
142 } |
30 |
143 |
31 |
144 |
32 EXPORT_C TAny* RFastAllocator::Alloc(TInt aSize) |
145 EXPORT_C TAny* RFastAllocator::Alloc(TInt aSize) |
33 { |
146 { |
|
147 #ifdef TRACK_ALLOCATIONS |
|
148 return TraceAlloc(iPool, &aSize, &iNextCellId); |
|
149 #else |
34 return iPool->Allocate( aSize ); |
150 return iPool->Allocate( aSize ); |
|
151 #endif |
35 } |
152 } |
36 |
153 |
37 EXPORT_C void RFastAllocator::Free(TAny* aPtr) |
154 EXPORT_C void RFastAllocator::Free(TAny* aPtr) |
38 { |
155 { |
|
156 #ifdef TRACK_ALLOCATIONS |
|
157 TraceFree(&aPtr); |
|
158 #endif |
39 // make sure we go to the right memory pool |
159 // make sure we go to the right memory pool |
40 if( IsLocatedInHeap( aPtr ) ) |
160 if( IsLocatedInHeap( aPtr ) ) |
41 iHeap.Free( aPtr ); |
161 iHeap.Free( aPtr ); |
42 else |
162 else |
43 iPool->Free( aPtr ); |
163 iPool->Free( aPtr ); |
44 } |
164 } |
45 |
165 |
46 EXPORT_C TAny* RFastAllocator::ReAlloc(TAny* aPtr, TInt aSize, TInt aMode) |
166 EXPORT_C TAny* RFastAllocator::ReAlloc(TAny* aPtr, TInt aSize, TInt aMode) |
47 { |
167 { |
|
168 #ifdef TRACK_ALLOCATIONS |
|
169 TUint32 originalCellId; |
|
170 TBool issueNewCellId = TracePreRealloc(&aPtr, &aSize, originalCellId); |
|
171 #endif |
48 TAny* p(NULL); |
172 TAny* p(NULL); |
49 TInt originalSize(0); |
173 TInt originalSize(0); |
50 |
174 |
51 // check the right memory pool |
175 // check the right memory pool |
52 if( IsLocatedInHeap( aPtr ) ) |
176 if( IsLocatedInHeap( aPtr ) ) |