Process Hacker
ref.h
Go to the documentation of this file.
1 /*
2  * Process Hacker -
3  * internal object manager
4  *
5  * Copyright (C) 2009-2015 wj32
6  *
7  * This file is part of Process Hacker.
8  *
9  * Process Hacker is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * Process Hacker is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with Process Hacker. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef _PH_REF_H
24 #define _PH_REF_H
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 // Configuration
31 
32 #define PH_OBJECT_SMALL_OBJECT_SIZE 48
33 #define PH_OBJECT_SMALL_OBJECT_COUNT 512
34 
35 // Object type flags
36 #define PH_OBJECT_TYPE_USE_FREE_LIST 0x00000001
37 #define PH_OBJECT_TYPE_VALID_FLAGS 0x00000001
38 
39 // Object type callbacks
40 
49  _In_ PVOID Object,
50  _In_ ULONG Flags
51  );
52 
53 struct _PH_OBJECT_TYPE;
55 
56 struct _PH_QUEUED_LOCK;
58 
59 #ifdef DEBUG
60 typedef VOID (NTAPI *PPH_CREATE_OBJECT_HOOK)(
61  _In_ PVOID Object,
62  _In_ SIZE_T Size,
63  _In_ ULONG Flags,
64  _In_ PPH_OBJECT_TYPE ObjectType
65  );
66 #endif
67 
68 #ifndef _PH_REF_PRIVATE
69 extern PPH_OBJECT_TYPE PhObjectTypeObject;
70 extern PPH_OBJECT_TYPE PhAllocType;
71 
72 #ifdef DEBUG
73 extern LIST_ENTRY PhDbgObjectListHead;
74 extern PH_QUEUED_LOCK PhDbgObjectListLock;
75 extern PPH_CREATE_OBJECT_HOOK PhDbgCreateObjectHook;
76 #endif
77 #endif
78 
80 {
81  SIZE_T FreeListSize;
84 
86 {
87  PWSTR Name;
89  USHORT Flags;
90  UCHAR TypeIndex;
91  UCHAR Reserved;
93 
94 NTSTATUS PhInitializeRef(
95  VOID
96  );
97 
100 PVOID
101 NTAPI
103  _In_ SIZE_T ObjectSize,
104  _In_ PPH_OBJECT_TYPE ObjectType
105  );
106 
107 PHLIBAPI
108 PVOID
109 NTAPI
111  _In_ PVOID Object
112  );
113 
115 PHLIBAPI
116 LONG
117 NTAPI
119  _In_ PVOID Object,
120  _In_ LONG RefCount
121  );
122 
123 PHLIBAPI
124 BOOLEAN
125 NTAPI
127  _In_ PVOID Object
128  );
129 
130 PHLIBAPI
131 VOID
132 NTAPI
134  _In_ PVOID Object
135  );
136 
137 PHLIBAPI
138 BOOLEAN
139 NTAPI
141  _In_ PVOID Object
142  );
143 
145 PHLIBAPI
146 LONG
147 NTAPI
149  _In_ PVOID Object,
150  _In_ LONG RefCount,
151  _In_ BOOLEAN DeferDelete
152  );
153 
154 PHLIBAPI
155 PPH_OBJECT_TYPE
156 NTAPI
158  _In_ PVOID Object
159  );
160 
161 PHLIBAPI
162 PPH_OBJECT_TYPE
163 NTAPI
165  _In_ PWSTR Name,
166  _In_ ULONG Flags,
167  _In_opt_ PPH_TYPE_DELETE_PROCEDURE DeleteProcedure
168  );
169 
170 PHLIBAPI
171 PPH_OBJECT_TYPE
172 NTAPI
174  _In_ PWSTR Name,
175  _In_ ULONG Flags,
176  _In_opt_ PPH_TYPE_DELETE_PROCEDURE DeleteProcedure,
177  _In_opt_ PPH_OBJECT_TYPE_PARAMETERS Parameters
178  );
179 
180 PHLIBAPI
181 VOID
182 NTAPI
184  _In_ PPH_OBJECT_TYPE ObjectType,
185  _Out_ PPH_OBJECT_TYPE_INFORMATION Information
186  );
187 
188 PHLIBAPI
189 PVOID
190 NTAPI
192  _In_ SIZE_T Size
193  );
194 
195 // Object reference functions
196 
197 FORCEINLINE
198 VOID
200  _Inout_ PVOID *ObjectReference,
201  _In_opt_ PVOID NewObject
202  )
203 {
204  PVOID oldObject;
205 
206  oldObject = *ObjectReference;
207  *ObjectReference = NewObject;
208 
209  if (NewObject) PhReferenceObject(NewObject);
210  if (oldObject) PhDereferenceObject(oldObject);
211 }
212 
213 FORCEINLINE
214 VOID
216  _Inout_ PVOID *ObjectReference,
217  _In_opt_ _Assume_refs_(1) PVOID NewObject
218  )
219 {
220  PVOID oldObject;
221 
222  oldObject = *ObjectReference;
223  *ObjectReference = NewObject;
224 
225  if (oldObject) PhDereferenceObject(oldObject);
226 }
227 
228 FORCEINLINE
229 VOID
231  _Out_ PVOID *ObjectReference,
232  _In_opt_ PVOID NewObject
233  )
234 {
235  *ObjectReference = NewObject;
236 
237  if (NewObject) PhReferenceObject(NewObject);
238 }
239 
240 FORCEINLINE
241 VOID
243  _Inout_ PVOID *ObjectReference
244  )
245 {
246  PhMoveReference(ObjectReference, NULL);
247 }
248 
249 // Auto-dereference pool
250 
252 #define PH_AUTO_POOL_STATIC_SIZE 64
253 
255 #define PH_AUTO_POOL_DYNAMIC_BIG_SIZE 256
256 
266 typedef struct _PH_AUTO_POOL
267 {
268  ULONG StaticCount;
270 
274 
277 
278 PHLIBAPI
279 VOID
280 NTAPI
282  _Out_ PPH_AUTO_POOL AutoPool
283  );
284 
286 PHLIBAPI
287 VOID
288 NTAPI
290  _Inout_ PPH_AUTO_POOL AutoPool
291  );
292 
293 PHLIBAPI
294 VOID
295 NTAPI
297  _In_ PPH_AUTO_POOL AutoPool
298  );
299 
301 PHLIBAPI
302 PVOID
303 NTAPI
305  _In_opt_ PVOID Object
306  );
307 
309 PHLIBAPI VOID NTAPI PhaDereferenceObject(PVOID Object);
310 
311 #ifdef __cplusplus
312 }
313 #endif
314 
315 #endif