Ignore:
Timestamp:
May 23, 2000, 10:41:28 PM (25 years ago)
Author:
jeroen
Message:

* empty log message *

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/opengl/mesa/hash.c

    r2962 r3598  
    1 /* $Id: hash.c,v 1.2 2000-03-01 18:49:29 jeroen Exp $ */
     1/* $Id: hash.c,v 1.3 2000-05-23 20:40:36 jeroen Exp $ */
    22
    33/*
    44 * Mesa 3-D graphics library
    5  * Version:  3.1
     5 * Version:  3.3
    66 *
    77 * Copyright (C) 1999  Brian Paul   All Rights Reserved.
     
    2626
    2727
    28 
    2928#ifdef PC_HEADER
    3029#include "all.h"
    3130#else
    32 #ifndef XFree86Server
    33 #include <assert.h>
    34 #include <stdlib.h>
    35 #include <stdio.h>
    36 #else
    37 #include "GL/xf86glx.h"
     31#include "glheader.h"
     32#include "macros.h"
     33#include "hash.h"
     34#include "mem.h"
     35#include "glthread.h"
    3836#endif
    39 #include "hash.h"
    40 #include "macros.h"
    41 #endif
    42 
    43 
    44 /*
    45  * Generic hash table.  Only dependency is the GLuint datatype.
     37
     38
     39/*
     40 * Generic hash table.
    4641 *
    4742 * This is used to implement display list and texture object lookup.
     
    5853};
    5954
    60 struct HashTable {
     55struct _mesa_HashTable {
    6156   struct HashEntry *Table[TABLE_SIZE];
    6257   GLuint MaxKey;
     58   _glthread_Mutex Mutex;
    6359};
    6460
     
    6864 * Return pointer to a new, empty hash table.
    6965 */
    70 struct HashTable *NewHashTable(void)
    71 {
    72    return CALLOC_STRUCT(HashTable);
     66struct _mesa_HashTable *_mesa_NewHashTable(void)
     67{
     68   return CALLOC_STRUCT(_mesa_HashTable);
    7369}
    7470
     
    7874 * Delete a hash table.
    7975 */
    80 void DeleteHashTable(struct HashTable *table)
     76void _mesa_DeleteHashTable(struct _mesa_HashTable *table)
    8177{
    8278   GLuint i;
    83    assert(table);
     79   ASSERT(table);
    8480   for (i=0;i<TABLE_SIZE;i++) {
    8581      struct HashEntry *entry = table->Table[i];
     
    10197 * Return:  user data pointer or NULL if key not in table
    10298 */
    103 void *HashLookup(const struct HashTable *table, GLuint key)
     99void *_mesa_HashLookup(const struct _mesa_HashTable *table, GLuint key)
    104100{
    105101   GLuint pos;
    106102   const struct HashEntry *entry;
    107103
    108 /* assert(table);
    109    assert(key);
    110 */
     104   ASSERT(table);
     105   ASSERT(key);
     106
    111107   pos = key & (TABLE_SIZE-1);
    112108   entry = table->Table[pos];
     
    129125 *         data - pointer to user data
    130126 */
    131 void HashInsert(struct HashTable *table, GLuint key, void *data)
     127void _mesa_HashInsert(struct _mesa_HashTable *table, GLuint key, void *data)
    132128{
    133129   /* search for existing entry with this key */
     
    135131   struct HashEntry *entry;
    136132
    137    assert(table);
    138    assert(key);
     133   ASSERT(table);
     134   ASSERT(key);
     135
     136   _glthread_LOCK_MUTEX(table->Mutex);
    139137
    140138   if (key > table->MaxKey)
     
    147145         /* replace entry's data */
    148146         entry->Data = data;
     147         _glthread_UNLOCK_MUTEX(table->Mutex);
    149148         return;
    150149      }
     
    158157   entry->Next = table->Table[pos];
    159158   table->Table[pos] = entry;
     159
     160   _glthread_UNLOCK_MUTEX(table->Mutex);
    160161}
    161162
     
    167168 *         key - key of entry to remove
    168169 */
    169 void HashRemove(struct HashTable *table, GLuint key)
     170void _mesa_HashRemove(struct _mesa_HashTable *table, GLuint key)
    170171{
    171172   GLuint pos;
    172173   struct HashEntry *entry, *prev;
    173174
    174    assert(table);
    175    assert(key);
     175   ASSERT(table);
     176   ASSERT(key);
     177
     178   _glthread_LOCK_MUTEX(table->Mutex);
    176179
    177180   pos = key & (TABLE_SIZE-1);
     
    188191         }
    189192         FREE(entry);
     193         _glthread_UNLOCK_MUTEX(table->Mutex);
    190194         return;
    191195      }
     
    193197      entry = entry->Next;
    194198   }
     199
     200   _glthread_UNLOCK_MUTEX(table->Mutex);
    195201}
    196202
     
    199205/*
    200206 * Return the key of the "first" entry in the hash table.
    201  * By calling this function until zero is returned we can get
    202  * the keys of all entries in the table.
    203  */
    204 GLuint HashFirstEntry(const struct HashTable *table)
     207 * This is used in the course of deleting all display lists when
     208 * a context is destroyed.
     209 */
     210GLuint _mesa_HashFirstEntry(struct _mesa_HashTable *table)
    205211{
    206212   GLuint pos;
    207    assert(table);
     213   ASSERT(table);
     214   _glthread_LOCK_MUTEX(table->Mutex);
    208215   for (pos=0; pos < TABLE_SIZE; pos++) {
    209       if (table->Table[pos])
     216      if (table->Table[pos]) {
     217         _glthread_UNLOCK_MUTEX(table->Mutex);
    210218         return table->Table[pos]->Key;
    211    }
     219      }
     220   }
     221   _glthread_UNLOCK_MUTEX(table->Mutex);
    212222   return 0;
    213223}
     
    218228 * Dump contents of hash table for debugging.
    219229 */
    220 void HashPrint(const struct HashTable *table)
     230void _mesa_HashPrint(const struct _mesa_HashTable *table)
    221231{
    222232   GLuint i;
    223    assert(table);
     233   ASSERT(table);
    224234   for (i=0;i<TABLE_SIZE;i++) {
    225235      const struct HashEntry *entry = table->Table[i];
     
    237247 * Input:  table - the hash table
    238248 *         numKeys - number of keys needed
    239  * Return:  startint key of free block or 0 if failure
    240  */
    241 GLuint HashFindFreeKeyBlock(const struct HashTable *table, GLuint numKeys)
     249 * Return:  starting key of free block or 0 if failure
     250 */
     251GLuint _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable *table, GLuint numKeys)
    242252{
    243253   GLuint maxKey = ~((GLuint) 0);
     254   _glthread_LOCK_MUTEX(table->Mutex);
    244255   if (maxKey - numKeys > table->MaxKey) {
    245256      /* the quick solution */
     257      _glthread_UNLOCK_MUTEX(table->Mutex);
    246258      return table->MaxKey + 1;
    247259   }
     
    249261      /* the slow solution */
    250262      GLuint freeCount = 0;
    251       GLuint freeStart = 0;
     263      GLuint freeStart = 1;
    252264      GLuint key;
    253       for (key=0; key!=maxKey; key++) {
    254          if (HashLookup(table, key)) {
     265      for (key=1; key!=maxKey; key++) {
     266         if (_mesa_HashLookup(table, key)) {
    255267            /* darn, this key is already in use */
    256268            freeCount = 0;
     
    261273            freeCount++;
    262274            if (freeCount == numKeys) {
     275               _glthread_UNLOCK_MUTEX(table->Mutex);
    263276               return freeStart;
    264277            }
     
    266279      }
    267280      /* cannot allocate a block of numKeys consecutive keys */
     281      _glthread_UNLOCK_MUTEX(table->Mutex);
    268282      return 0;
    269283   }
     
    281295   printf("&b = %p\n", &b);
    282296
    283    t = NewHashTable();
    284    HashInsert(t, 501, &a);
    285    HashInsert(t, 10, &c);
    286    HashInsert(t, 0xfffffff8, &b);
    287    HashPrint(t);
    288    printf("Find 501: %p\n", HashLookup(t,501));
    289    printf("Find 1313: %p\n", HashLookup(t,1313));
    290    printf("Find block of 100: %d\n", HashFindFreeKeyBlock(t, 100));
    291    DeleteHashTable(t);
     297   t = _mesa_NewHashTable();
     298   _mesa_HashInsert(t, 501, &a);
     299   _mesa_HashInsert(t, 10, &c);
     300   _mesa_HashInsert(t, 0xfffffff8, &b);
     301   _mesa_HashPrint(t);
     302   printf("Find 501: %p\n", _mesa_HashLookup(t,501));
     303   printf("Find 1313: %p\n", _mesa_HashLookup(t,1313));
     304   printf("Find block of 100: %d\n", _mesa_HashFindFreeKeyBlock(t, 100));
     305   _mesa_DeleteHashTable(t);
    292306
    293307   return 0;
Note: See TracChangeset for help on using the changeset viewer.