libdev.a

C Helper library
Log | Files | Refs | README | LICENSE | git clone https://git.ne02ptzero.me/git/libdev.a

commit d2c63a20d88f514785b3928f097159c1b5a68be5
parent c4bf78c222a2f207b0dfca940a1b9c4103e11c4c
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri,  7 Oct 2016 18:32:00 +0200

Add(Singletons):

Add singletons functions, not tested
add list_size(...) in lists.c aswell

Diffstat:
Mlibdev.h | 24++++++++++++++++++++++++
Mlists.c | 10++++++++++
Asingleton.c | 36++++++++++++++++++++++++++++++++++++
3 files changed, 70 insertions(+), 0 deletions(-)

diff --git a/libdev.h b/libdev.h @@ -21,6 +21,20 @@ # define list_add_before(org_list, p_tr1, p_tr2, sizeZ) org_list = list_insert_before(org_list, p_tr1, p_tr2, sizeZ); +/* TYPEDEFS */ +// Signed +typedef signed char s8_t; +typedef signed short s16_t; +typedef signed int s32_t; +typedef signed long long s64_t; + +// Unsigned +typedef unsigned char u8_t; +typedef unsigned short u16_t; +typedef unsigned int u32_t; +typedef unsigned long long u64_t; + + /* STRUCTS */ typedef struct s_list { void *member; // Actual member @@ -30,11 +44,21 @@ typedef struct s_list { struct s_list *head; // Head of the list } t_list; +typedef struct s_singleton { + u_char type; // List type (ENUM) + t_list *ptr; // Actual pointer to the list +} t_singleton; + + /* FUNCTIONS DEFINITIONS */ // lists.c t_list *list_add_member(t_list *list, void *member, size_t size); t_list *list_get_last(t_list *list); t_list *list_insert_after(t_list *org, t_list *ptr, void *member, size_t size); t_list *list_insert_before(t_list *org, t_list *ptr, void *member, size_t size); +size_t list_size(t_list *list); + +// singleton.c +t_list *singleton_lists(u_char list_type, t_list *ptr); #endif /* __LIBDEV__ */ diff --git a/lists.c b/lists.c @@ -82,3 +82,13 @@ t_list *list_insert_before(t_list *org, t_list *ptr, void *member, size_t size) n_member->head = org; return org; } + +size_t list_size(t_list *list) { + size_t i; + t_list *tmp; + + if (!list) + return 0; + for (tmp = list, i = 0; tmp; tmp = tmp->next, i++); + return i; +} diff --git a/singleton.c b/singleton.c @@ -0,0 +1,36 @@ +#include <libdev.h> + +t_list *singleton_lists(u_char list_type, t_list *ptr) { + t_list *tmp; + t_singleton *member; + static t_list *index = 0x0; + + if (ptr) { + if (!list_size(index)) { + member = malloc(sizeof(t_singleton)); + member->ptr = ptr; + member->type = list_type; + list_add(index, member, sizeof(t_singleton)); + } else { + list_for_each(index, tmp, member) { + if (member->type == list_type) { + member->ptr = ptr; + return 0x0; + } + } + member = malloc(sizeof(t_singleton)); + member->ptr = ptr; + member->type = list_type; + list_add(index, member, sizeof(t_singleton)); + } + } else { + if (!list_size(index)) + return 0x0; + list_for_each(index, tmp, member) { + if (member->type == list_type) + return member->ptr; + } + } + return 0x0; +} +