librsync  2.0.2
hashtable.c
1 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  *
3  * hashtable.c -- a generic hashtable implementation.
4  *
5  * Copyright (C) 2016 by Donovan Baarda <abo@minkirri.apana.org.au>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2.1 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include "hashtable.h"
25 
26 /* Open addressing works best if it can take advantage of memory caches using
27  locality for probes of adjacent buckets on collisions. So we pack the keys
28  tightly together in their own key table and avoid referencing the element
29  table and elements as much as possible. Key value zero is reserved as a
30  marker for an empty bucket to avoid checking for NULL in the element table.
31  If we do get a hash value of zero, we -1 to wrap it around to 0xffff. */
32 
33 /* Use max 0.8 load factor to avoid bad open addressing performance. */
34 #define HASHTABLE_LOADFACTOR_NUM 8
35 #define HASHTABLE_LOADFACTOR_DEN 10
36 
37 hashtable_t *_hashtable_new(int size)
38 {
39  hashtable_t *t;
40  int size2;
41 
42  /* Adjust requested size to account for max load factor. */
43  size = 1 + size * HASHTABLE_LOADFACTOR_DEN / HASHTABLE_LOADFACTOR_NUM;
44  /* Use next power of 2 larger than the requested size. */
45  for (size2 = 1; size2 < size; size2 <<= 1) ;
46  if (!(t = calloc(1, sizeof(hashtable_t)+ size2 * sizeof(unsigned))))
47  return NULL;
48  if (!(t->etable = calloc(size2, sizeof(void *)))) {
49  free(t);
50  return NULL;
51  }
52  t->size = size2;
53  t->count = 0;
54 #ifndef HASHTABLE_NSTATS
55  t->find_count = t->match_count = t->hashcmp_count = t->entrycmp_count = 0;
56 #endif
57  return t;
58 }
59 
60 void _hashtable_free(hashtable_t *t)
61 {
62  if (t) {
63  free(t->etable);
64  free(t);
65  }
66 }
67 
68 void *_hashtable_iter(hashtable_iter_t *i, hashtable_t *t)
69 {
70  assert(i != NULL);
71  assert(t != NULL);
72  i->htable = t;
73  i->index = 0;
74  return _hashtable_next(i);
75 }
76 
77 void *_hashtable_next(hashtable_iter_t *i)
78 {
79  assert(i->htable != NULL);
80  assert(i->index <= i->htable->size);
81  const hashtable_t *t = i->htable;
82  void *e;
83 
84  while (i->index < t->size) {
85  if ((e = t->etable[i->index++]))
86  return e;
87  }
88  return NULL;
89 }
long hashcmp_count
The count of hash compares done.
Definition: hashtable.h:133
long match_count
The count of matches found.
Definition: hashtable.h:132
The hashtable iterator type.
Definition: hashtable.h:141
int size
Size of allocated hashtable.
Definition: hashtable.h:127
long entrycmp_count
The count of entry compares done.
Definition: hashtable.h:134
hashtable_t * htable
The hashtable to iterate over.
Definition: hashtable.h:142
int index
The index to scan from next.
Definition: hashtable.h:143
long find_count
The count of finds tried.
Definition: hashtable.h:131
void ** etable
Table of pointers to entries.
Definition: hashtable.h:136
The hashtable type.
Definition: hashtable.h:126
A generic open addressing hashtable.
int count
Number of entries in hashtable.
Definition: hashtable.h:128