2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ob/object.c
5 * PURPOSE: Implements generic object managment functions
6 * PROGRAMMER: David Welch (welch@mcmail.com)
11 /* INCLUDES *****************************************************************/
13 #include <ddk/ntddk.h>
14 #include <internal/objmgr.h>
18 #include <internal/debug.h>
20 /* GLOBALS ****************************************************************/
23 * List of pointers to object types
25 static POBJECT_TYPE ObjectTypes
[OBJTYP_MAX
]={NULL
,};
27 /* FUNCTIONS ************************************************************/
29 NTSTATUS
ZwMakeTemporaryObject(HANDLE Handle
)
34 PVOID
ObGenericCreateObject(PHANDLE Handle
,
35 ACCESS_MASK DesiredAccess
,
36 POBJECT_ATTRIBUTES ObjectAttributes
,
39 POBJECT_HEADER hdr
= NULL
;
40 UNICODE_STRING ObjectName
;
43 PDIRECTORY_OBJECT parent
;
45 DPRINT("ObGenericCreateObject(Handle %x, DesiredAccess %x,"
46 "ObjectAttributes %x, Type %x)\n",Handle
,DesiredAccess
,ObjectAttributes
,
50 * Allocate the object body and header
52 hdr
=(POBJECT_HEADER
)ExAllocatePool(NonPagedPool
,OBJECT_ALLOC_SIZE(Type
));
59 * If unnamed then initalize
61 if (ObjectAttributes
==NULL
)
63 ObInitializeObjectHeader(Type
,NULL
,hdr
);
64 *Handle
= ObAddHandle(HEADER_TO_BODY(hdr
));
65 return(HEADER_TO_BODY(hdr
));
69 * Copy the object name into a buffer
71 DPRINT("ObjectAttributes->ObjectName %x\n",ObjectAttributes
->ObjectName
);
72 DPRINT("ObjectAttributes->ObjectName->Length %d\n",
73 ObjectAttributes
->ObjectName
->Length
);
74 ObjectName
.MaximumLength
= ObjectAttributes
->ObjectName
->Length
;
75 ObjectName
.Buffer
= ExAllocatePool(NonPagedPool
,
76 ((ObjectAttributes
->ObjectName
->Length
+1)*2));
77 if (ObjectName
.Buffer
==NULL
)
81 RtlCopyUnicodeString(&ObjectName
,ObjectAttributes
->ObjectName
);
84 * Seperate the name into a path and name
86 name
= wcsrchr(ObjectName
.Buffer
,'\\');
89 name
=ObjectName
.Buffer
;
94 path
=ObjectName
.Buffer
;
99 hdr
->Parent
= ObLookupObject(ObjectAttributes
->RootDirectory
,path
);
102 * Initialize the object header
104 ObInitializeObjectHeader(Type
,name
,hdr
);
105 ObCreateEntry(hdr
->Parent
,hdr
);
107 DPRINT("Handle %x\n",Handle
);
108 *Handle
= ObAddHandle(HEADER_TO_BODY(hdr
));
110 return(HEADER_TO_BODY(hdr
));
113 ULONG
ObSizeOf(CSHORT Type
)
115 DPRINT("ObSizeOf(Type %d)\n",Type
);
116 DPRINT("ObSizeOf() Returning %d\n",ObjectTypes
[Type
]->PagedPoolCharge
);
117 return(ObjectTypes
[Type
]->PagedPoolCharge
);
120 VOID
ObRegisterType(CSHORT id
, POBJECT_TYPE type
)
122 * FUNCTION: Registers a new type of object
124 * typ = Pointer to the type definition to register
127 DPRINT("ObRegisterType(id %d, type %x)\n",id
,type
);
128 ObjectTypes
[id
]=type
;
131 VOID
ObInitializeObjectHeader(CSHORT id
, PWSTR name
,
134 * FUNCTION: Creates a new object
136 * id = Identifier for the type of object
137 * obj = Pointer to the header of the object
144 DPRINT("ObInitializeObjectHeader(id %d name %w obj %x)\n",id
,
149 DPRINT("ObInitializeObjectHeader(id %d name %x obj %x)\n",id
,
153 obj
->HandleCount
= 0;
159 obj
->name
.Buffer
=NULL
;
163 DPRINT("name %w\n",name
);
164 obj
->name
.MaximumLength
= wstrlen(name
);
165 obj
->name
.Buffer
= ExAllocatePool(NonPagedPool
,
166 (obj
->name
.MaximumLength
+1)*2);
167 DPRINT("name %w\n",name
);
168 RtlInitUnicodeString(&obj
->name
,name
);
169 DPRINT("name %w\n",obj
->name
.Buffer
);
174 NTSTATUS
ObReferenceObjectByPointer(PVOID ObjectBody
,
175 ACCESS_MASK DesiredAccess
,
176 POBJECT_TYPE ObjectType
,
177 KPROCESSOR_MODE AccessMode
)
179 * FUNCTION: Increments the pointer reference count for a given object
181 * ObjectBody = Object's body
182 * DesiredAccess = Desired access to the object
183 * ObjectType = Points to the object type structure
184 * AccessMode = Type of access check to perform
188 POBJECT_HEADER Object
;
190 DPRINT("ObReferenceObjectByPointer(%x)\n",ObjectBody
);
192 Object
= BODY_TO_HEADER(ObjectBody
);
194 return(STATUS_SUCCESS
);
197 VOID
ObDereferenceObject(PVOID ObjectBody
)
199 * FUNCTION: Decrements a given object's reference count and performs
202 * ObjectBody = Body of the object
205 POBJECT_HEADER Object
= BODY_TO_HEADER(ObjectBody
);
209 NTSTATUS
ZwClose(HANDLE Handle
)
211 * FUNCTION: Closes a handle reference to an object
213 * Handle = handle to close
219 assert_irql(PASSIVE_LEVEL
);
221 ObjectBody
= ObGetObjectByHandle(Handle
);
222 if (ObjectBody
== NULL
)
224 return(STATUS_INVALID_HANDLE
);
226 ObDereferenceObject(ObjectBody
);
227 return(STATUS_SUCCESS
);
230 NTSTATUS
ObReferenceObjectByHandle(HANDLE Handle
,
231 ACCESS_MASK DesiredAccess
,
232 POBJECT_TYPE ObjectType
,
233 KPROCESSOR_MODE AccessMode
,
235 POBJECT_HANDLE_INFORMATION
239 * FUNCTION: Increments the reference count for an object and returns a
240 * pointer to its body
242 * Handle = Handle for the object
243 * DesiredAccess = Desired access to the object
246 * Object (OUT) = Points to the object body on return
247 * HandleInformation (OUT) = Contains information about the handle
254 ASSERT_IRQL(PASSIVE_LEVEL
);
255 assert(HandleInformationPtr
==NULL
);
256 assert(Object
!=NULL
);
257 assert(Handle
!=NULL
);
259 ObjectBody
= ObGetObjectByHandle(Handle
);
260 if (ObjectBody
== NULL
)
262 return(STATUS_INVALID_HANDLE
);
264 return(ObReferenceObjectByPointer(ObjectBody
,DesiredAccess
,
265 ObjectType
,AccessMode
));