|
1 /* |
|
2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * |
|
17 */ |
|
18 #include "FastAllocator.h" |
|
19 #include "MemoryManager.h" |
|
20 #include "MemoryPool.h" |
|
21 |
|
22 EXPORT_C RFastAllocator::RFastAllocator(CFastMemoryPool* aPool) : iHeap( User::Heap() ), iPool( aPool ) |
|
23 { |
|
24 iHeapBase = (TUint32)iHeap.Base() & 0xffff0000; |
|
25 } |
|
26 |
|
27 EXPORT_C RFastAllocator::~RFastAllocator() |
|
28 { |
|
29 } |
|
30 |
|
31 |
|
32 EXPORT_C TAny* RFastAllocator::Alloc(TInt aSize) |
|
33 { |
|
34 return iPool->Allocate( aSize ); |
|
35 } |
|
36 |
|
37 EXPORT_C void RFastAllocator::Free(TAny* aPtr) |
|
38 { |
|
39 // make sure we go to the right memory pool |
|
40 if( IsLocatedInHeap( aPtr ) ) |
|
41 iHeap.Free( aPtr ); |
|
42 else |
|
43 iPool->Free( aPtr ); |
|
44 } |
|
45 |
|
46 EXPORT_C TAny* RFastAllocator::ReAlloc(TAny* aPtr, TInt aSize, TInt aMode) |
|
47 { |
|
48 TAny* p(NULL); |
|
49 TInt originalSize(0); |
|
50 |
|
51 // check the right memory pool |
|
52 if( IsLocatedInHeap( aPtr ) ) |
|
53 { |
|
54 // allocate in memory manager's pool |
|
55 p = iPool->Allocate( aSize ); |
|
56 TInt cpSize = iHeap.AllocLen( aPtr ); |
|
57 originalSize = cpSize; |
|
58 cpSize = cpSize < aSize ? cpSize : aSize; |
|
59 memcpy( p, aPtr, cpSize ); |
|
60 iHeap.Free( aPtr ); |
|
61 } |
|
62 else |
|
63 { |
|
64 originalSize = iPool->MemorySize(aPtr); |
|
65 p = iPool->ReAllocate( aPtr, aSize ); |
|
66 } |
|
67 |
|
68 // Handle aMode. |
|
69 // ENeverMove: Don't move memory on realloc, return NULL if it was moved. |
|
70 // EAllowMoveOnShrink: Allow moving memory on realloc when size requested |
|
71 // is smaller, return NULL if moved and request size is greater (equal). |
|
72 if ( (aMode == ENeverMove && p != aPtr) || |
|
73 (aMode == EAllowMoveOnShrink && originalSize >= aSize && p != aPtr) ) |
|
74 { |
|
75 Free(p); |
|
76 return NULL; |
|
77 } |
|
78 |
|
79 return p; |
|
80 } |
|
81 |
|
82 EXPORT_C TInt RFastAllocator::AllocLen(const TAny* aCell) const |
|
83 { |
|
84 return iPool->MemorySize( (void*)(aCell) ); |
|
85 } |
|
86 |
|
87 EXPORT_C TInt RFastAllocator::Compress() |
|
88 { |
|
89 // DO NOTHING |
|
90 return 0; |
|
91 } |
|
92 |
|
93 EXPORT_C void RFastAllocator::Reset() |
|
94 { |
|
95 // DO NOTHING |
|
96 } |
|
97 |
|
98 EXPORT_C TInt RFastAllocator::AllocSize(TInt& aTotalAllocSize) const |
|
99 { |
|
100 // allocated cell size |
|
101 aTotalAllocSize = 0; |
|
102 return 0; |
|
103 } |
|
104 |
|
105 EXPORT_C TInt RFastAllocator::Available(TInt& aBiggestBlock) const |
|
106 { |
|
107 // make sure the caller is not scared of futher allocation |
|
108 aBiggestBlock = KMaxTInt; |
|
109 return KMaxTUint; |
|
110 } |
|
111 |
|
112 EXPORT_C TInt RFastAllocator::DebugFunction(TInt, TAny*, TAny*) |
|
113 { |
|
114 return 0; |
|
115 } |
|
116 |
|
117 EXPORT_C TInt RFastAllocator::Extension_(TUint, TAny*&, TAny*) |
|
118 { |
|
119 return 0; |
|
120 } |
|
121 |
|
122 TBool RFastAllocator::IsLocatedInHeap( TAny* aPtr ) const |
|
123 { |
|
124 // Quick check first, the initial heap size is usually defined as |
|
125 // 0x5000. For memory cell allocated in this heap, it's address |
|
126 // share the same upper 16 bits with heap base address. |
|
127 if( ((TUint32)aPtr & 0xffff0000) != iHeapBase ) return EFalse; |
|
128 |
|
129 // very rare case, check it anyway |
|
130 TUint32 top = iHeapBase + iHeap.Size(); |
|
131 return ( (TUint32)aPtr >= iHeapBase && (TUint32)aPtr <= top ); |
|
132 } |
|
133 |
|
134 // END OF FILE |