src/linked_list.c

Mon, 27 Sep 2021 17:00:19 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Sep 2021 17:00:19 +0200
changeset 436
ca9ce2297c29
parent 435
0fe204d50f54
child 437
9d4971ea0625
permissions
-rw-r--r--

add node destruction in cxLinkedListDestroy()

universe@398 1 /*
universe@398 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@398 3 *
universe@398 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@398 5 *
universe@398 6 * Redistribution and use in source and binary forms, with or without
universe@398 7 * modification, are permitted provided that the following conditions are met:
universe@398 8 *
universe@398 9 * 1. Redistributions of source code must retain the above copyright
universe@398 10 * notice, this list of conditions and the following disclaimer.
universe@398 11 *
universe@398 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@398 13 * notice, this list of conditions and the following disclaimer in the
universe@398 14 * documentation and/or other materials provided with the distribution.
universe@398 15 *
universe@398 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@398 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@398 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@398 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@398 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@398 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@398 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@398 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@398 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@398 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@398 26 * POSSIBILITY OF SUCH DAMAGE.
universe@398 27 */
universe@398 28
universe@398 29 #include "cx/linked_list.h"
universe@401 30 #include <stdint.h>
universe@401 31 #include <string.h>
universe@398 32
universe@398 33 /* LOW LEVEL LINKED LIST FUNCTIONS */
universe@398 34
universe@398 35 #define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off))
universe@398 36
universe@400 37 void *cx_linked_list_last(void **begin, void **end, ptrdiff_t loc_next) {
universe@398 38 if (end != NULL) {
universe@398 39 return *end;
universe@398 40 } else {
universe@398 41 if (begin == NULL || *begin == NULL)
universe@398 42 return NULL;
universe@398 43
universe@398 44 void *cur = *begin;
universe@398 45 void *last;
universe@398 46 do {
universe@398 47 last = cur;
universe@398 48 } while ((cur = *CX_LL_PTR(cur, loc_next)) != NULL);
universe@398 49
universe@398 50 return last;
universe@398 51 }
universe@398 52 }
universe@398 53
universe@406 54 int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) {
universe@398 55 void *last = cx_linked_list_last(begin, end, loc_next);
universe@398 56 if (last == NULL) {
universe@398 57 if (begin == NULL) {
universe@428 58 // no current list and no begin ptr to write to - we don't find something to append to
universe@398 59 return 1;
universe@398 60 } else {
universe@428 61 // start fresh list
universe@406 62 *begin = new_node;
universe@398 63 }
universe@428 64 } else {
universe@428 65 // if there is a last node, update its next pointer
universe@428 66 void **next = CX_LL_PTR(last, loc_next);
universe@428 67 *next = new_node;
universe@398 68 }
universe@398 69
universe@428 70 // if there is an end pointer, update it
universe@408 71 if (end != NULL) {
universe@408 72 *end = cx_linked_list_last(&new_node, NULL, loc_next);
universe@408 73 }
universe@428 74
universe@428 75 // if the nodes use a prev pointer, update it
universe@398 76 if (loc_prev >= 0) {
universe@406 77 void **prev = CX_LL_PTR(new_node, loc_prev);
universe@398 78 *prev = last;
universe@398 79 }
universe@398 80
universe@398 81 return 0;
universe@398 82 }
universe@398 83
universe@398 84 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
universe@398 85
universe@402 86 struct cx_linked_list_node {
universe@402 87 void *prev;
universe@402 88 void *next;
universe@403 89 char payload[];
universe@402 90 };
universe@402 91
universe@398 92 typedef struct {
universe@435 93 cx_list_s base;
universe@401 94 void *begin;
universe@401 95 void *end;
universe@406 96 ptrdiff_t loc_prev;
universe@406 97 ptrdiff_t loc_next;
universe@398 98 } cx_linked_list;
universe@398 99
universe@435 100 int cx_ll_add(cx_list_s *list, void *elem) {
universe@435 101 cx_linked_list *linkedlist = (cx_linked_list *) list;
universe@398 102
universe@435 103 struct cx_linked_list_node *node = cxMalloc(list->allocator,
universe@406 104 sizeof(struct cx_linked_list_node) + list->itemsize);
universe@398 105 if (node == NULL)
universe@398 106 return 1;
universe@398 107
universe@435 108 node->next = node->prev = NULL;
universe@406 109 memcpy(node->payload, elem, list->itemsize);
universe@399 110
universe@398 111 int ret = cx_linked_list_add(
universe@435 112 &linkedlist->begin,
universe@435 113 &linkedlist->end,
universe@402 114 offsetof(struct cx_linked_list_node, prev),
universe@402 115 offsetof(struct cx_linked_list_node, next),
universe@398 116 node
universe@398 117 );
universe@398 118 if (ret == 0) {
universe@401 119 list->size++;
universe@398 120 return 0;
universe@398 121 } else {
universe@398 122 return ret;
universe@398 123 }
universe@398 124 }
universe@398 125
universe@435 126 int cx_ll_insert(cx_list_s *list, size_t index, void *elem) {
universe@435 127 cx_linked_list *linkedList = (cx_linked_list *) list;
universe@398 128 // TODO: implement using low level API
universe@398 129 return 1;
universe@398 130 }
universe@398 131
universe@435 132 void *cx_ll_remove(cx_list_s *list, size_t index) {
universe@435 133 cx_linked_list *linkedList = (cx_linked_list *) list;
universe@398 134 // TODO: implement using low level API
universe@398 135 return NULL;
universe@398 136 }
universe@398 137
universe@435 138 size_t cx_ll_find(cx_list_s *list, void *elem) {
universe@435 139 cx_linked_list *linkedList = (cx_linked_list *) list;
universe@398 140 // TODO: implement using low level API
universe@398 141 return 0;
universe@398 142 }
universe@398 143
universe@435 144 void *cx_ll_last(cx_list_s *list) {
universe@435 145 cx_linked_list *linkedList = (cx_linked_list *) list;
universe@404 146 struct cx_linked_list_node *last = cx_linked_list_last(
universe@435 147 NULL, &linkedList->end, offsetof(struct cx_linked_list_node, next));
universe@404 148 return &last->payload;
universe@404 149 }
universe@398 150
universe@398 151 cx_list_class cx_linked_list_class = {
universe@398 152 cx_ll_add,
universe@398 153 cx_ll_insert,
universe@398 154 cx_ll_remove,
universe@404 155 cx_ll_find,
universe@404 156 cx_ll_last
universe@398 157 };
universe@398 158
universe@406 159 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
universe@435 160 cx_linked_list* list = cxMalloc(allocator, sizeof(cx_linked_list));
universe@398 161 if (list == NULL)
universe@398 162 return NULL;
universe@398 163
universe@435 164 list->base.cl = &cx_linked_list_class;
universe@435 165 list->base.allocator = allocator;
universe@435 166 list->base.cmpfunc = comparator;
universe@435 167 list->base.itemsize = item_size;
universe@435 168 list->base.capacity = SIZE_MAX;
universe@406 169
universe@435 170 list->begin = NULL;
universe@435 171 list->loc_prev = offsetof(struct cx_linked_list_node, prev);
universe@435 172 list->loc_next = offsetof(struct cx_linked_list_node, next);
universe@406 173
universe@435 174 CxList result = (CxList) list;
universe@435 175 cxLinkedListRecalculateSize(result);
universe@435 176 return result;
universe@406 177 }
universe@406 178
universe@409 179 void cxLinkedListDestroy(CxList list) {
universe@436 180 cx_linked_list* ll = (cx_linked_list*) list;
universe@436 181
universe@436 182 struct cx_linked_list_node* node = ll->begin;
universe@436 183 while (node) {
universe@436 184 void* next = node->next;
universe@436 185 cxFree(list->allocator, node);
universe@436 186 node = next;
universe@436 187 }
universe@436 188
universe@435 189 cxFree(list->allocator, list);
universe@409 190 }
universe@409 191
universe@406 192 size_t cxLinkedListRecalculateSize(CxList list) {
universe@435 193 cx_linked_list *ll = (cx_linked_list *) list;
universe@406 194
universe@406 195 if (ll->begin == NULL) {
universe@435 196 ll->base.size = 0;
universe@407 197 ll->end = NULL;
universe@406 198 return 0;
universe@406 199 } else {
universe@406 200 void *cur = ll->begin;
universe@407 201 void *last;
universe@435 202 size_t size = 0;
universe@406 203 do {
universe@407 204 last = cur;
universe@406 205 size++;
universe@406 206 } while ((cur = *CX_LL_PTR(cur, ll->loc_next)) != NULL);
universe@407 207 ll->end = last;
universe@435 208 ll->base.size = size;
universe@406 209 return size;
universe@398 210 }
universe@406 211 }

mercurial