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
->PointerToRefId
.NumberOfBuckets
; i
++)
73 PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry
;
74 for (XlatTableEntry
= pXlatTables
->PointerToRefId
.XlatTable
[i
];
77 PFULL_PTR_TO_REFID_ELEMENT Next
= XlatTableEntry
->Next
;
78 HeapFree(GetProcessHeap(), 0, XlatTableEntry
);
79 XlatTableEntry
= Next
;
83 HeapFree(GetProcessHeap(), 0, pXlatTables
->RefIdToPointer
.XlatTable
);
84 HeapFree(GetProcessHeap(), 0, pXlatTables
->RefIdToPointer
.StateTable
);
85 HeapFree(GetProcessHeap(), 0, pXlatTables
->PointerToRefId
.XlatTable
);
87 HeapFree(GetProcessHeap(), 0, pXlatTables
);
90 static void expand_pointer_table_if_necessary(PFULL_PTR_XLAT_TABLES pXlatTables
, ULONG RefId
)
92 if (RefId
>= pXlatTables
->RefIdToPointer
.NumberOfEntries
)
94 pXlatTables
->RefIdToPointer
.NumberOfEntries
= RefId
* 2;
95 pXlatTables
->RefIdToPointer
.XlatTable
=
96 HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
97 pXlatTables
->RefIdToPointer
.XlatTable
,
98 sizeof(void *) * pXlatTables
->RefIdToPointer
.NumberOfEntries
);
99 pXlatTables
->RefIdToPointer
.StateTable
=
100 HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
101 pXlatTables
->RefIdToPointer
.StateTable
,
102 sizeof(unsigned char) * pXlatTables
->RefIdToPointer
.NumberOfEntries
);
104 if (!pXlatTables
->RefIdToPointer
.XlatTable
|| !pXlatTables
->RefIdToPointer
.StateTable
)
105 pXlatTables
->RefIdToPointer
.NumberOfEntries
= 0;
109 int WINAPI
NdrFullPointerQueryPointer(PFULL_PTR_XLAT_TABLES pXlatTables
,
110 void *pPointer
, unsigned char QueryType
,
115 PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry
;
117 TRACE("(%p, %p, %d, %p)\n", pXlatTables
, pPointer
, QueryType
, pRefId
);
125 /* simple hashing algorithm, don't know whether it matches native */
126 for (i
= 0; i
< sizeof(pPointer
); i
++)
127 Hash
= (Hash
* 3) ^ ((unsigned char *)&pPointer
)[i
];
129 XlatTableEntry
= pXlatTables
->PointerToRefId
.XlatTable
[Hash
& pXlatTables
->PointerToRefId
.HashMask
];
130 for (; XlatTableEntry
; XlatTableEntry
= XlatTableEntry
->Next
)
131 if (pPointer
== XlatTableEntry
->Pointer
)
133 *pRefId
= XlatTableEntry
->RefId
;
134 if (XlatTableEntry
->State
& QueryType
)
136 XlatTableEntry
->State
|= QueryType
;
140 XlatTableEntry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*XlatTableEntry
));
141 XlatTableEntry
->Next
= pXlatTables
->PointerToRefId
.XlatTable
[Hash
& pXlatTables
->PointerToRefId
.HashMask
];
142 XlatTableEntry
->Pointer
= pPointer
;
143 XlatTableEntry
->RefId
= *pRefId
= pXlatTables
->NextRefId
++;
144 XlatTableEntry
->State
= QueryType
;
145 pXlatTables
->PointerToRefId
.XlatTable
[Hash
& pXlatTables
->PointerToRefId
.HashMask
] = XlatTableEntry
;
147 /* insert pointer into mapping table */
148 expand_pointer_table_if_necessary(pXlatTables
, XlatTableEntry
->RefId
);
149 if (pXlatTables
->RefIdToPointer
.NumberOfEntries
> XlatTableEntry
->RefId
)
151 pXlatTables
->RefIdToPointer
.XlatTable
[XlatTableEntry
->RefId
] = pPointer
;
152 pXlatTables
->RefIdToPointer
.StateTable
[XlatTableEntry
->RefId
] = QueryType
;
158 int WINAPI
NdrFullPointerQueryRefId(PFULL_PTR_XLAT_TABLES pXlatTables
,
159 ULONG RefId
, unsigned char QueryType
,
162 TRACE("(%p, 0x%x, %d, %p)\n", pXlatTables
, RefId
, QueryType
, ppPointer
);
164 expand_pointer_table_if_necessary(pXlatTables
, RefId
);
166 pXlatTables
->NextRefId
= max(RefId
+ 1, pXlatTables
->NextRefId
);
168 if (pXlatTables
->RefIdToPointer
.NumberOfEntries
> RefId
)
170 *ppPointer
= pXlatTables
->RefIdToPointer
.XlatTable
[RefId
];
173 if (pXlatTables
->RefIdToPointer
.StateTable
[RefId
] & QueryType
)
175 pXlatTables
->RefIdToPointer
.StateTable
[RefId
] |= QueryType
;
185 void WINAPI
NdrFullPointerInsertRefId(PFULL_PTR_XLAT_TABLES pXlatTables
,
186 ULONG RefId
, void *pPointer
)
190 PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry
;
192 TRACE("(%p, 0x%x, %p)\n", pXlatTables
, RefId
, pPointer
);
194 /* simple hashing algorithm, don't know whether it matches native */
195 for (i
= 0; i
< sizeof(pPointer
); i
++)
196 Hash
= (Hash
* 3) ^ ((unsigned char *)&pPointer
)[i
];
198 XlatTableEntry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*XlatTableEntry
));
199 XlatTableEntry
->Next
= pXlatTables
->PointerToRefId
.XlatTable
[Hash
& pXlatTables
->PointerToRefId
.HashMask
];
200 XlatTableEntry
->Pointer
= pPointer
;
201 XlatTableEntry
->RefId
= RefId
;
202 XlatTableEntry
->State
= 0;
203 pXlatTables
->PointerToRefId
.XlatTable
[Hash
& pXlatTables
->PointerToRefId
.HashMask
] = XlatTableEntry
;
205 /* insert pointer into mapping table */
206 expand_pointer_table_if_necessary(pXlatTables
, RefId
);
207 if (pXlatTables
->RefIdToPointer
.NumberOfEntries
> RefId
)
208 pXlatTables
->RefIdToPointer
.XlatTable
[XlatTableEntry
->RefId
] = pPointer
;
211 int WINAPI
NdrFullPointerFree(PFULL_PTR_XLAT_TABLES pXlatTables
, void *Pointer
)
215 PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry
;
218 TRACE("(%p, %p)\n", pXlatTables
, Pointer
);
223 /* simple hashing algorithm, don't know whether it matches native */
224 for (i
= 0; i
< sizeof(Pointer
); i
++)
225 Hash
= (Hash
* 3) ^ ((unsigned char *)&Pointer
)[i
];
227 XlatTableEntry
= pXlatTables
->PointerToRefId
.XlatTable
[Hash
& pXlatTables
->PointerToRefId
.HashMask
];
228 for (; XlatTableEntry
; XlatTableEntry
= XlatTableEntry
->Next
)
229 if (Pointer
== XlatTableEntry
->Pointer
)
231 if (XlatTableEntry
->State
& 0x20)
233 XlatTableEntry
->State
|= 0x20;
234 RefId
= XlatTableEntry
->RefId
;
241 if (pXlatTables
->RefIdToPointer
.NumberOfEntries
> RefId
)
243 pXlatTables
->RefIdToPointer
.StateTable
[RefId
] |= 0x20;