ReactOS  0.4.14-dev-583-g2a1ba2c
lookas.c
Go to the documentation of this file.
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/ex/lookas.c
5 * PURPOSE: Lookaside Lists
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8 
9 /* INCLUDES ******************************************************************/
10 
11 #include <ntoskrnl.h>
12 #define NDEBUG
13 #include <debug.h>
14 
15 #if defined (ALLOC_PRAGMA)
16 #pragma alloc_text(INIT, ExpInitLookasideLists)
17 #endif
18 
19 /* GLOBALS *******************************************************************/
20 
29 
30 /* PRIVATE FUNCTIONS *********************************************************/
31 
32 INIT_FUNCTION
33 VOID
34 NTAPI
37  IN ULONG Size,
38  IN ULONG Tag,
39  IN USHORT MaximumDepth,
40  IN PLIST_ENTRY ListHead)
41 {
42  /* Initialize the list */
43  List->Tag = Tag;
44  List->Type = Type;
45  List->Size = Size;
46  InsertHeadList(ListHead, &List->ListEntry);
47  List->MaximumDepth = MaximumDepth;
48  List->Depth = 2;
49  List->Allocate = ExAllocatePoolWithTag;
50  List->Free = ExFreePool;
51  InitializeSListHead(&List->ListHead);
52  List->TotalAllocates = 0;
53  List->AllocateHits = 0;
54  List->TotalFrees = 0;
55  List->FreeHits = 0;
56  List->LastTotalAllocates = 0;
57  List->LastAllocateHits = 0;
58 }
59 
60 INIT_FUNCTION
61 VOID
62 NTAPI
64 {
65  ULONG i;
66  PKPRCB Prcb = KeGetCurrentPrcb();
68 
69  /* Loop for all pool lists */
70  for (i = 0; i < MAXIMUM_PROCESSORS; i++)
71  {
72  /* Initialize the non-paged list */
74  InitializeSListHead(&Entry->ListHead);
75 
76  /* Bind to PRCB */
77  Prcb->PPNPagedLookasideList[i].P = Entry;
78  Prcb->PPNPagedLookasideList[i].L = Entry;
79 
80  /* Initialize the paged list */
82  InitializeSListHead(&Entry->ListHead);
83 
84  /* Bind to PRCB */
85  Prcb->PPPagedLookasideList[i].P = Entry;
86  Prcb->PPPagedLookasideList[i].L = Entry;
87  }
88 }
89 
90 INIT_FUNCTION
91 VOID
92 NTAPI
94 {
95  ULONG i;
96 
97  /* Initialize locks and lists */
104 
105  /* Initialize the system lookaside lists */
106  for (i = 0; i < MAXIMUM_PROCESSORS; i++)
107  {
108  /* Initialize the non-paged list */
110  NonPagedPool,
111  (i + 1) * 8,
112  'looP',
113  256,
115 
116  /* Initialize the paged list */
118  PagedPool,
119  (i + 1) * 8,
120  'looP',
121  256,
123  }
124 }
125 
126 /* PUBLIC FUNCTIONS **********************************************************/
127 
128 /*
129  * @implemented
130  */
131 PVOID
132 NTAPI
134 {
135  PVOID Entry;
136 
137  Lookaside->L.TotalAllocates++;
138  Entry = InterlockedPopEntrySList(&Lookaside->L.ListHead);
139  if (!Entry)
140  {
141  Lookaside->L.AllocateMisses++;
142  Entry = (Lookaside->L.Allocate)(Lookaside->L.Type,
143  Lookaside->L.Size,
144  Lookaside->L.Tag);
145  }
146  return Entry;
147 }
148 
149 /*
150  * @implemented
151  */
152 VOID
153 NTAPI
155  IN PVOID Entry)
156 {
157  Lookaside->L.TotalFrees++;
158  if (ExQueryDepthSList(&Lookaside->L.ListHead) >= Lookaside->L.Depth)
159  {
160  Lookaside->L.FreeMisses++;
161  (Lookaside->L.Free)(Entry);
162  }
163  else
164  {
166  }
167 }
168 
169 /*
170  * @implemented
171  */
172 VOID
173 NTAPI
175 {
176  KIRQL OldIrql;
177  PVOID Entry;
178 
179  /* Pop all entries off the stack and release their resources */
180  for (;;)
181  {
182  Entry = InterlockedPopEntrySList(&Lookaside->L.ListHead);
183  if (!Entry) break;
184  (*Lookaside->L.Free)(Entry);
185  }
186 
187  /* Remove from list */
189  RemoveEntryList(&Lookaside->L.ListEntry);
191 }
192 
193 /*
194  * @implemented
195  */
196 VOID
197 NTAPI
199 {
200  KIRQL OldIrql;
201  PVOID Entry;
202 
203  /* Pop all entries off the stack and release their resources */
204  for (;;)
205  {
206  Entry = InterlockedPopEntrySList(&Lookaside->L.ListHead);
207  if (!Entry) break;
208  (*Lookaside->L.Free)(Entry);
209  }
210 
211  /* Remove from list */
213  RemoveEntryList(&Lookaside->L.ListEntry);
215 }
216 
217 /*
218  * @implemented
219  */
220 VOID
221 NTAPI
223  IN PALLOCATE_FUNCTION Allocate OPTIONAL,
224  IN PFREE_FUNCTION Free OPTIONAL,
225  IN ULONG Flags,
226  IN SIZE_T Size,
227  IN ULONG Tag,
228  IN USHORT Depth)
229 {
230  /* Initialize the Header */
231  ExInitializeSListHead(&Lookaside->L.ListHead);
232  Lookaside->L.TotalAllocates = 0;
233  Lookaside->L.AllocateMisses = 0;
234  Lookaside->L.TotalFrees = 0;
235  Lookaside->L.FreeMisses = 0;
236  Lookaside->L.Type = NonPagedPool | Flags;
237  Lookaside->L.Tag = Tag;
238  Lookaside->L.Size = (ULONG)Size;
239  Lookaside->L.Depth = 4;
240  Lookaside->L.MaximumDepth = 256;
241  Lookaside->L.LastTotalAllocates = 0;
242  Lookaside->L.LastAllocateMisses = 0;
243 
244  /* Set the Allocate/Free Routines */
245  if (Allocate)
246  {
247  Lookaside->L.Allocate = Allocate;
248  }
249  else
250  {
251  Lookaside->L.Allocate = ExAllocatePoolWithTag;
252  }
253 
254  if (Free)
255  {
256  Lookaside->L.Free = Free;
257  }
258  else
259  {
260  Lookaside->L.Free = ExFreePool;
261  }
262 
263  /* Insert it into the list */
265  &Lookaside->L.ListEntry,
267 }
268 
269 /*
270  * @implemented
271  */
272 VOID
273 NTAPI
275  IN PALLOCATE_FUNCTION Allocate OPTIONAL,
276  IN PFREE_FUNCTION Free OPTIONAL,
277  IN ULONG Flags,
278  IN SIZE_T Size,
279  IN ULONG Tag,
280  IN USHORT Depth)
281 {
282  /* Initialize the Header */
283  ExInitializeSListHead(&Lookaside->L.ListHead);
284  Lookaside->L.TotalAllocates = 0;
285  Lookaside->L.AllocateMisses = 0;
286  Lookaside->L.TotalFrees = 0;
287  Lookaside->L.FreeMisses = 0;
288  Lookaside->L.Type = PagedPool | Flags;
289  Lookaside->L.Tag = Tag;
290  Lookaside->L.Size = (ULONG)Size;
291  Lookaside->L.Depth = 4;
292  Lookaside->L.MaximumDepth = 256;
293  Lookaside->L.LastTotalAllocates = 0;
294  Lookaside->L.LastAllocateMisses = 0;
295 
296  /* Set the Allocate/Free Routines */
297  if (Allocate)
298  {
299  Lookaside->L.Allocate = Allocate;
300  }
301  else
302  {
303  Lookaside->L.Allocate = ExAllocatePoolWithTag;
304  }
305 
306  if (Free)
307  {
308  Lookaside->L.Free = Free;
309  }
310  else
311  {
312  Lookaside->L.Free = ExFreePool;
313  }
314 
315  /* Insert it into the list */
317  &Lookaside->L.ListEntry,
319 }
320 
321 /* EOF */
#define MAXIMUM_PROCESSORS
Definition: rwlock.h:5
#define IN
Definition: typedefs.h:38
_In_opt_ PALLOCATE_FUNCTION _In_opt_ PFREE_FUNCTION _In_ ULONG _In_ SIZE_T _In_ ULONG _In_ USHORT Depth
Definition: exfuncs.h:656
GENERAL_LOOKASIDE_POOL PPNPagedLookasideList[NUMBER_POOL_LOOKASIDE_LISTS]
Definition: ketypes.h:625
VOID NTAPI ExInitializePagedLookasideList(IN PPAGED_LOOKASIDE_LIST Lookaside, IN PALLOCATE_FUNCTION Allocate OPTIONAL, IN PFREE_FUNCTION Free OPTIONAL, IN ULONG Flags, IN SIZE_T Size, IN ULONG Tag, IN USHORT Depth)
Definition: lookas.c:274
Type
Definition: Type.h:6
LIST_ENTRY ExPoolLookasideListHead
Definition: lookas.c:26
struct _Entry Entry
Definition: kefuncs.h:640
FORCEINLINE VOID InsertHeadList(_Inout_ PLIST_ENTRY ListHead, _Inout_ __drv_aliasesMem PLIST_ENTRY Entry)
Definition: rtlfuncs.h:201
PLIST_ENTRY NTAPI ExInterlockedInsertTailList(IN OUT PLIST_ENTRY ListHead, IN OUT PLIST_ENTRY ListEntry, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:140
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1062
_In_opt_ PALLOCATE_FUNCTION Allocate
Definition: exfuncs.h:656
PSLIST_ENTRY WINAPI InterlockedPopEntrySList(PSLIST_HEADER ListHead)
Definition: interlocked.c:55
_In_opt_ PALLOCATE_FUNCTION _In_opt_ PFREE_FUNCTION Free
Definition: exfuncs.h:656
struct LOOKASIDE_ALIGN _NPAGED_LOOKASIDE_LIST * PNPAGED_LOOKASIDE_LIST
struct LOOKASIDE_ALIGN _GENERAL_LOOKASIDE GENERAL_LOOKASIDE
FORCEINLINE BOOLEAN RemoveEntryList(_In_ PLIST_ENTRY Entry)
Definition: rtlfuncs.h:105
UCHAR KIRQL
Definition: env_spec_w32.h:591
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
GENERAL_LOOKASIDE ExpSmallNPagedPoolLookasideLists[MAXIMUM_PROCESSORS]
Definition: lookas.c:27
struct LOOKASIDE_ALIGN _GENERAL_LOOKASIDE * PGENERAL_LOOKASIDE
FORCEINLINE VOID KeInitializeSpinLock(_Out_ PKSPIN_LOCK SpinLock)
Definition: kefuncs.h:251
VOID NTAPI ExDeletePagedLookasideList(IN PPAGED_LOOKASIDE_LIST Lookaside)
Definition: lookas.c:198
FORCEINLINE VOID InitializeSListHead(_Out_ PSLIST_HEADER SListHead)
Definition: rtlfuncs.h:3353
INIT_FUNCTION VOID NTAPI ExInitializeSystemLookasideList(IN PGENERAL_LOOKASIDE List, IN POOL_TYPE Type, IN ULONG Size, IN ULONG Tag, IN USHORT MaximumDepth, IN PLIST_ENTRY ListHead)
Definition: lookas.c:35
PVOID NTAPI ExiAllocateFromPagedLookasideList(IN PPAGED_LOOKASIDE_LIST Lookaside)
Definition: lookas.c:133
LIST_ENTRY ExpNonPagedLookasideListHead
Definition: lookas.c:21
GENERAL_LOOKASIDE ExpSmallPagedPoolLookasideLists[MAXIMUM_PROCESSORS]
Definition: lookas.c:28
INIT_FUNCTION VOID NTAPI ExpInitLookasideLists(VOID)
Definition: lookas.c:93
VOID NTAPI ExInitializeNPagedLookasideList(IN PNPAGED_LOOKASIDE_LIST Lookaside, IN PALLOCATE_FUNCTION Allocate OPTIONAL, IN PFREE_FUNCTION Free OPTIONAL, IN ULONG Flags, IN SIZE_T Size, IN ULONG Tag, IN USHORT Depth)
Definition: lookas.c:222
_Inout_ PVOID Lookaside
Definition: fltkernel.h:2532
LIST_ENTRY List
Definition: psmgr.c:57
INT POOL_TYPE
Definition: typedefs.h:76
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
VOID NTAPI ExiFreeToPagedLookasideList(IN PPAGED_LOOKASIDE_LIST Lookaside, IN PVOID Entry)
Definition: lookas.c:154
#define PSLIST_ENTRY
Definition: rtltypes.h:130
FORCEINLINE USHORT ExQueryDepthSList(_In_ PSLIST_HEADER SListHead)
Definition: exfuncs.h:153
KSPIN_LOCK ExpPagedLookasideListLock
Definition: lookas.c:24
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
INIT_FUNCTION VOID NTAPI ExInitPoolLookasidePointers(VOID)
Definition: lookas.c:63
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:803
KSPIN_LOCK ExpNonPagedLookasideListLock
Definition: lookas.c:22
Definition: typedefs.h:117
NTKERNELAPI PSLIST_ENTRY FASTCALL InterlockedPushEntrySList(IN PSLIST_HEADER ListHead, IN PSLIST_ENTRY ListEntry)
Definition: interlocked.c:82
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
ULONG_PTR SIZE_T
Definition: typedefs.h:78
unsigned short USHORT
Definition: pedump.c:61
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
ULONG KSPIN_LOCK
Definition: env_spec_w32.h:72
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
IN ULONG IN ULONG Tag
Definition: evtlib.h:159
#define ExInitializeSListHead
unsigned int ULONG
Definition: retypes.h:1
struct LOOKASIDE_ALIGN _PAGED_LOOKASIDE_LIST * PPAGED_LOOKASIDE_LIST
VOID NTAPI ExDeleteNPagedLookasideList(IN PNPAGED_LOOKASIDE_LIST Lookaside)
Definition: lookas.c:174
base of all file and directory entries
Definition: entries.h:82
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
LIST_ENTRY ExpPagedLookasideListHead
Definition: lookas.c:23
LIST_ENTRY ExSystemLookasideListHead
Definition: lookas.c:25
GENERAL_LOOKASIDE_POOL PPPagedLookasideList[NUMBER_POOL_LOOKASIDE_LISTS]
Definition: ketypes.h:626
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68