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>
15 #include <internal/kernel.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
;
41 DPRINT("ObGenericCreateObject(Handle %x ObjectAttributes %x Type %x)\n",
42 Handle
,ObjectAttributes
,Type
);
45 * Allocate the object body and header
47 hdr
=(POBJECT_HEADER
)ExAllocatePool(NonPagedPool
,OBJECT_ALLOC_SIZE(Type
));
54 * Initialize the object header
56 if (ObjectAttributes
!=NULL
)
58 ObInitializeObjectHeader(Type
,&ObjectAttributes
->name
,hdr
);
62 ObInitializeObjectHeader(Type
,NULL
,hdr
);
65 // DPRINT("ObjectAttributes->parent->Type %d\n",
66 // ObjectAttributes->parent->Type);
67 if (ObjectAttributes
!=NULL
)
70 * Add the object to its parent directory
72 DPRINT("hdr->name.Buffer %x\n",hdr
->name
.Buffer
);
73 ObCreateEntry(ObjectAttributes
->parent
,hdr
);
76 DPRINT("Handle %x\n",Handle
);
77 *Handle
= ObAddHandle(HEADER_TO_BODY(hdr
));
79 return(HEADER_TO_BODY(hdr
));
82 ULONG
ObSizeOf(CSHORT Type
)
84 return(ObjectTypes
[Type
]->PagedPoolCharge
);
87 VOID
ObRegisterType(CSHORT id
, POBJECT_TYPE type
)
89 * FUNCTION: Registers a new type of object
91 * typ = Pointer to the type definition to register
94 DPRINT("ObRegisterType(id %d, type %x)\n",id
,type
);
98 VOID
ObInitializeObjectHeader(CSHORT id
, PUNICODE_STRING name
,
101 * FUNCTION: Creates a new object
103 * id = Identifier for the type of object
104 * obj = Pointer to the header of the object
109 DPRINT("ObInitializeObjectHeader(id %d name %w obj %x)\n",id
,
114 DPRINT("ObInitializeObjectHeader(id %d name %x obj %x)\n",id
,
118 obj
->HandleCount
= 0;
124 obj
->name
.Buffer
=NULL
;
128 obj
->name
.Length
= 0;
129 obj
->name
.MaximumLength
= name
->Length
;
130 obj
->name
.Buffer
= ExAllocatePool(NonPagedPool
,
131 (name
->Length
+1)*sizeof(WCHAR
));
132 DPRINT("obj->name.Buffer %x\n",obj
->name
.Buffer
);
133 RtlCopyUnicodeString(&obj
->name
,name
);
134 DPRINT("obj->name.Buffer %x\n",obj
->name
.Buffer
);
139 NTSTATUS
ObReferenceObjectByPointer(PVOID ObjectBody
,
140 ACCESS_MASK DesiredAccess
,
141 POBJECT_TYPE ObjectType
,
142 KPROCESSOR_MODE AccessMode
)
144 * FUNCTION: Increments the pointer reference count for a given object
146 * ObjectBody = Object's body
147 * DesiredAccess = Desired access to the object
148 * ObjectType = Points to the object type structure
149 * AccessMode = Type of access check to perform
153 POBJECT_HEADER Object
= BODY_TO_HEADER(ObjectBody
);
155 DPRINT("ObReferenceObjectByPointer(%x %x)\n",ObjectBody
,Object
);
158 return(STATUS_SUCCESS
);
161 VOID
ObDereferenceObject(PVOID ObjectBody
)
163 * FUNCTION: Decrements a given object's reference count and performs
166 * ObjectBody = Body of the object
169 POBJECT_HEADER Object
= BODY_TO_HEADER(ObjectBody
);
173 NTSTATUS
ZwClose(HANDLE Handle
)
175 * FUNCTION: Closes a handle reference to an object
177 * Handle = handle to close
183 ASSERT_IRQL(PASSIVE_LEVEL
);
185 ObjectBody
= ObGetObjectByHandle(Handle
);
186 if (ObjectBody
== NULL
)
188 return(STATUS_INVALID_HANDLE
);
190 ObDereferenceObject(ObjectBody
);
191 return(STATUS_SUCCESS
);
194 NTSTATUS
ObReferenceObjectByHandle(HANDLE Handle
,
195 ACCESS_MASK DesiredAccess
,
196 POBJECT_TYPE ObjectType
,
197 KPROCESSOR_MODE AccessMode
,
199 POBJECT_HANDLE_INFORMATION
203 * FUNCTION: Increments the reference count for an object and returns a
204 * pointer to its body
206 * Handle = Handle for the object
207 * DesiredAccess = Desired access to the object
210 * Object (OUT) = Points to the object body on return
211 * HandleInformation (OUT) = Contains information about the handle
218 ASSERT_IRQL(PASSIVE_LEVEL
);
219 assert(HandleInformation
==NULL
);
220 assert(Object
!=NULL
);
221 assert(Handle
!=NULL
);
223 ObjectBody
= ObGetObjectByHandle(Handle
);
224 if (ObjectBody
== NULL
)
226 return(STATUS_INVALID_HANDLE
);
228 return(ObReferenceObjectByPointer(ObjectBody
,DesiredAccess
,
229 ObjectType
,AccessMode
));