2 * Full Pointer Translation Routines
4 * Copyright 2006 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
32 PFULL_PTR_XLAT_TABLES WINAPI NdrFullPointerXlatInit(ULONG NumberOfPointers,
35 ULONG NumberOfBuckets;
36 PFULL_PTR_XLAT_TABLES pXlatTables = HeapAlloc(GetProcessHeap(), 0, sizeof(*pXlatTables));
38 TRACE("(%d, %d)\n", NumberOfPointers, XlatSide);
40 if (!NumberOfPointers) NumberOfPointers = 512;
41 NumberOfBuckets = ((NumberOfPointers + 3) & ~3) - 1;
43 pXlatTables->RefIdToPointer.XlatTable =
44 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
45 sizeof(void *) * NumberOfPointers);
46 pXlatTables->RefIdToPointer.StateTable =
47 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
48 sizeof(unsigned char) * NumberOfPointers);
49 pXlatTables->RefIdToPointer.NumberOfEntries = NumberOfPointers;
51 TRACE("NumberOfBuckets = %d\n", NumberOfBuckets);
52 pXlatTables->PointerToRefId.XlatTable =
53 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
54 sizeof(PFULL_PTR_TO_REFID_ELEMENT) * NumberOfBuckets);
55 pXlatTables->PointerToRefId.NumberOfBuckets = NumberOfBuckets;
56 pXlatTables->PointerToRefId.HashMask = NumberOfBuckets - 1;
58 pXlatTables->NextRefId = 1;
59 pXlatTables->XlatSide = XlatSide;
64 void WINAPI NdrFullPointerXlatFree(PFULL_PTR_XLAT_TABLES pXlatTables)
68 TRACE("(%p)\n", pXlatTables);
70 /* free the entries in the table */
71 for (i = 0; i < pXlatTables->RefIdToPointer.NumberOfEntries; i++)
72 HeapFree(GetProcessHeap(), 0, pXlatTables->RefIdToPointer.XlatTable[i]);
74 HeapFree(GetProcessHeap(), 0, pXlatTables->RefIdToPointer.XlatTable);
75 HeapFree(GetProcessHeap(), 0, pXlatTables->RefIdToPointer.StateTable);
76 HeapFree(GetProcessHeap(), 0, pXlatTables->PointerToRefId.XlatTable);
78 HeapFree(GetProcessHeap(), 0, pXlatTables);
81 static void expand_pointer_table_if_necessary(PFULL_PTR_XLAT_TABLES pXlatTables, ULONG RefId)
83 if (RefId >= pXlatTables->RefIdToPointer.NumberOfEntries)
85 pXlatTables->RefIdToPointer.NumberOfEntries = RefId * 2;
86 pXlatTables->RefIdToPointer.XlatTable =
87 HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
88 pXlatTables->RefIdToPointer.XlatTable,
89 sizeof(void *) * pXlatTables->RefIdToPointer.NumberOfEntries);
90 pXlatTables->RefIdToPointer.StateTable =
91 HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
92 pXlatTables->RefIdToPointer.StateTable,
93 sizeof(unsigned char) * pXlatTables->RefIdToPointer.NumberOfEntries);
95 if (!pXlatTables->RefIdToPointer.XlatTable || !pXlatTables->RefIdToPointer.StateTable)
96 pXlatTables->RefIdToPointer.NumberOfEntries = 0;
100 int WINAPI NdrFullPointerQueryPointer(PFULL_PTR_XLAT_TABLES pXlatTables,
101 void *pPointer, unsigned char QueryType,
106 PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry;
108 TRACE("(%p, %p, %d, %p)\n", pXlatTables, pPointer, QueryType, pRefId);
116 /* simple hashing algorithm, don't know whether it matches native */
117 for (i = 0; i < sizeof(pPointer); i++)
118 Hash = (Hash * 3) ^ ((unsigned char *)&pPointer)[i];
120 XlatTableEntry = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
121 for (; XlatTableEntry; XlatTableEntry = XlatTableEntry->Next)
122 if (pPointer == XlatTableEntry->Pointer)
124 *pRefId = XlatTableEntry->RefId;
125 if (XlatTableEntry->State & QueryType)
127 XlatTableEntry->State |= QueryType;
131 XlatTableEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(*XlatTableEntry));
132 XlatTableEntry->Next = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
133 XlatTableEntry->Pointer = pPointer;
134 XlatTableEntry->RefId = *pRefId = pXlatTables->NextRefId++;
135 XlatTableEntry->State = QueryType;
136 pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask] = XlatTableEntry;
138 /* insert pointer into mapping table */
139 expand_pointer_table_if_necessary(pXlatTables, XlatTableEntry->RefId);
140 if (pXlatTables->RefIdToPointer.NumberOfEntries > XlatTableEntry->RefId)
142 pXlatTables->RefIdToPointer.XlatTable[XlatTableEntry->RefId] = pPointer;
143 pXlatTables->RefIdToPointer.StateTable[XlatTableEntry->RefId] = QueryType;
149 int WINAPI NdrFullPointerQueryRefId(PFULL_PTR_XLAT_TABLES pXlatTables,
150 ULONG RefId, unsigned char QueryType,
153 TRACE("(%p, 0x%x, %d, %p)\n", pXlatTables, RefId, QueryType, ppPointer);
155 expand_pointer_table_if_necessary(pXlatTables, RefId);
157 pXlatTables->NextRefId = max(RefId + 1, pXlatTables->NextRefId);
159 if (pXlatTables->RefIdToPointer.NumberOfEntries > RefId)
161 *ppPointer = pXlatTables->RefIdToPointer.XlatTable[RefId];
164 if (pXlatTables->RefIdToPointer.StateTable[RefId] & QueryType)
166 pXlatTables->RefIdToPointer.StateTable[RefId] |= QueryType;
176 void WINAPI NdrFullPointerInsertRefId(PFULL_PTR_XLAT_TABLES pXlatTables,
177 ULONG RefId, void *pPointer)
181 PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry;
183 TRACE("(%p, 0x%x, %p)\n", pXlatTables, RefId, pPointer);
185 /* simple hashing algorithm, don't know whether it matches native */
186 for (i = 0; i < sizeof(pPointer); i++)
187 Hash = (Hash * 3) ^ ((unsigned char *)&pPointer)[i];
189 XlatTableEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(*XlatTableEntry));
190 XlatTableEntry->Next = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
191 XlatTableEntry->Pointer = pPointer;
192 XlatTableEntry->RefId = RefId;
193 XlatTableEntry->State = 0;
194 pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask] = XlatTableEntry;
196 /* insert pointer into mapping table */
197 expand_pointer_table_if_necessary(pXlatTables, RefId);
198 if (pXlatTables->RefIdToPointer.NumberOfEntries > RefId)
199 pXlatTables->RefIdToPointer.XlatTable[XlatTableEntry->RefId] = pPointer;
202 int WINAPI NdrFullPointerFree(PFULL_PTR_XLAT_TABLES pXlatTables, void *Pointer)
206 PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry;
209 TRACE("(%p, %p)\n", pXlatTables, Pointer);
214 /* simple hashing algorithm, don't know whether it matches native */
215 for (i = 0; i < sizeof(Pointer); i++)
216 Hash = (Hash * 3) ^ ((unsigned char *)&Pointer)[i];
218 XlatTableEntry = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
219 for (; XlatTableEntry; XlatTableEntry = XlatTableEntry->Next)
220 if (Pointer == XlatTableEntry->Pointer)
222 if (XlatTableEntry->State & 0x20)
224 XlatTableEntry->State |= 0x20;
225 RefId = XlatTableEntry->RefId;
232 if (pXlatTables->RefIdToPointer.NumberOfEntries > RefId)
234 pXlatTables->RefIdToPointer.StateTable[RefId] |= 0x20;