ReactOS 0.4.16-dev-1946-g52006dd
ndr_fullpointer.c
Go to the documentation of this file.
1/*
2 * Full Pointer Translation Routines
3 *
4 * Copyright 2006 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21#include <stdarg.h>
22#include <stdlib.h>
23
24#include "windef.h"
25#include "winbase.h"
26#include "rpc.h"
27#include "rpcndr.h"
28
29#include "wine/debug.h"
30
32
34 XLAT_SIDE XlatSide)
35{
36 ULONG NumberOfBuckets;
37 FULL_PTR_XLAT_TABLES *pXlatTables = malloc(sizeof(*pXlatTables));
38
39 TRACE("(%ld, %d)\n", NumberOfPointers, XlatSide);
40
41 if (!NumberOfPointers) NumberOfPointers = 512;
42 NumberOfBuckets = ((NumberOfPointers + 3) & ~3) - 1;
43
44 pXlatTables->RefIdToPointer.XlatTable = calloc(NumberOfPointers, sizeof(void *));
45 pXlatTables->RefIdToPointer.StateTable = calloc(NumberOfPointers, sizeof(unsigned char));
46 pXlatTables->RefIdToPointer.NumberOfEntries = NumberOfPointers;
47
48 TRACE("NumberOfBuckets = %ld\n", NumberOfBuckets);
49 pXlatTables->PointerToRefId.XlatTable = calloc(NumberOfBuckets, sizeof(FULL_PTR_TO_REFID_ELEMENT *));
50 pXlatTables->PointerToRefId.NumberOfBuckets = NumberOfBuckets;
51 pXlatTables->PointerToRefId.HashMask = NumberOfBuckets - 1;
52
53 pXlatTables->NextRefId = 1;
54 pXlatTables->XlatSide = XlatSide;
55
56 return pXlatTables;
57}
58
60{
61 ULONG i;
62
63 TRACE("(%p)\n", pXlatTables);
64
65 /* free the entries in the table */
66 for (i = 0; i < pXlatTables->PointerToRefId.NumberOfBuckets; i++)
67 {
68 PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry;
69 for (XlatTableEntry = pXlatTables->PointerToRefId.XlatTable[i];
70 XlatTableEntry; )
71 {
72 PFULL_PTR_TO_REFID_ELEMENT Next = XlatTableEntry->Next;
73 free(XlatTableEntry);
74 XlatTableEntry = Next;
75 }
76 }
77
78 free(pXlatTables->RefIdToPointer.XlatTable);
79 free(pXlatTables->RefIdToPointer.StateTable);
80 free(pXlatTables->PointerToRefId.XlatTable);
81
82 free(pXlatTables);
83}
84
86{
87 if (RefId >= pXlatTables->RefIdToPointer.NumberOfEntries)
88 {
89#ifdef __REACTOS__
90 void* ptrNew;
91 ptrNew = realloc(pXlatTables->RefIdToPointer.XlatTable, sizeof(void *) * RefId * 2);
92 if (!ptrNew)
93 {
94 ERR("Couldn't reallocate RefIdToPointer.XlatTable from %lu to %lu entries\n",
95 pXlatTables->RefIdToPointer.NumberOfEntries, RefId * 2);
96 free(pXlatTables->RefIdToPointer.XlatTable);
97 }
98 pXlatTables->RefIdToPointer.XlatTable = ptrNew;
99 if (ptrNew)
100 {
101 ptrNew = realloc(pXlatTables->RefIdToPointer.StateTable, RefId * 2);
102 if (!ptrNew)
103 {
104 ERR("Couldn't reallocate RefIdToPointer.StateTable from %lu to %lu entries\n",
105 pXlatTables->RefIdToPointer.NumberOfEntries, RefId * 2);
106 free(pXlatTables->RefIdToPointer.StateTable);
107 }
108 pXlatTables->RefIdToPointer.StateTable = ptrNew;
109 }
110#else
111 pXlatTables->RefIdToPointer.XlatTable =
112 realloc(pXlatTables->RefIdToPointer.XlatTable, sizeof(void *) * RefId * 2);
113 pXlatTables->RefIdToPointer.StateTable =
114 realloc(pXlatTables->RefIdToPointer.StateTable, RefId * 2);
115#endif
116 if (!pXlatTables->RefIdToPointer.XlatTable || !pXlatTables->RefIdToPointer.StateTable)
117 {
118#ifdef __REACTOS__
119 free(pXlatTables->RefIdToPointer.XlatTable);
120 free(pXlatTables->RefIdToPointer.StateTable);
121 pXlatTables->RefIdToPointer.XlatTable = NULL;
122 pXlatTables->RefIdToPointer.StateTable = NULL;
123#endif
124 pXlatTables->RefIdToPointer.NumberOfEntries = 0;
125 return;
126 }
127 memset(pXlatTables->RefIdToPointer.XlatTable + pXlatTables->RefIdToPointer.NumberOfEntries, 0,
128 (RefId * 2 - pXlatTables->RefIdToPointer.NumberOfEntries) * sizeof(void *));
129 memset(pXlatTables->RefIdToPointer.StateTable + pXlatTables->RefIdToPointer.NumberOfEntries, 0,
130 RefId * 2 - pXlatTables->RefIdToPointer.NumberOfEntries);
131 pXlatTables->RefIdToPointer.NumberOfEntries = RefId * 2;
132 }
133}
134
136 void *pPointer, unsigned char QueryType,
137 ULONG *pRefId )
138{
139 ULONG Hash = 0;
140 unsigned int i;
141 PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry;
142
143 TRACE("(%p, %p, %d, %p)\n", pXlatTables, pPointer, QueryType, pRefId);
144
145 if (!pPointer)
146 {
147 *pRefId = 0;
148 return 1;
149 }
150
151 /* simple hashing algorithm, don't know whether it matches native */
152 for (i = 0; i < sizeof(pPointer); i++)
153 Hash = (Hash * 3) ^ ((unsigned char *)&pPointer)[i];
154
155 XlatTableEntry = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
156 for (; XlatTableEntry; XlatTableEntry = XlatTableEntry->Next)
157 if (pPointer == XlatTableEntry->Pointer)
158 {
159 *pRefId = XlatTableEntry->RefId;
160 if (XlatTableEntry->State & QueryType)
161 return 1;
162 XlatTableEntry->State |= QueryType;
163 return 0;
164 }
165
166 XlatTableEntry = malloc(sizeof(*XlatTableEntry));
167 XlatTableEntry->Next = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
168 XlatTableEntry->Pointer = pPointer;
169 XlatTableEntry->RefId = *pRefId = pXlatTables->NextRefId++;
170 XlatTableEntry->State = QueryType;
171 pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask] = XlatTableEntry;
172
173 /* insert pointer into mapping table */
174 expand_pointer_table_if_necessary(pXlatTables, XlatTableEntry->RefId);
175 if (pXlatTables->RefIdToPointer.NumberOfEntries > XlatTableEntry->RefId)
176 {
177 pXlatTables->RefIdToPointer.XlatTable[XlatTableEntry->RefId] = pPointer;
178 pXlatTables->RefIdToPointer.StateTable[XlatTableEntry->RefId] = QueryType;
179 }
180
181 return 0;
182}
183
185 ULONG RefId, unsigned char QueryType,
186 void **ppPointer)
187{
188 TRACE("(%p, 0x%lx, %d, %p)\n", pXlatTables, RefId, QueryType, ppPointer);
189
190 if (!RefId)
191 return 1;
192
193 expand_pointer_table_if_necessary(pXlatTables, RefId);
194
195 pXlatTables->NextRefId = max(RefId + 1, pXlatTables->NextRefId);
196
197 if (pXlatTables->RefIdToPointer.NumberOfEntries > RefId)
198 {
199 *ppPointer = pXlatTables->RefIdToPointer.XlatTable[RefId];
200 if (QueryType)
201 {
202 if (pXlatTables->RefIdToPointer.StateTable[RefId] & QueryType)
203 return 1;
204 pXlatTables->RefIdToPointer.StateTable[RefId] |= QueryType;
205 return 0;
206 }
207 else
208 return 0;
209 }
210 *ppPointer = NULL;
211 return 0;
212}
213
215 ULONG RefId, void *pPointer)
216{
217 ULONG Hash = 0;
218 unsigned int i;
219 PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry;
220
221 TRACE("(%p, 0x%lx, %p)\n", pXlatTables, RefId, pPointer);
222
223 /* simple hashing algorithm, don't know whether it matches native */
224 for (i = 0; i < sizeof(pPointer); i++)
225 Hash = (Hash * 3) ^ ((unsigned char *)&pPointer)[i];
226
227 XlatTableEntry = malloc(sizeof(*XlatTableEntry));
228 XlatTableEntry->Next = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
229 XlatTableEntry->Pointer = pPointer;
230 XlatTableEntry->RefId = RefId;
231 XlatTableEntry->State = 0;
232 pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask] = XlatTableEntry;
233
234 /* insert pointer into mapping table */
235 expand_pointer_table_if_necessary(pXlatTables, RefId);
236 if (pXlatTables->RefIdToPointer.NumberOfEntries > RefId)
237 pXlatTables->RefIdToPointer.XlatTable[XlatTableEntry->RefId] = pPointer;
238}
239
240int WINAPI NdrFullPointerFree(PFULL_PTR_XLAT_TABLES pXlatTables, void *Pointer)
241{
242 ULONG Hash = 0;
243 unsigned int i;
244 PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry;
245 ULONG RefId = 0;
246
247 TRACE("(%p, %p)\n", pXlatTables, Pointer);
248
249 if (!Pointer)
250 return 1;
251
252 /* simple hashing algorithm, don't know whether it matches native */
253 for (i = 0; i < sizeof(Pointer); i++)
254 Hash = (Hash * 3) ^ ((unsigned char *)&Pointer)[i];
255
256 XlatTableEntry = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
257 for (; XlatTableEntry; XlatTableEntry = XlatTableEntry->Next)
258 if (Pointer == XlatTableEntry->Pointer)
259 {
260 if (XlatTableEntry->State & 0x20)
261 return 0;
262 XlatTableEntry->State |= 0x20;
263 RefId = XlatTableEntry->RefId;
264 break;
265 }
266
267 if (!XlatTableEntry)
268 return 0;
269
270 if (pXlatTables->RefIdToPointer.NumberOfEntries > RefId)
271 {
272 pXlatTables->RefIdToPointer.StateTable[RefId] |= 0x20;
273 return 1;
274 }
275
276 return 0;
277}
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define ERR(fmt,...)
Definition: precomp.h:57
#define realloc
Definition: debug_ros.c:6
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
static int Hash(const char *)
Definition: reader.c:2237
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
int WINAPI NdrFullPointerFree(PFULL_PTR_XLAT_TABLES pXlatTables, void *Pointer)
int WINAPI NdrFullPointerQueryRefId(PFULL_PTR_XLAT_TABLES pXlatTables, ULONG RefId, unsigned char QueryType, void **ppPointer)
void WINAPI NdrFullPointerXlatFree(PFULL_PTR_XLAT_TABLES pXlatTables)
int WINAPI NdrFullPointerQueryPointer(PFULL_PTR_XLAT_TABLES pXlatTables, void *pPointer, unsigned char QueryType, ULONG *pRefId)
PFULL_PTR_XLAT_TABLES WINAPI NdrFullPointerXlatInit(ULONG NumberOfPointers, XLAT_SIDE XlatSide)
static void expand_pointer_table_if_necessary(PFULL_PTR_XLAT_TABLES pXlatTables, ULONG RefId)
void WINAPI NdrFullPointerInsertRefId(PFULL_PTR_XLAT_TABLES pXlatTables, ULONG RefId, void *pPointer)
_Must_inspect_result_ _In_ KTMOBJECT_TYPE QueryType
Definition: nttmapi.h:404
#define calloc
Definition: rosglue.h:14
XLAT_SIDE
Definition: rpcndr.h:498
#define memset(x, y, z)
Definition: compat.h:39
STDMETHOD() Next(THIS_ ULONG celt, IAssociationElement *pElement, ULONG *pceltFetched) PURE
#define TRACE(s)
Definition: solgame.cpp:4
struct _FULL_PTR_TO_REFID_ELEMENT * Next
Definition: rpcndr.h:504
unsigned char State
Definition: rpcndr.h:507
XLAT_SIDE XlatSide
Definition: rpcndr.h:525
struct _FULL_PTR_XLAT_TABLES::@3417 RefIdToPointer
struct _FULL_PTR_XLAT_TABLES::@3418 PointerToRefId
#define max(a, b)
Definition: svc.c:63
uint32_t ULONG
Definition: typedefs.h:59
#define WINAPI
Definition: msvc.h:6