4 * Copyright (C) 2004 Robert Shearman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/debug.h"
26 #include "ntdll_misc.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
30 /**************************************************************************
31 * RtlInitializeHandleTable (NTDLL.@)
33 * Initializes a handle table.
36 * MaxHandleCount [I] The maximum number of handles the handle table will support.
37 * HandleSize [I] The size of each handle.
38 * HandleTable [I/O] The handle table.
44 * RtlDestroyHandleTable().
46 void WINAPI RtlInitializeHandleTable(ULONG MaxHandleCount, ULONG HandleSize, RTL_HANDLE_TABLE * HandleTable)
48 TRACE("(%lu, %lu, %p)\n", MaxHandleCount, HandleSize, HandleTable);
50 memset(HandleTable, 0, sizeof(*HandleTable));
51 HandleTable->MaxHandleCount = MaxHandleCount;
52 HandleTable->HandleSize = HandleSize;
55 /**************************************************************************
56 * RtlDestroyHandleTable (NTDLL.@)
58 * Destroys a handle table and frees associated resources.
61 * HandleTable [I] The handle table.
64 * Any status code returned by NtFreeVirtualMemory().
67 * The native version of this API doesn't free the virtual memory that has
68 * been previously reserved, only the committed memory. There is no harm
69 * in also freeing the reserved memory because it won't have been handed out
70 * to any callers. I believe it is "more polite" to free everything.
73 * RtlInitializeHandleTable().
75 NTSTATUS WINAPI RtlDestroyHandleTable(RTL_HANDLE_TABLE * HandleTable)
79 TRACE("(%p)\n", HandleTable);
81 /* native version only releases committed memory, but we also release reserved */
82 return NtFreeVirtualMemory(
84 &HandleTable->FirstHandle,
89 /**************************************************************************
90 * RtlpAllocateSomeHandles (internal)
92 * Reserves memory for the handles if not previously done and commits memory
93 * for a batch of handles if none are free and adds them to the free list.
96 * HandleTable [I/O] The handle table.
101 static NTSTATUS RtlpAllocateSomeHandles(RTL_HANDLE_TABLE * HandleTable)
104 if (!HandleTable->FirstHandle)
106 PVOID FirstHandleAddr = NULL;
107 ULONG MaxSize = HandleTable->MaxHandleCount * HandleTable->HandleSize;
109 /* reserve memory for the handles, but don't commit it yet because we
110 * probably won't use most of it and it will use up physical memory */
111 status = NtAllocateVirtualMemory(
118 if (status != STATUS_SUCCESS)
120 HandleTable->FirstHandle = FirstHandleAddr;
121 HandleTable->ReservedMemory = HandleTable->FirstHandle;
122 HandleTable->MaxHandle = (char *)HandleTable->FirstHandle + MaxSize;
124 if (!HandleTable->NextFree)
126 ULONG CommitSize = 4096; /* one page */
128 RTL_HANDLE * FreeHandle = NULL;
129 PVOID NextAvailAddr = HandleTable->ReservedMemory;
131 if (HandleTable->ReservedMemory >= HandleTable->MaxHandle)
132 return STATUS_NO_MEMORY; /* the handle table is completely full */
134 status = NtAllocateVirtualMemory(
141 if (status != STATUS_SUCCESS)
144 for (Offset = 0; Offset < CommitSize; Offset += HandleTable->HandleSize)
146 /* make sure we don't go over handle limit, even if we can
147 * because of rounding of the table size up to the next page
149 if ((char *)HandleTable->ReservedMemory + Offset >= (char *)HandleTable->MaxHandle)
152 FreeHandle = (RTL_HANDLE *)((char *)HandleTable->ReservedMemory + Offset);
154 FreeHandle->Next = (RTL_HANDLE *)((char *)HandleTable->ReservedMemory +
155 Offset + HandleTable->HandleSize);
158 /* shouldn't happen because we already test for this above, but
159 * handle it just in case */
161 return STATUS_NO_MEMORY;
163 /* set the last handle's Next pointer to NULL so that when we run
164 * out of free handles we trigger another commit of memory and
165 * initialize the free pointers */
166 FreeHandle->Next = NULL;
168 HandleTable->NextFree = HandleTable->ReservedMemory;
170 HandleTable->ReservedMemory = (char *)HandleTable->ReservedMemory + CommitSize;
172 return STATUS_SUCCESS;
175 /**************************************************************************
176 * RtlAllocateHandle (NTDLL.@)
178 * Allocates a handle from the handle table.
181 * HandleTable [I/O] The handle table.
182 * HandleIndex [O] Index of the handle returned. Optional.
185 * Success: Pointer to allocated handle.
191 RTL_HANDLE * WINAPI RtlAllocateHandle(RTL_HANDLE_TABLE * HandleTable, ULONG * HandleIndex)
195 TRACE("(%p, %p)\n", HandleTable, HandleIndex);
197 if (!HandleTable->NextFree && RtlpAllocateSomeHandles(HandleTable) != STATUS_SUCCESS)
200 ret = (RTL_HANDLE *)HandleTable->NextFree;
201 HandleTable->NextFree = ret->Next;
204 *HandleIndex = (ULONG)(((PCHAR)ret - (PCHAR)HandleTable->FirstHandle) / HandleTable->HandleSize);
209 /**************************************************************************
210 * RtlFreeHandle (NTDLL.@)
212 * Frees an allocated handle.
215 * HandleTable [I/O] The handle table.
216 * Handle [I] The handle to be freed.
223 * RtlAllocateHandle().
225 BOOLEAN WINAPI RtlFreeHandle(RTL_HANDLE_TABLE * HandleTable, RTL_HANDLE * Handle)
227 TRACE("(%p, %p)\n", HandleTable, Handle);
228 /* NOTE: we don't validate the handle and we don't make Handle->Next even
229 * again to signal that it is no longer in user - that is done as a side
230 * effect of setting Handle->Next to the previously next free handle in
231 * the handle table */
232 memset(Handle, 0, HandleTable->HandleSize);
233 Handle->Next = (RTL_HANDLE *)HandleTable->NextFree;
234 HandleTable->NextFree = Handle;
238 /**************************************************************************
239 * RtlIsValidHandle (NTDLL.@)
241 * Determines whether a handle is valid or not.
244 * HandleTable [I] The handle table.
245 * Handle [I] The handle to be tested.
251 BOOLEAN WINAPI RtlIsValidHandle(const RTL_HANDLE_TABLE * HandleTable, const RTL_HANDLE * Handle)
253 TRACE("(%p, %p)\n", HandleTable, Handle);
254 /* make sure handle is within used region and that it is aligned on
255 * a HandleTable->HandleSize boundary and that Handle->Next is odd,
256 * indicating that the handle is active */
257 if ((Handle >= (RTL_HANDLE *)HandleTable->FirstHandle) &&
258 (Handle < (RTL_HANDLE *)HandleTable->ReservedMemory) &&
259 !((ULONG_PTR)Handle & (HandleTable->HandleSize - 1)) &&
260 ((ULONG_PTR)Handle->Next & 1))
266 /**************************************************************************
267 * RtlIsValidIndexHandle (NTDLL.@)
269 * Determines whether a handle index is valid or not.
272 * HandleTable [I] The handle table.
273 * Index [I] The index of the handle to be tested.
274 * ValidHandle [O] The handle Index refers to.
280 BOOLEAN WINAPI RtlIsValidIndexHandle(const RTL_HANDLE_TABLE * HandleTable, ULONG Index, RTL_HANDLE ** ValidHandle)
284 TRACE("(%p, %lu, %p)\n", HandleTable, Index, ValidHandle);
285 Handle = (RTL_HANDLE *)
286 ((char *)HandleTable->FirstHandle + Index * HandleTable->HandleSize);
288 if (RtlIsValidHandle(HandleTable, Handle))
290 *ValidHandle = Handle;