snapshot v4/p9; restructuring

This commit is contained in:
Gregory Burd 2024-03-29 22:43:46 -04:00
parent 440ade6b82
commit 62eafa3cd6
3 changed files with 1600 additions and 1524 deletions

View file

@ -3,7 +3,7 @@ OBJS = skiplist.o
STATIC_LIB = libskiplist.a
SHARED_LIB = libskiplist.so
CFLAGS = -DDEBUG -DSKIPLIST_DIAGNOSTIC -Wall -Wextra -Wpedantic -Og -g -std=c99 -Iinclude/ -fPIC
CFLAGS = -Wall -Wextra -Wpedantic -Og -g -std=c99 -Iinclude/ -fPIC
TEST_FLAGS = -Itests/
#-fsanitize=address,undefined

View file

@ -2,7 +2,6 @@
#include <assert.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
@ -10,33 +9,43 @@
#include <time.h>
#include <unistd.h>
#define DEBUG 1
#define SKIPLIST_DEBUG slex
#define SKIPLIST_MAX_HEIGHT 12
/* Setting this will do two things:
// OPTIONS to set before including sl.h
// ---------------------------------------------------------------------------
#define DEBUG
#define SKIPLIST_DIAGNOSTIC
/* Setting SKIPLIST_MAX_HEIGHT will do two things:
* 1) limit our max height across all instances of this data structure.
* 2) remove a heap allocation on frequently used paths, insert/remove/etc.
* so, use it when you need it.
*/
#define SKIPLIST_MAX_HEIGHT 12
// Include our monolithic ADT, the Skiplist!
// ---------------------------------------------------------------------------
#include "../include/sl.h"
//define INTEGRITY
#ifdef INTEGRITY
#define INTEGRITY_CHK __skip_integrity_check_slex(list, 0)
#else
#define INTEGRITY_CHK ((void)0)
#endif
#define SNAPSHOTS
// Local demo application OPTIONS:
// ---------------------------------------------------------------------------
#define VALIDATE
// TODO define SNAPSHOTS
#define DOT
#define TEST_ARRAY_SIZE 10
// ---------------------------------------------------------------------------
#ifdef VALIDATE
#define CHECK __skip_integrity_check_sample(list, 0)
#else
#define CHECK ((void)0)
#endif
/*
* SKIPLIST EXAMPLE:
*
* This example creates a Skiplist keys and values are integers.
* 'slex' - meaning: SkipList EXample
* This example creates a "sample" Skiplist where keys are integers, values are
* strings allocated on the heap.
*
* 'sample' - meaning: EXample
*/
/*
@ -47,26 +56,44 @@
* node against another, logic you'll provide in SKIP_DECL as a
* block below.
*/
struct slex_node {
struct sample_node {
int key;
char *value;
SKIPLIST_ENTRY(slex_node) entries;
SKIPLIST_ENTRY(sample_node) entries;
// TODO SKIPLIST_SNAPS(sample_node) snaps;
};
/*
* Generate all the access functions for our type of Skiplist.
*/
SKIPLIST_DECL(
slex, api_, entries,
/* free node */ { free(node->value); },
/* update node */
sample, api_, entries,
/* compare entries: list, a, b, aux */
{
//char *old = node->value;
dest->value = src->value;
// In this case, don't free, we're just calling to_upper and using the same memory.
// free(old);
(void)list;
(void)aux;
if (a->key < b->key)
return -1;
if (a->key > b->key)
return 1;
return 0;
},
/* archive a node */
/* free entry: node */
{
free(node->value);
},
/* update entry: rc, src, dest */
{
char *new = calloc(strlen(src->value) + 1, sizeof(char));
if (new == NULL) {
rc = ENOMEM;
} else {
strncpy(new, src->value, strlen(src->value));
free(dest->value);
dest->value = new;
}
},
/* archive an entry: rc, src, dest */
{
dest->key = src->key;
char *nv = calloc(strlen(src->value) + 1, sizeof(char));
@ -77,38 +104,13 @@ SKIPLIST_DECL(
dest->value = nv;
}
},
/* size in bytes of the content stored in an entry by you */
{ size = strlen(node->value) + 1; })
/*
* Optional: Create a function that validates as much as possible the
* integrity of a Skiplist. This is called by the DOT function to
* ensure that it's possible to generate a graph.
*/
SKIPLIST_INTEGRITY_CHECK(slex, api_, entries)
/* Optional: Create the functions used to visualize a Skiplist (DOT/Graphviz) */
SKIPLIST_DECL_DOT(slex, api_, entries)
void
sprintf_slex_node(slex_node_t *node, char *buf)
/* size in bytes of the content stored in an entry: bytes */
{
sprintf(buf, "%d:%s", node->key, node->value);
}
bytes = strlen(node->value) + 1;
})
/*
* Getters and Setters
* It can be useful to have simple get/put-style API, but to
* do that you'll have to supply some blocks of code used to
* extract data from within your nodes.
*/
SKIPLIST_KV_ACCESS(
slex, api_, key, int, value, char *,
/* query blk */ { query.key = key; },
/* return blk */ { return node->value; })
/*
* Now we need a way to compare the nodes you defined above.
* Skiplists are ordered, we need a way to compare entries.
* Let's create a function with four arguments:
* - a reference to the Skiplist, `slist`
* - the two nodes to compare, `a` and `b`
@ -127,9 +129,8 @@ SKIPLIST_KV_ACCESS(
* happen when `a` or `b` are references to the head or tail of the
* list or when `a == b`. In those cases the comparison function
* returns before using the code in your block, don't panic. :)
*/
int
__slm_key_compare(slex_t *list, slex_node_t *a, slex_node_t *b, void *aux)
__sample_key_compare(sample_t *list, sample_node_t *a, sample_node_t *b, void *aux)
{
(void)list;
(void)aux;
@ -139,7 +140,63 @@ __slm_key_compare(slex_t *list, slex_node_t *a, slex_node_t *b, void *aux)
return 1;
return 0;
}
*/
/*
* Optional: Getters and Setters
* - get, put, dup(put), del, etc. functions
*
* It can be useful to have simple get/put-style API, but to
* do that you'll have to supply some blocks of code used to
* extract data from within your nodes.
*/
SKIPLIST_DECL_ACCESS(
sample, api_, key, int, value, char *,
/* query blk */ { query.key = key; },
/* return blk */ { return node->value; })
/*
* Optional: Snapshots
*
* TODO
*/
//SKIPLIST_DECL_SNAPSHOTS(sample, api_, entries, snaps)
/*
* Optional: Archive to/from bytes
*
* TODO
*/
SKIPLIST_DECL_ARCHIVE(sample, api_, entries)
/*
* Optional: As Hashtable
*
* Turn your Skiplist into a hash table. TODO
*/
//SKIPLIST_DECL_HASHTABLE(sample, api_, entries, snaps)
/*
* Optional: Check Skiplists at runtime
*
* Create a functions that validate the integrity of a Skiplist.
*/
SKIPLIST_DECL_VALIDATE(sample, api_, entries)
/* Optional: Visualize your Skiplist using DOT/Graphviz in PDF
*
* Create the functions used to annotate a visualization of a Skiplist.
*/
SKIPLIST_DECL_DOT(sample, api_, entries)
void
sprintf_sample_node(sample_node_t *node, char *buf)
{
sprintf(buf, "%d:%s", node->key, node->value);
}
// Function for this demo application.
// ---------------------------------------------------------------------------
static char *
to_lower(char *str)
{
@ -200,12 +257,15 @@ shuffle(int *array, size_t n)
}
}
// ---------------------------------------------------------------------------
int
main()
{
int rc;
#ifdef SNAPSHOTS
size_t snap_i = 0;
uint64_t snaps[2048];
#endif
#ifdef DOT
size_t gen = 0;
@ -217,24 +277,24 @@ main()
#endif
/* Allocate and initialize a Skiplist. */
slex_t *list = (slex_t *)malloc(sizeof(slex_t));
sample_t *list = (sample_t *)malloc(sizeof(sample_t));
if (list == NULL)
return ENOMEM;
rc = api_skip_init_slex(list, -12, __slm_key_compare);
rc = api_skip_init_sample(list, 12); //TODO -12
if (rc)
return rc;
#ifdef DOT
api_skip_dot_slex(of, list, gen++, "init", sprintf_slex_node);
api_skip_dot_sample(of, list, gen++, "init", sprintf_sample_node);
#endif
if (api_skip_get_slex(list, 0) != NULL)
if (api_skip_get_sample(list, 0) != NULL)
perror("found a non-existent item!");
api_skip_del_slex(list, 0);
INTEGRITY_CHK;
api_skip_del_sample(list, 0);
CHECK;
#ifdef SNAPSHOTS
/* Test creating a snapshot of an empty Skiplist */
snaps[snap_i++] = api_skip_snapshot_slex(list);
snaps[snap_i++] = api_skip_snapshot_sample(list);
#endif
/* Insert 7 key/value pairs into the list. */
@ -251,98 +311,97 @@ main()
for (i = 0; i < asz; i++) {
numeral = int_to_roman_numeral(array[i]);
rc = api_skip_put_slex(list, array[i], to_lower(numeral));
//rc = api_skip_put_slex(list, array[i], numeral);
INTEGRITY_CHK;
rc = api_skip_put_sample(list, array[i], to_lower(numeral));
//rc = api_skip_put_sample(list, array[i], numeral);
CHECK;
#ifdef SNAPSHOTS
if (i > TEST_ARRAY_SIZE + 1) {
snaps[snap_i++] = api_skip_snapshot_slex(list);
INTEGRITY_CHK;
snaps[snap_i++] = api_skip_snapshot_sample(list);
CHECK;
}
#endif
#ifdef DOT
sprintf(msg, "put key: %d value: %s", i, numeral);
api_skip_dot_slex(of, list, gen++, msg, sprintf_slex_node);
INTEGRITY_CHK;
api_skip_dot_sample(of, list, gen++, msg, sprintf_sample_node);
CHECK;
#endif
char *v = api_skip_get_slex(list, array[i]);
INTEGRITY_CHK;
api_skip_set_slex(list, array[i], to_upper(v));
INTEGRITY_CHK;
char *v = api_skip_get_sample(list, array[i]);
CHECK;
api_skip_set_sample(list, array[i], to_upper(v));
CHECK;
}
numeral = int_to_roman_numeral(-1);
api_skip_dup_slex(list, -1, numeral);
INTEGRITY_CHK;
api_skip_dup_sample(list, -1, numeral);
CHECK;
#ifdef DOT
sprintf(msg, "put dup key: %d value: %s", i, numeral);
api_skip_dot_slex(of, list, gen++, msg, sprintf_slex_node);
INTEGRITY_CHK;
api_skip_dot_sample(of, list, gen++, msg, sprintf_sample_node);
CHECK;
#endif
numeral = int_to_roman_numeral(1);
api_skip_dup_slex(list, 1, numeral);
INTEGRITY_CHK;
api_skip_dup_sample(list, 1, numeral);
CHECK;
#ifdef DOT
sprintf(msg, "put dup key: %d value: %s", i, numeral);
api_skip_dot_slex(of, list, gen++, msg, sprintf_slex_node);
INTEGRITY_CHK;
api_skip_dot_sample(of, list, gen++, msg, sprintf_sample_node);
CHECK;
#endif
api_skip_del_slex(list, 0);
INTEGRITY_CHK;
if (api_skip_get_slex(list, 0) != NULL)
api_skip_del_sample(list, 0);
CHECK;
if (api_skip_get_sample(list, 0) != NULL)
perror("found a deleted item!");
api_skip_del_slex(list, 0);
INTEGRITY_CHK;
if (api_skip_get_slex(list, 0) != NULL)
api_skip_del_sample(list, 0);
CHECK;
if (api_skip_get_sample(list, 0) != NULL)
perror("found a deleted item!");
int key = TEST_ARRAY_SIZE + 1;
numeral = int_to_roman_numeral(key);
api_skip_del_slex(list, key);
INTEGRITY_CHK;
api_skip_del_sample(list, key);
CHECK;
key = -(TEST_ARRAY_SIZE) - 1;
numeral = int_to_roman_numeral(key);
api_skip_del_slex(list, key);
INTEGRITY_CHK;
api_skip_del_sample(list, key);
CHECK;
#ifdef DOT
sprintf(msg, "deleted key: %d, value: %s", 0, numeral);
api_skip_dot_slex(of, list, gen++, msg, sprintf_slex_node);
INTEGRITY_CHK;
api_skip_dot_sample(of, list, gen++, msg, sprintf_sample_node);
CHECK;
#endif
#ifdef SNAPSHOTS
slex_t *restored = api_skip_restore_snapshot_slex(list, snaps[snap_i - 1] );
api_skip_release_snapshot_slex(list);
api_skip_free_slex(restored);
sample_t *restored = api_skip_restore_snapshot_sample(list, snaps[snap_i - 1] );
api_skip_release_snapshot_sample(list);
api_skip_free_sample(restored);
#endif
assert(strcmp(api_skip_pos_slex(list, SKIP_GTE, -(TEST_ARRAY_SIZE)-1)->value, int_to_roman_numeral(-(TEST_ARRAY_SIZE))) == 0);
assert(strcmp(api_skip_pos_slex(list, SKIP_GTE, -2)->value, int_to_roman_numeral(-2)) == 0);
assert(strcmp(api_skip_pos_slex(list, SKIP_GTE, 0)->value, int_to_roman_numeral(1)) == 0);
assert(strcmp(api_skip_pos_slex(list, SKIP_GTE, 2)->value, int_to_roman_numeral(2)) == 0);
assert(api_skip_pos_slex(list, SKIP_GTE, (TEST_ARRAY_SIZE + 1)) == NULL);
assert(strcmp(api_skip_pos_sample(list, SKIP_GTE, -(TEST_ARRAY_SIZE)-1)->value, int_to_roman_numeral(-(TEST_ARRAY_SIZE))) == 0);
assert(strcmp(api_skip_pos_sample(list, SKIP_GTE, -2)->value, int_to_roman_numeral(-2)) == 0);
assert(strcmp(api_skip_pos_sample(list, SKIP_GTE, 0)->value, int_to_roman_numeral(1)) == 0);
assert(strcmp(api_skip_pos_sample(list, SKIP_GTE, 2)->value, int_to_roman_numeral(2)) == 0);
assert(api_skip_pos_sample(list, SKIP_GTE, (TEST_ARRAY_SIZE + 1)) == NULL);
assert(strcmp(api_skip_pos_slex(list, SKIP_GT, -(TEST_ARRAY_SIZE)-1)->value, int_to_roman_numeral(-(TEST_ARRAY_SIZE))) == 0);
assert(strcmp(api_skip_pos_slex(list, SKIP_GT, -2)->value, int_to_roman_numeral(-1)) == 0);
assert(strcmp(api_skip_pos_slex(list, SKIP_GT, 0)->value, int_to_roman_numeral(1)) == 0);
assert(strcmp(api_skip_pos_slex(list, SKIP_GT, 1)->value, int_to_roman_numeral(2)) == 0);
assert(api_skip_pos_slex(list, SKIP_GT, TEST_ARRAY_SIZE) == NULL);
assert(strcmp(api_skip_pos_sample(list, SKIP_GT, -(TEST_ARRAY_SIZE)-1)->value, int_to_roman_numeral(-(TEST_ARRAY_SIZE))) == 0);
assert(strcmp(api_skip_pos_sample(list, SKIP_GT, -2)->value, int_to_roman_numeral(-1)) == 0);
assert(strcmp(api_skip_pos_sample(list, SKIP_GT, 0)->value, int_to_roman_numeral(1)) == 0);
assert(strcmp(api_skip_pos_sample(list, SKIP_GT, 1)->value, int_to_roman_numeral(2)) == 0);
assert(api_skip_pos_sample(list, SKIP_GT, TEST_ARRAY_SIZE) == NULL);
assert(api_skip_pos_slex(list, SKIP_LT, -(TEST_ARRAY_SIZE)) == NULL);
assert(strcmp(api_skip_pos_slex(list, SKIP_LT, -1)->value, int_to_roman_numeral(-2)) == 0);
assert(strcmp(api_skip_pos_slex(list, SKIP_LT, 0)->value, int_to_roman_numeral(-1)) == 0);
assert(strcmp(api_skip_pos_slex(list, SKIP_LT, 2)->value, int_to_roman_numeral(1)) == 0);
assert(strcmp(api_skip_pos_slex(list, SKIP_LT, (TEST_ARRAY_SIZE + 1))->value, int_to_roman_numeral(TEST_ARRAY_SIZE)) == 0);
assert(api_skip_pos_sample(list, SKIP_LT, -(TEST_ARRAY_SIZE)) == NULL);
assert(strcmp(api_skip_pos_sample(list, SKIP_LT, -1)->value, int_to_roman_numeral(-2)) == 0);
assert(strcmp(api_skip_pos_sample(list, SKIP_LT, 0)->value, int_to_roman_numeral(-1)) == 0);
assert(strcmp(api_skip_pos_sample(list, SKIP_LT, 2)->value, int_to_roman_numeral(1)) == 0);
assert(strcmp(api_skip_pos_sample(list, SKIP_LT, (TEST_ARRAY_SIZE + 1))->value, int_to_roman_numeral(TEST_ARRAY_SIZE)) == 0);
assert(api_skip_pos_slex(list, SKIP_LTE, -(TEST_ARRAY_SIZE)-1) == NULL);
assert(strcmp(api_skip_pos_slex(list, SKIP_LTE, -2)->value, int_to_roman_numeral(-2)) == 0);
assert(strcmp(api_skip_pos_slex(list, SKIP_LTE, 0)->value, int_to_roman_numeral(-1)) == 0);
assert(strcmp(api_skip_pos_slex(list, SKIP_LTE, 2)->value, int_to_roman_numeral(2)) == 0);
assert(strcmp(api_skip_pos_slex(list, SKIP_LTE, (TEST_ARRAY_SIZE + 1))->value, int_to_roman_numeral(TEST_ARRAY_SIZE)) == 0);
assert(api_skip_pos_sample(list, SKIP_LTE, -(TEST_ARRAY_SIZE)-1) == NULL);
assert(strcmp(api_skip_pos_sample(list, SKIP_LTE, -2)->value, int_to_roman_numeral(-2)) == 0);
assert(strcmp(api_skip_pos_sample(list, SKIP_LTE, 0)->value, int_to_roman_numeral(-1)) == 0);
assert(strcmp(api_skip_pos_sample(list, SKIP_LTE, 2)->value, int_to_roman_numeral(2)) == 0);
assert(strcmp(api_skip_pos_sample(list, SKIP_LTE, (TEST_ARRAY_SIZE + 1))->value, int_to_roman_numeral(TEST_ARRAY_SIZE)) == 0);
api_skip_free_slex(list);
api_skip_free_sample(list);
#ifdef DOT
api_skip_dot_end_slex(of, gen);
api_skip_dot_end_sample(of, gen);
fclose(of);
#endif
return rc;

View file

@ -139,6 +139,21 @@
#pragma GCC diagnostic ignored "-Wvariadic-macros"
#define __skip_diag(format, ...) __skip_diag_(__FILE__, __LINE__, __func__, format, ##__VA_ARGS__)
#pragma GCC diagnostic pop
/** \
* -- __skip_diag__ \
* \
* Write debug message to stderr with origin of message. \
*/
void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int line, const char *func, const char *format, ...)
{
va_list args;
va_start(args, format);
fprintf(stderr, "%s:%d:%s(): ", file, line, func);
vfprintf(stderr, format, args);
va_end(args);
}
#else
#define __skip_diag(file, line, func, format, ...) ((void)0)
#endif
@ -151,7 +166,6 @@
#else
#define __skip_assert(expr) ((void)0)
#endif
#endif
#ifndef SKIPLIST_READ_RDTSC
@ -230,75 +244,6 @@ __skip_read_rdtsc(void)
uint64_t sle_gen; \
}
/*
* Skip List node comparison function. This macro builds a function used when
* comparing two nodes for equality. A portion of this function, `fn_blk`, is
* the code you supply to compare two nodes as a block (starts with `{`, your
* code, then `}`) that should compare the node referenced by `a` to the node
* `b` as follows:
*
* When:
* *a < *b : return -1
* *a == *b : return 0
* *a > *b : return 1
*
* As stated earlier, the ordered skiplist relies on a well-behaved comparison
* function. Specifically, given some ordering function f(a, b), it must satisfy
* the following properties:
*
* 1) Be well defined: f(a, b) should always return the same value
* 2) Be anti-symmetric: f(a, b) == Greater if and only if f(b, a) == Less, and
* f(a, b) == Equal == f(b, a).
* 3) Be transitive: If f(a, b) == Greater and f(b, c) == Greater then f(a, c)
* == Greater.
*
* Failure to satisfy these properties can result in unexpected behavior at
* best, and at worst will cause a segfault, null deref, or some other bad
* behavior.
*
* Example for nodes with keys that are signed integeters (`int`):
* {
* (void)list;
* (void)aux;
* if (a->key < b->key)
* return -1;
* if (a->key > b->key)
* return 1;
* return 0;
* }
*
* Note that the comparison function can also make use of the reference to the
* list as well as a reference to a variable `aux` that you can point to
* anything else required to perform your comparison. The auxiliary pointer
* is unused for other purposes. You could even use it a pointer to a function
* that chooses the proper comparison function for the two nodes in question.
*
* Example where the value of `decl` below is `ex`:
* {
* // Cast `aux` to a function that returns a function that properly compares
* // `a` and `b`, for example if they were objects or different structs.
* (skip_ex_cmp_t *(fn)(ex_node_t *, ex_node_t *)) =
* (skip_ex_cmp_t *()(ex_node_t *, ex_node_t *))aux;
*
* // Use the `fn` pointed to by `aux` to get the comparison function.
* skip_ex_cmp_t *cmp = fn(a, b);
*
* // Now call that function and return the proper result.
* return cmp(list, a, b, aux);
* }
*/
#define SKIP_COMPARATOR(list, decl, fn_blk) \
int __skip_cmp_##decl(struct list *head, struct decl *a, struct decl *b, void *aux) \
{ \
if (a == b) \
return 0; \
if (a == (head)->slh_head || b == (head)->slh_tail) \
return -1; \
if (a == (head)->slh_tail || b == (head)->slh_head) \
return 1; \
fn_blk \
}
#define SKIPLIST_FOREACH_H2T(decl, prefix, list, elm, iter) \
for (iter = 0, (elm) = prefix##skip_head_##decl(list); (elm) != NULL; iter++, (elm) = prefix##skip_next_node_##decl(list, elm))
@ -321,7 +266,10 @@ __skip_read_rdtsc(void)
/*
* Skip List declarations and access methods.
*/
#define SKIPLIST_DECL(decl, prefix, field, free_node_blk, update_node_blk, archive_node_blk, sizeof_entry_blk) \
#define SKIPLIST_DECL(decl, prefix, field, compare_entries_blk, free_entry_blk, update_entry_blk, archive_entry_blk, sizeof_entry_blk) \
\
/* Used when positioning a cursor within a Skip List. */ \
typedef enum { SKIP_EQ = 0, SKIP_LTE = -1, SKIP_LT = -2, SKIP_GTE = 1, SKIP_GT = 2 } skip_pos_##decl_t; \
\
/* Skip List node type */ \
typedef struct decl##_node decl##_node_t; \
@ -329,7 +277,13 @@ __skip_read_rdtsc(void)
/* Skip List structure and type */ \
typedef struct decl { \
size_t slh_length, slh_height, slh_max_height; \
int (*slh_cmp)(struct decl *, decl##_node_t *, decl##_node_t *, void *); \
struct { \
void (*free_entry)(decl##_node_t *); \
int (*update_entry)(decl##_node_t *, decl##_node_t *); \
int (*archive_entry)(decl##_node_t *, const decl##_node_t *); \
size_t (*sizeof_entry)(decl##_node_t *); \
int (*compare_entries)(struct decl *, decl##_node_t *, decl##_node_t *, void *); \
} slh_fns; \
void *slh_aux; \
decl##_node_t *slh_head; \
decl##_node_t *slh_tail; \
@ -339,17 +293,67 @@ __skip_read_rdtsc(void)
} slh_snap; \
} decl##_t; \
\
/* Skip List comparison function type */ \
typedef int (*skip_##decl##_cmp_t)(decl##_t *, decl##_node_t *, decl##_node_t *, void *); \
\
/* Used when positioning a cursor within a Skip List. */ \
typedef enum { SKIP_EQ = 0, SKIP_LTE = -1, SKIP_LT = -2, SKIP_GTE = 1, SKIP_GT = 2 } skip_pos_##decl_t; \
/** \
* -- __skip_compare_entries_fn \
* \
* Wraps the `compare_entries_blk` code into `slh_fns.compare_entries`. \
*/ \
static int __skip_compare_entries_fn_##decl(decl##_t *list, decl##_node_t *a, decl##_node_t *b, void *aux) \
{ \
compare_entries_blk; \
} \
\
/** \
* -- __skip_key_compare_ \
* -- __skip_free_entry_fn \
* \
* Wraps the `free_entry_blk` code into `slh_fns.free_entry`. \
*/ \
static void __skip_free_entry_fn_##decl(decl##_node_t *node) \
{ \
free_entry_blk; \
} \
\
/** \
* -- __skip_update_entry_fn \
* \
* Wraps the `update_entry_blk` code into `slh_fns.update_entry`. \
*/ \
static int __skip_update_entry_fn_##decl(decl##_node_t *dest, decl##_node_t *src) \
{ \
int rc = 0; \
update_entry_blk; \
return rc; \
} \
\
/** \
* -- __skip_archive_entry_fn \
* \
* Wraps the `archive_entry_blk` code into `slh_fns.archive_entry`. \
*/ \
static int __skip_archive_entry_fn_##decl(decl##_node_t *dest, const decl##_node_t *src) \
{ \
int rc = 0; \
archive_entry_blk; \
return rc; \
} \
\
/** \
* -- __skip_sizeof_entry_fn \
* \
* Wraps the `sizeof_entry_blk` code into `slh_fns.sizeof_entry`. \
*/ \
static size_t __skip_sizeof_entry_fn_##decl(decl##_node_t *node) \
{ \
size_t bytes = 0; \
sizeof_entry_blk; \
return bytes; \
} \
\
/** \
* -- __skip_compare_nodes_ \
* \
* This function takes four arguments: \
* - a reference to the Skip List \
* - a reference to the Skiplist \
* - the two nodes to compare, `a` and `b` \
* - `aux` an additional auxiliary argument \
* and returns: \
@ -357,7 +361,7 @@ __skip_read_rdtsc(void)
* a == b : return 0 \
* a > b : return 1 \
*/ \
static int __skip_key_compare_##decl(decl##_t *slist, decl##_node_t *a, decl##_node_t *b, void *aux) \
static int __skip_compare_nodes_##decl(decl##_t *slist, decl##_node_t *a, decl##_node_t *b, void *aux) \
{ \
if (a == b) \
return 0; \
@ -369,7 +373,7 @@ __skip_read_rdtsc(void)
return -1; \
if (a == slist->slh_tail || b == slist->slh_head) \
return 1; \
return slist->slh_cmp(slist, a, b, aux); \
return slist->slh_fns.compare_entries(slist, a, b, aux); \
} \
\
/** \
@ -439,7 +443,7 @@ __skip_read_rdtsc(void)
* Initializes a Skip List to the deafault values, this must be called \
* before using the list. \
*/ \
int prefix##skip_init_##decl(decl##_t *slist, int max, int (*cmp)(struct decl *, decl##_node_t *, decl##_node_t *, void *)) \
int prefix##skip_init_##decl(decl##_t *slist, int max) \
{ \
int rc = 0; \
size_t i; \
@ -451,7 +455,11 @@ __skip_read_rdtsc(void)
slist->slh_max_height = SKIPLIST_MAX_HEIGHT == 1 ? slist->slh_max_height : SKIPLIST_MAX_HEIGHT; \
if (SKIPLIST_MAX_HEIGHT > 1 && slist->slh_max_height > SKIPLIST_MAX_HEIGHT) \
return -1; \
slist->slh_cmp = cmp; \
slist->slh_fns.free_entry = __skip_free_entry_fn_##decl; \
slist->slh_fns.update_entry = __skip_update_entry_fn_##decl; \
slist->slh_fns.archive_entry = __skip_archive_entry_fn_##decl; \
slist->slh_fns.sizeof_entry = __skip_sizeof_entry_fn_##decl; \
slist->slh_fns.compare_entries = __skip_compare_entries_fn_##decl; \
rc = prefix##skip_alloc_node_##decl(slist, &slist->slh_head); \
if (rc) \
goto fail; \
@ -488,9 +496,9 @@ __skip_read_rdtsc(void)
* should release any heap objects or other resources held by \
* this node in the list. \
*/ \
void prefix##skip_free_node_##decl(decl##_node_t *node) \
void prefix##skip_free_node_##decl(decl##_t *slist, decl##_node_t *node) \
{ \
free_node_blk; \
slist->slh_fns.free_entry(node); \
free(node); \
} \
\
@ -584,14 +592,14 @@ __skip_read_rdtsc(void)
node = prefix##skip_head_##decl(slist); \
do { \
next = prefix##skip_next_node_##decl(slist, node); \
prefix##skip_free_node_##decl(node); \
prefix##skip_free_node_##decl(slist, node); \
node = next; \
} while (node != NULL); \
\
while (node) { \
next = node->field.sle_next[0]; \
if (next->field.sle_prev) \
free_node_blk; \
slist->slh_fns.free_entry(node); \
free(node); \
} \
return; \
@ -641,13 +649,13 @@ __skip_read_rdtsc(void)
/* Find the node that matches `node` or NULL. */ \
i = slist->slh_head->field.sle_height; \
do { \
while (elm != slist->slh_tail && elm->field.sle_next[i] && __skip_key_compare_##decl(slist, elm->field.sle_next[i], n, slist->slh_aux) < 0) \
while (elm != slist->slh_tail && elm->field.sle_next[i] && __skip_compare_nodes_##decl(slist, elm->field.sle_next[i], n, slist->slh_aux) < 0) \
elm = elm->field.sle_next[i]; \
path[i + 1] = elm; \
len++; \
} while (i--); \
elm = elm->field.sle_next[0]; \
if (__skip_key_compare_##decl(slist, elm, n, slist->slh_aux) == 0) { \
if (__skip_compare_nodes_##decl(slist, elm, n, slist->slh_aux) == 0) { \
path[0] = elm; \
} \
return len; \
@ -701,16 +709,16 @@ __skip_read_rdtsc(void)
/* ... if we're not preserving the head or the tail, ... */ \
if (!(src == slist->slh_head || src == slist->slh_tail)) { \
/* (c) then user-supplied copy */ \
archive_node_blk; \
slist->slh_fns.archive_entry(dest, src); \
if (rc) { \
prefix##skip_free_node_##decl(dest); \
prefix##skip_free_node_##decl(slist, dest); \
return rc; \
} \
} \
\
/* (d) is this a duplicate? */ \
if (__skip_key_compare_##decl(slist, dest, dest->field.sle_next[0], slist->slh_aux) == 0 || \
__skip_key_compare_##decl(slist, dest, dest->field.sle_prev, slist->slh_aux) == 0) \
if (__skip_compare_nodes_##decl(slist, dest, dest->field.sle_next[0], slist->slh_aux) == 0 || \
__skip_compare_nodes_##decl(slist, dest, dest->field.sle_prev, slist->slh_aux) == 0) \
is_dup = (decl##_node_t *)0x1; \
\
/* (e) zero out the next pointers */ \
@ -890,11 +898,11 @@ __skip_read_rdtsc(void)
i = slist->slh_head->field.sle_height; \
\
do { \
while (elm != slist->slh_tail && elm->field.sle_next[i] && __skip_key_compare_##decl(slist, elm->field.sle_next[i], n, slist->slh_aux) < 0) \
while (elm != slist->slh_tail && elm->field.sle_next[i] && __skip_compare_nodes_##decl(slist, elm->field.sle_next[i], n, slist->slh_aux) < 0) \
elm = elm->field.sle_next[i]; \
} while (i--); \
elm = elm->field.sle_next[0]; \
if (__skip_key_compare_##decl(slist, elm, n, slist->slh_aux) == 0) { \
if (__skip_compare_nodes_##decl(slist, elm, n, slist->slh_aux) == 0) { \
return elm; \
} \
return NULL; \
@ -922,12 +930,12 @@ __skip_read_rdtsc(void)
i = slist->slh_head->field.sle_height; \
\
do { \
while (elm != slist->slh_tail && elm->field.sle_next[i] && __skip_key_compare_##decl(slist, elm->field.sle_next[i], query, slist->slh_aux) < 0) \
while (elm != slist->slh_tail && elm->field.sle_next[i] && __skip_compare_nodes_##decl(slist, elm->field.sle_next[i], query, slist->slh_aux) < 0) \
elm = elm->field.sle_next[i]; \
} while (i--); \
do { \
elm = elm->field.sle_next[0]; \
cmp = __skip_key_compare_##decl(slist, elm, query, slist->slh_aux); \
cmp = __skip_compare_nodes_##decl(slist, elm, query, slist->slh_aux); \
} while (cmp < 0); \
return elm; \
} \
@ -954,12 +962,12 @@ __skip_read_rdtsc(void)
i = slist->slh_head->field.sle_height; \
\
do { \
while (elm != slist->slh_tail && elm->field.sle_next[i] && __skip_key_compare_##decl(slist, elm->field.sle_next[i], query, slist->slh_aux) < 0) \
while (elm != slist->slh_tail && elm->field.sle_next[i] && __skip_compare_nodes_##decl(slist, elm->field.sle_next[i], query, slist->slh_aux) < 0) \
elm = elm->field.sle_next[i]; \
} while (i--); \
do { \
elm = elm->field.sle_next[0]; \
cmp = __skip_key_compare_##decl(slist, elm, query, slist->slh_aux); \
cmp = __skip_compare_nodes_##decl(slist, elm, query, slist->slh_aux); \
} while (cmp <= 0); \
return elm; \
} \
@ -986,16 +994,16 @@ __skip_read_rdtsc(void)
i = slist->slh_head->field.sle_height; \
\
do { \
while (elm != slist->slh_tail && elm->field.sle_next[i] && __skip_key_compare_##decl(slist, elm->field.sle_next[i], query, slist->slh_aux) < 0) \
while (elm != slist->slh_tail && elm->field.sle_next[i] && __skip_compare_nodes_##decl(slist, elm->field.sle_next[i], query, slist->slh_aux) < 0) \
elm = elm->field.sle_next[i]; \
} while (i--); \
elm = elm->field.sle_next[0]; \
if (__skip_key_compare_##decl(slist, elm, query, slist->slh_aux) == 0) { \
if (__skip_compare_nodes_##decl(slist, elm, query, slist->slh_aux) == 0) { \
return elm; \
} else { \
do { \
elm = elm->field.sle_prev; \
cmp = __skip_key_compare_##decl(slist, elm, query, slist->slh_aux); \
cmp = __skip_compare_nodes_##decl(slist, elm, query, slist->slh_aux); \
} while (cmp >= 0); \
} \
return elm; \
@ -1022,13 +1030,13 @@ __skip_read_rdtsc(void)
i = slist->slh_head->field.sle_height; \
\
do { \
while (elm != slist->slh_tail && elm->field.sle_next[i] && __skip_key_compare_##decl(slist, elm->field.sle_next[i], query, slist->slh_aux) < 0) \
while (elm != slist->slh_tail && elm->field.sle_next[i] && __skip_compare_nodes_##decl(slist, elm->field.sle_next[i], query, slist->slh_aux) < 0) \
elm = elm->field.sle_next[i]; \
} while (i--); \
elm = elm->field.sle_next[0]; \
do { \
elm = elm->field.sle_prev; \
cmp = __skip_key_compare_##decl(slist, elm, query, slist->slh_aux); \
cmp = __skip_compare_nodes_##decl(slist, elm, query, slist->slh_aux); \
} while (cmp >= 0); \
return elm; \
} \
@ -1070,7 +1078,7 @@ __skip_read_rdtsc(void)
* -- skip_update_ \
* \
* Locates a node in the list that equals the `new` node and then \
* uses the `update_node_blk` to update the contents. \
* uses the `update_entry_blk` to update the contents. \
* \
* WARNING: Do not update the portion of the node used for ordering \
* (e.g. `key`) unless you really know what you're doing. \
@ -1110,7 +1118,7 @@ __skip_read_rdtsc(void)
return np; \
} \
\
update_node_blk; \
slist->slh_fns.update_entry(dest, src); \
src->field.sle_gen = cur_gen; \
return rc; \
} \
@ -1176,7 +1184,7 @@ __skip_read_rdtsc(void)
\
/* If we didn't preserve any nodes we can free this one. */ \
if (np == 0) \
free_node_blk; \
slist->slh_fns.free_entry(node); \
\
/* Reduce the height of the header. */ \
i = 0; \
@ -1247,7 +1255,7 @@ __skip_read_rdtsc(void)
prev->field.sle_next[0] = node->field.sle_next[0]; \
} \
\
prefix##skip_free_node_##decl(node); \
prefix##skip_free_node_##decl(slist, node); \
} \
\
/* c */ \
@ -1296,7 +1304,9 @@ __skip_read_rdtsc(void)
\
free(slist->slh_head); \
free(slist->slh_tail); \
} \
}
#define SKIPLIST_DECL_ARCHIVE(decl, prefix, field) \
\
/* Archive of a Skip List */ \
typedef struct decl##_archive { \
@ -1314,9 +1324,9 @@ __skip_read_rdtsc(void)
decl##_archive_t *prefix##skip_to_bytes_##decl(decl##_t *slist) \
{ \
int rc = 0; \
size_t size, bytes, i; \
size_t size = 0, bytes, i; \
decl##_archive_t *archive; \
decl##_node_t *src, *dest, *node; \
decl##_node_t *src; \
\
if (slist == NULL) \
return NULL; \
@ -1324,8 +1334,7 @@ __skip_read_rdtsc(void)
bytes = sizeof(decl##_archive_t) + (slist->slh_length * sizeof(decl##_node_t)); \
src = prefix##skip_head_##decl(slist); \
while (src) { \
node = src; \
sizeof_entry_blk; \
slist->slh_fns.sizeof_entry(src); \
bytes += sizeof(size_t); \
bytes += size; \
src = prefix##skip_next_node_##decl(slist, src); \
@ -1343,8 +1352,7 @@ __skip_read_rdtsc(void)
src = prefix##skip_head_##decl(slist); \
while (src) { \
decl##_node_t *n = (decl##_node_t *)archive->nodes + (i++ * sizeof(decl##_node_t)); \
dest = (decl##_node_t *)&n; \
archive_node_blk; \
slist->slh_fns.archive_entry(n, src); \
if (rc) \
return NULL; \
src = prefix##skip_next_node_##decl(slist, src); \
@ -1356,20 +1364,19 @@ __skip_read_rdtsc(void)
* -- skip_from_bytes_ TODO/WIP \
* \
*/ \
decl##_t *prefix##skip_from_bytes_##decl(decl##_archive_t *archive, int (*cmp)(decl##_t * head, decl##_node_t * a, decl##_node_t * b, void *aux)) \
decl##_t *prefix##skip_from_bytes_##decl(decl##_archive_t *archive) \
{ \
int rc; \
size_t i; \
decl##_t *slist; \
decl##_node_t *src, *dest; \
\
if (archive == NULL || cmp == NULL) \
if (archive == NULL) \
return 0; \
slist = (decl##_t *)calloc(1, sizeof(decl##_t)); \
if (slist == NULL) \
return NULL; \
\
slist->slh_cmp = cmp; \
slist->slh_max_height = archive->list.slh_max_height; \
\
rc = prefix##skip_alloc_node_##decl(slist, &slist->slh_head); \
@ -1394,7 +1401,7 @@ __skip_read_rdtsc(void)
decl##_node_t *n = (decl##_node_t *)archive->nodes + (i++ * sizeof(decl##_node_t)); \
src = (decl##_node_t *)&n; \
rc = prefix##skip_alloc_node_##decl(slist, &dest); \
archive_node_blk; \
slist->slh_fns.archive_entry(dest, src); \
__skip_insert_##decl(slist, dest, 1); \
archive->list.slh_length--; \
} \
@ -1407,21 +1414,7 @@ __skip_read_rdtsc(void)
return NULL; \
}
/** \
* -- __skip_diag__ \
* \
* Write debug message to stderr with origin of message. \
*/
void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int line, const char *func, const char *format, ...)
{
va_list args;
va_start(args, format);
fprintf(stderr, "%s:%d:%s(): ", file, line, func);
vfprintf(stderr, format, args);
va_end(args);
}
#define SKIPLIST_INTEGRITY_CHECK(decl, prefix, field) \
#define SKIPLIST_DECL_VALIDATE(decl, prefix, field) \
/** \
* -- __skip_integrity_failure_ \
*/ \
@ -1463,8 +1456,32 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li
return n_err; \
} \
\
if (slist->slh_cmp == NULL) { \
__skip_integrity_failure_##decl("skiplist comparison function (cmp) is NULL\n"); \
if (slist->slh_fns.free_entry == NULL) { \
__skip_integrity_failure_##decl("skiplist free_entry fn is NULL\n"); \
n_err++; \
return n_err; \
} \
\
if (slist->slh_fns.update_entry == NULL) { \
__skip_integrity_failure_##decl("skiplist update_entry fn is NULL\n"); \
n_err++; \
return n_err; \
} \
\
if (slist->slh_fns.archive_entry == NULL) { \
__skip_integrity_failure_##decl("skiplist archive_entry fn is NULL\n"); \
n_err++; \
return n_err; \
} \
\
if (slist->slh_fns.sizeof_entry == NULL) { \
__skip_integrity_failure_##decl("skiplist sizeof_entry fn is NULL\n"); \
n_err++; \
return n_err; \
} \
\
if (slist->slh_fns.compare_entries == NULL) { \
__skip_integrity_failure_##decl("skiplist compare_entries fn is NULL\n"); \
n_err++; \
return n_err; \
} \
@ -1606,35 +1623,35 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li
\
next = this->sle_next[0]; \
prev = this->sle_prev; \
if (__skip_key_compare_##decl(slist, node, node, slist->slh_aux) != 0) { \
if (__skip_compare_nodes_##decl(slist, node, node, slist->slh_aux) != 0) { \
__skip_integrity_failure_##decl("the %luth node [%p] is not equal to itself\n", nth, (void *)node); \
n_err++; \
if (flags) \
return n_err; \
} \
\
if (__skip_key_compare_##decl(slist, node, prev, slist->slh_aux) < 0) { \
if (__skip_compare_nodes_##decl(slist, node, prev, slist->slh_aux) < 0) { \
__skip_integrity_failure_##decl("the %luth node [%p] is not greater than the prev node [%p]\n", nth, (void *)node, (void *)prev); \
n_err++; \
if (flags) \
return n_err; \
} \
\
if (__skip_key_compare_##decl(slist, node, next, slist->slh_aux) > 0) { \
if (__skip_compare_nodes_##decl(slist, node, next, slist->slh_aux) > 0) { \
__skip_integrity_failure_##decl("the %luth node [%p] is not less than the next node [%p]\n", nth, (void *)node, (void *)next); \
n_err++; \
if (flags) \
return n_err; \
} \
\
if (__skip_key_compare_##decl(slist, prev, node, slist->slh_aux) > 0) { \
if (__skip_compare_nodes_##decl(slist, prev, node, slist->slh_aux) > 0) { \
__skip_integrity_failure_##decl("the prev node [%p] is not less than the %luth node [%p]\n", (void *)prev, nth, (void *)node); \
n_err++; \
if (flags) \
return n_err; \
} \
\
if (__skip_key_compare_##decl(slist, next, node, slist->slh_aux) < 0) { \
if (__skip_compare_nodes_##decl(slist, next, node, slist->slh_aux) < 0) { \
__skip_integrity_failure_##decl("the next node [%p] is not greater than the %luth node [%p]\n", (void *)next, nth, (void *)node); \
n_err++; \
if (flags) \
@ -1643,7 +1660,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li
} \
\
if (slist->slh_length != nth) { \
__skip_integrity_failure_slex("slist->slh_length (%lu) doesn't match the count (%lu) of nodes between the head and tail\n", slist->slh_length, \
__skip_integrity_failure_##decl("slist->slh_length (%lu) doesn't match the count (%lu) of nodes between the head and tail\n", slist->slh_length, \
nth); \
n_err++; \
if (flags) \
@ -1653,7 +1670,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li
return 0; \
}
#define SKIPLIST_KV_ACCESS(decl, prefix, key, ktype, value, vtype, qblk, rblk) \
#define SKIPLIST_DECL_ACCESS(decl, prefix, key, ktype, value, vtype, qblk, rblk) \
/** \
* skip_get_ -- \
* \
@ -1729,7 +1746,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li
node->value = value; \
rc = prefix##skip_insert_##decl(slist, node); \
if (rc) \
prefix##skip_free_node_##decl(node); \
prefix##skip_free_node_##decl(slist, node); \
return rc; \
} \
\
@ -1750,7 +1767,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li
node->value = value; \
rc = prefix##skip_insert_dup_##decl(slist, node); \
if (rc) \
prefix##skip_free_node_##decl(node); \
prefix##skip_free_node_##decl(slist, node); \
return rc; \
} \
\