sl@0
|
1 |
// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
#include <hal.h>
|
sl@0
|
17 |
#include <fbs.h>
|
sl@0
|
18 |
#include <bitmap.h>
|
sl@0
|
19 |
#include "UTILS.H"
|
sl@0
|
20 |
|
sl@0
|
21 |
|
sl@0
|
22 |
GLREF_C void Panic(TFbsPanic aPanic);
|
sl@0
|
23 |
|
sl@0
|
24 |
LOCAL_C TInt GranularRoundUp(TInt aCount)
|
sl@0
|
25 |
{
|
sl@0
|
26 |
return (aCount + 7) & ~7;
|
sl@0
|
27 |
}
|
sl@0
|
28 |
|
sl@0
|
29 |
CChunkPile::CChunkPile(const RChunk& aChunk)
|
sl@0
|
30 |
: iChunk(aChunk)
|
sl@0
|
31 |
{}
|
sl@0
|
32 |
|
sl@0
|
33 |
EXPORT_C CChunkPile::~CChunkPile()
|
sl@0
|
34 |
{
|
sl@0
|
35 |
iChunk.Close();
|
sl@0
|
36 |
iSmallCells.Close();
|
sl@0
|
37 |
iFreeSmallCellLinks.Close();
|
sl@0
|
38 |
iLargeCells.Close();
|
sl@0
|
39 |
iFreeLargeCellLinks.Close();
|
sl@0
|
40 |
iLock.Close();
|
sl@0
|
41 |
}
|
sl@0
|
42 |
|
sl@0
|
43 |
EXPORT_C CChunkPile* CChunkPile::NewL(const RChunk& aChunk)
|
sl@0
|
44 |
{
|
sl@0
|
45 |
CChunkPile* thisptr=new(ELeave) CChunkPile(aChunk);
|
sl@0
|
46 |
CleanupStack::PushL(thisptr);
|
sl@0
|
47 |
thisptr->ConstructL();
|
sl@0
|
48 |
CleanupStack::Pop();
|
sl@0
|
49 |
return(thisptr);
|
sl@0
|
50 |
}
|
sl@0
|
51 |
|
sl@0
|
52 |
void CChunkPile::ConstructL()
|
sl@0
|
53 |
{
|
sl@0
|
54 |
TInt ret = iChunk.Duplicate(RThread());
|
sl@0
|
55 |
__ASSERT_ALWAYS(ret == KErrNone, Panic(EFbsPanicChunkError));
|
sl@0
|
56 |
HAL::Get(HAL::EMemoryPageSize, iPageSize);
|
sl@0
|
57 |
__ASSERT_ALWAYS(iPageSize > 4, Panic(EFbsPanicChunkError));
|
sl@0
|
58 |
iPageMask = iPageSize - 1; // assume page size is a power of 2
|
sl@0
|
59 |
__ASSERT_ALWAYS(KFbServLargeChunkMinPhysicalSize > 0, Panic(EFbsPanicChunkError));
|
sl@0
|
60 |
User::LeaveIfError(iChunk.Commit(iPageSize, KFbServLargeChunkMinPhysicalSize));
|
sl@0
|
61 |
iSmallCells.AppendL(iChunk.Base() + iPageSize); // prevent a data offset of 0 from chunk base
|
sl@0
|
62 |
iSmallCells.AppendL(iChunk.Base() + iPageSize + KFbServLargeChunkMinPhysicalSize);
|
sl@0
|
63 |
iFreeSmallCellLinks.AppendL(0);
|
sl@0
|
64 |
iLargeSectionBottom = iChunk.MaxSize() >> KFbServLargeChunkSizeShifter;
|
sl@0
|
65 |
iLargeCells.AppendL(iChunk.Base() + iLargeSectionBottom);
|
sl@0
|
66 |
iLargeCells.AppendL(iChunk.Base() + iChunk.MaxSize());
|
sl@0
|
67 |
iFreeLargeCellLinks.AppendL(0);
|
sl@0
|
68 |
User::LeaveIfError(iLock.CreateLocal());
|
sl@0
|
69 |
}
|
sl@0
|
70 |
|
sl@0
|
71 |
EXPORT_C TUint8* CChunkPile::ChunkBase() const
|
sl@0
|
72 |
{
|
sl@0
|
73 |
return(iChunk.Base());
|
sl@0
|
74 |
}
|
sl@0
|
75 |
|
sl@0
|
76 |
EXPORT_C TInt CChunkPile::VirtualSize()
|
sl@0
|
77 |
{
|
sl@0
|
78 |
TInt maxmem = 0;
|
sl@0
|
79 |
HAL::Get(HALData::EMemoryRAM, maxmem);
|
sl@0
|
80 |
ASSERT(maxmem > 0);
|
sl@0
|
81 |
return Min(Max(KFbServLargeChunkMinVirtualSize, maxmem << KFbServLargeChunkSizeShifter), KFbServLargeChunkMaxVirtualSize);
|
sl@0
|
82 |
}
|
sl@0
|
83 |
|
sl@0
|
84 |
EXPORT_C TUint8* CChunkPile::Alloc(TInt aSize)
|
sl@0
|
85 |
{
|
sl@0
|
86 |
__ASSERT_ALWAYS(aSize > 0 && aSize < KMaxTInt / 2, Panic(EFbsPanicChunkError));
|
sl@0
|
87 |
TUint8* cell;
|
sl@0
|
88 |
iLock.Wait();
|
sl@0
|
89 |
if (aSize < KMaxLargeBitmapAlloc)
|
sl@0
|
90 |
{
|
sl@0
|
91 |
aSize = Align4(aSize); // round size up to nearest four bytes
|
sl@0
|
92 |
TInt err = DoAlloc(cell, aSize, iSmallCells, iFreeSmallCellLinks, EFalse);
|
sl@0
|
93 |
if (err != KErrNone && cell != NULL)
|
sl@0
|
94 |
{
|
sl@0
|
95 |
// OOM after the small section grew? if so decommit as much physical memory as possible
|
sl@0
|
96 |
TInt topIndex = iSmallCells.Count() - 1;
|
sl@0
|
97 |
if (cell == iSmallCells[topIndex - 1])
|
sl@0
|
98 |
ShrinkSmallSection((iSmallCells[topIndex] - cell) & ~iPageMask);
|
sl@0
|
99 |
cell = NULL;
|
sl@0
|
100 |
}
|
sl@0
|
101 |
}
|
sl@0
|
102 |
else
|
sl@0
|
103 |
{
|
sl@0
|
104 |
aSize = (aSize + iPageMask) & ~iPageMask; // round size up to nearest page boundary
|
sl@0
|
105 |
TInt err = DoAlloc(cell, aSize, iLargeCells, iFreeLargeCellLinks, ETrue);
|
sl@0
|
106 |
if (err != KErrNone && cell != NULL)
|
sl@0
|
107 |
{
|
sl@0
|
108 |
iChunk.Decommit(cell - iChunk.Base(), aSize);
|
sl@0
|
109 |
cell = NULL;
|
sl@0
|
110 |
}
|
sl@0
|
111 |
}
|
sl@0
|
112 |
iLock.Signal();
|
sl@0
|
113 |
return cell;
|
sl@0
|
114 |
}
|
sl@0
|
115 |
|
sl@0
|
116 |
EXPORT_C void CChunkPile::Free(TAny* aCell)
|
sl@0
|
117 |
{
|
sl@0
|
118 |
iLock.Wait();
|
sl@0
|
119 |
if (static_cast<TUint8*>(aCell) < iChunk.Base() + iLargeSectionBottom)
|
sl@0
|
120 |
DoFree(static_cast<TUint8*>(aCell), iSmallCells, iFreeSmallCellLinks, EFalse);
|
sl@0
|
121 |
else
|
sl@0
|
122 |
DoFree(static_cast<TUint8*>(aCell), iLargeCells, iFreeLargeCellLinks, ETrue);
|
sl@0
|
123 |
iLock.Signal();
|
sl@0
|
124 |
}
|
sl@0
|
125 |
|
sl@0
|
126 |
TInt CChunkPile::DoAlloc(TUint8*& aCell, TInt aSize, RPointerArray<TUint8>& aCells, RArray<TInt>& aFreeCellLinks, TBool aLarge)
|
sl@0
|
127 |
/*
|
sl@0
|
128 |
This function tries to allocate a cell of aSize bytes and returns its address in aCell if successful.
|
sl@0
|
129 |
In case of failure aCell is either NULL, if there is not enough memory for a cell of that size,
|
sl@0
|
130 |
or it contains the address of a free cell of at least aSize bytes,
|
sl@0
|
131 |
if there is not enough memory for the internal structures used by CChunkPile.
|
sl@0
|
132 |
*/
|
sl@0
|
133 |
{
|
sl@0
|
134 |
__ASSERT_DEBUG(aCells.Count() >= 2, Panic(EFbsPanicChunkError));
|
sl@0
|
135 |
aCell = NULL;
|
sl@0
|
136 |
TInt cellIndex = 0;
|
sl@0
|
137 |
TInt freeCount = aFreeCellLinks.Count();
|
sl@0
|
138 |
for (TInt freeIndex = 0; freeIndex < freeCount; ++freeIndex)
|
sl@0
|
139 |
{
|
sl@0
|
140 |
TInt freeCellLink = aFreeCellLinks[freeIndex];
|
sl@0
|
141 |
cellIndex += freeCellLink;
|
sl@0
|
142 |
TInt size = aCells[cellIndex + 1] - aCells[cellIndex];
|
sl@0
|
143 |
if (size >= aSize)
|
sl@0
|
144 |
{
|
sl@0
|
145 |
if (aLarge)
|
sl@0
|
146 |
{
|
sl@0
|
147 |
TInt err = iChunk.Commit(aCells[cellIndex] - iChunk.Base(), aSize);
|
sl@0
|
148 |
if (err != KErrNone)
|
sl@0
|
149 |
return err;
|
sl@0
|
150 |
}
|
sl@0
|
151 |
aCell = aCells[cellIndex];
|
sl@0
|
152 |
if (size == aSize) // exact fit?
|
sl@0
|
153 |
{
|
sl@0
|
154 |
aFreeCellLinks.Remove(freeIndex);
|
sl@0
|
155 |
if (freeIndex < aFreeCellLinks.Count())
|
sl@0
|
156 |
aFreeCellLinks[freeIndex] += freeCellLink;
|
sl@0
|
157 |
}
|
sl@0
|
158 |
else
|
sl@0
|
159 |
{
|
sl@0
|
160 |
// make sure there are always enough empty slots in aFreeCellLinks
|
sl@0
|
161 |
TInt err = aFreeCellLinks.Reserve(GranularRoundUp((aCells.Count() + 1) >> 1));
|
sl@0
|
162 |
if (err != KErrNone)
|
sl@0
|
163 |
return err;
|
sl@0
|
164 |
err = aCells.Insert(aCell + aSize, cellIndex + 1);
|
sl@0
|
165 |
if (err != KErrNone)
|
sl@0
|
166 |
return err;
|
sl@0
|
167 |
++aFreeCellLinks[freeIndex];
|
sl@0
|
168 |
}
|
sl@0
|
169 |
return KErrNone;
|
sl@0
|
170 |
}
|
sl@0
|
171 |
}
|
sl@0
|
172 |
if (aLarge)
|
sl@0
|
173 |
return KErrNoMemory;
|
sl@0
|
174 |
TInt err = GrowSmallSection(aSize, cellIndex);
|
sl@0
|
175 |
if (err != KErrNone)
|
sl@0
|
176 |
return err;
|
sl@0
|
177 |
TInt topIndex = iSmallCells.Count() - 1;
|
sl@0
|
178 |
aCell = iSmallCells[topIndex - 1];
|
sl@0
|
179 |
if (iSmallCells[topIndex] - aCell == aSize) // exact fit?
|
sl@0
|
180 |
{
|
sl@0
|
181 |
iFreeSmallCellLinks.Remove(iFreeSmallCellLinks.Count() - 1);
|
sl@0
|
182 |
}
|
sl@0
|
183 |
else
|
sl@0
|
184 |
{
|
sl@0
|
185 |
// make sure there are always enough empty slots in aFreeCellLinks
|
sl@0
|
186 |
err = iFreeSmallCellLinks.Reserve(GranularRoundUp((iSmallCells.Count() + 1) >> 1));
|
sl@0
|
187 |
if (err != KErrNone)
|
sl@0
|
188 |
return err;
|
sl@0
|
189 |
err = iSmallCells.Insert(aCell + aSize, topIndex);
|
sl@0
|
190 |
if (err != KErrNone)
|
sl@0
|
191 |
return err;
|
sl@0
|
192 |
++iFreeSmallCellLinks[iFreeSmallCellLinks.Count() - 1];
|
sl@0
|
193 |
}
|
sl@0
|
194 |
return KErrNone;
|
sl@0
|
195 |
}
|
sl@0
|
196 |
|
sl@0
|
197 |
void CChunkPile::DoFree(TUint8* aCell, RPointerArray<TUint8>& aCells, RArray<TInt>& aFreeCellLinks, TBool aLarge)
|
sl@0
|
198 |
{
|
sl@0
|
199 |
TInt cellIndex = aCells.FindInAddressOrder(aCell);
|
sl@0
|
200 |
__ASSERT_DEBUG(cellIndex >= 0 && cellIndex < aCells.Count() - 1, Panic(EFbsPanicChunkError));
|
sl@0
|
201 |
if (cellIndex < 0 || cellIndex == aCells.Count() - 1)
|
sl@0
|
202 |
return;
|
sl@0
|
203 |
TInt prevIndex = KMaxTInt;
|
sl@0
|
204 |
TInt nextIndex = 0;
|
sl@0
|
205 |
TInt freeCount = aFreeCellLinks.Count();
|
sl@0
|
206 |
TInt freeIndex;
|
sl@0
|
207 |
for (freeIndex = 0; freeIndex < freeCount; ++freeIndex)
|
sl@0
|
208 |
{
|
sl@0
|
209 |
nextIndex += aFreeCellLinks[freeIndex];
|
sl@0
|
210 |
if (nextIndex >= cellIndex)
|
sl@0
|
211 |
{
|
sl@0
|
212 |
__ASSERT_DEBUG(nextIndex > cellIndex, Panic(EFbsPanicChunkError));
|
sl@0
|
213 |
if (nextIndex == cellIndex)
|
sl@0
|
214 |
return;
|
sl@0
|
215 |
break;
|
sl@0
|
216 |
}
|
sl@0
|
217 |
prevIndex = nextIndex;
|
sl@0
|
218 |
}
|
sl@0
|
219 |
if (aLarge)
|
sl@0
|
220 |
iChunk.Decommit(aCell - iChunk.Base(), aCells[cellIndex + 1] - aCell);
|
sl@0
|
221 |
if (prevIndex == cellIndex - 1 && nextIndex == cellIndex + 1)
|
sl@0
|
222 |
{
|
sl@0
|
223 |
aFreeCellLinks.Remove(freeIndex);
|
sl@0
|
224 |
aCells.Remove(cellIndex + 1);
|
sl@0
|
225 |
aCells.Remove(cellIndex--);
|
sl@0
|
226 |
}
|
sl@0
|
227 |
else if (prevIndex == cellIndex - 1)
|
sl@0
|
228 |
{
|
sl@0
|
229 |
if (freeIndex < freeCount)
|
sl@0
|
230 |
--aFreeCellLinks[freeIndex];
|
sl@0
|
231 |
aCells.Remove(cellIndex--);
|
sl@0
|
232 |
}
|
sl@0
|
233 |
else if (nextIndex == cellIndex + 1)
|
sl@0
|
234 |
{
|
sl@0
|
235 |
--aFreeCellLinks[freeIndex];
|
sl@0
|
236 |
aCells.Remove(cellIndex + 1);
|
sl@0
|
237 |
}
|
sl@0
|
238 |
else
|
sl@0
|
239 |
{
|
sl@0
|
240 |
if (freeIndex < freeCount)
|
sl@0
|
241 |
aFreeCellLinks[freeIndex] = nextIndex - cellIndex;
|
sl@0
|
242 |
aFreeCellLinks.Insert(freeIndex > 0 ? cellIndex - prevIndex : cellIndex, freeIndex); // can't fail
|
sl@0
|
243 |
}
|
sl@0
|
244 |
if (aLarge)
|
sl@0
|
245 |
return;
|
sl@0
|
246 |
TInt topIndex = aCells.Count() - 1;
|
sl@0
|
247 |
if (cellIndex == topIndex - 1)
|
sl@0
|
248 |
{
|
sl@0
|
249 |
// last cell is free and has just grown, shrink with granularity iPageSize << KFbServLargeChunkGrowByShifter
|
sl@0
|
250 |
TInt roundMask = (iPageSize << KFbServLargeChunkGrowByShifter) - 1;
|
sl@0
|
251 |
ShrinkSmallSection((aCells[topIndex] - aCells[cellIndex]) & ~roundMask);
|
sl@0
|
252 |
}
|
sl@0
|
253 |
}
|
sl@0
|
254 |
|
sl@0
|
255 |
TInt CChunkPile::GrowSmallSection(TInt aSize, TInt aLastFreeCell)
|
sl@0
|
256 |
/*
|
sl@0
|
257 |
This function tries to grow the section for small cells with granularity iPageSize << KFbServLargeChunkGrowByShifter.
|
sl@0
|
258 |
It appends or expands the last free cell so that it has at least aSize bytes if successful.
|
sl@0
|
259 |
aLastFreeCell must be the index of the last free cell in the small section or 0 if there is none.
|
sl@0
|
260 |
*/
|
sl@0
|
261 |
{
|
sl@0
|
262 |
TInt roundMask = (iPageSize << KFbServLargeChunkGrowByShifter) - 1;
|
sl@0
|
263 |
TInt topIndex = iSmallCells.Count() - 1;
|
sl@0
|
264 |
TInt maxGrowBy = (iChunk.Base() + iLargeSectionBottom) - iSmallCells[topIndex];
|
sl@0
|
265 |
__ASSERT_DEBUG(maxGrowBy >= 0, Panic(EFbsPanicChunkError));
|
sl@0
|
266 |
if (iFreeSmallCellLinks.Count() > 0 && aLastFreeCell == topIndex - 1)
|
sl@0
|
267 |
{
|
sl@0
|
268 |
// last cell is free
|
sl@0
|
269 |
TInt growBy = (iSmallCells[aLastFreeCell] + aSize) - iSmallCells[topIndex];
|
sl@0
|
270 |
__ASSERT_DEBUG(growBy > 0, Panic(EFbsPanicChunkError));
|
sl@0
|
271 |
if (growBy > maxGrowBy)
|
sl@0
|
272 |
return KErrNoMemory;
|
sl@0
|
273 |
growBy = Min((growBy + roundMask) & ~roundMask, maxGrowBy);
|
sl@0
|
274 |
TInt err = iChunk.Commit(iSmallCells[topIndex] - iChunk.Base(), growBy);
|
sl@0
|
275 |
if (err != KErrNone)
|
sl@0
|
276 |
return err;
|
sl@0
|
277 |
iSmallCells[topIndex] += growBy;
|
sl@0
|
278 |
}
|
sl@0
|
279 |
else
|
sl@0
|
280 |
{
|
sl@0
|
281 |
// last cell is not free
|
sl@0
|
282 |
if (aSize > maxGrowBy)
|
sl@0
|
283 |
return KErrNoMemory;
|
sl@0
|
284 |
TInt growBy = Min((aSize + roundMask) & ~roundMask, maxGrowBy);
|
sl@0
|
285 |
TInt err = iSmallCells.Reserve(GranularRoundUp(iSmallCells.Count() + 1));
|
sl@0
|
286 |
if (err != KErrNone)
|
sl@0
|
287 |
return err;
|
sl@0
|
288 |
err = iFreeSmallCellLinks.Reserve(GranularRoundUp((iSmallCells.Count() + 1) >> 1));
|
sl@0
|
289 |
if (err != KErrNone)
|
sl@0
|
290 |
return err;
|
sl@0
|
291 |
err = iChunk.Commit(iSmallCells[topIndex] - iChunk.Base(), growBy);
|
sl@0
|
292 |
if (err != KErrNone)
|
sl@0
|
293 |
return err;
|
sl@0
|
294 |
iSmallCells.Append(iSmallCells[topIndex] + growBy); // can't fail
|
sl@0
|
295 |
iFreeSmallCellLinks.Append(topIndex - aLastFreeCell); // can't fail
|
sl@0
|
296 |
}
|
sl@0
|
297 |
return KErrNone;
|
sl@0
|
298 |
}
|
sl@0
|
299 |
|
sl@0
|
300 |
void CChunkPile::ShrinkSmallSection(TInt aShrinkBy)
|
sl@0
|
301 |
{
|
sl@0
|
302 |
TInt topIndex = iSmallCells.Count() - 1;
|
sl@0
|
303 |
TInt maxShrinkBy = (iSmallCells[topIndex] - iChunk.Base()) - (KFbServLargeChunkMinPhysicalSize + iPageSize);
|
sl@0
|
304 |
__ASSERT_DEBUG(maxShrinkBy >= 0, Panic(EFbsPanicChunkError));
|
sl@0
|
305 |
aShrinkBy = Min(aShrinkBy, maxShrinkBy);
|
sl@0
|
306 |
if (aShrinkBy > 0)
|
sl@0
|
307 |
{
|
sl@0
|
308 |
iChunk.Decommit((iSmallCells[topIndex] -= aShrinkBy) - iChunk.Base(), aShrinkBy);
|
sl@0
|
309 |
if (iSmallCells[topIndex] == iSmallCells[topIndex - 1])
|
sl@0
|
310 |
{
|
sl@0
|
311 |
iSmallCells.Remove(topIndex);
|
sl@0
|
312 |
iFreeSmallCellLinks.Remove(iFreeSmallCellLinks.Count() - 1);
|
sl@0
|
313 |
}
|
sl@0
|
314 |
}
|
sl@0
|
315 |
}
|