xref: /inferno-os/liblogfs/ust.c (revision 46439007cf417cbd9ac8049bb4122c890097a0fa)
1 #include "lib9.h"
2 #include "logfs.h"
3 #include "local.h"
4 
5 enum {
6 	USTMOD = 127
7 };
8 
9 typedef struct UstNode {
10 	char s[1];
11 } UstNode;
12 
13 struct Ust {
14 	UstNode *head[USTMOD];
15 };
16 
17 int
18 logfshashstring(void *s, int n)
19 {
20 	int h = 0;
21 	char *p;
22 	for(p = s; *p; p++) {
23 		ulong g;
24 		h = (h << 4) + *p;
25 		g = h & 0xf0000000;
26 		if(g != 0)
27 			h ^= ((g >> 24) & 0xff) | g;
28 	}
29 	return (h & ~(1 << 31)) % n;
30 }
31 
32 static int
33 compare(char *entry, char *key)
34 {
35 	return strcmp(entry, key) == 0;
36 }
37 
38 static int
39 allocsize(void *key)
40 {
41 	return strlen(key) + 1;
42 }
43 
44 char *
45 logfsustnew(Ust **ustp)
46 {
47 	return logfsmapnew(USTMOD, logfshashstring, (int (*)(void *, void *))compare, allocsize, nil, ustp);
48 }
49 
50 char *
51 logfsustadd(Ust *ust, char *s)
52 {
53 	char *errmsg;
54 	char *ep;
55 	ep = logfsmapfindentry(ust, s);
56 	if(ep) {
57 //		print("ust: found %s\n", s);
58 		return ep;
59 	}
60 	errmsg = logfsmapnewentry(ust, s, &ep);
61 	if(errmsg)
62 		return errmsg;
63 //	print("ust: new %s\n", s);
64 	return strcpy(ep, s);
65 }
66