Contiki 2.5
Files | Macros | Typedefs | Functions
Linked list library

The linked list library provides a set of functions for manipulating linked lists. More...

Files

file  list.c
 Linked list library implementation.
 
file  list.h
 Linked list manipulation routines.
 

Macros

#define LIST(name)
 Declare a linked list. More...
 
#define LIST_STRUCT(name)
 Declare a linked list inside a structure declaraction. More...
 
#define LIST_STRUCT_INIT(struct_ptr, name)
 Initialize a linked list that is part of a structure. More...
 

Typedefs

typedef void ** list_t
 The linked list type.
 

Functions

void list_init (list_t list)
 Initialize a list. More...
 
void * list_head (list_t list)
 Get a pointer to the first element of a list. More...
 
void list_copy (list_t dest, list_t src)
 Duplicate a list. More...
 
void * list_tail (list_t list)
 Get the tail of a list. More...
 
void list_add (list_t list, void *item)
 Add an item at the end of a list. More...
 
void list_push (list_t list, void *item)
 Add an item to the start of the list.
 
void * list_chop (list_t list)
 Remove the last object on the list. More...
 
void * list_pop (list_t list)
 Remove the first object on a list. More...
 
void list_remove (list_t list, void *item)
 Remove a specific element from a list. More...
 
int list_length (list_t list)
 Get the length of a list. More...
 
void list_insert (list_t list, void *previtem, void *newitem)
 Insert an item after a specified item on the list. More...
 
void * list_item_next (void *item)
 Get the next item following this item. More...
 

Detailed Description

The linked list library provides a set of functions for manipulating linked lists.

A linked list is made up of elements where the first element must be a pointer. This pointer is used by the linked list library to form lists of the elements.

Lists are declared with the LIST() macro. The declaration specifies the name of the list that later is used with all list functions.

Lists can be manipulated by inserting or removing elements from either sides of the list (list_push(), list_add(), list_pop(), list_chop()). A specified element can also be removed from inside a list with list_remove(). The head and tail of a list can be extracted using list_head() and list_tail(), respectively.

Macro Definition Documentation

#define LIST (   name)
Value:
static void *LIST_CONCAT(name,_list) = NULL; \
static list_t name = (list_t)&LIST_CONCAT(name,_list)

Declare a linked list.

This macro declares a linked list with the specified type. The type must be a structure (struct) with its first element being a pointer. This pointer is used by the linked list library to form the linked lists.

The list variable is declared as static to make it easy to use in a single C module without unnecessarily exporting the name to other modules.

Parameters
nameThe name of the list.
Examples:
example-list.c, example-multihop.c, and example-runicast.c.

Definition at line 89 of file list.h.

#define LIST_STRUCT (   name)
Value:
void *LIST_CONCAT(name,_list); \
list_t name

Declare a linked list inside a structure declaraction.

This macro declares a linked list with the specified type. The type must be a structure (struct) with its first element being a pointer. This pointer is used by the linked list library to form the linked lists.

Internally, the list is defined as two items: the list itself and a pointer to the list. The pointer has the name of the parameter to the macro and the name of the list is a concatenation of the name and the suffix "_list". The pointer must point to the list for the list to work. Thus the list must be initialized before using.

The list is initialized with the LIST_STRUCT_INIT() macro.

Parameters
nameThe name of the list.

Definition at line 111 of file list.h.

#define LIST_STRUCT_INIT (   struct_ptr,
  name 
)
Value:
do { \
(struct_ptr)->name = &((struct_ptr)->LIST_CONCAT(name,_list)); \
(struct_ptr)->LIST_CONCAT(name,_list) = NULL; \
list_init((struct_ptr)->name); \
} while(0)

Initialize a linked list that is part of a structure.

This macro sets up the internal pointers in a list that has been defined as part of a struct. This macro must be called before using the list.

Parameters
struct_ptrA pointer to the struct
nameThe name of the list.

Definition at line 125 of file list.h.

Function Documentation

void list_add ( list_t  list,
void *  item 
)
void * list_chop ( list_t  list)

Remove the last object on the list.

This function removes the last object on the list and returns it.

Parameters
listThe list
Returns
The removed object
Examples:
example-runicast.c.

Definition at line 186 of file list.c.

References next, and NULL.

void list_copy ( list_t  dest,
list_t  src 
)

Duplicate a list.

This function duplicates a list by copying the list reference, but not the elements.

Note
This function does not copy the elements of the list, but merely duplicates the pointer to the first element of the list.
Parameters
destThe destination list.
srcThe source list.

Definition at line 101 of file list.c.

void * list_head ( list_t  list)

Get a pointer to the first element of a list.

This function returns a pointer to the first element of the list. The element will not be removed from the list.

Parameters
listThe list.
Returns
A pointer to the first element on the list.
See Also
list_tail()
Examples:
example-list.c, example-multihop.c, and example-runicast.c.

Definition at line 83 of file list.c.

Referenced by announcement_heard(), announcement_list(), convergence_layer_is_blocked(), ctimer_expired(), delivery_deliver_bundle(), delivery_unblock_service(), discovery_basic_is_neighbour(), discovery_basic_list_neighbours(), discovery_basic_neighbour_found(), discovery_basic_refresh_neighbour(), discovery_basic_save_neighbour(), discovery_ipnd_delete_neighbour(), discovery_ipnd_is_neighbour(), discovery_ipnd_list_neighbours(), discovery_ipnd_refresh_neighbour(), packetqueue_dequeue(), packetqueue_first(), PROCESS_THREAD(), registration_get_application_id(), registration_get_process(), registration_is_local(), registration_new_application(), registration_remove_application(), registration_return_status(), registration_set_active(), registration_set_passive(), routing_epidemic_bundle_delivered_locally(), routing_epidemic_bundle_sent(), routing_epidemic_delete_bundle(), routing_epidemic_forward(), routing_epidemic_get_neighbour(), routing_epidemic_new_bundle(), routing_epidemic_send_to_known_neighbours(), routing_flooding_blacklist_add(), routing_flooding_blacklist_delete(), routing_flooding_bundle_delivered_locally(), routing_flooding_bundle_sent(), routing_flooding_check_keep_bundle(), routing_flooding_delete_bundle(), routing_flooding_new_bundle(), routing_flooding_send_to_known_neighbours(), storage_coffee_delete_bundle(), storage_coffee_get_bundles(), storage_coffee_lock_bundle(), storage_coffee_make_room(), storage_coffee_prune(), storage_coffee_read_bundle(), storage_coffee_reconstruct_bundles(), storage_coffee_reinit(), storage_coffee_save_bundle(), storage_coffee_unlock_bundle(), storage_mmem_delete_bundle(), storage_mmem_get_bundles(), storage_mmem_lock_bundle(), storage_mmem_make_room(), storage_mmem_prune(), storage_mmem_read_bundle(), storage_mmem_reinit(), storage_mmem_save_bundle(), and storage_mmem_unlock_bundle().

void list_init ( list_t  list)

Initialize a list.

This function initalizes a list. The list will be empty after this function has been called.

Parameters
listThe list to be initialized.
Examples:
example-list.c, example-multihop.c, and example-runicast.c.

Definition at line 66 of file list.c.

References NULL.

Referenced by announcement_init(), ctimer_init(), discovery_basic_init(), discovery_ipnd_init(), mmem_init(), packetqueue_init(), PROCESS_THREAD(), registration_init(), storage_coffee_init(), and storage_mmem_init().

void list_insert ( list_t  list,
void *  previtem,
void *  newitem 
)

Insert an item after a specified item on the list.

Parameters
listThe list
previtemThe item after which the new item should be inserted
newitemThe new item that is to be inserted
Author
Adam Dunkels
        This function inserts an item right after a specified
        item on the list. This function is useful when using
        the list module to ordered lists.

        If previtem is NULL, the new item is placed at the
        start of the list.

Definition at line 303 of file list.c.

References list_push(), and NULL.

Referenced by mmem_reparent().

void * list_item_next ( void *  item)

Get the next item following this item.

Parameters
itemA list item
Returns
A next item on the list
        This function takes a list item and returns the next
        item on the list, or NULL if there are no more items on
        the list. This function is used when iterating through
        lists.
Examples:
example-list.c.

Definition at line 325 of file list.c.

References NULL.

Referenced by announcement_heard(), convergence_layer_is_blocked(), delivery_deliver_bundle(), delivery_unblock_service(), registration_get_application_id(), registration_get_process(), registration_is_local(), registration_new_application(), registration_remove_application(), registration_return_status(), registration_set_active(), registration_set_passive(), routing_epidemic_bundle_delivered_locally(), routing_epidemic_bundle_sent(), routing_epidemic_delete_bundle(), routing_epidemic_forward(), routing_epidemic_get_neighbour(), routing_epidemic_new_bundle(), routing_epidemic_send_to_known_neighbours(), routing_flooding_blacklist_add(), routing_flooding_blacklist_delete(), routing_flooding_bundle_delivered_locally(), routing_flooding_bundle_sent(), routing_flooding_check_keep_bundle(), routing_flooding_delete_bundle(), routing_flooding_forward_directly(), routing_flooding_forward_normal(), routing_flooding_new_bundle(), routing_flooding_send_to_known_neighbours(), storage_coffee_delete_bundle(), storage_coffee_lock_bundle(), storage_coffee_make_room(), storage_coffee_prune(), storage_coffee_read_bundle(), storage_coffee_reconstruct_bundles(), storage_coffee_save_bundle(), storage_coffee_unlock_bundle(), storage_mmem_delete_bundle(), storage_mmem_lock_bundle(), storage_mmem_make_room(), storage_mmem_prune(), storage_mmem_read_bundle(), storage_mmem_reinit(), storage_mmem_save_bundle(), and storage_mmem_unlock_bundle().

int list_length ( list_t  list)

Get the length of a list.

This function counts the number of elements on a specified list.

Parameters
listThe list.
Returns
The length of the list.
Examples:
example-multihop.c.

Definition at line 275 of file list.c.

References NULL.

Referenced by packetqueue_len().

void * list_pop ( list_t  list)

Remove the first object on a list.

This function removes the first object on the list and returns a pointer to it.

Parameters
listThe list.
Returns
Pointer to the removed element of list.

Definition at line 218 of file list.c.

References NULL.

void list_remove ( list_t  list,
void *  item 
)
void * list_tail ( list_t  list)

Get the tail of a list.

This function returns a pointer to the elements following the first element of a list. No elements are removed by this function.

Parameters
listThe list
Returns
A pointer to the element after the first element on the list.
See Also
list_head()

Definition at line 118 of file list.c.

References NULL.

Referenced by list_add().