Process Hacker
refp.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_REFP_H
24 #define _PH_REFP_H
25 
26 #define PH_OBJECT_TYPE_TABLE_SIZE 256
27 
29 #define PH_OBJECT_FROM_SMALL_FREE_LIST 0x1
30 
31 #define PH_OBJECT_FROM_TYPE_FREE_LIST 0x2
32 
37 typedef struct _PH_OBJECT_HEADER
38 {
39  union
40  {
41  struct
42  {
43  union
44  {
46  PVOID PaddingDoNotUse; // Corresponds to DeferDeleteListEntry
47  };
48  USHORT TypeIndex;
49  UCHAR Flags;
50  UCHAR Reserved1;
51 #ifdef _WIN64
52  ULONG Reserved2;
53 #endif
54  };
55  SLIST_ENTRY DeferDeleteListEntry;
56  };
57 
58 #ifdef DEBUG
59  PVOID StackBackTrace[16];
60  LIST_ENTRY ObjectListEntry;
61 #endif
62 
67 
68 #ifndef DEBUG
69 #ifdef _WIN64
70 C_ASSERT(FIELD_OFFSET(PH_OBJECT_HEADER, RefCount) == 0x0);
71 C_ASSERT(FIELD_OFFSET(PH_OBJECT_HEADER, DeferDeleteListEntry) == 0x0);
72 C_ASSERT(FIELD_OFFSET(PH_OBJECT_HEADER, TypeIndex) == 0x8);
73 C_ASSERT(FIELD_OFFSET(PH_OBJECT_HEADER, Flags) == 0xa);
74 C_ASSERT(FIELD_OFFSET(PH_OBJECT_HEADER, Reserved1) == 0xb);
75 C_ASSERT(FIELD_OFFSET(PH_OBJECT_HEADER, Reserved2) == 0xc);
76 C_ASSERT(FIELD_OFFSET(PH_OBJECT_HEADER, Body) == 0x10);
77 #else
78 C_ASSERT(FIELD_OFFSET(PH_OBJECT_HEADER, RefCount) == 0x0);
79 C_ASSERT(FIELD_OFFSET(PH_OBJECT_HEADER, DeferDeleteListEntry) == 0x0);
80 C_ASSERT(FIELD_OFFSET(PH_OBJECT_HEADER, TypeIndex) == 0x4);
81 C_ASSERT(FIELD_OFFSET(PH_OBJECT_HEADER, Flags) == 0x6);
82 C_ASSERT(FIELD_OFFSET(PH_OBJECT_HEADER, Reserved1) == 0x7);
83 C_ASSERT(FIELD_OFFSET(PH_OBJECT_HEADER, Body) == 0x8);
84 #endif
85 #endif
86 
94 #define PhObjectToObjectHeader(Object) ((PPH_OBJECT_HEADER)CONTAINING_RECORD((PCHAR)(Object), PH_OBJECT_HEADER, Body))
95 
103 #define PhObjectHeaderToObject(ObjectHeader) ((PVOID)&((PPH_OBJECT_HEADER)(ObjectHeader))->Body)
104 
112 #define PhAddObjectHeaderSize(Size) ((Size) + FIELD_OFFSET(PH_OBJECT_HEADER, Body))
113 
118 typedef struct _PH_OBJECT_TYPE
119 {
121  USHORT Flags;
122  UCHAR TypeIndex;
123  UCHAR Reserved;
129  PWSTR Name;
133 
140 FORCEINLINE
141 BOOLEAN
143  _Inout_ PLONG RefCount
144  )
145 {
146  /* Here we will attempt to increment the reference count,
147  * making sure that it is not 0.
148  */
149  return _InterlockedIncrementNoZero(RefCount);
150 }
151 
152 PPH_OBJECT_HEADER PhpAllocateObject(
153  _In_ PPH_OBJECT_TYPE ObjectType,
154  _In_ SIZE_T ObjectSize
155  );
156 
158  _In_ PPH_OBJECT_HEADER ObjectHeader
159  );
160 
162  _In_ PPH_OBJECT_HEADER ObjectHeader
163  );
164 
166  _In_ PVOID Parameter
167  );
168 
169 #endif