mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-23 00:00:01 +01:00
75 lines
2.5 KiB
C
75 lines
2.5 KiB
C
/** A generic linked list
|
|
*
|
|
* Linked lists a used for several data structures in the code.
|
|
*
|
|
* @file
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
* @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC
|
|
* This file is part of S2SS. All Rights Reserved. Proprietary and confidential.
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
|
*********************************************************************************/
|
|
|
|
#ifndef _LIST_H_
|
|
#define _LIST_H_
|
|
|
|
#include <pthread.h>
|
|
|
|
#define LIST_CHUNKSIZE 16
|
|
|
|
/** Static list initialization */
|
|
#define LIST_INIT(dtor) { \
|
|
.start = NULL, \
|
|
.end = NULL, \
|
|
.length = 0, \
|
|
.capacity = 0, \
|
|
.lock = PTHREAD_MUTEX_INITIALIZER, \
|
|
.destructor = dtor \
|
|
}
|
|
|
|
#define list_length(list) ((list)->length)
|
|
#define list_at(list, index) ((list)->length > index ? (list)->start[index] : NULL)
|
|
|
|
#define list_first(list) list_at(list, 0)
|
|
#define list_last(list) list_at(list, (list)->length-1)
|
|
#define list_foreach(ptr, list) for (int _i = 0, _p; _p = 1, _i < (list)->length; _i++) \
|
|
for (ptr = (list)->start[_i]; _p--; )
|
|
|
|
/** Callback to destroy list elements.
|
|
*
|
|
* @param data A pointer to the data which should be freed.
|
|
*/
|
|
typedef void (*dtor_cb_t)(void *);
|
|
|
|
/** Callback to search or sort a list. */
|
|
typedef int (*cmp_cb_t)(const void *, const void *);
|
|
|
|
struct list {
|
|
void **start; /**< Array of pointers to list elements */
|
|
void **end; /**< Array of pointers to list elements */
|
|
size_t capacity; /**< Size of list::start in elements */
|
|
size_t length; /**< Number of elements of list::start which are in use */
|
|
dtor_cb_t destructor; /**< A destructor which gets called for every list elements during list_destroy() */
|
|
pthread_mutex_t lock; /**< A mutex to allow thread-safe accesses */
|
|
};
|
|
|
|
/** Initialize a list */
|
|
void list_init(struct list *l, dtor_cb_t dtor);
|
|
|
|
/** Destroy a list and call destructors for all list elements */
|
|
void list_destroy(struct list *l);
|
|
|
|
/** Append an element to the end of the list */
|
|
void list_push(struct list *l, void *p);
|
|
|
|
/** Search the list for an element whose first element is a character array which matches name.
|
|
*
|
|
* @see Only possible because of §1424 of http://c0x.coding-guidelines.com/6.7.2.1.html
|
|
*/
|
|
void * list_lookup(struct list *l, const char *name);
|
|
|
|
void * list_search(struct list *l, cmp_cb_t cmp, void *ctx);
|
|
|
|
/** Sort the list using the quicksort algorithm of libc */
|
|
void list_sort(struct list *l, cmp_cb_t cmp);
|
|
|
|
#endif /* _LIST_H_ */
|