Sun, 07 Feb 2021 20:08:13 +0100
changes off_t to ptrdiff_t
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 #include "cx/linked_list.h"
31 /* LOW LEVEL LINKED LIST FUNCTIONS */
33 #define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off))
35 void *cx_linked_list_last(void **begin, void **end, ptrdiff_t loc_next) {
36 if (end != NULL) {
37 return *end;
38 } else {
39 if (begin == NULL || *begin == NULL)
40 return NULL;
42 void *cur = *begin;
43 void *last;
44 do {
45 last = cur;
46 } while ((cur = *CX_LL_PTR(cur, loc_next)) != NULL);
48 return last;
49 }
50 }
52 int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_next, ptrdiff_t loc_prev, void *newnode) {
53 // TODO: how do we report error messages?
54 if (loc_next < 0 || (begin == NULL && end == NULL)) {
55 return 1;
56 }
58 void *last = cx_linked_list_last(begin, end, loc_next);
59 if (last == NULL) {
60 if (begin == NULL) {
61 return 1;
62 } else {
63 *begin = newnode;
64 return 0;
65 }
66 }
68 void **next = CX_LL_PTR(last, loc_next);
69 *next = newnode;
70 if (loc_prev >= 0) {
71 void **prev = CX_LL_PTR(newnode, loc_prev);
72 *prev = last;
73 }
75 return 0;
76 }
78 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
80 typedef struct cx_list_node_s cx_list_node;
82 struct cx_list_node_s {
83 cx_list_node *next;
84 cx_list_node *prev;
85 void *data;
86 };
88 typedef struct {
89 cx_list_node *begin;
90 cx_list_node *end;
91 size_t size;
92 } cx_linked_list;
94 int cx_ll_add(cx_list *list, void *elem) {
95 cx_linked_list *listdata = list->listdata;
96 CxAllocator allocator = list->allocator;
98 struct cx_list_node_s *node = cxMalloc(allocator, sizeof(struct cx_list_node_s));
99 if (node == NULL)
100 return 1;
102 node->next = NULL;
103 node->data = elem;
105 int ret = cx_linked_list_add(
106 (void **) &listdata->begin,
107 (void **) &listdata->end,
108 offsetof(struct cx_list_node_s, next),
109 offsetof(struct cx_list_node_s, prev),
110 node
111 );
112 if (ret == 0) {
113 listdata->size++;
114 return 0;
115 } else {
116 return ret;
117 }
118 }
120 int cx_ll_insert(cx_list *list, size_t index, void *elem) {
121 cx_linked_list *listdata = list->listdata;
122 // TODO: implement using low level API
123 return 1;
124 }
126 void *cx_ll_remove(cx_list *list, size_t index) {
127 cx_linked_list *listdata = list->listdata;
128 // TODO: implement using low level API
129 return NULL;
130 }
132 size_t cx_ll_find(cx_list *list, void *elem) {
133 cx_linked_list *listdata = list->listdata;
134 // TODO: implement using low level API
135 return 0;
136 }
138 size_t cx_ll_size(cx_list *list) {
139 cx_linked_list *listdata = list->listdata;
140 return listdata->size;
141 }
143 cx_list_class cx_linked_list_class = {
144 cx_ll_add,
145 cx_ll_insert,
146 cx_ll_remove,
147 cx_ll_find,
148 cx_ll_size
149 };
151 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator) {
152 CxList list = cxMalloc(allocator, sizeof(list));
153 if (list == NULL)
154 return NULL;
156 list->cl = &cx_linked_list_class;
157 list->data.allocator = allocator;
158 list->data.cmpfunc = comparator;
159 list->data.listdata = cxCalloc(allocator, 1, sizeof(cx_linked_list));
160 if (list->data.listdata == NULL) {
161 cxFree(allocator, list);
162 return NULL;
163 }
164 }