2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ex/lookas.c
5 * PURPOSE: Lookaside lists
6 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
7 * David Welch (welch@mcmail.com)
8 * Casper S. Hornstrup (chorns@users.sourceforge.net)
11 /* INCLUDES *****************************************************************/
15 #include <internal/debug.h>
17 #if defined (ALLOC_PRAGMA)
18 #pragma alloc_text(INIT, ExpInitLookasideLists)
21 /* GLOBALS *******************************************************************/
23 LIST_ENTRY ExpNonPagedLookasideListHead
;
24 KSPIN_LOCK ExpNonPagedLookasideListLock
;
25 LIST_ENTRY ExpPagedLookasideListHead
;
26 KSPIN_LOCK ExpPagedLookasideListLock
;
28 /* FUNCTIONS *****************************************************************/
33 ExpInitLookasideLists()
35 /* Initialize Lock and Listhead */
36 InitializeListHead(&ExpNonPagedLookasideListHead
);
37 KeInitializeSpinLock(&ExpNonPagedLookasideListLock
);
38 InitializeListHead(&ExpPagedLookasideListHead
);
39 KeInitializeSpinLock(&ExpPagedLookasideListLock
);
47 ExiAllocateFromPagedLookasideList(IN PPAGED_LOOKASIDE_LIST Lookaside
)
51 Lookaside
->L
.TotalAllocates
++;
52 Entry
= InterlockedPopEntrySList(&Lookaside
->L
.ListHead
);
55 Lookaside
->L
.AllocateMisses
++;
56 Entry
= (Lookaside
->L
.Allocate
)(Lookaside
->L
.Type
,
68 ExiFreeToPagedLookasideList(IN PPAGED_LOOKASIDE_LIST Lookaside
,
71 Lookaside
->L
.TotalFrees
++;
72 if (ExQueryDepthSList(&Lookaside
->L
.ListHead
) >= Lookaside
->L
.Depth
)
74 Lookaside
->L
.FreeMisses
++;
75 (Lookaside
->L
.Free
)(Entry
);
79 InterlockedPushEntrySList(&Lookaside
->L
.ListHead
, (PSLIST_ENTRY
)Entry
);
88 ExDeleteNPagedLookasideList(PNPAGED_LOOKASIDE_LIST Lookaside
)
93 /* Pop all entries off the stack and release the resources allocated
97 Entry
= InterlockedPopEntrySList(&Lookaside
->L
.ListHead
);
99 (*Lookaside
->L
.Free
)(Entry
);
102 /* Remove from list */
103 KeAcquireSpinLock(&ExpNonPagedLookasideListLock
, &OldIrql
);
104 RemoveEntryList(&Lookaside
->L
.ListEntry
);
105 KeReleaseSpinLock(&ExpNonPagedLookasideListLock
, OldIrql
);
113 ExDeletePagedLookasideList(PPAGED_LOOKASIDE_LIST Lookaside
)
118 /* Pop all entries off the stack and release the resources allocated
122 Entry
= InterlockedPopEntrySList(&Lookaside
->L
.ListHead
);
124 (*Lookaside
->L
.Free
)(Entry
);
127 /* Remove from list */
128 KeAcquireSpinLock(&ExpPagedLookasideListLock
, &OldIrql
);
129 RemoveEntryList(&Lookaside
->L
.ListEntry
);
130 KeReleaseSpinLock(&ExpPagedLookasideListLock
, OldIrql
);
138 ExInitializeNPagedLookasideList(PNPAGED_LOOKASIDE_LIST Lookaside
,
139 PALLOCATE_FUNCTION Allocate
,
146 DPRINT("Initializing nonpaged lookaside list at 0x%p\n", Lookaside
);
148 /* Initialize the Header */
149 ExInitializeSListHead(&Lookaside
->L
.ListHead
);
150 Lookaside
->L
.TotalAllocates
= 0;
151 Lookaside
->L
.AllocateMisses
= 0;
152 Lookaside
->L
.TotalFrees
= 0;
153 Lookaside
->L
.FreeMisses
= 0;
154 Lookaside
->L
.Type
= NonPagedPool
| Flags
;
155 Lookaside
->L
.Tag
= Tag
;
156 Lookaside
->L
.Size
= Size
;
157 Lookaside
->L
.Depth
= 4;
158 Lookaside
->L
.MaximumDepth
= 256;
159 Lookaside
->L
.LastTotalAllocates
= 0;
160 Lookaside
->L
.LastAllocateMisses
= 0;
162 /* Set the Allocate/Free Routines */
165 Lookaside
->L
.Allocate
= Allocate
;
169 Lookaside
->L
.Allocate
= ExAllocatePoolWithTag
;
174 Lookaside
->L
.Free
= Free
;
178 Lookaside
->L
.Free
= ExFreePool
;
181 /* Insert it into the list */
182 ExInterlockedInsertTailList(&ExpNonPagedLookasideListHead
,
183 &Lookaside
->L
.ListEntry
,
184 &ExpNonPagedLookasideListLock
);
192 ExInitializePagedLookasideList (PPAGED_LOOKASIDE_LIST Lookaside
,
193 PALLOCATE_FUNCTION Allocate
,
200 DPRINT("Initializing paged lookaside list at 0x%p\n", Lookaside
);
202 /* Initialize the Header */
203 ExInitializeSListHead(&Lookaside
->L
.ListHead
);
204 Lookaside
->L
.TotalAllocates
= 0;
205 Lookaside
->L
.AllocateMisses
= 0;
206 Lookaside
->L
.TotalFrees
= 0;
207 Lookaside
->L
.FreeMisses
= 0;
208 Lookaside
->L
.Type
= PagedPool
| Flags
;
209 Lookaside
->L
.Tag
= Tag
;
210 Lookaside
->L
.Size
= Size
;
211 Lookaside
->L
.Depth
= 4;
212 Lookaside
->L
.MaximumDepth
= 256;
213 Lookaside
->L
.LastTotalAllocates
= 0;
214 Lookaside
->L
.LastAllocateMisses
= 0;
216 /* Set the Allocate/Free Routines */
219 Lookaside
->L
.Allocate
= Allocate
;
223 Lookaside
->L
.Allocate
= ExAllocatePoolWithTag
;
228 Lookaside
->L
.Free
= Free
;
232 Lookaside
->L
.Free
= ExFreePool
;
235 /* Insert it into the list */
236 ExInterlockedInsertTailList(&ExpNonPagedLookasideListHead
,
237 &Lookaside
->L
.ListEntry
,
238 &ExpNonPagedLookasideListLock
);