2 * Written by Josh Dybnis and released to the public domain, as explained at
3 * http://creativecommons.org/licenses/publicdomain
5 * non thread safe skiplist
31 const datatype_t *key_type;
32 int high_water; // max level of any item in the list
35 static int random_levels (skiplist_t *sl) {
36 unsigned r = nbd_rand();
37 int z = __builtin_ctz(r);
38 int levels = (int)(z / 1.5);
41 if (levels > sl->high_water) {
42 levels = SYNC_ADD(&sl->high_water, 1);
43 TRACE("s2", "random_levels: increased high water mark to %lld", sl->high_water, 0);
45 if (levels > MAX_LEVELS) { levels = MAX_LEVELS; }
49 static node_t *node_alloc (int num_levels, map_key_t key, map_val_t val) {
50 assert(num_levels > 0 && num_levels <= MAX_LEVELS);
51 size_t sz = sizeof(node_t) + (num_levels - 1) * sizeof(node_t *);
52 node_t *item = (node_t *)nbd_malloc(sz);
56 item->num_levels = num_levels;
57 TRACE("s2", "node_alloc: new node %p (%llu levels)", item, num_levels);
61 skiplist_t *sl_alloc (const datatype_t *key_type) {
62 skiplist_t *sl = (skiplist_t *)nbd_malloc(sizeof(skiplist_t));
63 sl->key_type = key_type;
65 sl->head = node_alloc(MAX_LEVELS, 0, 0);
66 memset(sl->head->next, 0, MAX_LEVELS * sizeof(skiplist_t *));
70 void sl_free (skiplist_t *sl) {
71 node_t *item = sl->head->next[0];
73 node_t *next = item->next[0];
74 if (sl->key_type != NULL) {
75 nbd_free((void *)item->key);
82 size_t sl_count (skiplist_t *sl) {
84 node_t *item = sl->head->next[0];
92 static node_t *find_preds (node_t **preds, node_t **succs, int n, skiplist_t *sl, map_key_t key, int unlink) {
93 node_t *pred = sl->head;
95 TRACE("s2", "find_preds: searching for key %p in skiplist (head is %p)", key, pred);
98 // Traverse the levels of <sl> from the top level to the bottom
99 for (int level = sl->high_water - 1; level >= 0; --level) {
100 node_t *next = pred->next[level];
101 if (next == DOES_NOT_EXIST && level >= n)
103 TRACE("s3", "find_preds: traversing level %p starting at %p", level, pred);
105 while (item != NULL) {
106 next = item->next[level];
108 if (EXPECT_TRUE(sl->key_type == NULL)) {
111 d = sl->key_type->cmp((void *)item->key, (void *)key);
115 if (d == 0 && unlink) {
116 pred->next[level] = next;
117 TRACE("s3", "find_preds: unlinked item from pred %p", pred, 0);
119 next = (item != NULL) ? item->next[level] : DOES_NOT_EXIST;
128 TRACE("s3", "find_preds: found pred %p next %p", pred, item);
141 TRACE("s2", "find_preds: found matching item %p in skiplist, pred is %p", item, pred);
144 TRACE("s2", "find_preds: found proper place for key %p in skiplist, pred is %p. returning null", key, pred);
148 // Fast find that does not return the node's predecessors.
149 map_val_t sl_lookup (skiplist_t *sl, map_key_t key) {
150 TRACE("s1", "sl_lookup: searching for key %p in skiplist %p", key, sl);
151 node_t *item = find_preds(NULL, NULL, 0, sl, key, FALSE);
153 // If we found an <item> matching the <key> return its value.
155 map_val_t val = item->val;
159 TRACE("s1", "sl_lookup: no item in the skiplist matched the key", 0, 0);
160 return DOES_NOT_EXIST;
163 map_key_t sl_min_key (skiplist_t *sl) {
164 node_t *item = sl->head->next[0];
167 return DOES_NOT_EXIST;
170 map_val_t sl_cas (skiplist_t *sl, map_key_t key, map_val_t expectation, map_val_t new_val) {
171 TRACE("s1", "sl_cas: key %p skiplist %p", key, sl);
172 TRACE("s1", "sl_cas: expectation %p new value %p", expectation, new_val);
173 ASSERT((int64_t)new_val > 0);
175 node_t *preds[MAX_LEVELS];
176 node_t *nexts[MAX_LEVELS];
177 node_t *new_item = NULL;
178 int n = random_levels(sl);
179 node_t *old_item = find_preds(preds, nexts, n, sl, key, FALSE);
181 // If there is already an item in the skiplist that matches the key just update its value.
182 if (old_item != NULL) {
183 map_val_t old_val = old_item->val;
184 if (expectation == CAS_EXPECT_DOES_NOT_EXIST ||
185 (expectation != CAS_EXPECT_WHATEVER && expectation != CAS_EXPECT_EXISTS && expectation != old_val)) {
186 TRACE("s1", "sl_cas: the expectation was not met; the skiplist was not changed", 0, 0);
189 old_item->val = new_val;
193 if (EXPECT_FALSE(expectation != CAS_EXPECT_DOES_NOT_EXIST && expectation != CAS_EXPECT_WHATEVER)) {
194 TRACE("s1", "sl_cas: the expectation was not met, the skiplist was not changed", 0, 0);
195 return DOES_NOT_EXIST; // failure, the caller expected an item for the <key> to already exist
198 TRACE("s3", "sl_cas: inserting a new item between %p and %p", preds[0], nexts[0]);
200 // Create a new node and insert it into the skiplist.
201 map_key_t new_key = sl->key_type == NULL ? key : (map_key_t)sl->key_type->clone((void *)key);
202 new_item = node_alloc(n, new_key, new_val);
204 // Set <new_item>'s next pointers to their proper values
205 for (int level = 0; level < new_item->num_levels; ++level) {
206 new_item->next[level] = nexts[level];
209 // Link <new_item> into <sl>
210 for (int level = 0; level < new_item->num_levels; ++level) {
211 preds[level]->next[level] = new_item;
214 return DOES_NOT_EXIST; // success, inserted a new item
217 map_val_t sl_remove (skiplist_t *sl, map_key_t key) {
218 TRACE("s1", "sl_remove: removing item with key %p from skiplist %p", key, sl);
219 node_t *preds[MAX_LEVELS];
220 node_t *item = find_preds(preds, NULL, sl->high_water, sl, key, FALSE);
222 TRACE("s3", "sl_remove: remove failed, an item with a matching key does not exist in the skiplist", 0, 0);
223 return DOES_NOT_EXIST;
225 map_val_t val = item->val;
228 find_preds(NULL, NULL, 0, sl, key, TRUE);
231 if (sl->key_type != NULL) {
232 nbd_free((void *)item->key);
239 void sl_print (skiplist_t *sl) {
241 printf("high water: %d levels\n", sl->high_water);
242 for (int level = MAX_LEVELS - 1; level >= 0; --level) {
243 node_t *item = sl->head;
244 if (item->next[level] == DOES_NOT_EXIST)
246 printf("(%d) ", level);
249 node_t *next = item->next[level];
260 node_t *item = sl->head;
263 printf("%p:0x%llx ", item, (uint64_t)item->key);
264 if (item != sl->head) {
265 printf("[%d]", item->num_levels);
269 for (int level = 1; level < item->num_levels; ++level) {
270 node_t *next = item->next[level];
272 if (item == sl->head && item->next[level] == DOES_NOT_EXIST)
277 item = item->next[0];
285 sl_iter_t *sl_iter_begin (skiplist_t *sl, map_key_t key) {
286 sl_iter_t *iter = (sl_iter_t *)nbd_malloc(sizeof(sl_iter_t));
287 if (key != DOES_NOT_EXIST) {
288 find_preds(NULL, &iter->next, 1, sl, key, FALSE);
290 iter->next = sl->head->next[0];
295 map_val_t sl_iter_next (sl_iter_t *iter, map_key_t *key_ptr) {
297 node_t *item = iter->next;
300 return DOES_NOT_EXIST;
302 iter->next = item->next[0];
303 if (key_ptr != NULL) {
304 *key_ptr = item->key;
309 void sl_iter_free (sl_iter_t *iter) {