summaryrefslogtreecommitdiff
path: root/src/libpcp/src/hash.c
blob: 9bdf97fb25ac0e289f27de72a725211b87021239 (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
/*
 * Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
 * Copyright (c) 2013 Red Hat, Inc.
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 * 
 * This library 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 Lesser General Public
 * License for more details.
 */

#include "pmapi.h"
#include "impl.h"
#include <stddef.h>

void
__pmHashInit(__pmHashCtl *hcp)
{
    memset(hcp, 0, sizeof(*hcp));
}

__pmHashNode *
__pmHashSearch(unsigned int key, __pmHashCtl *hcp)
{
    __pmHashNode	*hp;

    if (hcp->hsize == 0)
	return NULL;

    for (hp = hcp->hash[key % hcp->hsize]; hp != NULL; hp = hp->next) {
	if (hp->key == key)
	    return hp;
    }
    return NULL;
}

int
__pmHashAdd(unsigned int key, void *data, __pmHashCtl *hcp)
{
    __pmHashNode    *hp;
    int		k;

    hcp->nodes++;

    if (hcp->hsize == 0) {
	hcp->hsize = 1;	/* arbitrary number */
	if ((hcp->hash = (__pmHashNode **)calloc(hcp->hsize, sizeof(__pmHashNode *))) == NULL) {
	    hcp->hsize = 0;
	    return -oserror();
	}
    }
    else if (hcp->nodes / 4 > hcp->hsize) {
	__pmHashNode	*tp;
	__pmHashNode	**old = hcp->hash;
	int		oldsize = hcp->hsize;

	hcp->hsize *= 2;
	if (hcp->hsize % 2) hcp->hsize++;
	if (hcp->hsize % 3) hcp->hsize += 2;
	if (hcp->hsize % 5) hcp->hsize += 2;
	if ((hcp->hash = (__pmHashNode **)calloc(hcp->hsize, sizeof(__pmHashNode *))) == NULL) {
	    hcp->hsize = oldsize;
	    hcp->hash = old;
	    return -oserror();
	}
	/*
	 * re-link chains
	 */
	while (oldsize) {
	    for (hp = old[--oldsize]; hp != NULL; ) {
		tp = hp;
		hp = hp->next;
		k = tp->key % hcp->hsize;
		tp->next = hcp->hash[k];
		hcp->hash[k] = tp;
	    }
	}
	free(old);
    }

    if ((hp = (__pmHashNode *)malloc(sizeof(__pmHashNode))) == NULL)
	return -oserror();

    k = key % hcp->hsize;
    hp->key = key;
    hp->data = data;
    hp->next = hcp->hash[k];
    hcp->hash[k] = hp;

    return 1;
}

int
__pmHashDel(unsigned int key, void *data, __pmHashCtl *hcp)
{
    __pmHashNode    *hp;
    __pmHashNode    *lhp = NULL;

    if (hcp->hsize == 0)
	return 0;

    for (hp = hcp->hash[key % hcp->hsize]; hp != NULL; hp = hp->next) {
	if (hp->key == key && hp->data == data) {
	    if (lhp == NULL)
		hcp->hash[key % hcp->hsize] = hp->next;
	    else
		lhp->next = hp->next;
	    free(hp);
	    return 1;
	}
	lhp = hp;
    }

    return 0;
}

void
__pmHashClear(__pmHashCtl *hcp)
{
    if (hcp->hsize != 0) {
	free(hcp->hash);
	hcp->hsize = 0;
    }
}

/*
 * Iterate over the entire hash table.  For each entry, call *cb,
 * passing *cdata and the current key/value pair.  The function's
 * return value decides how to continue or abort iteration.  The
 * callback function must not modify the hash table.
 */
void
__pmHashWalkCB(__pmHashWalkCallback cb, void *cdata, const __pmHashCtl *hcp)
{
    int n;

    for (n = 0; n < hcp->hsize; n++) {
        __pmHashNode *tp = hcp->hash[n];
        __pmHashNode **tpp = & hcp->hash[n];

        while (tp != NULL) {
            __pmHashWalkState state = (*cb)(tp, cdata);

            switch (state) {
            case PM_HASH_WALK_DELETE_STOP:
                *tpp = tp->next;  /* unlink */
                free(tp);         /* delete */
                return;           /* & stop */

            case PM_HASH_WALK_NEXT:
                tpp = &tp->next;
                tp = *tpp;
                break;

            case PM_HASH_WALK_DELETE_NEXT:
                *tpp = tp->next;  /* unlink */
                /* NB: do not change tpp.  It will still point at the previous
                 * node's "next" pointer.  Consider consecutive CONTINUE_DELETEs.
                 */
                free(tp);         /* delete */
                tp = *tpp; /* == tp->next, except that tp is already freed. */
                break;            /* & next */

            case PM_HASH_WALK_STOP:
            default:
                return;
            }
        }
    }
}

/*
 * Walk a hash table; state flow is START ... NEXT ... NEXT ...
 */
__pmHashNode *
__pmHashWalk(__pmHashCtl *hcp, __pmHashWalkState state)
{
    __pmHashNode	*node;

    if (hcp->hsize == 0)
	return NULL;

    if (state == PM_HASH_WALK_START) {
        hcp->index = 0;
        hcp->next = hcp->hash[0];
    }

    while (hcp->next == NULL) {
        hcp->index++;
        if (hcp->index >= hcp->hsize)
            return NULL;
        hcp->next = hcp->hash[hcp->index];
    }

    node = hcp->next;
    hcp->next = node->next;
    return node;
}