summaryrefslogtreecommitdiff
path: root/lib/libalpm/pkghash.c
blob: e5626e80371a8d01a02e71f7d517c433592b0324 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 *  pkghash.c
 *
 *  Copyright (c) 2011 Pacman Development Team <pacman-dev@archlinux.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>

#include "pkghash.h"
#include "util.h"

/* List of primes for possible sizes of hash tables.
 *
 * The maximum table size is the last prime under 1,000,000.  That is
 * more than an order of magnitude greater than the number of packages
 * in any Linux distribution, and well under UINT_MAX.
 */
static const unsigned int prime_list[] =
{
  11u13u17u19u23u29u31u37u41u43u47u,
  53u59u61u67u71u73u79u83u89u97u103u,
  109u113u127u137u139u149u157u167u179u193u,
  199u211u227u241u257u277u293u313u337u359u,
  383u409u439u467u503u541u577u619u661u709u,
  761u823u887u953u1031u1109u1193u1289u1381u,
  1493u1613u1741u1879u2029u2179u2357u2549u,
  2753u2971u3209u3469u3739u4027u4349u4703u,
  5087u5503u5953u6427u6949u7517u8123u8783u,
  9497u10273u11113u12011u12983u14033u15173u,
  16411u17749u19183u20753u22447u24281u26267u,
  28411u30727u33223u35933u38873u42043u45481u,
  49201u53201u57557u62233u67307u72817u78779u,
  85229u92203u99733u107897u116731u126271u136607u,
  147793u159871u172933u187091u202409u218971u236897u,
  256279u277261u299951u324503u351061u379787u410857u,
  444487u480881u520241u562841u608903u658753u712697u,
  771049u834181u902483u976369u
};

/* How far forward do we look when linear probing for a spot? */
static const unsigned int stride = 1;
/* What is the maximum load percentage of our hash table? */
static const double max_hash_load = 0.68;
/* Initial load percentage given a certain size */
static const double initial_hash_load = 0.58;

/* Allocate a hash table with space for at least "size" elements */
alpm_pkghash_t *_alpm_pkghash_create(unsigned int size)
{
  alpm_pkghash_t *hash = NULL;
  unsigned int i, loopsize;

  CALLOC(hash, 1sizeof(alpm_pkghash_t), return NULL);
  size = size / initial_hash_load + 1;

  loopsize = sizeof(prime_list) / sizeof(*prime_list);
  for(i = 0; i < loopsize; i++) {
    if(prime_list[i] > size) {
      hash->buckets = prime_list[i];
      hash->limit = hash->buckets * max_hash_load;
      break;
    }
  }

  if(hash->buckets < size) {
    errno = ERANGE;
    free(hash);
    return NULL;
  }

  CALLOC(hash->hash_table, hash->buckets, sizeof(alpm_list_t *), \
        free(hash); return NULL);

  return hash;
}

static unsigned int get_hash_position(unsigned long name_hash,
    alpm_pkghash_t *hash)
{
  unsigned int position;

  position = name_hash % hash->buckets;

  /* collision resolution using open addressing with linear probing */
  while(hash->hash_table[position] != NULL) {
    position += stride;
    while(position >= hash->buckets) {
      position -= hash->buckets;
    }
  }

  return position;
}

/* Expand the hash table size to the next increment and rebin the entries */
static alpm_pkghash_t *rehash(alpm_pkghash_t *oldhash)
{
  alpm_pkghash_t *newhash;
  unsigned int newsize, i;

  /* Hash tables will need resized in two cases:
   *  - adding packages to the local database
   *  - poor estimation of the number of packages in sync database
   *
   * For small hash tables sizes (<500) the increase in size is by a
   * minimum of a factor of 2 for optimal rehash efficiency.  For
   * larger database sizes, this increase is reduced to avoid excess
   * memory allocation as both scenarios requiring a rehash should not
   * require a table size increase that large. */
  if(oldhash->buckets < 500) {
    newsize = oldhash->buckets * 2;
  } else if(oldhash->buckets < 2000) {
    newsize = oldhash->buckets * 3 / 2;
  } else if(oldhash->buckets < 5000) {
    newsize = oldhash->buckets * 4 / 3;
  } else {
    newsize = oldhash->buckets + 1;
  }

  newhash = _alpm_pkghash_create(newsize);
  if(newhash == NULL) {
    /* creation of newhash failed, stick with old one... */
    return oldhash;
  }

  newhash->list = oldhash->list;
  oldhash->list = NULL;

  for(i = 0; i < oldhash->buckets; i++) {
    if(oldhash->hash_table[i] != NULL) {
      alpm_pkg_t *package = oldhash->hash_table[i]->data;
      unsigned int position = get_hash_position(package->name_hash, newhash);

      newhash->hash_table[position] = oldhash->hash_table[i];
      oldhash->hash_table[i] = NULL;
    }
  }

  newhash->entries = oldhash->entries;

  _alpm_pkghash_free(oldhash);

  return newhash;
}

static alpm_pkghash_t *pkghash_add_pkg(alpm_pkghash_t *hash, alpm_pkg_t *pkg,
    int sorted)
{
  alpm_list_t *ptr;
  unsigned int position;

  if(pkg == NULL || hash == NULL) {
    return hash;
  }

  if(hash->entries >= hash->limit) {
    hash = rehash(hash);
  }

  position = get_hash_position(pkg->name_hash, hash);

  ptr = malloc(sizeof(alpm_list_t));
  if(ptr == NULL) {
    return hash;
  }

  ptr->data = pkg;
  ptr->prev = ptr;
  ptr->next = NULL;

  hash->hash_table[position] = ptr;
  if(!sorted) {
    hash->list = alpm_list_join(hash->list, ptr);
  } else {
    hash->list = alpm_list_mmerge(hash->list, ptr, _alpm_pkg_cmp);
  }

  hash->entries += 1;
  return hash;
}

alpm_pkghash_t *_alpm_pkghash_add(alpm_pkghash_t *hash, alpm_pkg_t *pkg)
{
  return pkghash_add_pkg(hash, pkg, 0);
}

alpm_pkghash_t *_alpm_pkghash_add_sorted(alpm_pkghash_t *hash, alpm_pkg_t *pkg)
{
  return pkghash_add_pkg(hash, pkg, 1);
}

static unsigned int move_one_entry(alpm_pkghash_t *hash,
    unsigned int start, unsigned int end)
{
  /* Iterate backwards from 'end' to 'start', seeing if any of the items
   * would hash to 'start'. If we find one, we move it there and break.  If
   * we get all the way back to position and find none that hash to it, we
   * also end iteration. Iterating backwards helps prevent needless shuffles;
   * we will never need to move more than one item per function call.  The
   * return value is our current iteration location; if this is equal to
   * 'start' we can stop this madness. */
  while(end != start) {
    alpm_list_t *i = hash->hash_table[end];
    alpm_pkg_t *info = i->data;
    unsigned int new_position = get_hash_position(info->name_hash, hash);

    if(new_position == start) {
      hash->hash_table[start] = i;
      hash->hash_table[end] = NULL;
      break;
    }

    /* the odd math ensures we are always positive, e.g.
     * e.g. (0 - 1) % 47      == -1
     * e.g. (47 + 0 - 1) % 47 == 46 */
    end = (hash->buckets + end - stride) % hash->buckets;
  }
  return end;
}

/**
 * @brief Remove a package from a pkghash.
 *
 * @param hash     the hash to remove the package from
 * @param pkg      the package we are removing
 * @param data     output parameter containing the removed item
 *
 * @return the resultant hash
 */
alpm_pkghash_t *_alpm_pkghash_remove(alpm_pkghash_t *hash, alpm_pkg_t *pkg,
    alpm_pkg_t **data)
{
  alpm_list_t *i;
  unsigned int position;

  if(data) {
    *data = NULL;
  }

  if(pkg == NULL || hash == NULL) {
    return hash;
  }

  position = pkg->name_hash % hash->buckets;
  while((i = hash->hash_table[position]) != NULL) {
    alpm_pkg_t *info = i->data;

    if(info->name_hash == pkg->name_hash &&
          strcmp(info->name, pkg->name) == 0) {
      unsigned int stop, prev;

      /* remove from list and hash */
      hash->list = alpm_list_remove_item(hash->list, i);
      if(data) {
        *data = info;
      }
      hash->hash_table[position] = NULL;
      free(i);
      hash->entries -= 1;

      /* Potentially move entries following removed entry to keep open
       * addressing collision resolution working. We start by finding the
       * next null bucket to know how far we have to look. */
      stop = position + stride;
      while(stop >= hash->buckets) {
        stop -= hash->buckets;
      }
      while(hash->hash_table[stop] != NULL && stop != position) {
        stop += stride;
        while(stop >= hash->buckets) {
          stop -= hash->buckets;
        }
      }
      stop = (hash->buckets + stop - stride) % hash->buckets;

      /* We now search backwards from stop to position. If we find an
       * item that now hashes to position, we will move it, and then try
       * to plug the new hole we just opened up, until we finally don't
       * move anything. */
      while((prev = move_one_entry(hash, position, stop)) != position) {
        position = prev;
      }

      return hash;
    }

    position += stride;
    while(position >= hash->buckets) {
      position -= hash->buckets;
    }
  }

  return hash;
}

void _alpm_pkghash_free(alpm_pkghash_t *hash)
{
  if(hash != NULL) {
    unsigned int i;
    for(i = 0; i < hash->buckets; i++) {
      free(hash->hash_table[i]);
    }
    free(hash->hash_table);
  }
  free(hash);
}

alpm_pkg_t *_alpm_pkghash_find(alpm_pkghash_t *hash, const char *name)
{
  alpm_list_t *lp;
  unsigned long name_hash;
  unsigned int position;

  if(name == NULL || hash == NULL) {
    return NULL;
  }

  name_hash = _alpm_hash_sdbm(name);

  position = name_hash % hash->buckets;

  while((lp = hash->hash_table[position]) != NULL) {
    alpm_pkg_t *info = lp->data;

    if(info->name_hash == name_hash && strcmp(info->name, name) == 0) {
      return info;
    }

    position += stride;
    while(position >= hash->buckets) {
      position -= hash->buckets;
    }
  }

  return NULL;
}

/* vim: set ts=2 sw=2 noet: */