os4ec /Chapter_07_Threads/00_Start/lib/list.c

Language C Lines 214
MD5 Hash d33ce54ce3981e263f7c73d4dc1c1c89
Repository https://github.com/l30nard0/os4ec.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*! List manipulation functions
 *
 * Double linked lists are used
 * List header points to first and last list element
 */

#include "list.h"

#include <lib/types.h>
#include ASSERT_H

void list_init ( list_t *list )
{
	ASSERT ( list );

	list->first = list->last = NULL;
}

/*! Add element to list, add to tail - as last element */
void list_append ( list_t *list, void *object, list_h *hdr )
{
	ASSERT ( list && object && hdr );

	hdr->object = object; /* save reference to object */
	hdr->next = NULL; /* put it at list end (as last element) */

	if ( list->first )
	{
		list->last->next = hdr;
		hdr->prev = list->last;
		list->last = hdr;
	}
	else {
		list->first = list->last = hdr;
		hdr->prev = NULL;
	}
}

/*! Add element to list, add to head - as first element */
void list_prepend ( list_t *list, void *object, list_h *hdr )
{
	ASSERT ( list && object && hdr );

	hdr->object = object; /* save reference to object */
	hdr->prev = NULL; /* put it at list start (as first element) */

	hdr->next = list->first;

	if ( list->first )
		list->first->prev = hdr; /* list was not empty */
	else
		list->last = hdr; /* list was empty */

	list->first = hdr;
}

/*! Add element to sorted list */
void list_sort_add ( list_t *list, void *object, list_h *hdr,
				   int (*cmp) ( void *, void * ) )
{
	list_h *iter;

	ASSERT ( list && object && hdr && cmp );

	hdr->object = object; /* save reference to object */

	/* speedup search if elem is to be last in list */
	if ( list->last && cmp ( object, list->last->object ) >= 0 )
		iter = NULL; /* insert after last */
	else
		iter = list->first; /* start searching from beginning */

	while ( iter && cmp ( object, iter->object ) >= 0 )
		iter = iter->next;

	/* insert before 'iter' */
	hdr->next = iter;

	if ( iter )
	{
		hdr->prev = iter->prev;
		if ( iter->prev )
			iter->prev->next = hdr;
		else
			list->first = hdr; /* 'iter' was first in list */
		iter->prev = hdr;
	}
	else {
		/* 'iter' == NULL => add to the end of list */
		if ( list->last )
		{
			/* list was not empty */
			hdr->prev = list->last;
			list->last->next = hdr;
			list->last = hdr;
		}
		else {
			/* list was empty */
			list->first = list->last = hdr;
			hdr->prev = NULL;
		}
	}
}

/*! Get pointer to first or last list element */
void *list_get ( list_t *list, unsigned int flags )
{
	list_h *hdr;

	ASSERT ( list );

	if ( flags & LAST )
		hdr = list->last;
	else
		hdr = list->first;

	if ( hdr )
		return hdr->object;
	else
		return NULL;
}

/*! Get pointer to next object in list */
void *list_get_next ( list_h *hdr )
{
	if ( !hdr || !hdr->next )
		return NULL;
	else
		return hdr->next->object;
}

/*!
 * Remove element from list
 * \param list	List identifier (pointer)
 * \param flags	Constant: FIRST(0) or LAST(1) - which element to remove from
 *		list and return pointer to it
 * \param ref	Reference (pointer) to element to be removed from list
 * \return pointer to removed list element, NULL if list is empty
 * NOTE function assumes that element is in list - it doesn't check!!!
 */
void *list_remove ( list_t *list, unsigned int flags, list_h *ref )
{
	list_h *hdr;

	ASSERT ( list );

	if ( ref )
		hdr = ref;
	else if ( flags & LAST )
		hdr = list->last;
	else
		hdr = list->first;

	if ( hdr )
	{
		if ( hdr->prev )
			hdr->prev->next = hdr->next;

		if ( hdr->next )
			hdr->next->prev = hdr->prev;

		if ( list->first == hdr )
			list->first = hdr->next;

		if ( list->last == hdr )
			list->last = hdr->prev;

		return hdr->object;
	}
	else {
		return NULL;
	}
}

/*!
 * Remove element from list if element is in list
 * \param list	List identifier (pointer)
 * \param flags	Constant: FIRST(0) or LAST(1) - which element to remove from
 *		list and return pointer to it
 * \param ref	Reference (pointer) to element to be removed from list
 * \return pointer to removed list element, NULL if list is empty
 */
void *list_find_and_remove ( list_t *list, list_h *ref )
{
	list_h *iter;

	ASSERT ( list && ref );

	iter = list->first;
	while ( iter )
	{
		if ( iter == ref )
		{
			if ( ref->prev )
				ref->prev->next = ref->next;

			if ( ref->next )
				ref->next->prev = ref->prev;

			if ( list->first == ref )
				list->first = ref->next;

			if ( list->last == ref )
				list->last = ref->prev;

			return ref->object;
		}
		else {
			iter = iter->next;
		}
	}

	return NULL;
}
Back to Top