1 /*
2 * Copyright 2004-2008, François Revol, <revol@free.fr>.
3 * Distributed under the terms of the MIT License.
4 */
5
6 #include <KernelExport.h>
7 #include <OS.h>
8 #include "lists2.h"
9
10 #include <stdio.h>
11
sll_find(long nextoff,void * head,sll_compare_func func,void * id)12 void *sll_find(long nextoff, void *head, sll_compare_func func, void *id)
13 {
14 void *p = head;
15 int count = 5000;
16 if (head == NULL)
17 return NULL;
18 while (p) {
19 if (func(p, id) == 0)
20 return p;
21 p = sll_next(nextoff, p);
22 if (!count--) {
23 fprintf(stderr, "sll_find: WARNING: 5000 nodes to search ??? looks more of a loop.\n");
24 return NULL;
25 }
26 }
27 return NULL;
28 }
29
sll_insert_head(long nextoff,void ** head,void * item)30 status_t sll_insert_head(long nextoff, void **head, void *item)
31 {
32 void *next = NULL;
33 if (head == NULL || item == NULL)
34 return EINVAL;
35 if (*head)
36 next = *head;
37 *(void **)(((char *)item)+nextoff) = next;
38 *head = item;
39 return B_OK;
40 }
41
sll_insert_tail(long nextoff,void ** head,void * item)42 status_t sll_insert_tail(long nextoff, void **head, void *item)
43 {
44 void *p;
45 if (head == NULL || item == NULL)
46 return EINVAL;
47
48 if (*(void **)(((char *)item)+nextoff)) {
49 fprintf(stderr, "sll_insert_tail: WARNING: %p->next NOT NULL\n", item);
50 *(void **)(((char *)item)+nextoff) = NULL;
51 }
52
53 p = *head;
54 if (!p) {
55 *head = item;
56 return B_OK;
57 }
58 while (sll_next(nextoff, p))
59 p = sll_next(nextoff, p);
60 *(void **)(((char *)p)+nextoff) = item;
61 return B_OK;
62 }
63
sll_dequeue_tail(long nextoff,void ** head)64 void *sll_dequeue_tail(long nextoff, void **head)
65 {
66 void **prev = NULL;
67 void *curr = NULL;
68 if (head == NULL || *head == NULL)
69 return NULL;
70 prev = head;
71 curr = *head;
72 while (sll_next(nextoff, curr)) {
73 prev = (void **)(((char *)curr)+nextoff);
74 curr = sll_next(nextoff, curr);
75 }
76 *prev = NULL;
77 return curr;
78 }
79
sll_remove(long nextoff,void ** head,void * item)80 status_t sll_remove(long nextoff, void **head, void *item)
81 {
82 void **prev = NULL;
83 void *curr = NULL;
84 if (head == NULL || *head == NULL || item == NULL)
85 return EINVAL;
86 prev = head;
87 curr = *head;
88 while (prev && curr) {
89 if (curr == item) {
90 *prev = sll_next(nextoff, curr);
91 *(void **)(((char *)item)+nextoff) = NULL;
92 return B_OK;
93 }
94 prev = (void **)(((char *)curr)+nextoff);
95 curr = sll_next(nextoff, curr);
96 }
97 return ENOENT;
98 }
99
sll_next(long nextoff,void * item)100 void *sll_next(long nextoff, void *item)
101 {
102 void *next;
103 if (!item)
104 return NULL;
105 next = *(void **)(((char *)item)+nextoff);
106 return next;
107 }
108