list.c (2963B)
1 /* Copyright 1998-1999, Brian J. Swetland. All rights reserved. 2 ** Distributed under the terms of the OpenBLT License 3 */ 4 5 #include "list.h" 6 #include "memory.h" 7 8 #include <blt/error.h> 9 10 void list_init(list_t *list) 11 { 12 list->next = (node_t *) list; 13 list->prev = (node_t *) list; 14 list->count = 0; 15 } 16 17 void list_add_head(list_t *list, void *data) 18 { 19 node_t *node = kmalloc(node_t); 20 node->data = data; 21 node->next = list->next; 22 node->next->prev = node; 23 node->prev = (node_t*) list; 24 list->next = node; 25 list->count++; 26 } 27 28 void list_add_tail(list_t *list, void *data) 29 { 30 node_t *node = kmalloc(node_t); 31 node->data = data; 32 node->prev = list->prev; 33 node->prev->next = node; 34 node->next = (node_t*) list; 35 list->prev = node; 36 list->count++; 37 } 38 39 void *list_peek_head(list_t *list) 40 { 41 if(list->next == (node_t*) list) { 42 return NULL; 43 } else { 44 return list->next->data; 45 } 46 } 47 48 void *list_peek_tail(list_t *list) 49 { 50 if(list->prev == (node_t*) list) { 51 return NULL; 52 } else { 53 return list->prev->data; 54 } 55 } 56 57 void *list_remove_head(list_t *list) 58 { 59 node_t *node = list->next; 60 void *data; 61 62 if(node == (node_t*) list){ 63 return NULL; 64 } else { 65 data = node->data; 66 node->prev->next = node->next; 67 node->next->prev = node->prev; 68 kfree(node_t, node); 69 list->count--; 70 return data; 71 } 72 } 73 74 void *list_remove_tail(list_t *list) 75 { 76 node_t *node = list->prev; 77 void *data; 78 79 if(node == (node_t*) list){ 80 return NULL; 81 } else { 82 data = node->data; 83 node->prev->next = node->next; 84 node->next->prev = node->prev; 85 kfree(node_t, node); 86 list->count--; 87 return data; 88 } 89 } 90 91 int list_remove(list_t *list, void *data) 92 { 93 node_t *node = list->next; 94 while(node != (node_t *) list){ 95 if(node->data == data){ 96 node->next->prev = node->prev; 97 node->prev->next = node->next; 98 kfree(node_t, node); 99 list->count--; 100 return ERR_NONE; 101 } 102 node = node->next; 103 } 104 return -1; 105 } 106 107 108 void list_attach_head(list_t *list, node_t *node) 109 { 110 node->next = list->next; 111 node->next->prev = node; 112 node->prev = (node_t*) list; 113 list->next = node; 114 list->count++; 115 } 116 117 void list_attach_tail(list_t *list, node_t *node) 118 { 119 node->prev = list->prev; 120 node->prev->next = node; 121 node->next = (node_t*) list; 122 list->prev = node; 123 list->count++; 124 } 125 126 void *list_detach_head(list_t *list) 127 { 128 node_t *node = list->next; 129 if(node == (node_t*) list){ 130 return NULL; 131 } else { 132 node->prev->next = node->next; 133 node->next->prev = node->prev; 134 list->count--; 135 return node->data; 136 } 137 } 138 139 void *list_detach_tail(list_t *list) 140 { 141 node_t *node = list->prev; 142 if(node == (node_t*) list){ 143 return NULL; 144 } else { 145 node->prev->next = node->next; 146 node->next->prev = node->prev; 147 list->count--; 148 return node->data; 149 } 150 } 151 152 int list_detach(list_t *list, void *data) 153 { 154 node_t *node = list->next; 155 while(node != (node_t *) list){ 156 if(node->data == data){ 157 node->next->prev = node->prev; 158 node->prev->next = node->next; 159 list->count--; 160 return ERR_NONE; 161 } 162 node = node->next; 163 } 164 return -1; 165 }