2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ex/lookas.c
5 * PURPOSE: Lookaside lists
7 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
8 * David Welch (welch@mcmail.com)
9 * Casper S. Hornstrup (chorns@users.sourceforge.net)
12 /* INCLUDES *****************************************************************/
16 #include <internal/debug.h>
18 #if defined (ALLOC_PRAGMA)
19 #pragma alloc_text(INIT, ExpInitLookasideLists)
22 /* GLOBALS *******************************************************************/
24 LIST_ENTRY ExpNonPagedLookasideListHead
;
25 KSPIN_LOCK ExpNonPagedLookasideListLock
;
26 LIST_ENTRY ExpPagedLookasideListHead
;
27 KSPIN_LOCK ExpPagedLookasideListLock
;
29 /* FUNCTIONS *****************************************************************/
34 ExpInitLookasideLists()
36 /* Initialize Lock and Listhead */
37 InitializeListHead(&ExpNonPagedLookasideListHead
);
38 KeInitializeSpinLock(&ExpNonPagedLookasideListLock
);
39 InitializeListHead(&ExpPagedLookasideListHead
);
40 KeInitializeSpinLock(&ExpPagedLookasideListLock
);
48 ExiAllocateFromPagedLookasideList(IN PPAGED_LOOKASIDE_LIST Lookaside
)
52 Lookaside
->L
.TotalAllocates
++;
53 Entry
= InterlockedPopEntrySList(&Lookaside
->L
.ListHead
);
56 Lookaside
->L
.AllocateMisses
++;
57 Entry
= (Lookaside
->L
.Allocate
)(Lookaside
->L
.Type
,
69 ExiFreeToPagedLookasideList(IN PPAGED_LOOKASIDE_LIST Lookaside
,
72 Lookaside
->L
.TotalFrees
++;
73 if (ExQueryDepthSList(&Lookaside
->L
.ListHead
) >= Lookaside
->L
.Depth
)
75 Lookaside
->L
.FreeMisses
++;
76 (Lookaside
->L
.Free
)(Entry
);
80 InterlockedPushEntrySList(&Lookaside
->L
.ListHead
, (PSLIST_ENTRY
)Entry
);
89 ExDeleteNPagedLookasideList(PNPAGED_LOOKASIDE_LIST Lookaside
)
94 /* Pop all entries off the stack and release the resources allocated
98 Entry
= InterlockedPopEntrySList(&Lookaside
->L
.ListHead
);
100 (*Lookaside
->L
.Free
)(Entry
);
103 /* Remove from list */
104 KeAcquireSpinLock(&ExpNonPagedLookasideListLock
, &OldIrql
);
105 RemoveEntryList(&Lookaside
->L
.ListEntry
);
106 KeReleaseSpinLock(&ExpNonPagedLookasideListLock
, OldIrql
);
114 ExDeletePagedLookasideList(PPAGED_LOOKASIDE_LIST Lookaside
)
119 /* Pop all entries off the stack and release the resources allocated
123 Entry
= InterlockedPopEntrySList(&Lookaside
->L
.ListHead
);
125 (*Lookaside
->L
.Free
)(Entry
);
128 /* Remove from list */
129 KeAcquireSpinLock(&ExpPagedLookasideListLock
, &OldIrql
);
130 RemoveEntryList(&Lookaside
->L
.ListEntry
);
131 KeReleaseSpinLock(&ExpPagedLookasideListLock
, OldIrql
);
139 ExInitializeNPagedLookasideList(PNPAGED_LOOKASIDE_LIST Lookaside
,
140 PALLOCATE_FUNCTION Allocate
,
147 DPRINT("Initializing nonpaged lookaside list at 0x%p\n", Lookaside
);
149 /* Initialize the Header */
150 ExInitializeSListHead(&Lookaside
->L
.ListHead
);
151 Lookaside
->L
.TotalAllocates
= 0;
152 Lookaside
->L
.AllocateMisses
= 0;
153 Lookaside
->L
.TotalFrees
= 0;
154 Lookaside
->L
.FreeMisses
= 0;
155 Lookaside
->L
.Type
= NonPagedPool
| Flags
;
156 Lookaside
->L
.Tag
= Tag
;
157 Lookaside
->L
.Size
= Size
;
158 Lookaside
->L
.Depth
= 4;
159 Lookaside
->L
.MaximumDepth
= 256;
160 Lookaside
->L
.LastTotalAllocates
= 0;
161 Lookaside
->L
.LastAllocateMisses
= 0;
163 /* Set the Allocate/Free Routines */
166 Lookaside
->L
.Allocate
= Allocate
;
170 Lookaside
->L
.Allocate
= ExAllocatePoolWithTag
;
175 Lookaside
->L
.Free
= Free
;
179 Lookaside
->L
.Free
= ExFreePool
;
182 /* Insert it into the list */
183 ExInterlockedInsertTailList(&ExpNonPagedLookasideListHead
,
184 &Lookaside
->L
.ListEntry
,
185 &ExpNonPagedLookasideListLock
);
194 ExInitializePagedLookasideList (PPAGED_LOOKASIDE_LIST Lookaside
,
195 PALLOCATE_FUNCTION Allocate
,
202 DPRINT("Initializing paged lookaside list at 0x%p\n", Lookaside
);
204 /* Initialize the Header */
205 ExInitializeSListHead(&Lookaside
->L
.ListHead
);
206 Lookaside
->L
.TotalAllocates
= 0;
207 Lookaside
->L
.AllocateMisses
= 0;
208 Lookaside
->L
.TotalFrees
= 0;
209 Lookaside
->L
.FreeMisses
= 0;
210 Lookaside
->L
.Type
= PagedPool
| Flags
;
211 Lookaside
->L
.Tag
= Tag
;
212 Lookaside
->L
.Size
= Size
;
213 Lookaside
->L
.Depth
= 4;
214 Lookaside
->L
.MaximumDepth
= 256;
215 Lookaside
->L
.LastTotalAllocates
= 0;
216 Lookaside
->L
.LastAllocateMisses
= 0;
218 /* Set the Allocate/Free Routines */
221 Lookaside
->L
.Allocate
= Allocate
;
225 Lookaside
->L
.Allocate
= ExAllocatePoolWithTag
;
230 Lookaside
->L
.Free
= Free
;
234 Lookaside
->L
.Free
= ExFreePool
;
237 /* Insert it into the list */
238 ExInterlockedInsertTailList(&ExpNonPagedLookasideListHead
,
239 &Lookaside
->L
.ListEntry
,
240 &ExpNonPagedLookasideListLock
);