File: Hash.cpp

package info (click to toggle)
xmail 1.25-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,592 kB
  • ctags: 16,393
  • sloc: cpp: 33,249; ansic: 19,434; sh: 476; perl: 200; makefile: 107; sql: 31
file content (228 lines) | stat: -rw-r--r-- 5,987 bytes parent folder | download
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
/*
 *  XMail by Davide Libenzi ( Intranet and Internet mail server )
 *  Copyright (C) 1999,..,2004  Davide Libenzi
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Davide Libenzi <davidel@xmailserver.org>
 *
 */

#include "SysInclude.h"
#include "SysDep.h"
#include "SvrDefines.h"
#include "ShBlocks.h"
#include "StrUtils.h"
#include "BuffSock.h"
#include "MiscUtils.h"
#include "Hash.h"



#define HMASK_TOP_BIT (1UL << (sizeof(long) * 8 - 1))


struct Hash {
	unsigned long ulCount;
	unsigned long ulHMask;
	SysListHead *pBkts;
	SysListHead NodeList;
};



static int HashGrow(Hash *pHash);



HASH_HANDLE HashCreate(unsigned long ulSize) {
	unsigned long i, ulHMask;
	Hash *pHash;

	if ((pHash = (Hash *) SysAlloc(sizeof(Hash))) == NULL)
		return INVALID_HASH_HANDLE;
	for (ulHMask = 2; !(ulHMask & HMASK_TOP_BIT) && ulHMask <= ulSize; ulHMask <<= 1);
	ulHMask--;
	pHash->ulCount = 0;
	pHash->ulHMask = ulHMask;
	if ((pHash->pBkts = (SysListHead *)
	     SysAlloc((ulHMask + 1) * sizeof(SysListHead))) == NULL) {
		SysFree(pHash);
		return INVALID_HASH_HANDLE;
	}
	for (i = 0; i <= ulHMask; i++)
		SYS_INIT_LIST_HEAD(&pHash->pBkts[i]);
	SYS_INIT_LIST_HEAD(&pHash->NodeList);

	return (HASH_HANDLE) pHash;
}

void HashFree(HASH_HANDLE hHash, void (*pfFree)(void *, HashNode *),
	      void *pPrivate) {
	Hash *pHash;
	SysListHead *pPos;
	HashNode *pHNode;

	/*
	 * Having *Free() functions to accept NULL handles is useful in
	 * order to streamline the cleanup path of functions using them.
	 */
	if (hHash == INVALID_HASH_HANDLE)
		return;
	pHash = (Hash *) hHash;
	if (pfFree != NULL) {
		while ((pPos = SYS_LIST_FIRST(&pHash->NodeList)) != NULL) {
			pHNode = SYS_LIST_ENTRY(pPos, HashNode, LLnk);
			SYS_LIST_DEL(&pHNode->LLnk);
			SYS_LIST_DEL(&pHNode->HLnk);
			(*pfFree)(pPrivate, pHNode);
		}
	}
	SysFree(pHash->pBkts);
	SysFree(pHash);
}

void HashInitNode(HashNode *pHNode) {

	ZeroData(*pHNode);
	SYS_INIT_LIST_HEAD(&pHNode->LLnk);
	SYS_INIT_LIST_HEAD(&pHNode->HLnk);
}

static int HashGrow(Hash *pHash) {
	unsigned long i, ulHIdx, ulHMask;
	SysListHead *pBkts, *pPos;
	HashNode *pHNode;

	ulHMask = pHash->ulHMask + 1;
	if (ulHMask & HMASK_TOP_BIT)
		return 0;
	ulHMask = (ulHMask << 1) - 1;
	if ((pBkts = (SysListHead *)
	     SysAlloc((ulHMask + 1) * sizeof(SysListHead))) == NULL)
		return ErrGetErrorCode();
	for (i = 0; i <= ulHMask; i++)
		SYS_INIT_LIST_HEAD(&pBkts[i]);
	for (pPos = SYS_LIST_FIRST(&pHash->NodeList); pPos != NULL;
	     pPos = SYS_LIST_NEXT(pPos, &pHash->NodeList)) {
		pHNode = SYS_LIST_ENTRY(pPos, HashNode, LLnk);
		SYS_LIST_DEL(&pHNode->HLnk);
		ulHIdx = MscHashString(pHNode->Key.pData, pHNode->Key.lSize) & ulHMask;
		SYS_LIST_ADDT(&pHNode->HLnk, &pBkts[ulHIdx]);
	}
	SysFree(pHash->pBkts);
	pHash->pBkts = pBkts;
	pHash->ulHMask = ulHMask;

	return 0;
}

int HashAdd(HASH_HANDLE hHash, HashNode *pHNode) {
	Hash *pHash = (Hash *) hHash;
	unsigned long ulHIdx;
	SysListHead *pHead;

	if (pHash->ulCount >= pHash->ulHMask && HashGrow(pHash) < 0)
		return ErrGetErrorCode();
	ulHIdx = MscHashString(pHNode->Key.pData, pHNode->Key.lSize) & pHash->ulHMask;
	pHead = &pHash->pBkts[ulHIdx];
	SYS_LIST_ADDT(&pHNode->HLnk, pHead);
	SYS_LIST_ADDT(&pHNode->LLnk, &pHash->NodeList);
	pHash->ulCount++;

	return 0;
}

void HashDel(HASH_HANDLE hHash, HashNode *pHNode) {
	Hash *pHash = (Hash *) hHash;

	SYS_LIST_DEL(&pHNode->HLnk);
	SYS_LIST_DEL(&pHNode->LLnk);
	pHash->ulCount--;
}

int HashGetFirst(HASH_HANDLE hHash, Datum const *Key,
		 HashEnum *pHEnum, HashNode **ppHNode) {
	Hash *pHash = (Hash *) hHash;
	unsigned long ulHIdx;
	SysListHead *pHead, *pPos;
	HashNode *pHNode;

	ulHIdx = MscHashString(Key->pData, Key->lSize) & pHash->ulHMask;
	pHead = &pHash->pBkts[ulHIdx];
	for (pPos = SYS_LIST_FIRST(pHead); pPos != NULL;
	     pPos = SYS_LIST_NEXT(pPos, pHead)) {
		pHNode = SYS_LIST_ENTRY(pPos, HashNode, HLnk);
		if (EquivDatum(Key, &pHNode->Key)) {
			*ppHNode = pHNode;
			pHEnum->ulHIdx = ulHIdx;
			pHEnum->pNext = SYS_LIST_NEXT(pPos, pHead);
			return 0;
		}
	}

	ErrSetErrorCode(ERR_NOT_FOUND);
	return ERR_NOT_FOUND;
}

int HashGetNext(HASH_HANDLE hHash, Datum const *Key,
		HashEnum *pHEnum, HashNode **ppHNode) {
	Hash *pHash = (Hash *) hHash;
	SysListHead *pPos, *pHead;
	HashNode *pHNode;

	pHead = &pHash->pBkts[pHEnum->ulHIdx];
	for (pPos = pHEnum->pNext; pPos != NULL;
	     pPos = SYS_LIST_NEXT(pPos, pHead)) {
		pHNode = SYS_LIST_ENTRY(pPos, HashNode, HLnk);
		if (EquivDatum(Key, &pHNode->Key)) {
			*ppHNode = pHNode;
			pHEnum->pNext = SYS_LIST_NEXT(pPos, pHead);
			return 0;
		}
	}

	ErrSetErrorCode(ERR_NOT_FOUND);
	return ERR_NOT_FOUND;
}

int HashFirst(HASH_HANDLE hHash, SysListHead **ppPos, HashNode **ppHNode) {
	Hash *pHash = (Hash *) hHash;
	SysListHead *pPos;

	if ((pPos = SYS_LIST_FIRST(&pHash->NodeList)) == NULL) {
		ErrSetErrorCode(ERR_NOT_FOUND);
		return ERR_NOT_FOUND;
	}
	*ppHNode = SYS_LIST_ENTRY(pPos, HashNode, LLnk);
	*ppPos = SYS_LIST_NEXT(pPos, &pHash->NodeList);

	return 0;
}

int HashNext(HASH_HANDLE hHash, SysListHead **ppPos, HashNode **ppHNode) {
	Hash *pHash = (Hash *) hHash;
	SysListHead *pPos = *ppPos;

	if (pPos == NULL) {
		ErrSetErrorCode(ERR_NOT_FOUND);
		return ERR_NOT_FOUND;
	}
	*ppHNode = SYS_LIST_ENTRY(pPos, HashNode, LLnk);
	*ppPos = SYS_LIST_NEXT(pPos, &pHash->NodeList);

	return 0;
}