Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2009,2012 Intel Corporation
  3.  * Copyright © 1988-2004 Keith Packard and Bart Massey.
  4.  *
  5.  * Permission is hereby granted, free of charge, to any person obtaining a
  6.  * copy of this software and associated documentation files (the "Software"),
  7.  * to deal in the Software without restriction, including without limitation
  8.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9.  * and/or sell copies of the Software, and to permit persons to whom the
  10.  * Software is furnished to do so, subject to the following conditions:
  11.  *
  12.  * The above copyright notice and this permission notice (including the next
  13.  * paragraph) shall be included in all copies or substantial portions of the
  14.  * Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22.  * IN THE SOFTWARE.
  23.  *
  24.  * Except as contained in this notice, the names of the authors
  25.  * or their institutions shall not be used in advertising or
  26.  * otherwise to promote the sale, use or other dealings in this
  27.  * Software without prior written authorization from the
  28.  * authors.
  29.  *
  30.  * Authors:
  31.  *    Eric Anholt <eric@anholt.net>
  32.  *    Keith Packard <keithp@keithp.com>
  33.  */
  34.  
  35. /**
  36.  * Implements an open-addressing, linear-reprobing hash table.
  37.  *
  38.  * For more information, see:
  39.  *
  40.  * http://cgit.freedesktop.org/~anholt/hash_table/tree/README
  41.  */
  42.  
  43. #include <stdlib.h>
  44. #include <string.h>
  45.  
  46. #include "main/hash_table.h"
  47. #include "main/macros.h"
  48. #include "ralloc.h"
  49.  
  50. static const uint32_t deleted_key_value;
  51.  
  52. /**
  53.  * From Knuth -- a good choice for hash/rehash values is p, p-2 where
  54.  * p and p-2 are both prime.  These tables are sized to have an extra 10%
  55.  * free to avoid exponential performance degradation as the hash table fills
  56.  */
  57. static const struct {
  58.    uint32_t max_entries, size, rehash;
  59. } hash_sizes[] = {
  60.    { 2,                 5,              3         },
  61.    { 4,                 7,              5         },
  62.    { 8,                 13,             11        },
  63.    { 16,                19,             17        },
  64.    { 32,                43,             41        },
  65.    { 64,                73,             71        },
  66.    { 128,               151,            149       },
  67.    { 256,               283,            281       },
  68.    { 512,               571,            569       },
  69.    { 1024,              1153,           1151      },
  70.    { 2048,              2269,           2267      },
  71.    { 4096,              4519,           4517      },
  72.    { 8192,              9013,           9011      },
  73.    { 16384,             18043,          18041     },
  74.    { 32768,             36109,          36107     },
  75.    { 65536,             72091,          72089     },
  76.    { 131072,            144409,         144407    },
  77.    { 262144,            288361,         288359    },
  78.    { 524288,            576883,         576881    },
  79.    { 1048576,           1153459,        1153457   },
  80.    { 2097152,           2307163,        2307161   },
  81.    { 4194304,           4613893,        4613891   },
  82.    { 8388608,           9227641,        9227639   },
  83.    { 16777216,          18455029,       18455027  },
  84.    { 33554432,          36911011,       36911009  },
  85.    { 67108864,          73819861,       73819859  },
  86.    { 134217728,         147639589,      147639587 },
  87.    { 268435456,         295279081,      295279079 },
  88.    { 536870912,         590559793,      590559791 },
  89.    { 1073741824,        1181116273,     1181116271},
  90.    { 2147483648ul,      2362232233ul,   2362232231ul}
  91. };
  92.  
  93. static int
  94. entry_is_free(const struct hash_entry *entry)
  95. {
  96.    return entry->key == NULL;
  97. }
  98.  
  99. static int
  100. entry_is_deleted(const struct hash_table *ht, struct hash_entry *entry)
  101. {
  102.    return entry->key == ht->deleted_key;
  103. }
  104.  
  105. static int
  106. entry_is_present(const struct hash_table *ht, struct hash_entry *entry)
  107. {
  108.    return entry->key != NULL && entry->key != ht->deleted_key;
  109. }
  110.  
  111. struct hash_table *
  112. _mesa_hash_table_create(void *mem_ctx,
  113.                         bool (*key_equals_function)(const void *a,
  114.                                                     const void *b))
  115. {
  116.    struct hash_table *ht;
  117.  
  118.    ht = ralloc(mem_ctx, struct hash_table);
  119.    if (ht == NULL)
  120.       return NULL;
  121.  
  122.    ht->mem_ctx = mem_ctx;
  123.    ht->size_index = 0;
  124.    ht->size = hash_sizes[ht->size_index].size;
  125.    ht->rehash = hash_sizes[ht->size_index].rehash;
  126.    ht->max_entries = hash_sizes[ht->size_index].max_entries;
  127.    ht->key_equals_function = key_equals_function;
  128.    ht->table = rzalloc_array(ht, struct hash_entry, ht->size);
  129.    ht->entries = 0;
  130.    ht->deleted_entries = 0;
  131.    ht->deleted_key = &deleted_key_value;
  132.  
  133.    if (ht->table == NULL) {
  134.       ralloc_free(ht);
  135.       return NULL;
  136.    }
  137.  
  138.    return ht;
  139. }
  140.  
  141. /**
  142.  * Frees the given hash table.
  143.  *
  144.  * If delete_function is passed, it gets called on each entry present before
  145.  * freeing.
  146.  */
  147. void
  148. _mesa_hash_table_destroy(struct hash_table *ht,
  149.                          void (*delete_function)(struct hash_entry *entry))
  150. {
  151.    if (!ht)
  152.       return;
  153.  
  154.    if (delete_function) {
  155.       struct hash_entry *entry;
  156.  
  157.       hash_table_foreach(ht, entry) {
  158.          delete_function(entry);
  159.       }
  160.    }
  161.    ralloc_free(ht);
  162. }
  163.  
  164. /** Sets the value of the key pointer used for deleted entries in the table.
  165.  *
  166.  * The assumption is that usually keys are actual pointers, so we use a
  167.  * default value of a pointer to an arbitrary piece of storage in the library.
  168.  * But in some cases a consumer wants to store some other sort of value in the
  169.  * table, like a uint32_t, in which case that pointer may conflict with one of
  170.  * their valid keys.  This lets that user select a safe value.
  171.  *
  172.  * This must be called before any keys are actually deleted from the table.
  173.  */
  174. void
  175. _mesa_hash_table_set_deleted_key(struct hash_table *ht, const void *deleted_key)
  176. {
  177.    ht->deleted_key = deleted_key;
  178. }
  179.  
  180. /**
  181.  * Finds a hash table entry with the given key and hash of that key.
  182.  *
  183.  * Returns NULL if no entry is found.  Note that the data pointer may be
  184.  * modified by the user.
  185.  */
  186. struct hash_entry *
  187. _mesa_hash_table_search(struct hash_table *ht, uint32_t hash,
  188.                         const void *key)
  189. {
  190.    uint32_t start_hash_address = hash % ht->size;
  191.    uint32_t hash_address = start_hash_address;
  192.  
  193.    do {
  194.       uint32_t double_hash;
  195.  
  196.       struct hash_entry *entry = ht->table + hash_address;
  197.  
  198.       if (entry_is_free(entry)) {
  199.          return NULL;
  200.       } else if (entry_is_present(ht, entry) && entry->hash == hash) {
  201.          if (ht->key_equals_function(key, entry->key)) {
  202.             return entry;
  203.          }
  204.       }
  205.  
  206.       double_hash = 1 + hash % ht->rehash;
  207.  
  208.       hash_address = (hash_address + double_hash) % ht->size;
  209.    } while (hash_address != start_hash_address);
  210.  
  211.    return NULL;
  212. }
  213.  
  214. static void
  215. _mesa_hash_table_rehash(struct hash_table *ht, int new_size_index)
  216. {
  217.    struct hash_table old_ht;
  218.    struct hash_entry *table, *entry;
  219.  
  220.    if (new_size_index >= ARRAY_SIZE(hash_sizes))
  221.       return;
  222.  
  223.    table = rzalloc_array(ht, struct hash_entry,
  224.                          hash_sizes[new_size_index].size);
  225.    if (table == NULL)
  226.       return;
  227.  
  228.    old_ht = *ht;
  229.  
  230.    ht->table = table;
  231.    ht->size_index = new_size_index;
  232.    ht->size = hash_sizes[ht->size_index].size;
  233.    ht->rehash = hash_sizes[ht->size_index].rehash;
  234.    ht->max_entries = hash_sizes[ht->size_index].max_entries;
  235.    ht->entries = 0;
  236.    ht->deleted_entries = 0;
  237.  
  238.    hash_table_foreach(&old_ht, entry) {
  239.       _mesa_hash_table_insert(ht, entry->hash,
  240.                               entry->key, entry->data);
  241.    }
  242.  
  243.    ralloc_free(old_ht.table);
  244. }
  245.  
  246. /**
  247.  * Inserts the key with the given hash into the table.
  248.  *
  249.  * Note that insertion may rearrange the table on a resize or rehash,
  250.  * so previously found hash_entries are no longer valid after this function.
  251.  */
  252. struct hash_entry *
  253. _mesa_hash_table_insert(struct hash_table *ht, uint32_t hash,
  254.                         const void *key, void *data)
  255. {
  256.    uint32_t start_hash_address, hash_address;
  257.  
  258.    if (ht->entries >= ht->max_entries) {
  259.       _mesa_hash_table_rehash(ht, ht->size_index + 1);
  260.    } else if (ht->deleted_entries + ht->entries >= ht->max_entries) {
  261.       _mesa_hash_table_rehash(ht, ht->size_index);
  262.    }
  263.  
  264.    start_hash_address = hash % ht->size;
  265.    hash_address = start_hash_address;
  266.    do {
  267.       struct hash_entry *entry = ht->table + hash_address;
  268.       uint32_t double_hash;
  269.  
  270.       if (!entry_is_present(ht, entry)) {
  271.          if (entry_is_deleted(ht, entry))
  272.             ht->deleted_entries--;
  273.          entry->hash = hash;
  274.          entry->key = key;
  275.          entry->data = data;
  276.          ht->entries++;
  277.          return entry;
  278.       }
  279.  
  280.       /* Implement replacement when another insert happens
  281.        * with a matching key.  This is a relatively common
  282.        * feature of hash tables, with the alternative
  283.        * generally being "insert the new value as well, and
  284.        * return it first when the key is searched for".
  285.        *
  286.        * Note that the hash table doesn't have a delete
  287.        * callback.  If freeing of old data pointers is
  288.        * required to avoid memory leaks, perform a search
  289.        * before inserting.
  290.        */
  291.       if (entry->hash == hash &&
  292.           ht->key_equals_function(key, entry->key)) {
  293.          entry->key = key;
  294.          entry->data = data;
  295.          return entry;
  296.       }
  297.  
  298.  
  299.       double_hash = 1 + hash % ht->rehash;
  300.  
  301.       hash_address = (hash_address + double_hash) % ht->size;
  302.    } while (hash_address != start_hash_address);
  303.  
  304.    /* We could hit here if a required resize failed. An unchecked-malloc
  305.     * application could ignore this result.
  306.     */
  307.    return NULL;
  308. }
  309.  
  310. /**
  311.  * This function deletes the given hash table entry.
  312.  *
  313.  * Note that deletion doesn't otherwise modify the table, so an iteration over
  314.  * the table deleting entries is safe.
  315.  */
  316. void
  317. _mesa_hash_table_remove(struct hash_table *ht,
  318.                         struct hash_entry *entry)
  319. {
  320.    if (!entry)
  321.       return;
  322.  
  323.    entry->key = ht->deleted_key;
  324.    ht->entries--;
  325.    ht->deleted_entries++;
  326. }
  327.  
  328. /**
  329.  * This function is an iterator over the hash table.
  330.  *
  331.  * Pass in NULL for the first entry, as in the start of a for loop.  Note that
  332.  * an iteration over the table is O(table_size) not O(entries).
  333.  */
  334. struct hash_entry *
  335. _mesa_hash_table_next_entry(struct hash_table *ht,
  336.                             struct hash_entry *entry)
  337. {
  338.    if (entry == NULL)
  339.       entry = ht->table;
  340.    else
  341.       entry = entry + 1;
  342.  
  343.    for (; entry != ht->table + ht->size; entry++) {
  344.       if (entry_is_present(ht, entry)) {
  345.          return entry;
  346.       }
  347.    }
  348.  
  349.    return NULL;
  350. }
  351.  
  352. /**
  353.  * Returns a random entry from the hash table.
  354.  *
  355.  * This may be useful in implementing random replacement (as opposed
  356.  * to just removing everything) in caches based on this hash table
  357.  * implementation.  @predicate may be used to filter entries, or may
  358.  * be set to NULL for no filtering.
  359.  */
  360. struct hash_entry *
  361. _mesa_hash_table_random_entry(struct hash_table *ht,
  362.                               bool (*predicate)(struct hash_entry *entry))
  363. {
  364.    struct hash_entry *entry;
  365.    uint32_t i = rand() % ht->size;
  366.  
  367.    if (ht->entries == 0)
  368.       return NULL;
  369.  
  370.    for (entry = ht->table + i; entry != ht->table + ht->size; entry++) {
  371.       if (entry_is_present(ht, entry) &&
  372.           (!predicate || predicate(entry))) {
  373.          return entry;
  374.       }
  375.    }
  376.  
  377.    for (entry = ht->table; entry != ht->table + i; entry++) {
  378.       if (entry_is_present(ht, entry) &&
  379.           (!predicate || predicate(entry))) {
  380.          return entry;
  381.       }
  382.    }
  383.  
  384.    return NULL;
  385. }
  386.  
  387.  
  388. /**
  389.  * Quick FNV-1 hash implementation based on:
  390.  * http://www.isthe.com/chongo/tech/comp/fnv/
  391.  *
  392.  * FNV-1 is not be the best hash out there -- Jenkins's lookup3 is supposed to
  393.  * be quite good, and it probably beats FNV.  But FNV has the advantage that
  394.  * it involves almost no code.  For an improvement on both, see Paul
  395.  * Hsieh's http://www.azillionmonkeys.com/qed/hash.html
  396.  */
  397. uint32_t
  398. _mesa_hash_data(const void *data, size_t size)
  399. {
  400.    uint32_t hash = 2166136261ul;
  401.    const uint8_t *bytes = data;
  402.  
  403.    while (size-- != 0) {
  404.       hash ^= *bytes;
  405.       hash = hash * 0x01000193;
  406.       bytes++;
  407.    }
  408.  
  409.    return hash;
  410. }
  411.  
  412. /** FNV-1 string hash implementation */
  413. uint32_t
  414. _mesa_hash_string(const char *key)
  415. {
  416.    uint32_t hash = 2166136261ul;
  417.  
  418.    while (*key != 0) {
  419.       hash ^= *key;
  420.       hash = hash * 0x01000193;
  421.       key++;
  422.    }
  423.  
  424.    return hash;
  425. }
  426.  
  427. /**
  428.  * String compare function for use as the comparison callback in
  429.  * _mesa_hash_table_create().
  430.  */
  431. bool
  432. _mesa_key_string_equal(const void *a, const void *b)
  433. {
  434.    return strcmp(a, b) == 0;
  435. }
  436.  
  437. bool
  438. _mesa_key_pointer_equal(const void *a, const void *b)
  439. {
  440.    return a == b;
  441. }
  442.