a83d29e93b840b8f2c5cef4267c8bec625d8bc06
[reactos.git] / reactos / ntoskrnl / ob / object.c
1 /* $Id: object.c,v 1.78 2004/07/16 17:19:15 ekohl Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ob/object.c
6 * PURPOSE: Implements generic object managment functions
7 * PROGRAMMERS David Welch (welch@cwcom.net), Skywing (skywing@valhallalegends.com)
8 * UPDATE HISTORY:
9 * 10/06/98: Created
10 * 09/13/03: Fixed various ObXxx routines to not call retention
11 * checks directly at a raised IRQL.
12 */
13
14 /* INCLUDES *****************************************************************/
15
16 #define NTOS_MODE_KERNEL
17 #include <ntos.h>
18 #include <roscfg.h>
19 #include <internal/ob.h>
20 #include <internal/ps.h>
21 #include <internal/id.h>
22 #include <internal/ke.h>
23
24 #define NDEBUG
25 #include <internal/debug.h>
26
27
28 typedef struct _RETENTION_CHECK_PARAMS
29 {
30 WORK_QUEUE_ITEM WorkItem;
31 POBJECT_HEADER ObjectHeader;
32 } RETENTION_CHECK_PARAMS, *PRETENTION_CHECK_PARAMS;
33
34
35 /* FUNCTIONS ************************************************************/
36
37 PVOID HEADER_TO_BODY(POBJECT_HEADER obj)
38 {
39 return(((char*)obj)+sizeof(OBJECT_HEADER)-sizeof(COMMON_BODY_HEADER));
40 }
41
42
43 POBJECT_HEADER BODY_TO_HEADER(PVOID body)
44 {
45 PCOMMON_BODY_HEADER chdr = (PCOMMON_BODY_HEADER)body;
46 return(CONTAINING_RECORD((&(chdr->Type)),OBJECT_HEADER,Type));
47 }
48
49
50 /**********************************************************************
51 * NAME PRIVATE
52 * ObFindObject@16
53 *
54 * DESCRIPTION
55 *
56 * ARGUMENTS
57 * ObjectAttributes
58 *
59 * ReturnedObject
60 *
61 * RemainigPath
62 * Pointer to a unicode string that will contain the
63 * remaining path if the function returns successfully.
64 * The caller must free the buffer after use by calling
65 * RtlFreeUnicodeString ().
66 *
67 * ObjectType
68 * Optional pointer to an object type. This is used to
69 * descide if a symbolic link object will be parsed or not.
70 *
71 * RETURN VALUE
72 */
73 NTSTATUS
74 ObFindObject(POBJECT_ATTRIBUTES ObjectAttributes,
75 PVOID* ReturnedObject,
76 PUNICODE_STRING RemainingPath,
77 POBJECT_TYPE ObjectType)
78 {
79 PVOID NextObject;
80 PVOID CurrentObject;
81 PVOID RootObject;
82 POBJECT_HEADER CurrentHeader;
83 NTSTATUS Status;
84 PWSTR current;
85 UNICODE_STRING PathString;
86 ULONG Attributes;
87 PUNICODE_STRING ObjectName;
88
89 DPRINT("ObFindObject(ObjectAttributes %x, ReturnedObject %x, "
90 "RemainingPath %x)\n",ObjectAttributes,ReturnedObject,RemainingPath);
91 DPRINT("ObjectAttributes->ObjectName %wZ\n",
92 ObjectAttributes->ObjectName);
93
94 RtlInitUnicodeString (RemainingPath, NULL);
95
96 if (ObjectAttributes->RootDirectory == NULL)
97 {
98 ObReferenceObjectByPointer(NameSpaceRoot,
99 DIRECTORY_TRAVERSE,
100 NULL,
101 UserMode);
102 CurrentObject = NameSpaceRoot;
103 }
104 else
105 {
106 Status = ObReferenceObjectByHandle(ObjectAttributes->RootDirectory,
107 DIRECTORY_TRAVERSE,
108 NULL,
109 UserMode,
110 &CurrentObject,
111 NULL);
112 if (!NT_SUCCESS(Status))
113 {
114 return Status;
115 }
116 }
117
118 ObjectName = ObjectAttributes->ObjectName;
119 if (ObjectName->Length == 0 ||
120 ObjectName->Buffer[0] == UNICODE_NULL)
121 {
122 *ReturnedObject = CurrentObject;
123 return STATUS_SUCCESS;
124 }
125
126 if (ObjectAttributes->RootDirectory == NULL &&
127 ObjectName->Buffer[0] != L'\\')
128 {
129 ObDereferenceObject (CurrentObject);
130 return STATUS_UNSUCCESSFUL;
131 }
132
133 /* Create a zero-terminated copy of the object name */
134 PathString.Length = ObjectName->Length;
135 PathString.MaximumLength = ObjectName->Length + sizeof(WCHAR);
136 PathString.Buffer = ExAllocatePool (NonPagedPool,
137 PathString.MaximumLength);
138 if (PathString.Buffer == NULL)
139 {
140 ObDereferenceObject (CurrentObject);
141 return STATUS_INSUFFICIENT_RESOURCES;
142 }
143
144 RtlCopyMemory (PathString.Buffer,
145 ObjectName->Buffer,
146 ObjectName->Length);
147 PathString.Buffer[PathString.Length / sizeof(WCHAR)] = UNICODE_NULL;
148
149 current = PathString.Buffer;
150
151 RootObject = CurrentObject;
152 Attributes = ObjectAttributes->Attributes;
153 if (ObjectType == ObSymbolicLinkType)
154 Attributes |= OBJ_OPENLINK;
155
156 while (TRUE)
157 {
158 DPRINT("current %S\n",current);
159 CurrentHeader = BODY_TO_HEADER(CurrentObject);
160
161 DPRINT("Current ObjectType %wZ\n",
162 &CurrentHeader->ObjectType->TypeName);
163
164 if (CurrentHeader->ObjectType->Parse == NULL)
165 {
166 DPRINT("Current object can't parse\n");
167 break;
168 }
169 Status = CurrentHeader->ObjectType->Parse(CurrentObject,
170 &NextObject,
171 &PathString,
172 &current,
173 Attributes);
174 if (Status == STATUS_REPARSE)
175 {
176 /* reparse the object path */
177 NextObject = NameSpaceRoot;
178 current = PathString.Buffer;
179
180 ObReferenceObjectByPointer(NextObject,
181 DIRECTORY_TRAVERSE,
182 NULL,
183 UserMode);
184 }
185
186 if (NextObject == NULL)
187 {
188 break;
189 }
190 ObDereferenceObject(CurrentObject);
191 CurrentObject = NextObject;
192 }
193
194 if (current)
195 RtlCreateUnicodeString (RemainingPath, current);
196 RtlFreeUnicodeString (&PathString);
197 *ReturnedObject = CurrentObject;
198
199 return STATUS_SUCCESS;
200 }
201
202
203 /**********************************************************************
204 * NAME EXPORTED
205 * ObQueryNameString@16
206 *
207 * DESCRIPTION
208 *
209 * ARGUMENTS
210 *
211 * RETURN VALUE
212 *
213 * @implemented
214 */
215 NTSTATUS STDCALL
216 ObQueryNameString (IN PVOID Object,
217 OUT POBJECT_NAME_INFORMATION ObjectNameInfo,
218 IN ULONG Length,
219 OUT PULONG ReturnLength)
220 {
221 POBJECT_NAME_INFORMATION LocalInfo;
222 POBJECT_HEADER ObjectHeader;
223 ULONG LocalReturnLength;
224 NTSTATUS Status;
225
226 *ReturnLength = 0;
227
228 if (Length < sizeof(OBJECT_NAME_INFORMATION) + sizeof(WCHAR))
229 return STATUS_INVALID_BUFFER_SIZE;
230
231 ObjectNameInfo->Name.MaximumLength = (USHORT)(Length - sizeof(OBJECT_NAME_INFORMATION));
232 ObjectNameInfo->Name.Length = 0;
233 ObjectNameInfo->Name.Buffer =
234 (PWCHAR)((ULONG_PTR)ObjectNameInfo + sizeof(OBJECT_NAME_INFORMATION));
235 ObjectNameInfo->Name.Buffer[0] = 0;
236
237 ObjectHeader = BODY_TO_HEADER(Object);
238
239 if (ObjectHeader->ObjectType != NULL &&
240 ObjectHeader->ObjectType->QueryName != NULL)
241 {
242 DPRINT ("Calling %x\n", ObjectHeader->ObjectType->QueryName);
243 Status = ObjectHeader->ObjectType->QueryName (Object,
244 ObjectNameInfo,
245 Length,
246 ReturnLength);
247 }
248 else if (ObjectHeader->Name.Length > 0 && ObjectHeader->Name.Buffer != NULL)
249 {
250 DPRINT ("Object does not have a 'QueryName' function\n");
251
252 if (ObjectHeader->Parent == NameSpaceRoot)
253 {
254 DPRINT ("Reached the root directory\n");
255 ObjectNameInfo->Name.Length = 0;
256 ObjectNameInfo->Name.Buffer[0] = 0;
257 Status = STATUS_SUCCESS;
258 }
259 else if (ObjectHeader->Parent != NULL)
260 {
261 LocalInfo = ExAllocatePool (NonPagedPool,
262 sizeof(OBJECT_NAME_INFORMATION) +
263 MAX_PATH * sizeof(WCHAR));
264 if (LocalInfo == NULL)
265 return STATUS_INSUFFICIENT_RESOURCES;
266
267 Status = ObQueryNameString (ObjectHeader->Parent,
268 LocalInfo,
269 MAX_PATH * sizeof(WCHAR),
270 &LocalReturnLength);
271 if (!NT_SUCCESS (Status))
272 {
273 ExFreePool (LocalInfo);
274 return Status;
275 }
276
277 Status = RtlAppendUnicodeStringToString (&ObjectNameInfo->Name,
278 &LocalInfo->Name);
279
280 ExFreePool (LocalInfo);
281
282 if (!NT_SUCCESS (Status))
283 return Status;
284 }
285
286 DPRINT ("Object path %wZ\n", &ObjectHeader->Name);
287 Status = RtlAppendUnicodeToString (&ObjectNameInfo->Name,
288 L"\\");
289 if (!NT_SUCCESS (Status))
290 return Status;
291
292 Status = RtlAppendUnicodeStringToString (&ObjectNameInfo->Name,
293 &ObjectHeader->Name);
294 }
295 else
296 {
297 DPRINT ("Object is unnamed\n");
298
299 ObjectNameInfo->Name.MaximumLength = 0;
300 ObjectNameInfo->Name.Length = 0;
301 ObjectNameInfo->Name.Buffer = NULL;
302
303 Status = STATUS_SUCCESS;
304 }
305
306 if (NT_SUCCESS (Status))
307 {
308 ObjectNameInfo->Name.MaximumLength =
309 (ObjectNameInfo->Name.Length) ? ObjectNameInfo->Name.Length + sizeof(WCHAR) : 0;
310 *ReturnLength =
311 sizeof(OBJECT_NAME_INFORMATION) + ObjectNameInfo->Name.MaximumLength;
312 DPRINT ("Returned object path: %wZ\n", &ObjectNameInfo->Name);
313 }
314
315 return Status;
316 }
317
318
319 /**********************************************************************
320 * NAME EXPORTED
321 * ObCreateObject@36
322 *
323 * DESCRIPTION
324 *
325 * ARGUMENTS
326 *
327 * RETURN VALUE
328 * Status
329 *
330 * @implemented
331 */
332 NTSTATUS STDCALL
333 ObCreateObject (IN KPROCESSOR_MODE ObjectAttributesAccessMode OPTIONAL,
334 IN POBJECT_TYPE Type,
335 IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
336 IN KPROCESSOR_MODE AccessMode,
337 IN OUT PVOID ParseContext OPTIONAL,
338 IN ULONG ObjectSize,
339 IN ULONG PagedPoolCharge OPTIONAL,
340 IN ULONG NonPagedPoolCharge OPTIONAL,
341 OUT PVOID *Object)
342 {
343 PVOID Parent = NULL;
344 UNICODE_STRING RemainingPath;
345 POBJECT_HEADER Header;
346 POBJECT_HEADER ParentHeader = NULL;
347 NTSTATUS Status;
348 BOOLEAN ObjectAttached = FALSE;
349 PWCHAR NamePtr;
350
351 assert_irql(APC_LEVEL);
352
353 DPRINT("ObCreateObject(Type %p ObjectAttributes %p, Object %p)\n",
354 Type, ObjectAttributes, Object);
355
356 if (ObjectAttributes != NULL &&
357 ObjectAttributes->ObjectName != NULL &&
358 ObjectAttributes->ObjectName->Buffer != NULL)
359 {
360 Status = ObFindObject(ObjectAttributes,
361 &Parent,
362 &RemainingPath,
363 NULL);
364 if (!NT_SUCCESS(Status))
365 {
366 DPRINT("ObFindObject() failed! (Status 0x%x)\n", Status);
367 return(Status);
368 }
369 }
370 else
371 {
372 RtlInitUnicodeString(&RemainingPath, NULL);
373 }
374
375 Header = (POBJECT_HEADER)ExAllocatePoolWithTag(NonPagedPool,
376 OBJECT_ALLOC_SIZE(ObjectSize),
377 Type->Tag);
378 if (Header == NULL)
379 return STATUS_INSUFFICIENT_RESOURCES;
380 RtlZeroMemory(Header, OBJECT_ALLOC_SIZE(ObjectSize));
381
382 /* Initialize the object header */
383 Header->HandleCount = 0;
384 Header->RefCount = 1;
385 Header->ObjectType = Type;
386 if (ObjectAttributes != NULL &&
387 ObjectAttributes->Attributes & OBJ_PERMANENT)
388 {
389 Header->Permanent = TRUE;
390 }
391 else
392 {
393 Header->Permanent = FALSE;
394 }
395
396 if (ObjectAttributes != NULL &&
397 ObjectAttributes->Attributes & OBJ_INHERIT)
398 {
399 Header->Inherit = TRUE;
400 }
401 else
402 {
403 Header->Inherit = FALSE;
404 }
405
406 RtlInitUnicodeString(&(Header->Name),NULL);
407
408 if (Parent != NULL)
409 {
410 ParentHeader = BODY_TO_HEADER(Parent);
411 }
412
413 if (ParentHeader != NULL &&
414 ParentHeader->ObjectType == ObDirectoryType &&
415 RemainingPath.Buffer != NULL)
416 {
417 NamePtr = RemainingPath.Buffer;
418 if (*NamePtr == L'\\')
419 NamePtr++;
420
421 ObpAddEntryDirectory(Parent,
422 Header,
423 NamePtr);
424
425 ObjectAttached = TRUE;
426 }
427
428 if ((Header->ObjectType != NULL) &&
429 (Header->ObjectType->Create != NULL))
430 {
431 DPRINT("Calling %x\n", Header->ObjectType->Create);
432 Status = Header->ObjectType->Create(HEADER_TO_BODY(Header),
433 Parent,
434 RemainingPath.Buffer,
435 ObjectAttributes);
436 if (!NT_SUCCESS(Status))
437 {
438 if (ObjectAttached == TRUE)
439 {
440 ObpRemoveEntryDirectory(Header);
441 }
442 if (Parent)
443 {
444 ObDereferenceObject(Parent);
445 }
446 RtlFreeUnicodeString(&Header->Name);
447 RtlFreeUnicodeString(&RemainingPath);
448 ExFreePool(Header);
449 return(Status);
450 }
451 }
452 RtlFreeUnicodeString( &RemainingPath );
453
454 if (Header->ObjectType != NULL)
455 {
456 /* FIXME: Call SeAssignSecurity() to create a new security descriptor */
457
458 if (Header->ObjectType->Security != NULL)
459 {
460 /* FIXME: Call the security method */
461 Status = STATUS_SUCCESS;
462 }
463 else
464 {
465 /* Assign the security descriptor to the object header */
466 if (ObjectAttributes != NULL && ObjectAttributes->SecurityDescriptor != NULL)
467 {
468 Status = ObpAddSecurityDescriptor(ObjectAttributes->SecurityDescriptor,
469 &Header->SecurityDescriptor);
470 }
471 else
472 {
473 Status = STATUS_SUCCESS;
474 }
475 }
476 }
477
478 if (Object != NULL)
479 {
480 *Object = HEADER_TO_BODY(Header);
481 }
482
483 return(STATUS_SUCCESS);
484 }
485
486
487 /*
488 * @implemented
489 */
490 NTSTATUS STDCALL
491 ObReferenceObjectByPointer(IN PVOID Object,
492 IN ACCESS_MASK DesiredAccess,
493 IN POBJECT_TYPE ObjectType,
494 IN KPROCESSOR_MODE AccessMode)
495 /*
496 * FUNCTION: Increments the pointer reference count for a given object
497 * ARGUMENTS:
498 * ObjectBody = Object's body
499 * DesiredAccess = Desired access to the object
500 * ObjectType = Points to the object type structure
501 * AccessMode = Type of access check to perform
502 * RETURNS: Status
503 */
504 {
505 POBJECT_HEADER Header;
506
507 DPRINT("ObReferenceObjectByPointer(Object %x, ObjectType %x)\n",
508 Object,ObjectType);
509
510 Header = BODY_TO_HEADER(Object);
511
512 if (ObjectType != NULL && Header->ObjectType != ObjectType)
513 {
514 DPRINT("Failed %x (type was %x %S) should be %x %S\n",
515 Header,
516 Header->ObjectType,
517 Header->ObjectType->TypeName.Buffer,
518 ObjectType,
519 ObjectType->TypeName.Buffer);
520 return(STATUS_UNSUCCESSFUL);
521 }
522 if (Header->ObjectType == PsProcessType)
523 {
524 DPRINT("Ref p 0x%x refcount %d type %x ",
525 Object, Header->RefCount, PsProcessType);
526 DPRINT("eip %x\n", ((PULONG)&Object)[-1]);
527 }
528 if (Header->ObjectType == PsThreadType)
529 {
530 DPRINT("Deref t 0x%x with refcount %d type %x ",
531 Object, Header->RefCount, PsThreadType);
532 DPRINT("eip %x\n", ((PULONG)&Object)[-1]);
533 }
534
535 if (Header->CloseInProcess)
536 {
537 if (Header->ObjectType == PsProcessType)
538 {
539 return STATUS_PROCESS_IS_TERMINATING;
540 }
541 if (Header->ObjectType == PsThreadType)
542 {
543 return STATUS_THREAD_IS_TERMINATING;
544 }
545 return(STATUS_UNSUCCESSFUL);
546 }
547
548 InterlockedIncrement(&Header->RefCount);
549
550 return(STATUS_SUCCESS);
551 }
552
553
554 /*
555 * @implemented
556 */
557 NTSTATUS STDCALL
558 ObOpenObjectByPointer(IN POBJECT Object,
559 IN ULONG HandleAttributes,
560 IN PACCESS_STATE PassedAccessState,
561 IN ACCESS_MASK DesiredAccess,
562 IN POBJECT_TYPE ObjectType,
563 IN KPROCESSOR_MODE AccessMode,
564 OUT PHANDLE Handle)
565 {
566 NTSTATUS Status;
567
568 DPRINT("ObOpenObjectByPointer()\n");
569
570 Status = ObReferenceObjectByPointer(Object,
571 0,
572 ObjectType,
573 AccessMode);
574 if (!NT_SUCCESS(Status))
575 {
576 return Status;
577 }
578
579 Status = ObCreateHandle(PsGetCurrentProcess(),
580 Object,
581 DesiredAccess,
582 (BOOLEAN)(HandleAttributes & OBJ_INHERIT),
583 Handle);
584
585 ObDereferenceObject(Object);
586
587 return STATUS_SUCCESS;
588 }
589
590
591 static NTSTATUS
592 ObpDeleteObject(POBJECT_HEADER Header)
593 {
594 DPRINT("ObPerformRetentionChecks(Header %p)\n", Header);
595 if (KeGetCurrentIrql() != PASSIVE_LEVEL)
596 {
597 DPRINT("ObpPerformRetentionChecks called at an unsupported IRQL. Use ObpPerformRetentionChecksDpcLevel instead.\n");
598 KEBUGCHECK(0);
599 }
600
601 if (Header->SecurityDescriptor != NULL)
602 {
603 ObpRemoveSecurityDescriptor(Header->SecurityDescriptor);
604 }
605
606 if (Header->ObjectType != NULL &&
607 Header->ObjectType->Delete != NULL)
608 {
609 Header->ObjectType->Delete(HEADER_TO_BODY(Header));
610 }
611
612 if (Header->Name.Buffer != NULL)
613 {
614 ObpRemoveEntryDirectory(Header);
615 RtlFreeUnicodeString(&Header->Name);
616 }
617
618 DPRINT("ObPerformRetentionChecks() = Freeing object\n");
619 ExFreePool(Header);
620
621 return(STATUS_SUCCESS);
622 }
623
624
625 VOID STDCALL
626 ObpDeleteObjectWorkRoutine (IN PVOID Parameter)
627 {
628 PRETENTION_CHECK_PARAMS Params = (PRETENTION_CHECK_PARAMS)Parameter;
629 /* ULONG Tag; */ /* See below */
630
631 assert(Params);
632 assert(KeGetCurrentIrql() == PASSIVE_LEVEL); /* We need PAGED_CODE somewhere... */
633
634 /* Turn this on when we have ExFreePoolWithTag
635 Tag = Params->ObjectHeader->ObjectType->Tag; */
636 ObpDeleteObject(Params->ObjectHeader);
637 ExFreePool(Params);
638 /* ExFreePoolWithTag(Params, Tag); */
639 }
640
641
642 STATIC NTSTATUS
643 ObpDeleteObjectDpcLevel(IN POBJECT_HEADER ObjectHeader,
644 IN LONG OldRefCount)
645 {
646 if (ObjectHeader->RefCount < 0)
647 {
648 CPRINT("Object %p/%p has invalid reference count (%d)\n",
649 ObjectHeader, HEADER_TO_BODY(ObjectHeader),
650 ObjectHeader->RefCount);
651 KEBUGCHECK(0);
652 }
653
654 if (ObjectHeader->HandleCount < 0)
655 {
656 CPRINT("Object %p/%p has invalid handle count (%d)\n",
657 ObjectHeader, HEADER_TO_BODY(ObjectHeader),
658 ObjectHeader->HandleCount);
659 KEBUGCHECK(0);
660 }
661
662 if (ObjectHeader->CloseInProcess)
663 {
664 KEBUGCHECK(0);
665 return STATUS_UNSUCCESSFUL;
666 }
667 ObjectHeader->CloseInProcess = TRUE;
668
669 switch (KeGetCurrentIrql ())
670 {
671 case PASSIVE_LEVEL:
672 return ObpDeleteObject (ObjectHeader);
673
674 case APC_LEVEL:
675 case DISPATCH_LEVEL:
676 {
677 PRETENTION_CHECK_PARAMS Params;
678
679 /*
680 We use must succeed pool here because if the allocation fails
681 then we leak memory.
682 */
683 Params = (PRETENTION_CHECK_PARAMS)
684 ExAllocatePoolWithTag(NonPagedPoolMustSucceed,
685 sizeof(RETENTION_CHECK_PARAMS),
686 ObjectHeader->ObjectType->Tag);
687 Params->ObjectHeader = ObjectHeader;
688 ExInitializeWorkItem(&Params->WorkItem,
689 ObpDeleteObjectWorkRoutine,
690 (PVOID)Params);
691 ExQueueWorkItem(&Params->WorkItem,
692 CriticalWorkQueue);
693 }
694 return STATUS_PENDING;
695
696 default:
697 DPRINT("ObpPerformRetentionChecksDpcLevel called at unsupported "
698 "IRQL %u!\n", KeGetCurrentIrql());
699 KEBUGCHECK(0);
700 return STATUS_UNSUCCESSFUL;
701 }
702
703 return STATUS_SUCCESS;
704 }
705
706
707 /**********************************************************************
708 * NAME EXPORTED
709 * ObfReferenceObject@4
710 *
711 * DESCRIPTION
712 * Increments a given object's reference count and performs
713 * retention checks.
714 *
715 * ARGUMENTS
716 * ObjectBody = Body of the object.
717 *
718 * RETURN VALUE
719 * None.
720 *
721 * @implemented
722 */
723 VOID FASTCALL
724 ObfReferenceObject(IN PVOID Object)
725 {
726 POBJECT_HEADER Header;
727
728 assert(Object);
729
730 Header = BODY_TO_HEADER(Object);
731
732 /* No one should be referencing an object once we are deleting it. */
733 if (Header->CloseInProcess)
734 {
735 KEBUGCHECK(0);
736 }
737
738 (VOID)InterlockedIncrement(&Header->RefCount);
739 }
740
741
742 /**********************************************************************
743 * NAME EXPORTED
744 * ObfDereferenceObject@4
745 *
746 * DESCRIPTION
747 * Decrements a given object's reference count and performs
748 * retention checks.
749 *
750 * ARGUMENTS
751 * ObjectBody = Body of the object.
752 *
753 * RETURN VALUE
754 * None.
755 *
756 * @implemented
757 */
758 VOID FASTCALL
759 ObfDereferenceObject(IN PVOID Object)
760 {
761 POBJECT_HEADER Header;
762 LONG NewRefCount;
763 BOOL Permanent;
764 ULONG HandleCount;
765
766 assert(Object);
767
768 /* Extract the object header. */
769 Header = BODY_TO_HEADER(Object);
770 Permanent = Header->Permanent;
771 HandleCount = Header->HandleCount;
772
773 /*
774 Drop our reference and get the new count so we can tell if this was the
775 last reference.
776 */
777 NewRefCount = InterlockedDecrement(&Header->RefCount);
778 assert(NewRefCount >= 0);
779
780 /* Check whether the object can now be deleted. */
781 if (NewRefCount == 0 &&
782 HandleCount == 0 &&
783 !Permanent)
784 {
785 ObpDeleteObjectDpcLevel(Header, NewRefCount);
786 }
787 }
788
789
790 /**********************************************************************
791 * NAME EXPORTED
792 * ObGetObjectPointerCount@4
793 *
794 * DESCRIPTION
795 * Retrieves the pointer(reference) count of the given object.
796 *
797 * ARGUMENTS
798 * ObjectBody = Body of the object.
799 *
800 * RETURN VALUE
801 * Reference count.
802 *
803 * @implemented
804 */
805 ULONG STDCALL
806 ObGetObjectPointerCount(PVOID Object)
807 {
808 POBJECT_HEADER Header;
809
810 assert(Object);
811 Header = BODY_TO_HEADER(Object);
812
813 return(Header->RefCount);
814 }
815
816
817 /**********************************************************************
818 * NAME INTERNAL
819 * ObGetObjectHandleCount@4
820 *
821 * DESCRIPTION
822 * Retrieves the handle count of the given object.
823 *
824 * ARGUMENTS
825 * ObjectBody = Body of the object.
826 *
827 * RETURN VALUE
828 * Reference count.
829 */
830 ULONG
831 ObGetObjectHandleCount(PVOID Object)
832 {
833 POBJECT_HEADER Header;
834
835 assert(Object);
836 Header = BODY_TO_HEADER(Object);
837
838 return(Header->HandleCount);
839 }
840
841
842 /**********************************************************************
843 * NAME EXPORTED
844 * ObDereferenceObject@4
845 *
846 * DESCRIPTION
847 * Decrements a given object's reference count and performs
848 * retention checks.
849 *
850 * ARGUMENTS
851 * ObjectBody = Body of the object.
852 *
853 * RETURN VALUE
854 * None.
855 *
856 * @implemented
857 */
858
859 #ifdef ObDereferenceObject
860 #undef ObDereferenceObject
861 #endif
862
863 VOID STDCALL
864 ObDereferenceObject(IN PVOID Object)
865 {
866 ObfDereferenceObject(Object);
867 }
868
869 /* EOF */