ISIX-RTOS - small operating system for ARM microcontrollers 1.2

list.h

Go to the documentation of this file.
00001 #ifndef _ISIX_LIST_H
00002 #define _ISIX_LIST_H
00003 
00004 /* L. Bryndza list version 1.0  */
00005 
00006 #include <stddef.h>
00007 
00008 //List linkage structure
00009 typedef struct list
00010 {
00011     struct list *next;
00012     struct list *prev;
00013 } list_t;
00014 
00015 
00016 //List entry structure
00017 typedef struct list_entry
00018 {
00019    list_t head;
00020 } list_entry_t;
00021 
00022 
00023 //Initialize list
00024 static inline void list_init(list_entry_t *list)
00025 {
00026    list->head.next = &list->head;
00027    list->head.prev = &list->head;
00028 }
00029 
00030 //Insert beetween
00031 static inline void list_insert_between(list_t *a,list_t *b,list_t *elem)
00032 {
00033    b->prev = elem;
00034    a->next = elem;
00035    elem->prev = a;
00036    elem->next = b;
00037 }
00038 
00039 //Insert element before
00040 static inline void list_insert_before(list_t *before,list_t *elem)
00041 {
00042     list_insert_between(before->prev,before,elem);
00043 }
00044 
00045 
00046 //Insert element after
00047 static inline void list_insert_after(list_t *after,list_t *elem)
00048 {
00049     list_insert_between(after,after->next,elem);
00050 }
00051 
00052 // Add element at end of list
00053 static inline void list_insert_end(list_entry_t *list,list_t *elem)
00054 {
00055     list_insert_after(list->head.prev,elem);
00056 }
00057 
00058 
00059 // offsetof(TYPE,MEMBER) ?? <stddef.h>
00060 #define list_getitem(list,type,member) (type*)((char*)(list)-offsetof(type,member))
00061 
00062 //list for each element
00063 #define list_for_each(list_entryp,iterator) \
00064       for( (iterator)=(list_entryp)->head.next; \
00065            (iterator)!=&(list_entryp)->head;   \
00066            (iterator) = (iterator)->next \
00067          )
00068 //List for each element backward
00069 #define list_for_each_reverse(list_entryp,iterator) \
00070       for( (iterator)=(list_entryp)->head.prev; \
00071            (iterator)!=&(list_entryp)->head;   \
00072            (iterator) = (iterator)->prev \
00073          )
00074 
00075 //list for each with given type
00076 #define list_for_each_entry(list_entryp,iterator,field) \
00077   for( list_t *__ilist__=(list_entryp)->head.next; \
00078        (iterator) = list_getitem(__ilist__,typeof(*(iterator)),field),__ilist__!=&(list_entryp)->head;   \
00079            __ilist__ = __ilist__->next \
00080          )
00081 
00082 //list for each reverse order
00083 #define list_for_each_entry_reverse(list_entryp,iterator,field) \
00084   for( list_t *__ilist__=(list_entryp)->head.prev; \
00085        (iterator) = list_getitem(__ilist__,typeof(*(iterator)),field),__ilist__!=&(list_entryp)->head;   \
00086            __ilist__ = __ilist__->prev \
00087          )
00088 
00089 
00090 //Get first element of list
00091 #define list_get_first(list_entryp,field,type) \
00092     (type*)((char*)(list_entryp)->head.next - offsetof(type,field))
00093 
00094 
00095 //Get first element of list
00096 #define list_get_last(list_entryp,field,type) \
00097     (type*)((char*)(list_entryp)->head.prev - offsetof(type,field))
00098 
00099 //Delete from list
00100 static inline void list_delete(list_t *elem)
00101 {
00102     elem->prev->next = elem->next;
00103     elem->next->prev = elem->prev;
00104     elem->next = NULL;
00105     elem->prev = NULL;
00106 }
00107 
00108 
00109  //List is empty check
00110 #define list_isempty(list_entryp) ((list_entryp)->head.next==&(list_entryp)->head)
00111 
00112 //Check if the element is free
00113 static inline bool list_is_elem_assigned( list_t *elem )
00114 {
00115         return elem->next!=NULL && elem->prev!=NULL;
00116 }
00117 
00118 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines