1 /* 2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited 3 */ 4 #pragma once 5 6 #ifndef __LIST_H__ 7 #define __LIST_H__ 8 9 #include <stdbool.h> 10 #include <stdlib.h> 11 12 #ifdef __cplusplus 13 extern "C" { 14 #endif 15 16 typedef void *(*list_mempool_zmalloc)(size_t); 17 typedef void (*list_mempool_free)(void *); 18 19 typedef struct { 20 list_mempool_zmalloc zmalloc; 21 list_mempool_free free; 22 } list_mempool_functions_t; 23 24 typedef void (*list_free_cb)(void *data); 25 typedef bool (*list_iter_cb)(void *data); 26 27 typedef struct list_node_t { 28 struct list_node_t *next; 29 void *data; 30 } list_node_t; 31 32 typedef struct list_t { 33 list_node_t *head; 34 list_node_t *tail; 35 size_t length; 36 list_free_cb free_cb; 37 list_mempool_functions_t mempool_functions; 38 } list_t; 39 40 struct list_node_t; 41 typedef struct list_node_t list_node_t; 42 //struct list_t; 43 typedef struct list_t list_t; 44 45 int list_init(void); 46 47 // Lifecycle. 48 list_t *list_new(list_free_cb callback, list_mempool_zmalloc zmalloc, list_mempool_free free); 49 void list_free(list_t *list); 50 51 // Accessors. 52 bool list_is_empty(const list_t *list); 53 size_t list_length(const list_t *list); 54 void *list_front(const list_t *list); 55 void *list_back(const list_t *list); 56 57 // Mutators. 58 bool list_insert_after(list_t *list, list_node_t *prev_node, void *data); 59 bool list_prepend(list_t *list, void *data); 60 bool list_append(list_t *list, void *data); 61 bool list_remove(list_t *list, void *data); 62 void list_clear(list_t *list); 63 64 // Iteration. 65 void list_foreach(const list_t *list, list_iter_cb callback); 66 67 list_node_t *list_begin(const list_t *list); 68 list_node_t *list_end(const list_t *list); 69 list_node_t *list_next(const list_node_t *node); 70 void *list_node(const list_node_t *node); 71 72 #ifdef __cplusplus 73 } 74 #endif 75 76 #endif//__FMDEC_H__ 77 78