File: hash.c

package info (click to toggle)
s390-tools 2.40.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 13,288 kB
  • sloc: ansic: 187,079; sh: 12,157; cpp: 5,049; makefile: 2,812; perl: 2,541; asm: 1,097; python: 697; xml: 29
file content (128 lines) | stat: -rw-r--r-- 3,151 bytes parent folder | download | duplicates (6)
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
/*
 * zdev - Modify and display the persistent configuration of devices
 *
 * Copyright IBM Corp. 2016, 2017
 *
 * s390-tools is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include <string.h>

#include "hash.h"
#include "misc.h"

/* Initialize hash. */
void _hash_init(struct hash *hash, int buckets, hash_id_fn_t get_id,
		hash_cmp_fn_t cmp_id, hash_fn_t get_hash, unsigned long offset)
{
	int i;

	memset(hash, 0, sizeof(struct hash));
	util_list_init_offset(&hash->list, offset);
	hash->buckets = buckets;
	hash->get_id = get_id;
	hash->cmp_id = cmp_id;
	hash->get_hash = get_hash;
	hash->hash = misc_malloc(sizeof(struct util_list) * buckets);
	for (i = 0; i < buckets; i++)
		hash->hash[i] = ptrlist_new();
}

/* Return a newly allocated hash. */
struct hash *_hash_new(int buckets, hash_id_fn_t get_id, hash_cmp_fn_t cmp_id,
		       hash_fn_t get_hash, unsigned long offset)
{
	struct hash *hash;

	hash = misc_malloc(sizeof(struct hash));
	_hash_init(hash, buckets, get_id, cmp_id, get_hash, offset);

	return hash;
}

/* Release all resources associated with @hash, excluding @hash. If @free_fn
 * is specified, this function is called to release all resources associated
 * with each entry. */
void hash_clear(struct hash *hash, void (*free_fn)(void *))
{
	void *c, *n;
	int i;

	util_list_iterate_safe(&hash->list, c, n) {
		util_list_remove(&hash->list, c);
		if (free_fn)
			free_fn(c);
	}

	for (i = 0; i < hash->buckets; i++)
		ptrlist_free(hash->hash[i], 0);
	free(hash->hash);
}

/* Release all resources associated with @hash. If @free_fn is specified,
 * this function is called to release all resources associated with each
 * entry. */
void hash_free(struct hash *hash, void (*free_fn)(void *))
{
	hash_clear(hash, free_fn);
	free(hash);
}

/* Add a new entry to the hash. */
void hash_add(struct hash *hash, void *entry)
{
	int bucket;

	util_list_add_tail(&hash->list, entry);
	if (hash->buckets > 0) {
		bucket = hash->get_hash(hash->get_id(entry));
		ptrlist_add(hash->hash[bucket], entry);
	}
}

/* Remove an entry from the hash. */
void hash_remove(struct hash *hash, void *entry)
{
	int bucket;

	util_list_remove(&hash->list, entry);
	if (hash->buckets > 0) {
		bucket = hash->get_hash(hash->get_id(entry));
		ptrlist_remove(hash->hash[bucket], entry);
	}
}

void hash_print(struct hash *hash, int ind)
{
	int i;

	indent(ind, "hash at %p\n", hash);
	ind += 2;
	indent(ind, "buckets=%d\n", hash->buckets);
	indent(ind, "get_id=%p\n", hash->get_id);
	indent(ind, "cmp_id=%p\n", hash->cmp_id);
	indent(ind, "get_hash=%p\n", hash->get_hash);

	for (i = 0; i < hash->buckets; i++) {
		indent(ind, "bucket[%d]: len=%d\n", i,
		       util_list_len(hash->hash[i]));
	}
}

/* Find entry by ID. @cmp_fn compares two IDs and returns 0 when IDs match. */
void *hash_find_by_id(struct hash *hash, const void *id)
{
	int bucket;
	struct ptrlist_node *p;

	if (hash->buckets > 0) {
		bucket = hash->get_hash(id);
		util_list_iterate(hash->hash[bucket], p) {
			if (hash->cmp_id(hash->get_id(p->ptr), id) == 0)
				return p->ptr;
		}
	}

	return NULL;
}