X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Futil%2Fcontainer_slist.c;h=6b583258376ce101a49d76ec1072cc3f2564aeb5;hb=72c8645af31896829b674b575c5375706f362a30;hp=1dd0344e41df162acd23698081a278ad2db365da;hpb=cddbdf5b928c68ab71b40c950b9f01ea68a9fbdb;p=oweals%2Fgnunet.git diff --git a/src/util/container_slist.c b/src/util/container_slist.c index 1dd0344e4..6b5832583 100644 --- a/src/util/container_slist.c +++ b/src/util/container_slist.c @@ -27,6 +27,8 @@ #include "platform.h" #include "gnunet_container_lib.h" +#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) + /** * Element in our linked list. */ @@ -76,27 +78,6 @@ struct GNUNET_CONTAINER_SList }; -/** - * Handle to a singly linked list iterator - */ -struct GNUNET_CONTAINER_SList_Iterator -{ - /** - * Linked list that we are iterating over. - */ - struct GNUNET_CONTAINER_SList *list; - - /** - * Last element accessed. - */ - struct GNUNET_CONTAINER_SList_Elem *last; - - /** - * Current list element. - */ - struct GNUNET_CONTAINER_SList_Elem *elem; -}; - /** * Create a new element that is to be inserted into the list @@ -184,21 +165,21 @@ void GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst, struct GNUNET_CONTAINER_SList *src) { - struct GNUNET_CONTAINER_SList_Iterator *i; + struct GNUNET_CONTAINER_SList_Iterator i; for (i = GNUNET_CONTAINER_slist_begin (src); - GNUNET_CONTAINER_slist_end (i) != GNUNET_YES; - GNUNET_CONTAINER_slist_next (i)) + GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES; + GNUNET_CONTAINER_slist_next (&i)) { GNUNET_CONTAINER_slist_add (dst, - (i->elem->disp == + (i.elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC) ? GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC : GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, - i->elem->elem, i->elem->len); + i.elem->elem, i.elem->len); } - GNUNET_CONTAINER_slist_iter_destroy (i); + GNUNET_CONTAINER_slist_iter_destroy (&i); } @@ -230,14 +211,14 @@ GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l) * @param l list * @return iterator pointing to the beginning */ -struct GNUNET_CONTAINER_SList_Iterator * +struct GNUNET_CONTAINER_SList_Iterator GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l) { - struct GNUNET_CONTAINER_SList_Iterator *ret; + struct GNUNET_CONTAINER_SList_Iterator ret; - ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Iterator)); - ret->elem = l->head; - ret->list = l; + memset (&ret, 0, sizeof (ret)); + ret.elem = l->head; + ret.list = l; return ret; } @@ -269,10 +250,11 @@ GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l) /** * Check if a list contains a certain element - * * @param l list * @param buf payload buffer to find * @param len length of the payload (number of bytes in buf) + * + * @return GNUNET_YES if found, GNUNET_NO otherwise */ int GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l, @@ -286,6 +268,32 @@ GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l, return GNUNET_NO; } +typedef int (*Comparator)(const void *, size_t, const void *, size_t); + +/** + * Check if a list contains a certain element + * + * @param l list + * @param buf payload buffer to find + * @param len length of the payload (number of bytes in buf) + * @param compare comparison function, should return 0 if compared elements match + * + * @return NULL if the 'buf' could not be found, pointer to the + * list element, if found + */ +void * +GNUNET_CONTAINER_slist_contains2 (const struct GNUNET_CONTAINER_SList *l, + const void *buf, size_t len, + Comparator compare) +{ + struct GNUNET_CONTAINER_SList_Elem *e; + + for (e = l->head; e != NULL; e = e->next) + if ((e->len == len) && (*compare)(buf, len, e->elem, e->len) == 0) + return e->elem; + return NULL; +} + /** * Count the elements of a list @@ -401,7 +409,6 @@ GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i, void GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator *i) { - GNUNET_free (i); } /* end of container_slist.c */