From 8172a163cd8ae41424cf1690b2ac1840c247a1bd Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Sun, 17 Mar 2024 14:12:56 -0400 Subject: [PATCH] fixing --- examples/slm.c | 16 ++++++++++------ include/sl.h | 16 +++++++++------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/examples/slm.c b/examples/slm.c index 3fa44fd..67a6458 100644 --- a/examples/slm.c +++ b/examples/slm.c @@ -48,7 +48,7 @@ int main() { #define STATIC_INIT #ifdef STATIC_INIT skip_t _list = SKIP_HEAD_DEFAULT_INITIALIZER(__skip_cmp_entry); - _list.slh_tail = (struct entry *)_list.slh_head; // TODO... + _list.slh_tail = (struct entry *)&_list.slh_head; // TODO... skip_t *list = &_list; #else /* Dynamic allocation, init. */ skip_t *list = (skip_t *)malloc(sizeof(skip_t)); @@ -56,13 +56,17 @@ int main() { #endif /* Insert 10 key/value pairs into the list. */ + struct entry *n; for (int i = 0; i < 10; i++) { - struct entry *n; - SKIP_ALLOC_NODE(list, n, entry, entries); - n->key = i; - n->value = i; - SKIP_INSERT(list, entry, n, entries); + SKIP_ALLOC_NODE(list, n, entry, entries); + n->key = i; + n->value = i; + SKIP_INSERT(list, entry, n, entries); } + SKIP_ALLOC_NODE(list, n, entry, entries); + n->key = -1; + n->value = -1; + SKIP_INSERT(list, entry, n, entries); #if 0 /* Delete a specific element in the list. */ diff --git a/include/sl.h b/include/sl.h index 22c256f..d8c7104 100644 --- a/include/sl.h +++ b/include/sl.h @@ -217,7 +217,7 @@ struct sl_trace { (var) = (struct type *)calloc(1, sizeof(struct type) + amt + 3); \ if ((var) != NULL) { \ (var)->field.sle_prev = (struct type *)(var) + sizeof(struct type); \ - (var)->field.sle_next = (struct type **)(var) + sizeof(struct type) + (sizeof(struct type *) * 3); \ + (var)->field.sle_next = (struct type **)((var)->field.sle_prev + (sizeof(void *) * 3)); \ ARRAY_SET_SIZE((var)->field.sle_next, (head)->max); \ ARRAY_SET_LENGTH((var)->field.sle_next, 0); \ } \ @@ -235,13 +235,14 @@ struct sl_trace { #define SKIP_INSERT(head, type, listelm, field) do { \ struct type *__elm = SKIP_FIRST(head); \ - unsigned int __i = (head)->level; \ + unsigned int __i; \ struct type **__path; \ - if (__elm == NULL) { \ + if ((listelm) == NULL) break; \ + if (__elm == NULL) { \ /* Empty list, setup header and add first element. */ \ - ARRAY_ALLOC((head)->slh_head, type, (head)->max); \ - ARRAY_FORALL(__j, (head)->slh_head) { \ - (head)->slh_head[__j] = (head)->slh_tail; \ + ARRAY_ALLOC((head)->slh_head, type, (head)->max); \ + ARRAY_FORALL(__i, (head)->slh_head) { \ + (head)->slh_head[__i] = (head)->slh_tail; \ } \ (head)->slh_head[0] = (listelm); \ (head)->slh_tail = (listelm); \ @@ -250,11 +251,12 @@ struct sl_trace { (head)->length = 1; \ break; \ } \ + __i = (head)->level; \ ARRAY_ALLOC(__path, type, (head)->max); \ if (__path == NULL) break; /* ENOMEM */ \ /* Find the position in the list where this element belongs. */ \ do { \ - while(__elm && (head)->cmp((head), __elm->field.sle_next[__i], __elm, (head)->aux) < 0) \ + while(__elm && (head)->cmp((head), __elm->field.sle_next[__i], (listelm), (head)->aux) < 0) \ __elm = __elm->field.sle_next[__i]; \ __path[__i] = __elm; \ } while(__i--); \