Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
LISTS: generic type list

Data Structures

struct  LIST
 Structure of a generic LIST container. More...
 
struct  LIST_NODE
 Structure of a generic list node. More...
 

Macros

#define link_node(__NODE_1, __NODE_2)
 Macro helper for linking two nodes, with NULL safety.
 
#define list_foreach(__ITEM_, __LIST_)
 ForEach macro helper, safe for node removal during iteration.
 
#define list_pop(__LIST_, __TYPE_)   (__TYPE_*)list_pop_f(__LIST_)
 Pop macro helper for void pointer casting.
 
#define list_shift(__LIST_, __TYPE_)   (__TYPE_*)list_shift_f(__LIST_, __FILE__, __LINE__)
 Shift macro helper for void pointer casting.
 
#define MAX_LIST_ITEMS   SIZE_MAX
 flag to pass to new_generic_list for the maximum possible number of item in a list
 
#define remove_list_node(__LIST_, __NODE_, __TYPE_)   (__TYPE_*)remove_list_node_f(__LIST_, __NODE_)
 Remove macro helper for void pointer casting.
 
#define UNLIMITED_LIST_ITEMS   0
 flag to pass to new_generic_list for an unlimited number of item in the list.
 

Functions

int list_destroy (LIST **list)
 free the list
 
int list_empty (LIST *list)
 empty the list
 
int list_empty_with_f (LIST *list, void(*free_fnct)(void *ptr))
 empty the list with a custom free function
 
LIST_NODElist_node_pop (LIST *list)
 pop a node from the end of the list
 
int list_node_push (LIST *list, LIST_NODE *node)
 push a node at the end of the list
 
LIST_NODElist_node_shift (LIST *list)
 shift a node from the beginning of the list
 
int list_node_unshift (LIST *list, LIST_NODE *node)
 unshift a node at the beginning of the list
 
void * list_pop_f (LIST *list)
 get last ptr from list
 
int list_push (LIST *list, void *ptr, void(*destructor)(void *ptr))
 add a pointer at the end of the list
 
int list_push_sorted (LIST *list, void *ptr, int(*comparator)(const void *a, const void *b), void(*destructor)(void *ptr))
 add a pointer sorted via comparator from the end of the list
 
LIST_NODElist_search (LIST *list, const void *ptr)
 search ptr in list
 
LIST_NODElist_search_with_f (LIST *list, int(*checkfunk)(void *ptr))
 search for data in list
 
void * list_shift_f (LIST *list, char *file, size_t line)
 get first ptr from list
 
int list_unshift (LIST *list, void *ptr, void(*destructor)(void *ptr))
 put a pointer at the beginning of list
 
int list_unshift_sorted (LIST *list, void *ptr, int(*comparator)(const void *a, const void *b), void(*destructor)(void *ptr))
 put a pointer sorted via comparator from the start to the end
 
LISTnew_generic_list (size_t max_items)
 initialize a list
 
LIST_NODEnew_list_node (void *ptr, void(*destructor)(void *ptr))
 create a new node
 
void * remove_list_node_f (LIST *list, LIST_NODE *node)
 remove a node
 

Detailed Description


Data Structure Documentation

◆ LIST

struct LIST
+ Collaboration diagram for LIST:
Data Fields
LIST_NODE * end pointer to the end of the list
size_t nb_items number of item currently in the list
size_t nb_max_items Maximum number of items in the list.

0 means unlimited

LIST_NODE * start pointer to the start of the list

◆ LIST_NODE

struct LIST_NODE

Structure of a generic list node.

Examples
ex_git.c, and ex_list.c.

Definition at line 43 of file n_list.h.

+ Collaboration diagram for LIST_NODE:

Data Fields

void(* destroy_func )(void *ptr)
 pointer to destructor function if any, else NULL
 
struct LIST_NODEnext
 pointer to the next node
 
struct LIST_NODEprev
 pointer to the previous node
 
void * ptr
 void pointer to store
 

Field Documentation

◆ destroy_func

void(* LIST_NODE::destroy_func) (void *ptr)

pointer to destructor function if any, else NULL

Examples
ex_list.c.

Definition at line 48 of file n_list.h.

Referenced by _sort_windows_by_zorder(), list_empty(), main(), n_gui_lower_window(), n_gui_raise_window(), new_list_node(), and tree_insert_child().

◆ next

◆ prev

◆ ptr

Macro Definition Documentation

◆ link_node

#define link_node (   __NODE_1,
  __NODE_2 
)
Value:
do { \
if (!(__NODE_1) || !(__NODE_2)) { \
n_log(LOG_ERR, "link_node: NULL argument (%s=%p, %s=%p)", #__NODE_1, (void*)(__NODE_1), #__NODE_2, (void*)(__NODE_2)); \
} else { \
(__NODE_2)->prev = (__NODE_1); \
(__NODE_1)->next = (__NODE_2); \
} \
} while (0)
#define LOG_ERR
error conditions
Definition n_log.h:75

Macro helper for linking two nodes, with NULL safety.

Definition at line 77 of file n_list.h.

◆ list_foreach

#define list_foreach (   __ITEM_,
  __LIST_ 
)
Value:
for (LIST_NODE* __ITEM_ = (__LIST_) ? (__LIST_)->start : NULL, *__next_##__ITEM_ = __ITEM_ ? __ITEM_->next : NULL; __ITEM_; __ITEM_ = __next_##__ITEM_, \
__next_##__ITEM_ = __ITEM_ ? __ITEM_->next : NULL)
Structure of a generic list node.
Definition n_list.h:43

ForEach macro helper, safe for node removal during iteration.

Examples
ex_file.c, ex_gui_dictionary.c, ex_gui_network.c, ex_hash.c, ex_kafka.c, ex_list.c, ex_network_ssl.c, and ex_network_ssl_hardened.c.

Definition at line 88 of file n_list.h.

◆ list_pop

#define list_pop (   __LIST_,
  __TYPE_ 
)    (__TYPE_*)list_pop_f(__LIST_)

Pop macro helper for void pointer casting.

Examples
ex_list.c.

Definition at line 93 of file n_list.h.

◆ list_shift

#define list_shift (   __LIST_,
  __TYPE_ 
)    (__TYPE_*)list_shift_f(__LIST_, __FILE__, __LINE__)

Shift macro helper for void pointer casting.

Examples
ex_list.c.

Definition at line 95 of file n_list.h.

◆ MAX_LIST_ITEMS

#define MAX_LIST_ITEMS   SIZE_MAX

flag to pass to new_generic_list for the maximum possible number of item in a list

Examples
ex_gui_dictionary.c, ex_gui_network.c, ex_gui_particles.c, ex_network_ssl.c, and ex_network_ssl_hardened.c.

Definition at line 74 of file n_list.h.

◆ remove_list_node

#define remove_list_node (   __LIST_,
  __NODE_,
  __TYPE_ 
)    (__TYPE_*)remove_list_node_f(__LIST_, __NODE_)

Remove macro helper for void pointer casting.

Examples
ex_list.c.

Definition at line 97 of file n_list.h.

◆ UNLIMITED_LIST_ITEMS

#define UNLIMITED_LIST_ITEMS   0

flag to pass to new_generic_list for an unlimited number of item in the list.

Can be dangerous !

Examples
ex_gui_network.c, and ex_list.c.

Definition at line 72 of file n_list.h.

Function Documentation

◆ list_destroy()

int list_destroy ( LIST **  list)

free the list

free the list

Call the destructor each time.

Parameters
listThe list to destroy. An already empty or null list will cause an error and a _log message.
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 547 of file n_list.c.

References __n_assert, Free, list_empty(), LOG_ERR, and n_log.

Referenced by _destroy_ht(), _destroy_window(), _ht_search(), _ht_search_trie(), _n_mock_request_clean(), create_msg(), delete_msg(), destroy_config_file(), destroy_n_fluid(), destroy_threaded_pool(), free_entry(), free_particle_system(), ht_get_completion_list(), ht_resize(), load_config_file(), main(), main(), main(), n_gui_destroy_ctx(), n_gui_new_ctx(), n_kafka_delete(), n_kafka_event_destroy_ptr(), netw_close(), netw_set(), new_ht(), new_thread_pool(), tree_delete_node(), update_completion(), and userlist_destroy().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ list_empty()

int list_empty ( LIST list)

empty the list

empty the list

Parameters
listThe list to empty. An already empty or null list will cause an error and a _log message.
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 500 of file n_list.c.

References __n_assert, LIST_NODE::destroy_func, end, Free, LOG_ERR, n_log, nb_items, LIST_NODE::next, LIST_NODE::ptr, and start.

Referenced by empty_msg(), list_destroy(), main(), netw_set(), userlist_del_user(), userlist_send_waiting_msgs(), and userlist_user_send_waiting_msgs().

+ Here is the caller graph for this function:

◆ list_empty_with_f()

int list_empty_with_f ( LIST list,
void(*)(void *ptr)  free_fnct 
)

empty the list with a custom free function

empty the list with a custom free function

Parameters
listThe list to empty. An already empty or null list will cause an error and a _log message.
free_fncta function ptr to a void func( void *ptr )
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 525 of file n_list.c.

References __n_assert, end, Free, LOG_ERR, n_log, nb_items, LIST_NODE::next, LIST_NODE::ptr, and start.

◆ list_node_pop()

LIST_NODE * list_node_pop ( LIST list)

pop a node from the end of the list

pop a node from the end of the list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
Returns
A LIST_NODE *node or NULL. Check error messages in DEBUG mode.

Definition at line 142 of file n_list.c.

References __n_assert, end, LOG_ERR, n_log, nb_items, LIST_NODE::next, LIST_NODE::prev, and start.

Referenced by main().

+ Here is the caller graph for this function:

◆ list_node_push()

int list_node_push ( LIST list,
LIST_NODE node 
)

push a node at the end of the list

push a node at the end of the list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
nodeThe node pointer you want to put in the list. A null value will cause an error and a _log message.
Returns
TRUE or FALSE

Definition at line 116 of file n_list.c.

References __n_assert, end, LOG_ERR, n_log, nb_items, nb_max_items, LIST_NODE::next, LIST_NODE::prev, and start.

Referenced by ht_resize(), main(), and tree_insert_child().

+ Here is the caller graph for this function:

◆ list_node_shift()

LIST_NODE * list_node_shift ( LIST list)

shift a node from the beginning of the list

shift a node from the beginning of the list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
Returns
A LIST_NODE *node or NULL. Check error messages in DEBUG mode.

Definition at line 169 of file n_list.c.

References __n_assert, end, LOG_ERR, n_log, nb_items, LIST_NODE::next, LIST_NODE::prev, and start.

Referenced by ht_resize(), and main().

+ Here is the caller graph for this function:

◆ list_node_unshift()

int list_node_unshift ( LIST list,
LIST_NODE node 
)

unshift a node at the beginning of the list

unshift a node at the beginning of the list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
nodeThe node pointer you wan to put in the list. A null value will cause an error and a _log message.
Returns
TRUE or FALSE

Definition at line 199 of file n_list.c.

References __n_assert, end, link_node, LOG_ERR, n_log, nb_items, nb_max_items, LIST_NODE::next, LIST_NODE::prev, and start.

Referenced by main().

+ Here is the caller graph for this function:

◆ list_pop_f()

void * list_pop_f ( LIST list)

get last ptr from list

get last ptr from list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
Returns
A void *ptr or NULL. Check error messages in DEBUG mode.

Definition at line 402 of file n_list.c.

References __n_assert, end, Free, LOG_ERR, n_log, nb_items, LIST_NODE::next, LIST_NODE::prev, LIST_NODE::ptr, and start.

◆ list_push()

int list_push ( LIST list,
void *  ptr,
void(*)(void *ptr)  destructor 
)

add a pointer at the end of the list

add a pointer at the end of the list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
ptrThe pointer you wan to put in the list. A null value will cause an error and a _log message.
destructorPointer to the ptr type destructor function. Leave to NULL if there isn't.
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 227 of file n_list.c.

References __n_assert, end, LOG_ERR, n_log, nb_items, nb_max_items, new_list_node(), LIST_NODE::next, LIST_NODE::prev, and start.

Referenced by _ht_depth_first_search(), _ht_put_double(), _ht_put_int(), _ht_put_ptr(), _ht_put_string(), _ht_put_string_ptr(), _ht_search(), _ht_search_trie_helper(), _n_mock_parse_request(), _sort_windows_by_zorder(), add_emitter(), add_int_to_msg(), add_nb_to_msg(), add_nstrdup_to_msg(), add_nstrptr_to_msg(), add_particle(), add_particle_at(), add_strdup_to_msg(), add_threaded_process(), ht_get_completion_list(), ht_put_ptr_ex(), list_push_sorted(), list_unshift_sorted(), load_config_file(), main(), main(), n_git_list_branches(), n_git_log(), n_git_status(), n_gui_add_button(), n_gui_add_checkbox(), n_gui_add_combobox(), n_gui_add_dropmenu(), n_gui_add_image(), n_gui_add_label(), n_gui_add_listbox(), n_gui_add_radiolist(), n_gui_add_scrollbar(), n_gui_add_slider(), n_gui_add_textarea(), n_gui_add_window(), n_gui_raise_window(), n_kafka_poll(), n_kafka_produce(), netw_add_msg(), netw_add_msg_ex(), netw_pool_add(), netw_recv_func(), new_n_fluid(), process_args(), scan_dir_ex(), and userlist_user_add_waiting_msg().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ list_push_sorted()

int list_push_sorted ( LIST list,
void *  ptr,
int(*)(const void *a, const void *b)  comparator,
void(*)(void *ptr)  destructor 
)

add a pointer sorted via comparator from the end of the list

add a pointer sorted via comparator from the end of the list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
ptrThe pointer you wan to put in the list. A null value will cause an error and a _log message.
comparatorA pointer to a function which take two void * pointers and return an int.
destructorPointer to the ptr type destructor function. Leave to NULL if there isn't.
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 260 of file n_list.c.

References __n_assert, end, link_node, list_push(), list_unshift(), LOG_ERR, n_log, nb_items, nb_max_items, new_list_node(), LIST_NODE::next, LIST_NODE::prev, and LIST_NODE::ptr.

Referenced by main(), and n_scan_dir().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ list_search()

LIST_NODE * list_search ( LIST list,
const void *  ptr 
)

search ptr in list

Parameters
listThe target list
ptrSearched pointer
Returns
The LIST_NODE *node or NULL

Definition at line 468 of file n_list.c.

References __n_assert, and list_foreach.

Referenced by main(), and netw_pool_remove().

+ Here is the caller graph for this function:

◆ list_search_with_f()

LIST_NODE * list_search_with_f ( LIST list,
int(*)(void *ptr)  checkfunk 
)

search for data in list

search for data in list

Parameters
listThe target list
checkfunkFunction pointer who should return 1 if it matches or 0 if it's not
Returns
The LIST_NODE *node or NULL

Definition at line 484 of file n_list.c.

References __n_assert, and list_foreach.

◆ list_shift_f()

void * list_shift_f ( LIST list,
char *  file,
size_t  line 
)

get first ptr from list

get first ptr from list

Helper function for the list_shift macro.

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
fileholder for FILE
lineholder for LINE
Returns
A void *ptr or NULL. Check error messages in DEBUG mode.

Definition at line 435 of file n_list.c.

References __n_assert, end, Free, LOG_ERR, n_log, nb_items, LIST_NODE::next, LIST_NODE::prev, LIST_NODE::ptr, and start.

◆ list_unshift()

int list_unshift ( LIST list,
void *  ptr,
void(*)(void *ptr)  destructor 
)

put a pointer at the beginning of list

put a pointer at the beginning of list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
ptrThe pointer you wan to put in the list. A null value will cause an error and a _log message.
destructorPointer to the ptr type destructor function. Leave to NULL if there isn't.
Returns
A void *ptr or NULL. Check error messages in DEBUG mode.

Definition at line 316 of file n_list.c.

References __n_assert, end, link_node, LOG_ERR, n_log, nb_items, nb_max_items, new_list_node(), and start.

Referenced by list_push_sorted(), list_unshift_sorted(), main(), and n_gui_lower_window().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ list_unshift_sorted()

int list_unshift_sorted ( LIST list,
void *  ptr,
int(*)(const void *a, const void *b)  comparator,
void(*)(void *ptr)  destructor 
)

put a pointer sorted via comparator from the start to the end

put a pointer sorted via comparator from the start to the end

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
ptrThe pointer you wan to put in the list. A null value will cause an error and a _log message.
comparatorA pointer to a function which take two void * pointers and return an int.
destructorPointer to the ptr type destructor function. Leave to NULL if there isn't.
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 348 of file n_list.c.

References __n_assert, link_node, list_push(), list_unshift(), LOG_ERR, n_log, nb_items, nb_max_items, new_list_node(), LIST_NODE::next, LIST_NODE::prev, and start.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ new_generic_list()

LIST * new_generic_list ( size_t  max_items)

initialize a list

initialize a list

Parameters
max_itemsSpecify a max size for the list container. From UNLIMITED_LIST_ITEMS (0) to MAX_LIST_ITEMS (SIZE_MAX).
Returns
a new LIST or NULL

Definition at line 36 of file n_list.c.

References __n_assert, end, Malloc, nb_items, nb_max_items, and start.

Referenced by _ht_search(), _ht_search_trie(), _n_mock_parse_request(), create_msg(), ht_get_completion_list(), ht_resize(), init_particle_system(), load_config_file(), main(), main(), main(), n_git_list_branches(), n_git_log(), n_git_status(), n_gui_add_window(), n_gui_new_ctx(), n_kafka_new(), n_kafka_poll(), netw_new(), new_ht(), new_n_fluid(), new_thread_pool(), tree_create_node(), tree_insert_child(), and userlist_new().

+ Here is the caller graph for this function:

◆ new_list_node()

LIST_NODE * new_list_node ( void *  ptr,
void(*)(void *ptr)  destructor 
)

create a new node

create a new node

Parameters
ptrThe pointer you want to put in the node.
destructorPointer to the ptr type destructor function. Leave to NULL if there isn't.
Returns
A new node or NULL on error. Check error messages in DEBUG mode.

Definition at line 56 of file n_list.c.

References __n_assert, LIST_NODE::destroy_func, LOG_ERR, Malloc, n_log, LIST_NODE::next, LIST_NODE::prev, and LIST_NODE::ptr.

Referenced by list_push(), list_push_sorted(), list_unshift(), list_unshift_sorted(), main(), and tree_insert_child().

+ Here is the caller graph for this function:

◆ remove_list_node_f()

void * remove_list_node_f ( LIST list,
LIST_NODE node 
)

remove a node

remove a node

Parameters
listThe list to pick in
nodeThe node to remove in the list
Returns
The node holded pointer or NULL

Definition at line 75 of file n_list.c.

References __n_assert, end, Free, LOG_ERR, n_log, nb_items, LIST_NODE::next, LIST_NODE::prev, LIST_NODE::ptr, and start.

Referenced by _sort_windows_by_zorder(), n_gui_lower_window(), n_gui_raise_window(), and tree_delete_node().

+ Here is the caller graph for this function: