ReactOS  0.4.15-dev-1171-gab82533
dictlib.c
Go to the documentation of this file.
1 /*++
2 
3 Copyright (C) Microsoft Corporation, 1990 - 1999
4 
5 Module Name:
6 
7  dictlib.c
8 
9 Abstract:
10 
11  Support library for maintaining a dictionary list (list of objects
12  referenced by a key value).
13 
14 Environment:
15 
16  kernel mode only
17 
18 Notes:
19 
20  This module generates a static library
21 
22 Revision History:
23 
24 --*/
25 
26 #include <ntddk.h>
27 #include <classpnp.h>
28 
29 #ifdef __REACTOS__
30 #undef MdlMappingNoExecute
31 #define MdlMappingNoExecute 0
32 #define NonPagedPoolNx NonPagedPool
33 #define NonPagedPoolNxCacheAligned NonPagedPoolCacheAligned
34 #undef POOL_NX_ALLOCATION
35 #define POOL_NX_ALLOCATION 0
36 #endif
37 
38 #define DICTIONARY_SIGNATURE 'tciD'
39 
40 #ifdef _MSC_VER
41 #pragma warning(push)
42 #pragma warning(disable:4200) // nonstandard extension used : zero-sized array in struct/union
43 #endif
47  UCHAR Data[0];
48 };
49 #ifdef _MSC_VER
50 #pragma warning(pop)
51 #endif
52 
53 struct _DICTIONARY_HEADER;
55 
56 
57 VOID
59  IN PDICTIONARY Dictionary
60  )
61 {
62  RtlZeroMemory(Dictionary, sizeof(DICTIONARY));
63  Dictionary->Signature = DICTIONARY_SIGNATURE;
64  KeInitializeSpinLock(&Dictionary->SpinLock);
65  return;
66 }
67 
68 
69 BOOLEAN
71  IN PDICTIONARY Dictionary
72  )
73 {
74  return Dictionary->Signature == DICTIONARY_SIGNATURE;
75 }
76 
79  IN PDICTIONARY Dictionary,
82  IN ULONG Tag,
83  OUT PVOID *Entry
84  )
85 {
87  KIRQL oldIrql;
89 
91 
92  *Entry = NULL;
93 
94  header = ExAllocatePoolWithTag(NonPagedPoolNx,
95  Size + sizeof(DICTIONARY_HEADER),
96  Tag);
97 
98  if(header == NULL) {
100  }
101 
103  header->Key = Key;
104 
105  //
106  // Find the correct location for this entry in the dictionary.
107  //
108 
109  KeAcquireSpinLock(&(Dictionary->SpinLock), &oldIrql);
110 
111  TRY {
112 
113  entry = &(Dictionary->List);
114 
115  while(*entry != NULL) {
116  if((*entry)->Key == Key) {
117 
118  //
119  // Dictionary must have unique keys.
120  //
121 
123  LEAVE;
124 
125  } else if ((*entry)->Key < Key) {
126 
127  //
128  // We will go ahead and insert the key in here.
129  //
130  break;
131  } else {
132  entry = &((*entry)->Next);
133  }
134  }
135 
136  //
137  // If we make it here then we will go ahead and do the insertion.
138  //
139 
140  header->Next = *entry;
141  *entry = header;
142 
143  } FINALLY {
144  KeReleaseSpinLock(&(Dictionary->SpinLock), oldIrql);
145 
146  if(!NT_SUCCESS(status)) {
147  FREE_POOL(header);
148  } else {
149  *Entry = (PVOID) header->Data;
150  }
151  }
152  return status;
153 }
154 
155 
156 PVOID
158  IN PDICTIONARY Dictionary,
160  )
161 {
163  PVOID data;
164  KIRQL oldIrql;
165 
166 
167  data = NULL;
168 
169  KeAcquireSpinLock(&(Dictionary->SpinLock), &oldIrql);
170 
171  entry = Dictionary->List;
172  while (entry != NULL) {
173 
174  if (entry->Key == Key) {
175  data = entry->Data;
176  break;
177  } else {
178  entry = entry->Next;
179  }
180  }
181 
182  KeReleaseSpinLock(&(Dictionary->SpinLock), oldIrql);
183 
184  return data;
185 }
186 
187 
188 VOID
190  IN PDICTIONARY Dictionary,
191  IN PVOID Entry
192  )
193 {
196  KIRQL oldIrql;
197  BOOLEAN found;
198 
199  found = FALSE;
201 
202  KeAcquireSpinLock(&(Dictionary->SpinLock), &oldIrql);
203 
204  entry = &(Dictionary->List);
205  while(*entry != NULL) {
206 
207  if(*entry == header) {
208  *entry = header->Next;
209  found = TRUE;
210  break;
211  } else {
212  entry = &(*entry)->Next;
213  }
214  }
215 
216  KeReleaseSpinLock(&(Dictionary->SpinLock), oldIrql);
217 
218  //
219  // calling this w/an invalid pointer invalidates the dictionary system,
220  // so NT_ASSERT() that we never try to Free something not in the list
221  //
222 
223  NT_ASSERT(found);
224  if (found) {
225  FREE_POOL(header);
226  }
227 
228  return;
229 
230 }
231 
#define STATUS_OBJECT_NAME_COLLISION
Definition: udferr_usr.h:150
BOOLEAN TestDictionarySignature(IN PDICTIONARY Dictionary)
Definition: dictlib.c:70
#define FINALLY
Definition: classpnp.h:116
#define IN
Definition: typedefs.h:39
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
IN PLARGE_INTEGER IN PLARGE_INTEGER PEPROCESS ULONG Key
Definition: fatprocs.h:2706
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
#define LEAVE
Definition: classpnp.h:115
PDICTIONARY_HEADER Next
Definition: dictlib.c:45
#define TRY(sps, bps)
VOID InitializeDictionary(IN PDICTIONARY Dictionary)
Definition: dictlib.c:58
VOID FreeDictionaryEntry(IN PDICTIONARY Dictionary, IN PVOID Entry)
Definition: dictlib.c:189
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define FALSE
Definition: types.h:117
#define FREE_POOL(_PoolPtr)
Definition: cdrom.h:782
FORCEINLINE VOID KeInitializeSpinLock(_Out_ PKSPIN_LOCK SpinLock)
Definition: kefuncs.h:238
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
void * PVOID
Definition: retypes.h:9
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
uint64_t ULONGLONG
Definition: typedefs.h:67
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
unsigned char UCHAR
Definition: xmlstorage.h:181
PVOID GetDictionaryEntry(IN PDICTIONARY Dictionary, IN ULONGLONG Key)
Definition: dictlib.c:157
NTSTATUS AllocateDictionaryEntry(IN PDICTIONARY Dictionary, IN ULONGLONG Key, _In_range_(0, sizeof(FILE_OBJECT_EXTENSION)) IN ULONG Size, IN ULONG Tag, OUT PVOID *Entry)
Definition: dictlib.c:78
uint32_t entry
Definition: isohybrid.c:63
struct _DICTIONARY_HEADER * PDICTIONARY_HEADER
Definition: dictlib.c:54
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
ULONGLONG Key
Definition: dictlib.c:46
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#define DICTIONARY_SIGNATURE
Definition: dictlib.c:38
IN ULONG IN ULONG Tag
Definition: evtlib.h:159
#define OUT
Definition: typedefs.h:40
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define _In_range_(lb, ub)
Definition: no_sal2.h:227
return STATUS_SUCCESS
Definition: btrfs.c:3014
static SERVICE_STATUS status
Definition: service.c:31
struct CFHEADER header
Definition: fdi.c:101
base of all file and directory entries
Definition: entries.h:82
#define NT_ASSERT
Definition: rtlfuncs.h:3312
Definition: ps.c:97