3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/rtl/handle.c
6 * PURPOSE: Handle table
8 * PROGRAMMERS: Eric Kohl <ekohl@rz-online.de>
11 /* INCLUDES ******************************************************************/
15 #include <internal/debug.h>
18 RtlpIsValidHandle(PRTL_HANDLE_TABLE HandleTable
, PRTL_HANDLE Handle
);
20 RtlpIsValidIndexHandle(PRTL_HANDLE_TABLE HandleTable
, PRTL_HANDLE
*Handle
, ULONG Index
);
23 /* FUNCTIONS *****************************************************************/
26 RtlpInitializeHandleTable(ULONG TableSize
,
27 PRTL_HANDLE_TABLE HandleTable
)
29 /* initialize handle table */
32 sizeof(RTL_HANDLE_TABLE
));
33 HandleTable
->TableSize
= TableSize
;
38 RtlpDestroyHandleTable(PRTL_HANDLE_TABLE HandleTable
)
40 ExFreePool((PVOID
)HandleTable
->Handles
);
45 RtlpAllocateHandle(PRTL_HANDLE_TABLE HandleTable
,
49 RTL_HANDLE
**pp_new
,**pph
,*ph
;
57 pp_new
= &HandleTable
->FirstFree
;
59 if (HandleTable
->FirstFree
== NULL
)
61 /* no free handle available */
62 if (HandleTable
->LastUsed
== NULL
)
64 /* allocate handle array */
65 ArraySize
= sizeof(RTL_HANDLE
) * HandleTable
->TableSize
;
66 ArrayPointer
= ExAllocatePoolWithTag(PagedPool
,
69 if (ArrayPointer
== NULL
)
72 /* update handle array pointers */
73 HandleTable
->Handles
= (PRTL_HANDLE
)ArrayPointer
;
74 HandleTable
->Limit
= (PRTL_HANDLE
)((char*)ArrayPointer
+ ArraySize
);
75 HandleTable
->LastUsed
= (PRTL_HANDLE
)ArrayPointer
;
78 /* build free list in handle array */
79 ph
= HandleTable
->LastUsed
;
81 while (ph
< HandleTable
->Limit
)
85 ph
= (PRTL_HANDLE
)((ULONG
)ph
+ sizeof(RTL_HANDLE
));
90 /* remove handle from free list */
92 *pp_new
= retval
->Next
;
94 retval
->Object
= Object
;
96 *Index
= ((ULONG
)retval
- (ULONG
)HandleTable
->Handles
) / sizeof(RTL_HANDLE
);
103 RtlpFreeHandle(PRTL_HANDLE_TABLE HandleTable
,
108 /* check if handle is valid */
109 if (! RtlpIsValidIndexHandle(HandleTable
,
115 memset(Handle
, 0, sizeof(RTL_HANDLE
));
117 /* add handle to free list */
118 Handle
->Next
= HandleTable
->FirstFree
;
119 HandleTable
->FirstFree
= Handle
;
126 RtlpIsValidHandle(PRTL_HANDLE_TABLE HandleTable
,
129 if ((HandleTable
!= NULL
)
131 && (Handle
>= HandleTable
->Handles
)
132 && (Handle
< HandleTable
->Limit
))
139 RtlpIsValidIndexHandle(PRTL_HANDLE_TABLE HandleTable
,
143 PRTL_HANDLE InternalHandle
;
145 DPRINT("RtlpIsValidIndexHandle(HandleTable %p Handle %p Index %x)\n", HandleTable
, Handle
, Index
);
147 if (HandleTable
== NULL
)
150 DPRINT("Handles %p\n", HandleTable
->Handles
);
152 InternalHandle
= (PRTL_HANDLE
)((ULONG
)HandleTable
->Handles
+ sizeof(RTL_HANDLE
) * Index
);
153 if (RtlpIsValidHandle(HandleTable
, InternalHandle
) == FALSE
)
156 DPRINT("InternalHandle %p\n", InternalHandle
);
159 *Handle
= InternalHandle
;
165 RtlpMapHandleToPointer(PRTL_HANDLE_TABLE HandleTable
,
170 if (!RtlpIsValidIndexHandle(HandleTable
,
177 return Handle
->Object
;