- Send whole OBJECT_ATTRIBUTES.Attributes to the server not just an
[wine] / dlls / ntdll / handletable.c
1 /*
2  * Handle Tables
3  *
4  * Copyright (C) 2004 Robert Shearman
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winternl.h"
25 #include "wine/debug.h"
26 #include "ntdll_misc.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
29
30 /**************************************************************************
31  *      RtlInitializeHandleTable   (NTDLL.@)
32  *
33  * Initializes a handle table.
34  *
35  * PARAMS
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.
39  *
40  * RETURNS
41  *  Nothing.
42  *
43  * SEE
44  *  RtlDestroyHandleTable().
45  */
46 void WINAPI RtlInitializeHandleTable(ULONG MaxHandleCount, ULONG HandleSize, RTL_HANDLE_TABLE * HandleTable)
47 {
48     TRACE("(%lu, %lu, %p)\n", MaxHandleCount, HandleSize, HandleTable);
49
50     memset(HandleTable, 0, sizeof(*HandleTable));
51     HandleTable->MaxHandleCount = MaxHandleCount;
52     HandleTable->HandleSize = HandleSize;
53 }
54
55 /**************************************************************************
56  *      RtlDestroyHandleTable   (NTDLL.@)
57  *
58  * Destroys a handle table and frees associated resources.
59  *
60  * PARAMS
61  *  HandleTable    [I] The handle table.
62  *
63  * RETURNS
64  *  Any status code returned by NtFreeVirtualMemory().
65  *
66  * NOTES
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.
71  *
72  * SEE
73  *  RtlInitializeHandleTable().
74  */
75 NTSTATUS WINAPI RtlDestroyHandleTable(RTL_HANDLE_TABLE * HandleTable)
76 {
77     SIZE_T Size = 0;
78
79     TRACE("(%p)\n", HandleTable);
80
81     /* native version only releases committed memory, but we also release reserved */
82     return NtFreeVirtualMemory(
83         NtCurrentProcess(),
84         &HandleTable->FirstHandle,
85         &Size,
86         MEM_RELEASE);
87 }
88
89 /**************************************************************************
90  *      RtlpAllocateSomeHandles   (internal)
91  *
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.
94  *
95  * PARAMS
96  *  HandleTable    [I/O] The handle table.
97  *
98  * RETURNS
99  *  NTSTATUS code.
100  */
101 static NTSTATUS RtlpAllocateSomeHandles(RTL_HANDLE_TABLE * HandleTable)
102 {
103     NTSTATUS status;
104     if (!HandleTable->FirstHandle)
105     {
106         PVOID FirstHandleAddr = NULL;
107         SIZE_T MaxSize = HandleTable->MaxHandleCount * HandleTable->HandleSize;
108
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(
112             NtCurrentProcess(),
113             &FirstHandleAddr,
114             0,
115             &MaxSize,
116             MEM_RESERVE,
117             PAGE_READWRITE);
118         if (status != STATUS_SUCCESS)
119             return status;
120         HandleTable->FirstHandle = FirstHandleAddr;
121         HandleTable->ReservedMemory = HandleTable->FirstHandle;
122         HandleTable->MaxHandle = (char *)HandleTable->FirstHandle + MaxSize;
123     }
124     if (!HandleTable->NextFree)
125     {
126         SIZE_T Offset, CommitSize = 4096; /* one page */
127         RTL_HANDLE * FreeHandle = NULL;
128         PVOID NextAvailAddr = HandleTable->ReservedMemory;
129
130         if (HandleTable->ReservedMemory >= HandleTable->MaxHandle)
131             return STATUS_NO_MEMORY; /* the handle table is completely full */
132
133         status = NtAllocateVirtualMemory(
134             NtCurrentProcess(),
135             &NextAvailAddr,
136             0,
137             &CommitSize,
138             MEM_COMMIT,
139             PAGE_READWRITE);
140         if (status != STATUS_SUCCESS)
141             return status;
142
143         for (Offset = 0; Offset < CommitSize; Offset += HandleTable->HandleSize)
144         {
145             /* make sure we don't go over handle limit, even if we can
146              * because of rounding of the table size up to the next page
147              * boundary */
148             if ((char *)HandleTable->ReservedMemory + Offset >= (char *)HandleTable->MaxHandle)
149                 break;
150
151             FreeHandle = (RTL_HANDLE *)((char *)HandleTable->ReservedMemory + Offset);
152
153             FreeHandle->Next = (RTL_HANDLE *)((char *)HandleTable->ReservedMemory + 
154                 Offset + HandleTable->HandleSize);
155         }
156
157         /* shouldn't happen because we already test for this above, but
158          * handle it just in case */
159         if (!FreeHandle)
160             return STATUS_NO_MEMORY;
161
162         /* set the last handle's Next pointer to NULL so that when we run
163          * out of free handles we trigger another commit of memory and
164          * initialize the free pointers */
165         FreeHandle->Next = NULL;
166
167         HandleTable->NextFree = HandleTable->ReservedMemory;
168
169         HandleTable->ReservedMemory = (char *)HandleTable->ReservedMemory + CommitSize;
170     }
171     return STATUS_SUCCESS;
172 }
173
174 /**************************************************************************
175  *      RtlAllocateHandle   (NTDLL.@)
176  *
177  * Allocates a handle from the handle table.
178  *
179  * PARAMS
180  *  HandleTable    [I/O] The handle table.
181  *  HandleIndex    [O] Index of the handle returned. Optional.
182  *
183  * RETURNS
184  *  Success: Pointer to allocated handle.
185  *  Failure: NULL.
186  *
187  * SEE
188  *  RtlFreeHandle().
189  */
190 RTL_HANDLE * WINAPI RtlAllocateHandle(RTL_HANDLE_TABLE * HandleTable, ULONG * HandleIndex)
191 {
192     RTL_HANDLE * ret;
193
194     TRACE("(%p, %p)\n", HandleTable, HandleIndex);
195
196     if (!HandleTable->NextFree && RtlpAllocateSomeHandles(HandleTable) != STATUS_SUCCESS)
197         return NULL;
198     
199     ret = (RTL_HANDLE *)HandleTable->NextFree;
200     HandleTable->NextFree = ret->Next;
201
202     if (HandleIndex)
203         *HandleIndex = (ULONG)(((PCHAR)ret - (PCHAR)HandleTable->FirstHandle) / HandleTable->HandleSize);
204
205     return ret;
206 }
207
208 /**************************************************************************
209  *      RtlFreeHandle   (NTDLL.@)
210  *
211  * Frees an allocated handle.
212  *
213  * PARAMS
214  *  HandleTable    [I/O] The handle table.
215  *  Handle         [I] The handle to be freed.
216  *
217  * RETURNS
218  *  Success: TRUE.
219  *  Failure: FALSE.
220  *
221  * SEE
222  *  RtlAllocateHandle().
223  */
224 BOOLEAN WINAPI RtlFreeHandle(RTL_HANDLE_TABLE * HandleTable, RTL_HANDLE * Handle)
225 {
226     TRACE("(%p, %p)\n", HandleTable, Handle);
227     /* NOTE: we don't validate the handle and we don't make Handle->Next even
228      * again to signal that it is no longer in user - that is done as a side
229      * effect of setting Handle->Next to the previously next free handle in
230      * the handle table */
231     memset(Handle, 0, HandleTable->HandleSize);
232     Handle->Next = (RTL_HANDLE *)HandleTable->NextFree;
233     HandleTable->NextFree = Handle;
234     return TRUE;
235 }
236
237 /**************************************************************************
238  *      RtlIsValidHandle   (NTDLL.@)
239  *
240  * Determines whether a handle is valid or not.
241  *
242  * PARAMS
243  *  HandleTable    [I] The handle table.
244  *  Handle         [I] The handle to be tested.
245  *
246  * RETURNS
247  *  Valid: TRUE.
248  *  Invalid: FALSE.
249  */
250 BOOLEAN WINAPI RtlIsValidHandle(const RTL_HANDLE_TABLE * HandleTable, const RTL_HANDLE * Handle)
251 {
252     TRACE("(%p, %p)\n", HandleTable, Handle);
253     /* make sure handle is within used region and that it is aligned on
254      * a HandleTable->HandleSize boundary and that Handle->Next is odd,
255      * indicating that the handle is active */
256     if ((Handle >= (RTL_HANDLE *)HandleTable->FirstHandle) &&
257       (Handle < (RTL_HANDLE *)HandleTable->ReservedMemory) &&
258       !((ULONG_PTR)Handle & (HandleTable->HandleSize - 1)) &&
259       ((ULONG_PTR)Handle->Next & 1))
260         return TRUE;
261     else
262         return FALSE;
263 }
264
265 /**************************************************************************
266  *      RtlIsValidIndexHandle   (NTDLL.@)
267  *
268  * Determines whether a handle index is valid or not.
269  *
270  * PARAMS
271  *  HandleTable    [I] The handle table.
272  *  Index          [I] The index of the handle to be tested.
273  *  ValidHandle    [O] The handle Index refers to.
274  *
275  * RETURNS
276  *  Valid: TRUE.
277  *  Invalid: FALSE.
278  */
279 BOOLEAN WINAPI RtlIsValidIndexHandle(const RTL_HANDLE_TABLE * HandleTable, ULONG Index, RTL_HANDLE ** ValidHandle)
280 {
281     RTL_HANDLE * Handle;
282
283     TRACE("(%p, %lu, %p)\n", HandleTable, Index, ValidHandle);
284     Handle = (RTL_HANDLE *)
285         ((char *)HandleTable->FirstHandle + Index * HandleTable->HandleSize);
286
287     if (RtlIsValidHandle(HandleTable, Handle))
288     {
289         *ValidHandle = Handle;
290         return TRUE;
291     }
292     return FALSE;
293 }