aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/pvr/hash.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpu/pvr/hash.c')
-rw-r--r--drivers/gpu/pvr/hash.c477
1 files changed, 477 insertions, 0 deletions
diff --git a/drivers/gpu/pvr/hash.c b/drivers/gpu/pvr/hash.c
new file mode 100644
index 00000000000..32b0779d456
--- /dev/null
+++ b/drivers/gpu/pvr/hash.c
@@ -0,0 +1,477 @@
1/**********************************************************************
2 *
3 * Copyright(c) 2008 Imagination Technologies Ltd. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful but, except
10 * as otherwise stated in writing, without any warranty; without even the
11 * implied warranty of merchantability or fitness for a particular purpose.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * Imagination Technologies Ltd. <gpl-support@imgtec.com>
23 * Home Park Estate, Kings Langley, Herts, WD4 8LZ, UK
24 *
25 ******************************************************************************/
26
27#include "pvr_debug.h"
28#include "img_defs.h"
29#include "services.h"
30#include "servicesint.h"
31#include "hash.h"
32#include "osfunc.h"
33
34#define PRIVATE_MAX(a,b) ((a)>(b)?(a):(b))
35
36#define KEY_TO_INDEX(pHash, key, uSize) \
37 ((pHash)->pfnHashFunc((pHash)->uKeySize, (key), (uSize)) % (uSize))
38
39#define KEY_COMPARE(pHash, pKey1, pKey2) \
40 ((pHash)->pfnKeyComp((pHash)->uKeySize, (pKey1), (pKey2)))
41
42struct _BUCKET_
43{
44
45 struct _BUCKET_ *pNext;
46
47
48 IMG_UINTPTR_T v;
49
50
51 IMG_UINTPTR_T k[];
52};
53typedef struct _BUCKET_ BUCKET;
54
55struct _HASH_TABLE_
56{
57
58 BUCKET **ppBucketTable;
59
60
61 IMG_UINT32 uSize;
62
63
64 IMG_UINT32 uCount;
65
66
67 IMG_UINT32 uMinimumSize;
68
69
70 IMG_UINT32 uKeySize;
71
72
73 HASH_FUNC *pfnHashFunc;
74
75
76 HASH_KEY_COMP *pfnKeyComp;
77};
78
79IMG_UINT32
80HASH_Func_Default (IMG_SIZE_T uKeySize, IMG_VOID *pKey, IMG_UINT32 uHashTabLen)
81{
82 IMG_UINTPTR_T *p = (IMG_UINTPTR_T *)pKey;
83 IMG_UINT32 uKeyLen = uKeySize / sizeof(IMG_UINTPTR_T);
84 IMG_UINT32 ui;
85 IMG_UINT32 uHashKey = 0;
86
87 PVR_UNREFERENCED_PARAMETER(uHashTabLen);
88
89 PVR_ASSERT((uKeySize % sizeof(IMG_UINTPTR_T)) == 0);
90
91 for (ui = 0; ui < uKeyLen; ui++)
92 {
93 IMG_UINT32 uHashPart = (IMG_UINT32)*p++;
94
95 uHashPart += (uHashPart << 12);
96 uHashPart ^= (uHashPart >> 22);
97 uHashPart += (uHashPart << 4);
98 uHashPart ^= (uHashPart >> 9);
99 uHashPart += (uHashPart << 10);
100 uHashPart ^= (uHashPart >> 2);
101 uHashPart += (uHashPart << 7);
102 uHashPart ^= (uHashPart >> 12);
103
104 uHashKey += uHashPart;
105 }
106
107 return uHashKey;
108}
109
110IMG_BOOL
111HASH_Key_Comp_Default (IMG_SIZE_T uKeySize, IMG_VOID *pKey1, IMG_VOID *pKey2)
112{
113 IMG_UINTPTR_T *p1 = (IMG_UINTPTR_T *)pKey1;
114 IMG_UINTPTR_T *p2 = (IMG_UINTPTR_T *)pKey2;
115 IMG_UINT32 uKeyLen = uKeySize / sizeof(IMG_UINTPTR_T);
116 IMG_UINT32 ui;
117
118 PVR_ASSERT((uKeySize % sizeof(IMG_UINTPTR_T)) == 0);
119
120 for (ui = 0; ui < uKeyLen; ui++)
121 {
122 if (*p1++ != *p2++)
123 return IMG_FALSE;
124 }
125
126 return IMG_TRUE;
127}
128
129static PVRSRV_ERROR
130_ChainInsert (HASH_TABLE *pHash, BUCKET *pBucket, BUCKET **ppBucketTable, IMG_UINT32 uSize)
131{
132 IMG_UINT32 uIndex;
133
134 PVR_ASSERT (pBucket != IMG_NULL);
135 PVR_ASSERT (ppBucketTable != IMG_NULL);
136 PVR_ASSERT (uSize != 0);
137
138 if ((pBucket == IMG_NULL) || (ppBucketTable == IMG_NULL) || (uSize == 0))
139 {
140 PVR_DPF((PVR_DBG_ERROR, "_ChainInsert: invalid parameter"));
141 return PVRSRV_ERROR_INVALID_PARAMS;
142 }
143
144 uIndex = KEY_TO_INDEX(pHash, pBucket->k, uSize);
145 pBucket->pNext = ppBucketTable[uIndex];
146 ppBucketTable[uIndex] = pBucket;
147
148 return PVRSRV_OK;
149}
150
151static PVRSRV_ERROR
152_Rehash (HASH_TABLE *pHash,
153 BUCKET **ppOldTable, IMG_UINT32 uOldSize,
154 BUCKET **ppNewTable, IMG_UINT32 uNewSize)
155{
156 IMG_UINT32 uIndex;
157 for (uIndex=0; uIndex< uOldSize; uIndex++)
158 {
159 BUCKET *pBucket;
160 pBucket = ppOldTable[uIndex];
161 while (pBucket != IMG_NULL)
162 {
163 PVRSRV_ERROR eError;
164 BUCKET *pNextBucket = pBucket->pNext;
165 eError = _ChainInsert (pHash, pBucket, ppNewTable, uNewSize);
166 if (eError != PVRSRV_OK)
167 {
168 PVR_DPF((PVR_DBG_ERROR, "_Rehash: call to _ChainInsert failed"));
169 return eError;
170 }
171 pBucket = pNextBucket;
172 }
173 }
174 return PVRSRV_OK;
175}
176
177static IMG_BOOL
178_Resize (HASH_TABLE *pHash, IMG_UINT32 uNewSize)
179{
180 if (uNewSize != pHash->uSize)
181 {
182 BUCKET **ppNewTable;
183 IMG_UINT32 uIndex;
184
185 PVR_DPF ((PVR_DBG_MESSAGE,
186 "HASH_Resize: oldsize=0x%x newsize=0x%x count=0x%x",
187 pHash->uSize, uNewSize, pHash->uCount));
188
189 OSAllocMem(PVRSRV_PAGEABLE_SELECT,
190 sizeof (BUCKET *) * uNewSize,
191 (IMG_PVOID*)&ppNewTable, IMG_NULL,
192 "Hash Table Buckets");
193 if (ppNewTable == IMG_NULL)
194 return IMG_FALSE;
195
196 for (uIndex=0; uIndex<uNewSize; uIndex++)
197 ppNewTable[uIndex] = IMG_NULL;
198
199 if (_Rehash (pHash, pHash->ppBucketTable, pHash->uSize, ppNewTable, uNewSize) != PVRSRV_OK)
200 {
201 return IMG_FALSE;
202 }
203
204 OSFreeMem (PVRSRV_PAGEABLE_SELECT, sizeof(BUCKET *)*pHash->uSize, pHash->ppBucketTable, IMG_NULL);
205
206 pHash->ppBucketTable = ppNewTable;
207 pHash->uSize = uNewSize;
208 }
209 return IMG_TRUE;
210}
211
212
213HASH_TABLE * HASH_Create_Extended (IMG_UINT32 uInitialLen, IMG_SIZE_T uKeySize, HASH_FUNC *pfnHashFunc, HASH_KEY_COMP *pfnKeyComp)
214{
215 HASH_TABLE *pHash;
216 IMG_UINT32 uIndex;
217
218 PVR_DPF ((PVR_DBG_MESSAGE, "HASH_Create_Extended: InitialSize=0x%x", uInitialLen));
219
220 if(OSAllocMem(PVRSRV_PAGEABLE_SELECT,
221 sizeof(HASH_TABLE),
222 (IMG_VOID **)&pHash, IMG_NULL,
223 "Hash Table") != PVRSRV_OK)
224 {
225 return IMG_NULL;
226 }
227
228 pHash->uCount = 0;
229 pHash->uSize = uInitialLen;
230 pHash->uMinimumSize = uInitialLen;
231 pHash->uKeySize = uKeySize;
232 pHash->pfnHashFunc = pfnHashFunc;
233 pHash->pfnKeyComp = pfnKeyComp;
234
235 OSAllocMem(PVRSRV_PAGEABLE_SELECT,
236 sizeof (BUCKET *) * pHash->uSize,
237 (IMG_PVOID*)&pHash->ppBucketTable, IMG_NULL,
238 "Hash Table Buckets");
239
240 if (pHash->ppBucketTable == IMG_NULL)
241 {
242 OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(HASH_TABLE), pHash, IMG_NULL);
243
244 return IMG_NULL;
245 }
246
247 for (uIndex=0; uIndex<pHash->uSize; uIndex++)
248 pHash->ppBucketTable[uIndex] = IMG_NULL;
249 return pHash;
250}
251
252HASH_TABLE * HASH_Create (IMG_UINT32 uInitialLen)
253{
254 return HASH_Create_Extended(uInitialLen, sizeof(IMG_UINTPTR_T),
255 &HASH_Func_Default, &HASH_Key_Comp_Default);
256}
257
258IMG_VOID
259HASH_Delete (HASH_TABLE *pHash)
260{
261 if (pHash != IMG_NULL)
262 {
263 PVR_DPF ((PVR_DBG_MESSAGE, "HASH_Delete"));
264
265 PVR_ASSERT (pHash->uCount==0);
266 if(pHash->uCount != 0)
267 {
268 PVR_DPF ((PVR_DBG_ERROR, "HASH_Delete: leak detected in hash table!"));
269 PVR_DPF ((PVR_DBG_ERROR, "Likely Cause: client drivers not freeing alocations before destroying devmemcontext"));
270 }
271 OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(BUCKET *)*pHash->uSize, pHash->ppBucketTable, IMG_NULL);
272 pHash->ppBucketTable = IMG_NULL;
273 OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(HASH_TABLE), pHash, IMG_NULL);
274
275 }
276}
277
278IMG_BOOL
279HASH_Insert_Extended (HASH_TABLE *pHash, IMG_VOID *pKey, IMG_UINTPTR_T v)
280{
281 BUCKET *pBucket;
282
283 PVR_DPF ((PVR_DBG_MESSAGE,
284 "HASH_Insert_Extended: Hash=0x%08x, pKey=0x%08x, v=0x%x",
285 (IMG_UINTPTR_T)pHash, (IMG_UINTPTR_T)pKey, v));
286
287 PVR_ASSERT (pHash != IMG_NULL);
288
289 if (pHash == IMG_NULL)
290 {
291 PVR_DPF((PVR_DBG_ERROR, "HASH_Insert_Extended: invalid parameter"));
292 return IMG_FALSE;
293 }
294
295 if(OSAllocMem(PVRSRV_PAGEABLE_SELECT,
296 sizeof(BUCKET) + pHash->uKeySize,
297 (IMG_VOID **)&pBucket, IMG_NULL,
298 "Hash Table entry") != PVRSRV_OK)
299 {
300 return IMG_FALSE;
301 }
302
303 pBucket->v = v;
304
305 OSMemCopy(pBucket->k, pKey, pHash->uKeySize);
306 if (_ChainInsert (pHash, pBucket, pHash->ppBucketTable, pHash->uSize) != PVRSRV_OK)
307 {
308 return IMG_FALSE;
309 }
310
311 pHash->uCount++;
312
313
314 if (pHash->uCount << 1 > pHash->uSize)
315 {
316
317
318 _Resize (pHash, pHash->uSize << 1);
319 }
320
321
322 return IMG_TRUE;
323}
324
325IMG_BOOL
326HASH_Insert (HASH_TABLE *pHash, IMG_UINTPTR_T k, IMG_UINTPTR_T v)
327{
328 PVR_DPF ((PVR_DBG_MESSAGE,
329 "HASH_Insert: Hash=0x%x, k=0x%x, v=0x%x",
330 (IMG_UINTPTR_T)pHash, k, v));
331
332 return HASH_Insert_Extended(pHash, &k, v);
333}
334
335IMG_UINTPTR_T
336HASH_Remove_Extended(HASH_TABLE *pHash, IMG_VOID *pKey)
337{
338 BUCKET **ppBucket;
339 IMG_UINT32 uIndex;
340
341 PVR_DPF ((PVR_DBG_MESSAGE, "HASH_Remove_Extended: Hash=0x%x, pKey=0x%x",
342 (IMG_UINTPTR_T)pHash, (IMG_UINTPTR_T)pKey));
343
344 PVR_ASSERT (pHash != IMG_NULL);
345
346 if (pHash == IMG_NULL)
347 {
348 PVR_DPF((PVR_DBG_ERROR, "HASH_Remove_Extended: Null hash table"));
349 return 0;
350 }
351
352 uIndex = KEY_TO_INDEX(pHash, pKey, pHash->uSize);
353
354 for (ppBucket = &(pHash->ppBucketTable[uIndex]); *ppBucket != IMG_NULL; ppBucket = &((*ppBucket)->pNext))
355 {
356
357 if (KEY_COMPARE(pHash, (*ppBucket)->k, pKey))
358 {
359 BUCKET *pBucket = *ppBucket;
360 IMG_UINTPTR_T v = pBucket->v;
361 (*ppBucket) = pBucket->pNext;
362
363 OSFreeMem(PVRSRV_PAGEABLE_SELECT, sizeof(BUCKET) + pHash->uKeySize, pBucket, IMG_NULL);
364
365
366 pHash->uCount--;
367
368
369 if (pHash->uSize > (pHash->uCount << 2) &&
370 pHash->uSize > pHash->uMinimumSize)
371 {
372
373
374 _Resize (pHash,
375 PRIVATE_MAX (pHash->uSize >> 1,
376 pHash->uMinimumSize));
377 }
378
379 PVR_DPF ((PVR_DBG_MESSAGE,
380 "HASH_Remove_Extended: Hash=0x%x, pKey=0x%x = 0x%x",
381 (IMG_UINTPTR_T)pHash, (IMG_UINTPTR_T)pKey, v));
382 return v;
383 }
384 }
385 PVR_DPF ((PVR_DBG_MESSAGE,
386 "HASH_Remove_Extended: Hash=0x%x, pKey=0x%x = 0x0 !!!!",
387 (IMG_UINTPTR_T)pHash, (IMG_UINTPTR_T)pKey));
388 return 0;
389}
390
391IMG_UINTPTR_T
392HASH_Remove (HASH_TABLE *pHash, IMG_UINTPTR_T k)
393{
394 PVR_DPF ((PVR_DBG_MESSAGE, "HASH_Remove: Hash=0x%x, k=0x%x",
395 (IMG_UINTPTR_T)pHash, k));
396
397 return HASH_Remove_Extended(pHash, &k);
398}
399
400IMG_UINTPTR_T
401HASH_Retrieve_Extended (HASH_TABLE *pHash, IMG_VOID *pKey)
402{
403 BUCKET **ppBucket;
404 IMG_UINT32 uIndex;
405
406 PVR_DPF ((PVR_DBG_MESSAGE, "HASH_Retrieve_Extended: Hash=0x%x, pKey=0x%x",
407 (IMG_UINTPTR_T)pHash, (IMG_UINTPTR_T)pKey));
408
409 PVR_ASSERT (pHash != IMG_NULL);
410
411 if (pHash == IMG_NULL)
412 {
413 PVR_DPF((PVR_DBG_ERROR, "HASH_Retrieve_Extended: Null hash table"));
414 return 0;
415 }
416
417 uIndex = KEY_TO_INDEX(pHash, pKey, pHash->uSize);
418
419 for (ppBucket = &(pHash->ppBucketTable[uIndex]); *ppBucket != IMG_NULL; ppBucket = &((*ppBucket)->pNext))
420 {
421
422 if (KEY_COMPARE(pHash, (*ppBucket)->k, pKey))
423 {
424 BUCKET *pBucket = *ppBucket;
425 IMG_UINTPTR_T v = pBucket->v;
426
427 PVR_DPF ((PVR_DBG_MESSAGE,
428 "HASH_Retrieve: Hash=0x%x, pKey=0x%x = 0x%x",
429 (IMG_UINTPTR_T)pHash, (IMG_UINTPTR_T)pKey, v));
430 return v;
431 }
432 }
433 PVR_DPF ((PVR_DBG_MESSAGE,
434 "HASH_Retrieve: Hash=0x%x, pKey=0x%x = 0x0 !!!!",
435 (IMG_UINTPTR_T)pHash, (IMG_UINTPTR_T)pKey));
436 return 0;
437}
438
439IMG_UINTPTR_T
440HASH_Retrieve (HASH_TABLE *pHash, IMG_UINTPTR_T k)
441{
442 PVR_DPF ((PVR_DBG_MESSAGE, "HASH_Retrieve: Hash=0x%x, k=0x%x",
443 (IMG_UINTPTR_T)pHash, k));
444 return HASH_Retrieve_Extended(pHash, &k);
445}
446
447#ifdef HASH_TRACE
448IMG_VOID
449HASH_Dump (HASH_TABLE *pHash)
450{
451 IMG_UINT32 uIndex;
452 IMG_UINT32 uMaxLength=0;
453 IMG_UINT32 uEmptyCount=0;
454
455 PVR_ASSERT (pHash != IMG_NULL);
456 for (uIndex=0; uIndex<pHash->uSize; uIndex++)
457 {
458 BUCKET *pBucket;
459 IMG_UINT32 uLength = 0;
460 if (pHash->ppBucketTable[uIndex] == IMG_NULL)
461 {
462 uEmptyCount++;
463 }
464 for (pBucket=pHash->ppBucketTable[uIndex];
465 pBucket != IMG_NULL;
466 pBucket = pBucket->pNext)
467 {
468 uLength++;
469 }
470 uMaxLength = PRIVATE_MAX (uMaxLength, uLength);
471 }
472
473 PVR_TRACE(("hash table: uMinimumSize=%d size=%d count=%d",
474 pHash->uMinimumSize, pHash->uSize, pHash->uCount));
475 PVR_TRACE((" empty=%d max=%d", uEmptyCount, uMaxLength));
476}
477#endif