[CMAKE]
[reactos.git] / subsystems / win32 / win32k / eng / driverobj.c
1 /*
2 * COPYRIGHT: GPL, see COPYING in the top level directory
3 * PROJECT: ReactOS win32 kernel mode sunsystem
4 * PURPOSE: GDI DRIVEROBJ Functions
5 * FILE: subsystems/win32k/eng/driverobj.c
6 * PROGRAMER: Timo Kreuzer
7 */
8
9 /** Includes ******************************************************************/
10
11 #include <win32k.h>
12
13 #define NDEBUG
14 #include <debug.h>
15
16
17 /** Internal interface ********************************************************/
18
19 /*!
20 * \brief DRIVEROBJ cleanup function
21 */
22 BOOL INTERNAL_CALL
23 DRIVEROBJ_Cleanup(PVOID pObject)
24 {
25 PEDRIVEROBJ pedo = pObject;
26 FREEOBJPROC pFreeProc;
27
28 pFreeProc = pedo->drvobj.pFreeProc;
29 if (pFreeProc)
30 {
31 return pFreeProc(pedo->drvobj.pvObj);
32 }
33
34 return TRUE;
35 }
36
37 /** Public interface **********************************************************/
38
39 HDRVOBJ
40 APIENTRY
41 EngCreateDriverObj(
42 IN PVOID pvObj,
43 IN FREEOBJPROC pFreeObjProc,
44 IN HDEV hdev)
45 {
46 PEDRIVEROBJ pedo;
47 HDRVOBJ hdo;
48 PDEVOBJ *ppdev = (PDEVOBJ*)hdev;
49
50 /* Allocate a new DRIVEROBJ */
51 pedo = DRIVEROBJ_AllocObjectWithHandle();
52 if (!pedo)
53 {
54 return NULL;
55 }
56 hdo = pedo->baseobj.hHmgr;
57
58 /* Fill in fields */
59 pedo->drvobj.pvObj = pvObj;
60 pedo->drvobj.pFreeProc = pFreeObjProc;
61 pedo->drvobj.hdev = hdev;
62 pedo->drvobj.dhpdev = ppdev->dhpdev;
63
64 /* Unlock the object */
65 DRIVEROBJ_UnlockObject(pedo);
66
67 /* Return the handle */
68 return hdo;
69 }
70
71
72 BOOL
73 APIENTRY
74 EngDeleteDriverObj(
75 IN HDRVOBJ hdo,
76 IN BOOL bCallBack,
77 IN BOOL bLocked)
78 {
79 PEDRIVEROBJ pedo;
80
81 /* Lock the object */
82 pedo = DRIVEROBJ_LockObject(hdo);
83 if (!pedo)
84 {
85 return FALSE;
86 }
87
88 /* Manually call cleanup callback */
89 if (bCallBack)
90 {
91 if (!pedo->drvobj.pFreeProc(pedo->drvobj.pvObj))
92 {
93 /* Callback failed */
94 DRIVEROBJ_UnlockObject(pedo);
95 return FALSE;
96 }
97 }
98
99 /* Prevent cleanup callback from being called again */
100 pedo->drvobj.pFreeProc = NULL;
101
102 /* NOTE: We don't care about the bLocked param, as our handle manager
103 allows freeing the object, while we hold any number of locks. */
104
105 /* Delete the object */
106 GDIOBJ_vDeleteObject(&pedo->baseobj);
107 return TRUE;
108 }
109
110
111 PDRIVEROBJ
112 APIENTRY
113 EngLockDriverObj(
114 IN HDRVOBJ hdo)
115 {
116 PEDRIVEROBJ pedo;
117
118 /* Lock the object */
119 pedo = DRIVEROBJ_LockObject(hdo);
120
121 /* Return pointer to the DRIVEROBJ structure */
122 return &pedo->drvobj;
123 }
124
125
126 BOOL
127 APIENTRY
128 EngUnlockDriverObj(
129 IN HDRVOBJ hdo)
130 {
131 PEDRIVEROBJ pedo;
132 ULONG cLocks;
133
134 /* First lock to get a pointer to the object */
135 pedo = DRIVEROBJ_LockObject(hdo);
136 if(!pedo)
137 {
138 /* Object could not be locked, fail. */
139 return FALSE;
140 }
141
142 /* Unlock object */
143 cLocks = pedo->baseobj.cExclusiveLock;
144 DRIVEROBJ_UnlockObject(pedo);
145
146 /* Check if we still hold a lock */
147 if (cLocks < 2)
148 {
149 /* Object wasn't locked before, fail. */
150 return FALSE;
151 }
152
153 /* Unlock again */
154 DRIVEROBJ_UnlockObject(pedo);
155
156 /* Success */
157 return TRUE;
158 }
159