forked from chaoran/fast-wait-free-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hzdptr.c
117 lines (93 loc) · 1.99 KB
/
hzdptr.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
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
#include <stdlib.h>
#include <string.h>
#include "hzdptr.h"
#include "xxhash.h"
#define HZDPTR_HTBL_SIZE(nprocs, nptrs) (4 * nprocs * nptrs)
typedef struct _node_t {
struct _node_t * next;
} node_t;
static int htable_insert(void ** tbl, size_t size, void * ptr)
{
int index = XXH32(ptr, 1, 0) % size;
int i;
for (i = index; i < size; ++i ) {
if (tbl[i] == NULL) {
tbl[i] = ptr;
return 0;
}
}
for (i = 0; i < index; ++i) {
if (tbl[i] == NULL) {
tbl[i] = ptr;
return 0;
}
}
return -1;
}
static int htable_lookup(void ** tbl, size_t size, void * ptr)
{
int index = XXH32(ptr, 1, 0) % size;
int i;
for (i = index; i < size; ++i) {
if (tbl[i] == ptr) {
return 1;
} else if (tbl[i] == NULL) {
return 0;
}
}
for (i = 0; i < index; ++i) {
if (tbl[i] == ptr) {
return 1;
} else if (tbl[i] == NULL) {
return 0;
}
}
return 0;
}
void hzdptr_init(hzdptr_t * hzd, int nprocs, int nptrs)
{
hzd->nprocs = nprocs;
hzd->nptrs = nptrs;
hzd->nretired = 0;
hzd->ptrs = calloc(hzdptr_size(nprocs, nptrs), 1);
_hzdptr_enlist(hzd);
}
void _hzdptr_retire(hzdptr_t * hzd, void ** rlist)
{
size_t size = HZDPTR_HTBL_SIZE(hzd->nprocs, hzd->nptrs);
void * plist[size];
memset(plist, 0, sizeof(plist));
hzdptr_t * me = hzd;
void * ptr;
while ((hzd = hzd->next) != me) {
int i;
for (i = 0; i < hzd->nptrs; ++i) {
ptr = hzd->ptrs[i];
if (ptr != NULL) {
htable_insert(plist, size, ptr);
}
}
}
int nretired = 0;
/** Check pointers in retire list with plist. */
int i;
for (i = 0; i < hzd->nretired; ++i) {
ptr = rlist[i];
if (htable_lookup(plist, size, ptr)) {
rlist[nretired++] = ptr;
} else {
free(ptr);
}
}
hzd->nretired = nretired;
}
void hzdptr_exit(hzdptr_t * hzd)
{
int i;
void ** rlist = &hzd->ptrs[hzd->nptrs];
for (i = 0; i < hzd->nretired; ++i) {
free(rlist[i]);
}
hzd->nretired = 0;
hzd->next = hzd;
}