summaryrefslogtreecommitdiff
path: root/liblogfs/ust.c
blob: 119ed33aac3beca37f4fb5fbda22a698b83a91f9 (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
#include "lib9.h"
#include "logfs.h"
#include "local.h"

enum {
	USTMOD = 127
};

typedef struct UstNode {
	char s[1];
} UstNode;

struct Ust {
	UstNode *head[USTMOD];
};

int
logfshashstring(void *s, int n)
{
	int h = 0;
	char *p;
	for(p = s; *p; p++) {
		ulong g;
		h = (h << 4) + *p;
		g = h & 0xf0000000;
		if(g != 0)
			h ^= ((g >> 24) & 0xff) | g;
	}
	return (h & ~(1 << 31)) % n;
}

static int
compare(char *entry, char *key)
{
	return strcmp(entry, key) == 0;
}

static int
allocsize(void *key)
{
	return strlen(key) + 1;
}

char *
logfsustnew(Ust **ustp)
{
	return logfsmapnew(USTMOD, logfshashstring, (int (*)(void *, void *))compare, allocsize, nil, ustp);
}

char *
logfsustadd(Ust *ust, char *s)
{
	char *errmsg;
	char *ep;
	ep = logfsmapfindentry(ust, s);
	if(ep) {
//		print("ust: found %s\n", s);
		return ep;
	}
	errmsg = logfsmapnewentry(ust, s, &ep);
	if(errmsg)
		return errmsg;
//	print("ust: new %s\n", s);
	return strcpy(ep, s);
}