ReactOS  0.4.14-dev-614-gbfd8a84
ddhmg.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS kernel
4  * PURPOSE: Native driver for dxg implementation
5  * FILE: win32ss/reactx/dxg/ddhmg.c
6  * PROGRAMER: Magnus olsen (magnus@greatlord.com)
7  * Sebastian Gasiorek (sebastian.gasiorek@reactos.org)
8  * REVISION HISTORY:
9  * 30/12-2007 Magnus Olsen
10  */
11 
12 #include <dxg_int.h>
13 
14 /* The DdHmgr manger stuff */
17 
20 
21 /* next free ddhmg handle number available to reuse */
24 
25 BOOL
28 {
29  DWORD Pid = (DWORD)(DWORD_PTR)PsGetCurrentProcessId() & 0xFFFFFFFC;
30  DWORD check = (DWORD_PTR)pEntry->Pid & 0xFFFFFFFE;
31  return ( (check == Pid) || (!check));
32 }
33 
34 /*++
35 * @name DdHmgCreate
36 * @implemented
37 *
38 * The function DdHmgCreate is used internally in dxg.sys
39 * It creates all DX kernel objects that are need it for creation of DX objects.
40 *
41 * @return
42 * Return FALSE for failure and TRUE for success in creating the DX object
43 *
44 * @remarks.
45 * Only used internally in dxg.sys
46 *--*/
47 BOOL
50 {
52  ghFreeDdHmgr = 0;
53  gcMaxDdHmgr = 1;
54 
55  if (gpentDdHmgr)
56  {
58 
59  if (ghsemHmgr)
60  {
62 
63  if (gpLockShortDelay)
64  {
66  return TRUE;
67  }
68 
70  ghsemHmgr = NULL;
71  }
72 
74  gpentDdHmgr = NULL;
75  }
76 
77  return FALSE;
78 }
79 
80 /*++
81 * @name DdHmgDestroy
82 * @implemented
83 *
84 * The function DdHmgDestroy is used internally in dxg.sys
85 * It destroys all DX kernel objects
86 *
87 * @return
88 * Always returns true, as a failure here would result in a BSOD.
89 *
90 * @remarks.
91 * Only used internally in dxg.sys
92 *--*/
93 BOOL
96 {
97  gcMaxDdHmgr = 0;
98  gcSizeDdHmgr = 0;
99  ghFreeDdHmgr = 0;
101 
102  if (gpentDdHmgr)
103  {
105  gpentDdHmgr = NULL;
106  }
107 
108  if (ghsemHmgr)
109  {
111  ghsemHmgr = NULL;
112  }
113 
114  return TRUE;
115 }
116 
117 /*++
118 * @name DdHmgLock
119 * @implemented
120 *
121 * The function DdHmgLock is used internally in dxg.sys
122 * It locks a DX kernel object
123 *
124 * @param HANDLE DdHandle
125 * The handle we want locked
126 *
127 * @param UCHAR ObjectType
128 * The type of the object we expected the handle to contain
129 * value 0 is for ?
130 * value 1 is for EDD_DIRECTDRAW_LOCAL
131 * value 2 is for EDD_SURFACE
132 * value 3 is for ?
133 * value 4 is for EDD_VIDEOPORT
134 * value 5 is for EDD_MOTIONCOMP
135 
136 * @param BOOLEAN LockOwned
137 * If it needs to call EngAcquireSemaphore or not
138 *
139 * @return
140 * Returns an EDD_* object, or NULL if it fails
141 *
142 * @remarks.
143 * Only used internally in dxg.sys
144 *--*/
145 PVOID
146 FASTCALL
147 DdHmgLock(HANDLE DdHandle, UCHAR ObjectType, BOOLEAN LockOwned)
148 {
149  DWORD Index = DDHMG_HTOI(DdHandle);
150 
151  PDD_ENTRY pEntry = NULL;
152  PVOID Object = NULL;
153 
154  if ( !LockOwned )
155  {
157  }
158 
159  if ( Index < gcMaxDdHmgr )
160  {
161  pEntry = (PDD_ENTRY)((PBYTE)gpentDdHmgr + (sizeof(DD_ENTRY) * Index));
162 
163  if ( VerifyObjectOwner(pEntry) )
164  {
165  if ( ( pEntry->Objt == ObjectType ) &&
166  ( pEntry->FullUnique == (((ULONG_PTR)DdHandle >> 21) & 0x7FF) ) &&
167  ( !pEntry->pobj->cExclusiveLock ) )
168  {
170  pEntry->pobj->Tid = KeGetCurrentThread();
171  Object = pEntry->pobj;
172  }
173  }
174  }
175 
176  if ( !LockOwned )
177  {
179  }
180 
181  return Object;
182 }
183 
184 /*++
185 * @name DdAllocateObject
186 * @implemented
187 *
188 * The function DdAllocateObject is used internally in dxg.sys
189 * It allocates memory for a DX kernel object
190 *
191 * @param UINT32 oSize
192 * Size of memory to be allocated
193 * @param UCHAR oType
194 * Object type
195 * @param BOOLEAN oZeroMemory
196 * Zero memory
197 *
198 * @remarks.
199 * Only used internally in dxg.sys
200 */
201 PVOID
202 FASTCALL
203 DdAllocateObject(ULONG objSize, UCHAR objType, BOOLEAN objZeroMemory)
204 {
205  PVOID pObject = NULL;
206 
207  if (objZeroMemory)
208  pObject = EngAllocMem(FL_ZERO_MEMORY, objSize, ((ULONG)objType << 24) + TAG_DH_0);
209  else
210  pObject = EngAllocMem(0, objSize, ((ULONG)objType << 24) + TAG_DH_0);
211 
212  if (!pObject)
213  {
215  }
216 
217  return pObject;
218 }
219 
220 /*++
221 * @name DdFreeObject
222 * @implemented
223 *
224 * The function DdFreeObject is used internally in dxg.sys
225 * It frees memory of DX kernel object
226 *
227 * @param PVOID pObject
228 * Object memory to be freed
229 *
230 * @remarks.
231 * Only used internally in dxg.sys
232 */
233 VOID
234 FASTCALL
236 {
237  EngFreeMem(pObject);
238 }
239 
240 
241 /*++
242 * @name DdGetFreeHandle
243 * @implemented
244 *
245 * The function DdGetFreeHandle is used internally in dxg.sys
246 * It allocates new handle for specified object type
247 *
248 * @param UCHAR oType
249 * Object type
250 *
251 * @return
252 * Returns handle or 0 if it fails.
253 *
254 * @remarks.
255 * Only used internally in dxg.sys
256 *--*/
257 HANDLE
258 FASTCALL
260 {
261  PVOID mAllocMem = NULL;
262  ULONG mAllocEntries = 0;
263  PDD_ENTRY pEntry = NULL;
264  ULONG_PTR retVal;
265  ULONG index;
266 
267  // check if memory is allocated
268  if (!gpentDdHmgr)
269  return 0;
270 
271  // check if we reached maximum handle index
273  return 0;
274 
275  // check if we have free handle to reuse
276  if (ghFreeDdHmgr)
277  {
279  pEntry = (PDD_ENTRY)((PBYTE)gpentDdHmgr + (sizeof(DD_ENTRY) * index));
280 
281  // put next free index to our global variable
282  ghFreeDdHmgr = pEntry->NextFree;
283 
284  // build handle
285  pEntry->FullUnique = objType | 8;
286  retVal = (pEntry->FullUnique << 21) | index;
287  return (HANDLE)retVal;
288  }
289 
290  // if all pre-allocated memory is already used then allocate more
291  if (gcSizeDdHmgr == gcMaxDdHmgr)
292  {
293  // allocate buffer for next 1024 handles
294  mAllocEntries = gcSizeDdHmgr + 1024;
295  mAllocMem = EngAllocMem(FL_ZERO_MEMORY, sizeof(DD_ENTRY) * (mAllocEntries), TAG_THDD);
296  if (!mAllocMem)
297  return 0;
298 
299  memmove(&mAllocMem, gpentDdHmgr, sizeof(DD_ENTRY) * gcSizeDdHmgr);
300  gcSizeDdHmgr = mAllocEntries;
303  gpentDdHmgr = mAllocMem;
304  }
305 
306  pEntry = (PDD_ENTRY)((PBYTE)gpentDdHmgr + (sizeof(DD_ENTRY) * gcMaxDdHmgr));
307 
308  // build handle
309  pEntry->FullUnique = objType | 8;
310  retVal = (pEntry->FullUnique << 21) | gcMaxDdHmgr;
311  gcMaxDdHmgr = gcMaxDdHmgr + 1;
312 
313  return (HANDLE)retVal;
314 }
315 
316 /*++
317 * @name DdHmgAlloc
318 * @implemented
319 *
320 * The function DdHmgAlloc is used internally in dxg.sys
321 * It allocates object
322 *
323 * @param ULONG objSize
324 * Size of memory to be allocated
325 * @param CHAR objType
326 * Object type
327 * @param UINT objLock
328 * Object lock flag
329 *
330 * @return
331 * Handle if object is not locked by objLock
332 * Object if lock is set in objLock
333 * 0 if it fails.
334 *
335 * @remarks.
336 * Only used internally in dxg.sys
337 *--*/
338 HANDLE
339 FASTCALL
340 DdHmgAlloc(ULONG objSize, CHAR objType, BOOLEAN objLock)
341 {
342  PVOID pObject = NULL;
343  HANDLE DdHandle = NULL;
344  PDD_ENTRY pEntry = NULL;
345  DWORD Index;
346 
347  pObject = DdAllocateObject(objSize, objType, TRUE);
348  if (!pObject)
349  return 0;
350 
352 
353  /* Get next free handle */
354  DdHandle = DdGetFreeHandle(objType);
355 
356  if (DdHandle)
357  {
358  Index = DDHMG_HTOI(DdHandle);
359 
360  pEntry = (PDD_ENTRY)((PBYTE)gpentDdHmgr + (sizeof(DD_ENTRY) * Index));
361 
362  pEntry->pobj = pObject;
363  pEntry->Objt = objType;
364 
365  pEntry->Pid = (HANDLE)(((ULONG_PTR)PsGetCurrentProcessId() & 0xFFFFFFFC) | ((ULONG_PTR)(pEntry->Pid) & 1));
366 
367  if (objLock)
368  {
370  pEntry->pobj->Tid = KeGetCurrentThread();
371  }
372  pEntry->pobj->hHmgr = DdHandle;
373 
375 
376  /* Return handle if object not locked */
377  if (!objLock)
378  return DdHandle;
379 
380  return (HANDLE)pEntry;
381  }
382 
384  DdFreeObject(pObject);
385  return 0;
386 }
387 
388 /*++
389 * @name DdHmgFree
390 * @implemented
391 *
392 * The function DdHmgFree is used internally in dxg.sys
393 * It frees DX object and memory allocated to it
394 *
395 * @param HANDLE DdHandle
396 * DX object handle
397 *
398 * @remarks.
399 * Only used internally in dxg.sys
400 *--*/
401 VOID
402 FASTCALL
403 DdHmgFree(HANDLE DdHandle)
404 {
405  PDD_ENTRY pEntry = NULL;
406 
407  DWORD Index = DDHMG_HTOI(DdHandle);
408 
410 
411  pEntry = (PDD_ENTRY)((PBYTE)gpentDdHmgr + (sizeof(DD_ENTRY) * Index));
412 
413  // check if we have object that should be freed
414  if (pEntry->pobj)
415  DdFreeObject(pEntry->pobj);
416 
417  pEntry->NextFree = ghFreeDdHmgr;
418 
419  // reset process ID
420  pEntry->Pid = (HANDLE)((DWORD_PTR)pEntry->Pid & 1);
422 
424 }
PW32THREAD Tid
Definition: dxg_int.h:41
VOID WINAPI EngReleaseSemaphore(IN HSEMAPHORE hsem)
Definition: eng.c:235
ObjectType
Definition: metafile.c:80
PDD_ENTRY gpentDdHmgrLast
Definition: ddhmg.c:19
return
Definition: dirsup.c:529
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
Definition: dxg_int.h:62
#define TRUE
Definition: types.h:120
VOID FASTCALL DdHmgFree(HANDLE DdHandle)
Definition: ddhmg.c:403
#define DDHMG_HTOI(DdHandle)
Definition: dxg_int.h:14
#define DWORD_PTR
Definition: treelist.c:76
UCHAR Objt
Definition: dxg_int.h:71
VOID FASTCALL DdFreeObject(PVOID pObject)
Definition: ddhmg.c:235
PDD_ENTRY gpentDdHmgr
Definition: ddhmg.c:16
#define EngFreeMem
Definition: polytest.cpp:56
char CHAR
Definition: xmlstorage.h:175
BOOL FASTCALL DdHmgDestroy(VOID)
Definition: ddhmg.c:95
_Requires_lock_not_held_ hsem ENGAPI VOID APIENTRY EngDeleteSemaphore(_Inout_ __drv_freesMem(Mem) HSEMAPHORE hsem)
Definition: semaphor.c:106
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
USHORT cExclusiveLock
Definition: dxg_int.h:39
#define FASTCALL
Definition: nt_native.h:50
#define DWORD
Definition: nt_native.h:44
uint32_t ULONG_PTR
Definition: typedefs.h:63
PDD_BASEOBJECT pobj
Definition: dxg_int.h:66
#define FL_NONPAGED_MEMORY
Definition: winddi.h:1359
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL FASTCALL VerifyObjectOwner(PDD_ENTRY pEntry)
Definition: ddhmg.c:27
unsigned char BOOLEAN
ENGAPI VOID APIENTRY EngAcquireSemaphore(_Inout_ HSEMAPHORE hsem)
smooth NULL
Definition: ftsmooth.c:416
HSEMAPHORE ghsemHmgr
Definition: ddhmg.c:23
ULONG NextFree
Definition: dxg_int.h:67
USHORT FullUnique
Definition: dxg_int.h:70
GLuint index
Definition: glext.h:6031
PVOID FASTCALL DdAllocateObject(ULONG objSize, UCHAR objType, BOOLEAN objZeroMemory)
Definition: ddhmg.c:203
DWORD_PTR HSEMAPHORE
Definition: axcore.idl:60
BOOL FASTCALL DdHmgCreate(VOID)
Definition: ddhmg.c:49
PVOID FASTCALL DdHmgLock(HANDLE DdHandle, UCHAR ObjectType, BOOLEAN LockOwned)
Definition: ddhmg.c:147
static const UCHAR Index[8]
Definition: usbohci.c:18
PVOID HANDLE
Definition: typedefs.h:71
unsigned long DWORD
Definition: ntddk_ex.h:95
ULONG gcMaxDdHmgr
Definition: ddhmg.c:18
void check(CONTEXT *pContext)
Definition: NtContinue.c:61
struct _DD_ENTRY DD_ENTRY
static IUnknown Object
Definition: main.c:512
void * EngAllocMem(int zero, unsigned long size, int tag=0)
Definition: polytest.cpp:70
unsigned char UCHAR
Definition: xmlstorage.h:181
#define index(s, c)
Definition: various.h:29
#define FL_ZERO_MEMORY
Definition: polytest.cpp:58
ULONG ghFreeDdHmgr
Definition: ddhmg.c:22
uint32_t DWORD_PTR
Definition: typedefs.h:63
#define TAG_GINI
Definition: tags.h:2
#define TAG_THDD
Definition: tags.h:1
#define InterlockedIncrement
Definition: armddk.h:53
HGDIOBJ hHmgr
Definition: dxg_int.h:37
HANDLE Pid
Definition: dxg_int.h:69
#define TAG_DH_0
Definition: tags.h:6
HANDLE NTAPI PsGetCurrentProcessId(VOID)
Definition: process.c:1123
HANDLE FASTCALL DdGetFreeHandle(UCHAR objType)
Definition: ddhmg.c:259
HANDLE FASTCALL DdHmgAlloc(ULONG objSize, CHAR objType, BOOLEAN objLock)
Definition: ddhmg.c:340
ULONG gcSizeDdHmgr
Definition: ddhmg.c:15
HSEMAPHORE WINAPI EngCreateSemaphore(VOID)
Definition: eng.c:75
unsigned int ULONG
Definition: retypes.h:1
#define ULONG_PTR
Definition: config.h:101
PLARGE_INTEGER gpLockShortDelay
Definition: main.c:18
struct _DD_ENTRY * PDD_ENTRY
#define KeGetCurrentThread
Definition: hal.h:44
BYTE * PBYTE
Definition: pedump.c:66
ENGAPI VOID APIENTRY EngSetLastError(_In_ ULONG iError)
Definition: error.c:27
#define DDHMG_HANDLE_LIMIT
Definition: dxg_int.h:13