-
Notifications
You must be signed in to change notification settings - Fork 42
/
skiplist.c
196 lines (171 loc) · 3.81 KB
/
skiplist.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
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
/*
* File: skiplist.c
* Author: Vincent Gramoli <[email protected]>,
* Vasileios Trigonakis <[email protected]>
* Description:
* skiplist.c is part of ASCYLIB
*
* Copyright (c) 2014 Vasileios Trigonakis <[email protected]>,
* Tudor David <[email protected]>
* Distributed Programming Lab (LPD), EPFL
*
* ASCYLIB 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, version 2
* of the License.
*
* 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.
*
*/
#include "skiplist.h"
unsigned int levelmax;
unsigned int size_pad_32;
__thread ssmem_allocator_t* alloc;
inline int
get_rand_level()
{
int i, level = 1;
for (i = 0; i < levelmax - 1; i++)
{
if ((rand_range(101)) < 50)
level++;
else
break;
}
/* 1 <= level <= levelmax */
return level;
}
int
floor_log_2(unsigned int n)
{
int pos = 0;
if (n >= 1<<16) { n >>= 16; pos += 16; }
if (n >= 1<< 8) { n >>= 8; pos += 8; }
if (n >= 1<< 4) { n >>= 4; pos += 4; }
if (n >= 1<< 2) { n >>= 2; pos += 2; }
if (n >= 1<< 1) { pos += 1; }
return ((n == 0) ? (-1) : pos);
}
/*
* Create a new node without setting its next fields.
*/
sl_node_t*
sl_new_simple_node(skey_t key, sval_t val, int toplevel, int initializing)
{
sl_node_t *node;
#if GC == 1
if (unlikely(initializing))
{
/* use levelmax instead of toplevel in order to be able to use the ssalloc allocator*/
size_t ns = size_pad_32;
size_t ns_rm = ns & 63;
if (ns_rm)
{
ns += 64 - ns_rm;
}
node = (sl_node_t*) ssalloc(ns);
}
else
{
size_t ns = size_pad_32;
# if defined(DO_PAD)
size_t ns_rm = ns & 63;
if (ns_rm)
{
ns += 64 - ns_rm;
}
# endif
node = (sl_node_t*) ssmem_alloc(alloc, ns);
}
#else
size_t ns = size_pad_32;
node = (sl_node_t *)ssalloc(ns);
#endif
if (node == NULL)
{
perror("malloc");
exit(1);
}
node->key = key;
node->val = val;
node->toplevel = toplevel;
#if defined(__tile__)
MEM_BARRIER;
#endif
return node;
}
/*
* Create a new node with its next field.
* If next=NULL, then this create a tail node.
*/
sl_node_t*
sl_new_node(skey_t key, sval_t val, sl_node_t *next, int toplevel, int transactional)
{
volatile sl_node_t *node;
int i;
node = sl_new_simple_node(key, val, toplevel, transactional);
for (i = 0; i < levelmax; i++)
{
node->next[i] = next;
}
MEM_BARRIER;
return (sl_node_t*) node;
}
void
sl_delete_node(sl_node_t *n)
{
#if GC == 1
# if SEQ_SSMEM_NO_FREE != 1
# warning ssmem_free() is never called in sl_delete_node
/* ssmem_free(alloc, (void*) n); *\/ */
# endif
#else
#endif
}
sl_intset_t*
sl_set_new()
{
sl_intset_t *set;
sl_node_t *min, *max;
if ((set = (sl_intset_t *)ssalloc_aligned(CACHE_LINE_SIZE, sizeof(sl_intset_t))) == NULL)
{
perror("malloc");
exit(1);
}
max = sl_new_node(KEY_MAX, 0, NULL, levelmax, 1);
min = sl_new_node(KEY_MIN, 0, max, levelmax, 1);
set->head = min;
return set;
}
void
sl_set_delete(sl_intset_t *set)
{
sl_node_t *node, *next;
node = set->head;
while (node != NULL)
{
next = node->next[0];
sl_delete_node(node);
node = next;
}
ssfree(set);
}
int
sl_set_size(sl_intset_t *set)
{
int size = 0;
sl_node_t *node;
node = GET_UNMARKED(set->head->next[0]);
while (node->next[0] != NULL)
{
if (!IS_MARKED(node->next[0]))
{
size++;
}
node = GET_UNMARKED(node->next[0]);
}
return size;
}