File: hash.c

package info (click to toggle)
onak 0.4.5-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,744 kB
  • ctags: 777
  • sloc: ansic: 11,570; perl: 268; sh: 268; makefile: 169; sql: 21
file content (139 lines) | stat: -rw-r--r-- 3,141 bytes parent folder | download | duplicates (2)
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
/*
 * hash.c - hashing routines mainly used for caching key details.
 *
 * Copyright 2000-2002 Jonathan McDowell <noodles@earth.li>
 *
 * 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; version 2 of the License.
 *
 * 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, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "hash.h"
#include "keystructs.h"
#include "ll.h"
#include "mem.h"
#include "stats.h"

/**
 *	hashtable - the hash table array.
 */
static struct ll *hashtable[HASHSIZE];

/**
 *	elements - the number of elements in the hash table.
 */
static unsigned long elements;

/**
 *	inithash - Initialize the hash ready for use.
 */
void inithash(void)
{
	unsigned int i;

	for (i = 0; i < HASHSIZE; i++) {
		hashtable[i] = NULL;
	}
	elements = 0;
}

/**
 *	destroyhash - Clean up the hash after use.
 *
 *	This function destroys the hash after use, freeing any memory that was
 *	used during its lifetime.
 */
void destroyhash(void)
{
	int i;
	struct ll *curll = NULL;

	for (i = 0; i < HASHSIZE; i++) {
		curll = hashtable[i];
		/*
		 * TODO: The problem is the object has pointers that
		 * need freed too.
		 */
		llfree(curll, (void (*)(void *)) free_statskey);
		hashtable[i] = NULL;
	}
	elements = 0;
}

/**
 *	addtohash - Adds a key to the hash.
 *	@key: The key to add.
 *
 *	Takes a key and stores it in the hash.
 */
void addtohash(struct stats_key *key)
{
	++elements;
	hashtable[key->keyid & HASHMASK]=
		lladd(hashtable[key->keyid & HASHMASK], key);
}

/**
 *	createandaddtohash - Creates a key and adds it to the hash.
 *	@keyid: The key to create and add.
 *
 *	Takes a key, checks if it exists in the hash and if not creates it
 *	and adds it to the hash. Returns the key from the hash whether it
 *	already existed or we just created it.
 */
struct stats_key *createandaddtohash(uint64_t keyid)
{
	struct stats_key *tmpkey;

	/*
	 * Check if the key already exists and if not create and add it.
	 */
	tmpkey = findinhash(keyid);
	if (tmpkey == NULL) {
		tmpkey = malloc(sizeof(*tmpkey));
		memset(tmpkey, 0, sizeof(*tmpkey));
		tmpkey->keyid = keyid;
		addtohash(tmpkey);
	}
	return tmpkey;
}

int stats_key_cmp(struct stats_key *key, uint64_t *keyid)
{
	return !(key != NULL && key->keyid == *keyid);
}

struct stats_key *findinhash(uint64_t keyid)
{
	int (*p)();
	struct ll *found;

	p = stats_key_cmp;
	if ((found = llfind(hashtable[keyid & HASHMASK], &keyid, p))==NULL) {
		return NULL;
	}
	return found->object;
}

unsigned long hashelements(void)
{
	return elements;
}

struct ll *gethashtableentry(unsigned int entry)
{
	return hashtable[entry];
}