Update contrib.
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
29 class RSlabAllocatorBase
32 RSlabAllocatorBase(TBool aDelayedCleanup);
33 ~RSlabAllocatorBase();
36 Construct a slab allocator.
38 @param aMaxSlabs Maximum number of slabs to use. (Number of bits in \a slabBits.)
39 @param aObjectSize Size of objects to allocate.
41 TInt Construct(TUint aMaxSlabs, TUint aObjectSize);
44 Construct a slab allocator using fixed virtual memory.
46 @param aMaxSlabs Maximum number of slabs to use. (Number of bits in \a slabBits.)
47 @param aObjectSize Size of objects to allocate.
48 @param aBase Virtual address for start of memory where slabs will be allocated.
49 Zero indicates 'anywhere'.
51 TInt Construct(TUint aMaxSlabs, TUint aObjectSize, TLinAddr aBase);
54 Set the memory object to be used for the slab allocator.
56 FORCE_INLINE void SetMemory(DMemoryObject* aMemory, TUint aReserveCount)
58 __NK_ASSERT_DEBUG(!iMemory);
60 iReserveCount = aReserveCount;
66 @return Allocated object, or the null pointer if there is insufficient memory to perform the allocation.
69 @post MmuLock held, but has always beet flashed
74 Free an object previously allocated with #Alloc.
76 @param aObject The object.
79 @post MmuLock held, but has always beet flashed
81 void Free(TAny* aObject);
84 class TSlabHeader : public SDblQueLink
87 SDblQue iFreeList; ///< List of unallocated objects in list.
88 TUint iAllocCount; ///< Number of objects allocated in this slab.
89 TAny* iHighWaterMark; ///< End of initialise region in slab.
94 void InitSlab(TLinAddr aPage);
95 void FreeSlab(TSlabHeader* aSlab);
96 static void CleanupTrampoline(TAny* aSelf);
99 void CheckSlab(TSlabHeader* aSlab);
103 SDblQue iFreeList; ///< List of slabs which have unallocated objects in them.
104 TUint iFreeCount; ///< Number of unallocated objects.
105 TUint iReserveCount; ///< Number of unallocated objects to keep in reserve (to allow for recursion during allocation).
106 TUint iObjectsPerSlab; ///< Number of objects in each slab.
107 TUint iObjectSize; ///< Size, in bytes, of objects to be allocated.
108 TSpinLock iSpinLock; ///< Spinlock which protects iFreeList, iFreeCount and TSlabHeader contents.
110 TMemoryCleanup iCleanup; ///< Used to queue Cleanup() if iDelayedCleanup is true.
111 TBool iDelayedCleanup; ///< True, if Free() should not free empty slabs.
113 TBool iAllocatingSlab; ///< True if a new slab page is being allocated.
114 TBitMapAllocator* iSlabMap; ///< Bitmap of allocated slabs.
115 DMemoryObject* iMemory; ///< The memory object used to store slabs.
116 DMemoryMapping* iMapping; ///< The memory mapping used for slabs.
117 TLinAddr iBase; ///< Address of first slab.
122 Template for a slab allocator which can allocate up to N objects of type T.
124 template <class T, TUint N>
125 class RSlabAllocator : public RSlabAllocatorBase
130 EObjectSize = sizeof(T)>sizeof(SDblQueLink) ? sizeof(T) : sizeof(SDblQueLink),
131 EObjectsPerSlab = (KPageSize-sizeof(TSlabHeader))/EObjectSize,
132 EMaxSlabs = (N+EObjectsPerSlab-1)/EObjectsPerSlab
135 FORCE_INLINE RSlabAllocator()
136 : RSlabAllocatorBase(EFalse)
138 __ASSERT_COMPILE(EObjectsPerSlab>0);
141 FORCE_INLINE TInt Construct()
143 return RSlabAllocatorBase::Construct(EMaxSlabs,EObjectSize);
149 @return Allocated object, or the null pointer if there is insufficient memory to perform the allocation.
151 FORCE_INLINE T* Alloc()
153 return (T*)RSlabAllocatorBase::Alloc();
157 Free an object previously allocated with #Alloc.
159 @param aObject Object.
161 FORCE_INLINE void Free(T* aObject)
163 RSlabAllocatorBase::Free(aObject);
169 Template for a slab allocator for allocating objects of type T, using the
170 virtual address region [B..E)
172 template <class T, TLinAddr B, TLinAddr E>
173 class RStaticSlabAllocator : public RSlabAllocatorBase
178 EObjectSize = sizeof(T)>sizeof(SDblQueLink) ? sizeof(T) : sizeof(SDblQueLink),
179 EObjectsPerSlab = (KPageSize-sizeof(TSlabHeader))/EObjectSize,
180 EMaxSlabs = (E-B)/KPageSize
183 FORCE_INLINE RStaticSlabAllocator()
184 : RSlabAllocatorBase(ETrue)
186 __ASSERT_COMPILE(EObjectsPerSlab>0);
189 FORCE_INLINE TInt Construct()
191 return RSlabAllocatorBase::Construct(EMaxSlabs,EObjectSize,B);
197 @return Allocated object, or the null pointer if there is insufficient memory to perform the allocation.
199 FORCE_INLINE T* Alloc()
201 return (T*)RSlabAllocatorBase::Alloc();
205 Free an object previously allocated with #Alloc.
207 @param aObject Object.
209 FORCE_INLINE void Free(T* aObject)
211 RSlabAllocatorBase::Free(aObject);