102e08b15SFrançois Revol/*
202e08b15SFrançois Revol * Copyright 2004-2008, Fran��ois Revol, <revol@free.fr>.
302e08b15SFrançois Revol * Distributed under the terms of the MIT License.
402e08b15SFrançois Revol */
502e08b15SFrançois Revol
6d256fad4SFrançois Revol#include <OS.h>
7d256fad4SFrançois Revol#include "lists.h"
8d256fad4SFrançois Revol
9d256fad4SFrançois Revolvoid slist_init(struct _slist_entry *item)
10d256fad4SFrançois Revol{
11d256fad4SFrançois Revol	item->next = NULL;
12d256fad4SFrançois Revol}
13d256fad4SFrançois Revol
14d256fad4SFrançois Revolvoid slist_uninit(struct _slist_entry *item)
15d256fad4SFrançois Revol{
16d256fad4SFrançois Revol	item->next = NULL;
17d256fad4SFrançois Revol}
18d256fad4SFrançois Revol
19d256fad4SFrançois Revolstruct _slist_entry *slist_find(struct _slist_entry *head, slist_compare_func func, void *id)
20d256fad4SFrançois Revol{
21d256fad4SFrançois Revol	struct _slist_entry *p = head;
22d256fad4SFrançois Revol	if (head == NULL)
23d256fad4SFrançois Revol		return NULL;
24d256fad4SFrançois Revol	while (p) {
25d256fad4SFrançois Revol		if (func(p, id) == 0)
26d256fad4SFrançois Revol			return p;
27d256fad4SFrançois Revol		p = p->next;
28d256fad4SFrançois Revol	}
29d256fad4SFrançois Revol	return NULL;
30d256fad4SFrançois Revol}
31d256fad4SFrançois Revol
32d256fad4SFrançois Revolstatus_t slist_insert_head(struct _slist_entry **head, struct _slist_entry *item)
33d256fad4SFrançois Revol{
34d256fad4SFrançois Revol	struct _slist_entry *next = NULL;
35d256fad4SFrançois Revol	if (head == NULL || item == NULL)
36d256fad4SFrançois Revol		return EINVAL;
37d256fad4SFrançois Revol	if (*head)
38d256fad4SFrançois Revol		next = *head;
39d256fad4SFrançois Revol	item->next = next;
40d256fad4SFrançois Revol	*head = item;
41d256fad4SFrançois Revol	return B_OK;
42d256fad4SFrançois Revol}
43d256fad4SFrançois Revol
44d256fad4SFrançois Revolstruct _slist_entry *slist_dequeue_tail(struct _slist_entry **head)
45d256fad4SFrançois Revol{
46d256fad4SFrançois Revol	struct _slist_entry **prev = NULL;
47d256fad4SFrançois Revol	struct _slist_entry *curr = NULL;
48d256fad4SFrançois Revol	if (head == NULL || *head == NULL)
49d256fad4SFrançois Revol		return NULL;
50d256fad4SFrançois Revol	prev = head;
51d256fad4SFrançois Revol	curr = *head;
52d256fad4SFrançois Revol	while (curr->next) {
53d256fad4SFrançois Revol		prev = &(curr->next);
54d256fad4SFrançois Revol		curr = curr->next;
55d256fad4SFrançois Revol	}
56d256fad4SFrançois Revol	*prev = NULL;
57d256fad4SFrançois Revol	return curr;
58d256fad4SFrançois Revol}
59d256fad4SFrançois Revol
60d256fad4SFrançois Revolstatus_t slist_remove(struct _slist_entry **head, struct _slist_entry *item)
61d256fad4SFrançois Revol{
62d256fad4SFrançois Revol	struct _slist_entry **prev = NULL;
63d256fad4SFrançois Revol	struct _slist_entry *curr = NULL;
64d256fad4SFrançois Revol	if (head == NULL || *head == NULL || item == NULL)
65d256fad4SFrançois Revol		return EINVAL;
66d256fad4SFrançois Revol	prev = head;
67d256fad4SFrançois Revol	curr = *head;
68d256fad4SFrançois Revol	while (prev && curr) {
69d256fad4SFrançois Revol		if (curr == item) {
70d256fad4SFrançois Revol			*prev = curr->next;
71d256fad4SFrançois Revol			curr->next = NULL;
72d256fad4SFrançois Revol			return B_OK;
73d256fad4SFrançois Revol		}
74d256fad4SFrançois Revol		prev = &(curr->next);
75d256fad4SFrançois Revol		curr = curr->next;
76d256fad4SFrançois Revol	}
77d256fad4SFrançois Revol	return ENOENT;
78d256fad4SFrançois Revol}
79d256fad4SFrançois Revol
80d256fad4SFrançois Revolstruct _slist_entry *slist_next(struct _slist_entry *item)
81d256fad4SFrançois Revol{
82d256fad4SFrançois Revol	if (!item || !item->next)
83d256fad4SFrançois Revol		return NULL;
84d256fad4SFrançois Revol	return item->next;
85d256fad4SFrançois Revol}