ReactOS  0.4.15-dev-1386-g5cb9f87
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 /* GLOBALS *******************************************************************/
16 
25 
26 /* PRIVATE FUNCTIONS *********************************************************/
27 
28 CODE_SEG("INIT")
29 VOID
30 NTAPI
33  IN ULONG Size,
34  IN ULONG Tag,
35  IN USHORT MaximumDepth,
36  IN PLIST_ENTRY ListHead)
37 {
38  /* Initialize the list */
39  List->Tag = Tag;
40  List->Type = Type;
41  List->Size = Size;
42  InsertHeadList(ListHead, &List->ListEntry);
43  List->MaximumDepth = MaximumDepth;
44  List->Depth = 2;
45  List->Allocate = ExAllocatePoolWithTag;
46  List->Free = ExFreePool;
47  InitializeSListHead(&List->ListHead);
48  List->TotalAllocates = 0;
49  List->AllocateHits = 0;
50  List->TotalFrees = 0;
51  List->FreeHits = 0;
52  List->LastTotalAllocates = 0;
53  List->LastAllocateHits = 0;
54 }
55 
56 CODE_SEG("INIT")
57 VOID
58 NTAPI
60 {
61  ULONG i;
62  PKPRCB Prcb = KeGetCurrentPrcb();
64 
65  /* Loop for all pool lists */
66  for (i = 0; i < MAXIMUM_PROCESSORS; i++)
67  {
68  /* Initialize the non-paged list */
70  InitializeSListHead(&Entry->ListHead);
71 
72  /* Bind to PRCB */
73  Prcb->PPNPagedLookasideList[i].P = Entry;
74  Prcb->PPNPagedLookasideList[i].L = Entry;
75 
76  /* Initialize the paged list */
78  InitializeSListHead(&Entry->ListHead);
79 
80  /* Bind to PRCB */
81  Prcb->PPPagedLookasideList[i].P = Entry;
82  Prcb->PPPagedLookasideList[i].L = Entry;
83  }
84 }
85 
86 CODE_SEG("INIT")
87 VOID
88 NTAPI
90 {
91  ULONG i;
92 
93  /* Initialize locks and lists */
100 
101  /* Initialize the system lookaside lists */
102  for (i = 0; i < MAXIMUM_PROCESSORS; i++)
103  {
104  /* Initialize the non-paged list */
106  NonPagedPool,
107  (i + 1) * 8,
108  'looP',
109  256,
111 
112  /* Initialize the paged list */
114  PagedPool,
115  (i + 1) * 8,
116  'looP',
117  256,
119  }
120 }
121 
122 /* PUBLIC FUNCTIONS **********************************************************/
123 
124 /*
125  * @implemented
126  */
127 PVOID
128 NTAPI
130 {
131  PVOID Entry;
132 
133  Lookaside->L.TotalAllocates++;
134  Entry = InterlockedPopEntrySList(&Lookaside->L.ListHead);
135  if (!Entry)
136  {
137  Lookaside->L.AllocateMisses++;
138  Entry = (Lookaside->L.Allocate)(Lookaside->L.Type,
139  Lookaside->L.Size,
140  Lookaside->L.Tag);
141  }
142  return Entry;
143 }
144 
145 /*
146  * @implemented
147  */
148 VOID
149 NTAPI
151  IN PVOID Entry)
152 {
153  Lookaside->L.TotalFrees++;
154  if (ExQueryDepthSList(&Lookaside->L.ListHead) >= Lookaside->L.Depth)
155  {
156  Lookaside->L.FreeMisses++;
157  (Lookaside->L.Free)(Entry);
158  }
159  else
160  {
162  }
163 }
164 
165 /*
166  * @implemented
167  */
168 VOID
169 NTAPI
171 {
172  KIRQL OldIrql;
173  PVOID Entry;
174 
175  /* Pop all entries off the stack and release their resources */
176  for (;;)
177  {
178  Entry = InterlockedPopEntrySList(&Lookaside->L.ListHead);
179  if (!Entry) break;
180  (*Lookaside->L.Free)(Entry);
181  }
182 
183  /* Remove from list */
185  RemoveEntryList(&Lookaside->L.ListEntry);
187 }
188 
189 /*
190  * @implemented
191  */
192 VOID
193 NTAPI
195 {
196  KIRQL OldIrql;
197  PVOID Entry;
198 
199  /* Pop all entries off the stack and release their resources */
200  for (;;)
201  {
202  Entry = InterlockedPopEntrySList(&Lookaside->L.ListHead);
203  if (!Entry) break;
204  (*Lookaside->L.Free)(Entry);
205  }
206 
207  /* Remove from list */
209  RemoveEntryList(&Lookaside->L.ListEntry);
211 }
212 
213 /*
214  * @implemented
215  */
216 VOID
217 NTAPI
219  IN PALLOCATE_FUNCTION Allocate OPTIONAL,
220  IN PFREE_FUNCTION Free OPTIONAL,
221  IN ULONG Flags,
222  IN SIZE_T Size,
223  IN ULONG Tag,
224  IN USHORT Depth)
225 {
226  /* Initialize the Header */
227  ExInitializeSListHead(&Lookaside->L.ListHead);
228  Lookaside->L.TotalAllocates = 0;
229  Lookaside->L.AllocateMisses = 0;
230  Lookaside->L.TotalFrees = 0;
231  Lookaside->L.FreeMisses = 0;
232  Lookaside->L.Type = NonPagedPool | Flags;
233  Lookaside->L.Tag = Tag;
234  Lookaside->L.Size = (ULONG)Size;
235  Lookaside->L.Depth = 4;
236  Lookaside->L.MaximumDepth = 256;
237  Lookaside->L.LastTotalAllocates = 0;
238  Lookaside->L.LastAllocateMisses = 0;
239 
240  /* Set the Allocate/Free Routines */
241  if (Allocate)
242  {
243  Lookaside->L.Allocate = Allocate;
244  }
245  else
246  {
247  Lookaside->L.Allocate = ExAllocatePoolWithTag;
248  }
249 
250  if (Free)
251  {
252  Lookaside->L.Free = Free;
253  }
254  else
255  {
256  Lookaside->L.Free = ExFreePool;
257  }
258 
259  /* Insert it into the list */
261  &Lookaside->L.ListEntry,
263 }
264 
265 /*
266  * @implemented
267  */
268 VOID
269 NTAPI
271  IN PALLOCATE_FUNCTION Allocate OPTIONAL,
272  IN PFREE_FUNCTION Free OPTIONAL,
273  IN ULONG Flags,
274  IN SIZE_T Size,
275  IN ULONG Tag,
276  IN USHORT Depth)
277 {
278  /* Initialize the Header */
279  ExInitializeSListHead(&Lookaside->L.ListHead);
280  Lookaside->L.TotalAllocates = 0;
281  Lookaside->L.AllocateMisses = 0;
282  Lookaside->L.TotalFrees = 0;
283  Lookaside->L.FreeMisses = 0;
284  Lookaside->L.Type = PagedPool | Flags;
285  Lookaside->L.Tag = Tag;
286  Lookaside->L.Size = (ULONG)Size;
287  Lookaside->L.Depth = 4;
288  Lookaside->L.MaximumDepth = 256;
289  Lookaside->L.LastTotalAllocates = 0;
290  Lookaside->L.LastAllocateMisses = 0;
291 
292  /* Set the Allocate/Free Routines */
293  if (Allocate)
294  {
295  Lookaside->L.Allocate = Allocate;
296  }
297  else
298  {
299  Lookaside->L.Allocate = ExAllocatePoolWithTag;
300  }
301 
302  if (Free)
303  {
304  Lookaside->L.Free = Free;
305  }
306  else
307  {
308  Lookaside->L.Free = ExFreePool;
309  }
310 
311  /* Insert it into the list */
313  &Lookaside->L.ListEntry,
315 }
316 
317 /* EOF */
#define MAXIMUM_PROCESSORS
Definition: rwlock.h:5
#define IN
Definition: typedefs.h:39
_In_opt_ PALLOCATE_FUNCTION _In_opt_ PFREE_FUNCTION _In_ ULONG _In_ SIZE_T _In_ ULONG _In_ USHORT Depth
Definition: exfuncs.h:814
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:270
_Must_inspect_result_ _In_opt_ PWDF_OBJECT_ATTRIBUTES _In_ _In_ _Strict_type_match_ POOL_TYPE _In_opt_ PWDF_OBJECT_ATTRIBUTES _In_opt_ ULONG _Out_ WDFLOOKASIDE * Lookaside
Definition: wdfmemory.h:407
LIST_ENTRY ExPoolLookasideListHead
Definition: lookas.c:22
struct _Entry Entry
Definition: kefuncs.h:627
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:1075
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
VOID NTAPI ExpInitLookasideLists(VOID)
Definition: lookas.c:89
_In_opt_ PALLOCATE_FUNCTION Allocate
Definition: exfuncs.h:814
PSLIST_ENTRY WINAPI InterlockedPopEntrySList(PSLIST_HEADER ListHead)
Definition: interlocked.c:55
_In_opt_ PALLOCATE_FUNCTION _In_opt_ PFREE_FUNCTION Free
Definition: exfuncs.h:814
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
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:23
struct LOOKASIDE_ALIGN _GENERAL_LOOKASIDE * PGENERAL_LOOKASIDE
FORCEINLINE VOID KeInitializeSpinLock(_Out_ PKSPIN_LOCK SpinLock)
Definition: kefuncs.h:238
VOID NTAPI ExDeletePagedLookasideList(IN PPAGED_LOOKASIDE_LIST Lookaside)
Definition: lookas.c:194
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:31
FORCEINLINE VOID InitializeSListHead(_Out_ PSLIST_HEADER SListHead)
Definition: rtlfuncs.h:3353
PVOID NTAPI ExiAllocateFromPagedLookasideList(IN PPAGED_LOOKASIDE_LIST Lookaside)
Definition: lookas.c:129
LIST_ENTRY ExpNonPagedLookasideListHead
Definition: lookas.c:17
GENERAL_LOOKASIDE ExpSmallPagedPoolLookasideLists[MAXIMUM_PROCESSORS]
Definition: lookas.c:24
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
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:218
INT POOL_TYPE
Definition: typedefs.h:78
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
Type
Definition: Type.h:6
VOID NTAPI ExiFreeToPagedLookasideList(IN PPAGED_LOOKASIDE_LIST Lookaside, IN PVOID Entry)
Definition: lookas.c:150
#define PSLIST_ENTRY
Definition: rtltypes.h:134
FORCEINLINE USHORT ExQueryDepthSList(_In_ PSLIST_HEADER SListHead)
Definition: exfuncs.h:153
CODE_SEG("INIT")
Definition: fsrtlpc.c:19
KSPIN_LOCK ExpPagedLookasideListLock
Definition: lookas.c:20
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:790
_Must_inspect_result_ _In_ WDFCMRESLIST List
Definition: wdfresource.h:550
KSPIN_LOCK ExpNonPagedLookasideListLock
Definition: lookas.c:18
Definition: typedefs.h:119
NTKERNELAPI PSLIST_ENTRY FASTCALL InterlockedPushEntrySList(IN PSLIST_HEADER ListHead, IN PSLIST_ENTRY ListEntry)
Definition: interlocked.c:82
_Must_inspect_result_ _In_ WDFDEVICE _In_ BOOLEAN _In_opt_ PVOID Tag
Definition: wdfdevice.h:4061
ULONG_PTR SIZE_T
Definition: typedefs.h:80
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
unsigned short USHORT
Definition: pedump.c:61
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
VOID NTAPI ExInitPoolLookasidePointers(VOID)
Definition: lookas.c:59
ULONG KSPIN_LOCK
Definition: env_spec_w32.h:72
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#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:170
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:19
LIST_ENTRY ExSystemLookasideListHead
Definition: lookas.c:21
GENERAL_LOOKASIDE_POOL PPPagedLookasideList[NUMBER_POOL_LOOKASIDE_LISTS]
Definition: ketypes.h:626
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68