-
Notifications
You must be signed in to change notification settings - Fork 10
/
sm_hash.c
40 lines (34 loc) · 705 Bytes
/
sm_hash.c
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
/*
* This file is a part of SMalloc.
* SMalloc is MIT licensed.
* Copyright (c) 2017 Andrey Rys.
*/
#include "smalloc_i.h"
/* An adopted Jenkins one-at-a-time hash */
#define UIHOP(x, s) do { \
hash += (x >> s) & 0xff;\
hash += hash << 10; \
hash ^= hash >> 6; \
} while (0)
uintptr_t smalloc_uinthash(uintptr_t x)
{
uintptr_t hash = 0;
UIHOP(x, 0);
UIHOP(x, 8);
UIHOP(x, 16);
UIHOP(x, 24);
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}
#undef UIHOP
uintptr_t smalloc_mktag(struct smalloc_hdr *shdr)
{
uintptr_t r = smalloc_uinthash(PTR_UINT(shdr));
r += shdr->rsz;
r = smalloc_uinthash(r);
r += shdr->usz;
r = smalloc_uinthash(r);
return r;
}