2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file include/gnunet_container_lib.h
23 * @brief container classes for GNUnet
25 * @author Christian Grothoff
29 #ifndef GNUNET_CONTAINER_LIB_H
30 #define GNUNET_CONTAINER_LIB_H
32 /* add error and config prototypes */
33 #include "gnunet_crypto_lib.h"
34 #include <extractor.h>
36 #ifndef EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME
37 /* hack for LE < 0.6.3 */
38 #define EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME 180
44 #if 0 /* keep Emacsens' auto-indent happy */
50 /* ******************* bloomfilter ***************** */
53 * @brief bloomfilter representation (opaque)
55 struct GNUNET_CONTAINER_BloomFilter;
58 * Iterator over HashCodes.
61 * @param next set to the next hash code
62 * @return GNUNET_YES if next was updated
63 * GNUNET_NO if there are no more entries
65 typedef int (*GNUNET_HashCodeIterator) (void *cls, struct GNUNET_HashCode * next);
69 * Load a bloom-filter from a file.
71 * @param filename the name of the file (or the prefix)
72 * @param size the size of the bloom-filter (number of
73 * bytes of storage space to use); will be rounded up
75 * @param k the number of GNUNET_CRYPTO_hash-functions to apply per
76 * element (number of bits set per element in the set)
77 * @return the bloomfilter
79 struct GNUNET_CONTAINER_BloomFilter *
80 GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
85 * Create a bloom filter from raw bits.
87 * @param data the raw bits in memory (maybe NULL,
88 * in which case all bits should be considered
90 * @param size the size of the bloom-filter (number of
91 * bytes of storage space to use); also size of data
92 * -- unless data is NULL. Must be a power of 2.
93 * @param k the number of GNUNET_CRYPTO_hash-functions to apply per
94 * element (number of bits set per element in the set)
95 * @return the bloomfilter
97 struct GNUNET_CONTAINER_BloomFilter *
98 GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size,
103 * Copy the raw data of this bloomfilter into
104 * the given data array.
106 * @param data where to write the data
107 * @param size the size of the given data array
108 * @return GNUNET_SYSERR if the data array of the wrong size
111 GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct
112 GNUNET_CONTAINER_BloomFilter *bf,
113 char *data, size_t size);
117 * Test if an element is in the filter.
118 * @param e the element
119 * @param bf the filter
120 * @return GNUNET_YES if the element is in the filter, GNUNET_NO if not
123 GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter
124 *bf, const struct GNUNET_HashCode * e);
128 * Add an element to the filter
129 * @param bf the filter
130 * @param e the element
133 GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
134 const struct GNUNET_HashCode * e);
138 * Remove an element from the filter.
139 * @param bf the filter
140 * @param e the element to remove
143 GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
144 const struct GNUNET_HashCode * e);
148 * Create a copy of a bloomfilter.
150 * @param bf the filter
153 struct GNUNET_CONTAINER_BloomFilter *
154 GNUNET_CONTAINER_bloomfilter_copy (const struct GNUNET_CONTAINER_BloomFilter
160 * Free the space associcated with a filter
161 * in memory, flush to drive if needed (do not
162 * free the space on the drive)
163 * @param bf the filter
166 GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf);
170 * Get size of the bloom filter.
172 * @param bf the filter
173 * @return number of bytes used for the data of the bloom filter
176 GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter
181 * Reset a bloom filter to empty.
182 * @param bf the filter
185 GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf);
188 * Or the entries of the given raw data array with the
189 * data of the given bloom filter. Assumes that
190 * the size of the data array and the current filter
193 * @param bf the filter
194 * @param data data to OR-in
195 * @param size size of data
196 * @return GNUNET_OK on success
199 GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
200 const char *data, size_t size);
203 * Or the entries of the given raw data array with the
204 * data of the given bloom filter. Assumes that
205 * the size of the data array and the current filter
208 * @param bf the filter
209 * @param to_or the bloomfilter to or-in
210 * @param size number of bytes in data
213 GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
214 const struct GNUNET_CONTAINER_BloomFilter
215 *to_or, size_t size);
218 * Resize a bloom filter. Note that this operation
219 * is pretty costly. Essentially, the bloom filter
220 * needs to be completely re-build.
222 * @param bf the filter
223 * @param iterator an iterator over all elements stored in the BF
224 * @param iterator_cls closure for iterator
225 * @param size the new size for the filter
226 * @param k the new number of GNUNET_CRYPTO_hash-function to apply per element
229 GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
230 GNUNET_HashCodeIterator iterator,
231 void *iterator_cls, size_t size,
234 /* ****************** metadata ******************* */
237 * Meta data to associate with a file, directory or namespace.
239 struct GNUNET_CONTAINER_MetaData;
242 * Create a fresh MetaData token.
244 * @return empty meta-data container
246 struct GNUNET_CONTAINER_MetaData *
247 GNUNET_CONTAINER_meta_data_create (void);
250 * Duplicate a MetaData token.
252 * @param md what to duplicate
253 * @return duplicate meta-data container
255 struct GNUNET_CONTAINER_MetaData *
256 GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData
262 * @param md what to free
265 GNUNET_CONTAINER_meta_data_destroy (struct GNUNET_CONTAINER_MetaData *md);
268 * Test if two MDs are equal. We consider them equal if
269 * the meta types, formats and content match (we do not
270 * include the mime types and plugins names in this
273 * @param md1 first value to check
274 * @param md2 other value to check
275 * @return GNUNET_YES if they are equal
278 GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData
280 const struct GNUNET_CONTAINER_MetaData
287 * @param md metadata to extend
288 * @param plugin_name name of the plugin that produced this value;
289 * special values can be used (i.e. '<zlib>' for zlib being
290 * used in the main libextractor library and yielding
292 * @param type libextractor-type describing the meta data
293 * @param format basic format information about data
294 * @param data_mime_type mime-type of data (not of the original file);
295 * can be NULL (if mime-type is not known)
296 * @param data actual meta-data found
297 * @param data_size number of bytes in data
298 * @return GNUNET_OK on success, GNUNET_SYSERR if this entry already exists
299 * data_mime_type and plugin_name are not considered for "exists" checks
302 GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
303 const char *plugin_name,
304 enum EXTRACTOR_MetaType type,
305 enum EXTRACTOR_MetaFormat format,
306 const char *data_mime_type, const char *data,
311 * Extend metadata. Merges the meta data from the second argument
312 * into the first, discarding duplicate key-value pairs.
314 * @param md metadata to extend
315 * @param in metadata to merge
318 GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md,
319 const struct GNUNET_CONTAINER_MetaData *in);
325 * @param md metadata to manipulate
326 * @param type type of the item to remove
327 * @param data specific value to remove, NULL to remove all
328 * entries of the given type
329 * @param data_size number of bytes in data
330 * @return GNUNET_OK on success, GNUNET_SYSERR if the item does not exist in md
333 GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
334 enum EXTRACTOR_MetaType type,
335 const char *data, size_t data_size);
339 * Remove all items in the container.
341 * @param md metadata to manipulate
344 GNUNET_CONTAINER_meta_data_clear (struct GNUNET_CONTAINER_MetaData *md);
348 * Add the current time as the publication date
351 * @param md metadata to modify
354 GNUNET_CONTAINER_meta_data_add_publication_date (struct
355 GNUNET_CONTAINER_MetaData *md);
359 * Iterate over MD entries.
361 * @param md metadata to inspect
362 * @param iter function to call on each entry
363 * @param iter_cls closure for iterator
364 * @return number of entries
367 GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData *md,
368 EXTRACTOR_MetaDataProcessor iter,
372 * Get the first MD entry of the given type. Caller
373 * is responsible for freeing the return value.
374 * Also, only meta data items that are strings (0-terminated)
375 * are returned by this function.
377 * @param md metadata to inspect
378 * @param type type to look for
379 * @return NULL if no entry was found
382 GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData
383 *md, enum EXTRACTOR_MetaType type);
387 * Get the first matching MD entry of the given types. Caller is
388 * responsible for freeing the return value. Also, only meta data
389 * items that are strings (0-terminated) are returned by this
392 * @param md metadata to inspect
393 * @param ... -1-terminated list of types
394 * @return NULL if we do not have any such entry,
395 * otherwise client is responsible for freeing the value!
398 GNUNET_CONTAINER_meta_data_get_first_by_types (const struct
399 GNUNET_CONTAINER_MetaData *md,
403 * Get a thumbnail from the meta-data (if present). Only matches meta
404 * data with mime type "image" and binary format.
406 * @param md metadata to inspect
407 * @param thumb will be set to the thumbnail data. Must be
408 * freed by the caller!
409 * @return number of bytes in thumbnail, 0 if not available
412 GNUNET_CONTAINER_meta_data_get_thumbnail (const struct GNUNET_CONTAINER_MetaData
413 *md, unsigned char **thumb);
418 * Options for metadata serialization.
420 enum GNUNET_CONTAINER_MetaDataSerializationOptions
423 * Serialize all of the data.
425 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL = 0,
428 * If not enough space is available, it is acceptable
429 * to only serialize some of the metadata.
431 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART = 1,
434 * Speed is of the essence, do not allow compression.
436 GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS = 2
441 * Serialize meta-data to target.
443 * @param md metadata to serialize
444 * @param target where to write the serialized metadata;
445 * *target can be NULL, in which case memory is allocated
446 * @param max maximum number of bytes available
447 * @param opt is it ok to just write SOME of the
448 * meta-data to match the size constraint,
449 * possibly discarding some data?
450 * @return number of bytes written on success,
451 * -1 on error (typically: not enough
455 GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData
456 *md, char **target, size_t max,
458 GNUNET_CONTAINER_MetaDataSerializationOptions
463 * Get the size of the full meta-data in serialized form.
465 * @param md metadata to inspect
466 * @return number of bytes needed for serialization, -1 on error
469 GNUNET_CONTAINER_meta_data_get_serialized_size (const struct
470 GNUNET_CONTAINER_MetaData *md);
474 * Deserialize meta-data. Initializes md.
476 * @param input serialized meta-data.
477 * @param size number of bytes available
478 * @return MD on success, NULL on error (i.e.
481 struct GNUNET_CONTAINER_MetaData *
482 GNUNET_CONTAINER_meta_data_deserialize (const char *input, size_t size);
485 /* ******************************* HashMap **************************** */
488 * Opaque handle for a HashMap.
490 struct GNUNET_CONTAINER_MultiHashMap;
493 * Options for storing values in the HashMap.
495 enum GNUNET_CONTAINER_MultiHashMapOption
499 * If a value with the given key exists, replace it. Note that the
500 * old value would NOT be freed by replace (the application has to
501 * make sure that this happens if required).
503 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE,
506 * Allow multiple values with the same key.
508 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE,
511 * There must only be one value per key; storing a value should fail
512 * if a value under the same key already exists.
514 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY,
517 * There must only be one value per key, but don't bother checking
518 * if a value already exists (faster than UNIQUE_ONLY; implemented
519 * just like MULTIPLE but this option documents better what is
520 * intended if UNIQUE is what is desired).
522 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
527 * Iterator over hash map entries.
530 * @param key current key code
531 * @param value value in the hash map
532 * @return GNUNET_YES if we should continue to
536 typedef int (*GNUNET_CONTAINER_HashMapIterator) (void *cls,
537 const struct GNUNET_HashCode *key,
542 * Create a multi hash map.
544 * @param len initial size (map will grow as needed)
545 * @param do_not_copy_keys GNUNET_NO is always safe and should be used by default;
546 * GNUNET_YES means that on 'put', the 'key' does not have
547 * to be copied as the destination of the pointer is
548 * guaranteed to be life as long as the value is stored in
549 * the hashmap. This can significantly reduce memory
550 * consumption, but of course is also a recipie for
551 * heap corruption if the assumption is not true. Only
552 * use this if (1) memory use is important in this case and
553 * (2) you have triple-checked that the invariant holds
554 * @return NULL on error
556 struct GNUNET_CONTAINER_MultiHashMap *
557 GNUNET_CONTAINER_multihashmap_create (unsigned int len,
558 int do_not_copy_keys);
562 * Destroy a hash map. Will not free any values
563 * stored in the hash map!
568 GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap
573 * Given a key find a value in the map matching the key.
576 * @param key what to look for
577 * @return NULL if no value was found; note that
578 * this is indistinguishable from values that just
579 * happen to be NULL; use "contains" to test for
580 * key-value pairs with value NULL
583 GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap
584 *map, const struct GNUNET_HashCode * key);
588 * Remove the given key-value pair from the map. Note that if the
589 * key-value pair is in the map multiple times, only one of the pairs
593 * @param key key of the key-value pair
594 * @param value value of the key-value pair
595 * @return GNUNET_YES on success, GNUNET_NO if the key-value pair
599 GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
600 const struct GNUNET_HashCode * key,
604 * Remove all entries for the given key from the map.
605 * Note that the values would not be "freed".
608 * @param key identifies values to be removed
609 * @return number of values removed
612 GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
613 *map, const struct GNUNET_HashCode * key);
617 * Check if the map contains any value under the given
618 * key (including values that are NULL).
621 * @param key the key to test if a value exists for it
622 * @return GNUNET_YES if such a value exists,
626 GNUNET_CONTAINER_multihashmap_contains (const struct
627 GNUNET_CONTAINER_MultiHashMap *map,
628 const struct GNUNET_HashCode * key);
632 * Check if the map contains the given value under the given
636 * @param key the key to test if a value exists for it
637 * @param value value to test for
638 * @return GNUNET_YES if such a value exists,
642 GNUNET_CONTAINER_multihashmap_contains_value (const struct
643 GNUNET_CONTAINER_MultiHashMap
644 *map, const struct GNUNET_HashCode * key,
649 * Store a key-value pair in the map.
652 * @param key key to use
653 * @param value value to use
654 * @param opt options for put
655 * @return GNUNET_OK on success,
656 * GNUNET_NO if a value was replaced (with REPLACE)
657 * GNUNET_SYSERR if UNIQUE_ONLY was the option and the
658 * value already exists
661 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
662 const struct GNUNET_HashCode * key, void *value,
663 enum GNUNET_CONTAINER_MultiHashMapOption
667 * Get the number of key-value pairs in the map.
670 * @return the number of key value pairs
673 GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap
678 * Iterate over all entries in the map.
681 * @param it function to call on each entry
682 * @param it_cls extra argument to it
683 * @return the number of key value pairs processed,
684 * GNUNET_SYSERR if it aborted iteration
687 GNUNET_CONTAINER_multihashmap_iterate (const struct
688 GNUNET_CONTAINER_MultiHashMap *map,
689 GNUNET_CONTAINER_HashMapIterator it,
694 * Iterate over all entries in the map that match a particular key.
697 * @param key key that the entries must correspond to
698 * @param it function to call on each entry
699 * @param it_cls extra argument to it
700 * @return the number of key value pairs processed,
701 * GNUNET_SYSERR if it aborted iteration
704 GNUNET_CONTAINER_multihashmap_get_multiple (const struct
705 GNUNET_CONTAINER_MultiHashMap *map,
706 const struct GNUNET_HashCode * key,
707 GNUNET_CONTAINER_HashMapIterator it,
710 /* Version of multihashmap with 32 bit keys */
713 * Opaque handle for the 32-bit key HashMap.
715 struct GNUNET_CONTAINER_MultiHashMap32;
719 * Iterator over hash map entries.
722 * @param key current key code
723 * @param value value in the hash map
724 * @return GNUNET_YES if we should continue to
728 typedef int (*GNUNET_CONTAINER_HashMapIterator32) (void *cls,
734 * Create a 32-bit key multi hash map.
736 * @param len initial size (map will grow as needed)
737 * @return NULL on error
739 struct GNUNET_CONTAINER_MultiHashMap32 *
740 GNUNET_CONTAINER_multihashmap32_create (unsigned int len);
744 * Destroy a 32-bit key hash map. Will not free any values
745 * stored in the hash map!
750 GNUNET_CONTAINER_multihashmap32_destroy (struct GNUNET_CONTAINER_MultiHashMap32
755 * Get the number of key-value pairs in the map.
758 * @return the number of key value pairs
761 GNUNET_CONTAINER_multihashmap32_size (const struct
762 GNUNET_CONTAINER_MultiHashMap32 *map);
766 * Given a key find a value in the map matching the key.
769 * @param key what to look for
770 * @return NULL if no value was found; note that
771 * this is indistinguishable from values that just
772 * happen to be NULL; use "contains" to test for
773 * key-value pairs with value NULL
776 GNUNET_CONTAINER_multihashmap32_get (const struct
777 GNUNET_CONTAINER_MultiHashMap32 *map,
782 * Iterate over all entries in the map.
785 * @param it function to call on each entry
786 * @param it_cls extra argument to it
787 * @return the number of key value pairs processed,
788 * GNUNET_SYSERR if it aborted iteration
791 GNUNET_CONTAINER_multihashmap32_iterate (const struct
792 GNUNET_CONTAINER_MultiHashMap32 *map,
793 GNUNET_CONTAINER_HashMapIterator32 it,
798 * Remove the given key-value pair from the map. Note that if the
799 * key-value pair is in the map multiple times, only one of the pairs
803 * @param key key of the key-value pair
804 * @param value value of the key-value pair
805 * @return GNUNET_YES on success, GNUNET_NO if the key-value pair
809 GNUNET_CONTAINER_multihashmap32_remove (struct GNUNET_CONTAINER_MultiHashMap32
816 * Remove all entries for the given key from the map.
817 * Note that the values would not be "freed".
820 * @param key identifies values to be removed
821 * @return number of values removed
824 GNUNET_CONTAINER_multihashmap32_remove_all (struct
825 GNUNET_CONTAINER_MultiHashMap32
831 * Check if the map contains any value under the given
832 * key (including values that are NULL).
835 * @param key the key to test if a value exists for it
836 * @return GNUNET_YES if such a value exists,
840 GNUNET_CONTAINER_multihashmap32_contains (const struct
841 GNUNET_CONTAINER_MultiHashMap32 *map,
846 * Check if the map contains the given value under the given
850 * @param key the key to test if a value exists for it
851 * @param value value to test for
852 * @return GNUNET_YES if such a value exists,
856 GNUNET_CONTAINER_multihashmap32_contains_value (const struct
857 GNUNET_CONTAINER_MultiHashMap32
864 * Store a key-value pair in the map.
867 * @param key key to use
868 * @param value value to use
869 * @param opt options for put
870 * @return GNUNET_OK on success,
871 * GNUNET_NO if a value was replaced (with REPLACE)
872 * GNUNET_SYSERR if UNIQUE_ONLY was the option and the
873 * value already exists
876 GNUNET_CONTAINER_multihashmap32_put (struct GNUNET_CONTAINER_MultiHashMap32
877 *map, uint32_t key, void *value,
878 enum GNUNET_CONTAINER_MultiHashMapOption
883 * Iterate over all entries in the map that match a particular key.
886 * @param key key that the entries must correspond to
887 * @param it function to call on each entry
888 * @param it_cls extra argument to it
889 * @return the number of key value pairs processed,
890 * GNUNET_SYSERR if it aborted iteration
893 GNUNET_CONTAINER_multihashmap32_get_multiple (const struct
894 GNUNET_CONTAINER_MultiHashMap32
896 GNUNET_CONTAINER_HashMapIterator32
902 /* ******************** doubly-linked list *************** */
903 /* To avoid mistakes: head->prev == tail->next == NULL */
906 * Insert an element at the head of a DLL. Assumes that head, tail and
907 * element are structs with prev and next fields.
909 * @param head pointer to the head of the DLL
910 * @param tail pointer to the tail of the DLL
911 * @param element element to insert
913 #define GNUNET_CONTAINER_DLL_insert(head,tail,element) do { \
914 GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
915 GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
916 (element)->next = (head); \
917 (element)->prev = NULL; \
918 if ((tail) == NULL) \
921 (head)->prev = element; \
922 (head) = (element); } while (0)
926 * Insert an element at the tail of a DLL. Assumes that head, tail and
927 * element are structs with prev and next fields.
929 * @param head pointer to the head of the DLL
930 * @param tail pointer to the tail of the DLL
931 * @param element element to insert
933 #define GNUNET_CONTAINER_DLL_insert_tail(head,tail,element) do { \
934 GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
935 GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
936 (element)->prev = (tail); \
937 (element)->next = NULL; \
938 if ((head) == NULL) \
941 (tail)->next = element; \
942 (tail) = (element); } while (0)
946 * Insert an element into a DLL after the given other element. Insert
947 * at the head if the other element is NULL.
949 * @param head pointer to the head of the DLL
950 * @param tail pointer to the tail of the DLL
951 * @param other prior element, NULL for insertion at head of DLL
952 * @param element element to insert
954 #define GNUNET_CONTAINER_DLL_insert_after(head,tail,other,element) do { \
955 GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
956 GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
957 (element)->prev = (other); \
960 (element)->next = (head); \
961 (head) = (element); \
965 (element)->next = (other)->next; \
966 (other)->next = (element); \
968 if (NULL == (element)->next) \
969 (tail) = (element); \
971 (element)->next->prev = (element); } while (0)
975 * Insert an element into a DLL before the given other element. Insert
976 * at the tail if the other element is NULL.
978 * @param head pointer to the head of the DLL
979 * @param tail pointer to the tail of the DLL
980 * @param other prior element, NULL for insertion at head of DLL
981 * @param element element to insert
983 #define GNUNET_CONTAINER_DLL_insert_before(head,tail,other,element) do { \
984 GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
985 GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
986 (element)->next = (other); \
989 (element)->prev = (tail); \
990 (tail) = (element); \
994 (element)->prev = (other)->prev; \
995 (other)->prev = (element); \
997 if (NULL == (element)->prev) \
998 (head) = (element); \
1000 (element)->prev->next = (element); } while (0)
1004 * Remove an element from a DLL. Assumes that head, tail and
1005 * element point to structs with prev and next fields.
1007 * Using the head or tail pointer as the element
1008 * argument does NOT work with this macro.
1009 * Make sure to store head/tail in another pointer
1010 * and use it to remove the head or tail of the list.
1012 * @param head pointer to the head of the DLL
1013 * @param tail pointer to the tail of the DLL
1014 * @param element element to remove
1016 #define GNUNET_CONTAINER_DLL_remove(head,tail,element) do { \
1017 GNUNET_assert ( ( (element)->prev != NULL) || ((head) == (element))); \
1018 GNUNET_assert ( ( (element)->next != NULL) || ((tail) == (element))); \
1019 if ((element)->prev == NULL) \
1020 (head) = (element)->next; \
1022 (element)->prev->next = (element)->next; \
1023 if ((element)->next == NULL) \
1024 (tail) = (element)->prev; \
1026 (element)->next->prev = (element)->prev; \
1027 (element)->next = NULL; \
1028 (element)->prev = NULL; } while (0)
1031 /* ************ Multi-DLL interface, allows DLL elements to be
1032 in multiple lists at the same time *********************** */
1035 * Insert an element at the head of a MDLL. Assumes that head, tail and
1036 * element are structs with prev and next fields.
1038 * @param mdll suffix name for the next and prev pointers in the element
1039 * @param head pointer to the head of the MDLL
1040 * @param tail pointer to the tail of the MDLL
1041 * @param element element to insert
1043 #define GNUNET_CONTAINER_MDLL_insert(mdll,head,tail,element) do { \
1044 GNUNET_assert ( ( (element)->prev_##mdll == NULL) && ((head) != (element))); \
1045 GNUNET_assert ( ( (element)->next_##mdll == NULL) && ((tail) != (element))); \
1046 (element)->next_##mdll = (head); \
1047 (element)->prev_##mdll = NULL; \
1048 if ((tail) == NULL) \
1051 (head)->prev_##mdll = element; \
1052 (head) = (element); } while (0)
1056 * Insert an element at the tail of a MDLL. Assumes that head, tail and
1057 * element are structs with prev and next fields.
1059 * @param mdll suffix name for the next and prev pointers in the element
1060 * @param head pointer to the head of the MDLL
1061 * @param tail pointer to the tail of the MDLL
1062 * @param element element to insert
1064 #define GNUNET_CONTAINER_MDLL_insert_tail(mdll,head,tail,element) do { \
1065 GNUNET_assert ( ( (element)->prev_##mdll == NULL) && ((head) != (element))); \
1066 GNUNET_assert ( ( (element)->next_##mdll == NULL) && ((tail) != (element))); \
1067 (element)->prev_##mdll = (tail); \
1068 (element)->next_##mdll = NULL; \
1069 if ((head) == NULL) \
1072 (tail)->next_##mdll = element; \
1073 (tail) = (element); } while (0)
1077 * Insert an element into a MDLL after the given other element. Insert
1078 * at the head if the other element is NULL.
1080 * @param mdll suffix name for the next and prev pointers in the element
1081 * @param head pointer to the head of the MDLL
1082 * @param tail pointer to the tail of the MDLL
1083 * @param other prior element, NULL for insertion at head of MDLL
1084 * @param element element to insert
1086 #define GNUNET_CONTAINER_MDLL_insert_after(mdll,head,tail,other,element) do { \
1087 GNUNET_assert ( ( (element)->prev_##mdll == NULL) && ((head) != (element))); \
1088 GNUNET_assert ( ( (element)->next_##mdll == NULL) && ((tail) != (element))); \
1089 (element)->prev_##mdll = (other); \
1090 if (NULL == other) \
1092 (element)->next_##mdll = (head); \
1093 (head) = (element); \
1097 (element)->next_##mdll = (other)->next_##mdll; \
1098 (other)->next_##mdll = (element); \
1100 if (NULL == (element)->next_##mdll) \
1101 (tail) = (element); \
1103 (element)->next->prev_##mdll = (element); } while (0)
1107 * Insert an element into a MDLL before the given other element. Insert
1108 * at the tail if the other element is NULL.
1110 * @param mdll suffix name for the next and prev pointers in the element
1111 * @param head pointer to the head of the MDLL
1112 * @param tail pointer to the tail of the MDLL
1113 * @param other prior element, NULL for insertion at head of MDLL
1114 * @param element element to insert
1116 #define GNUNET_CONTAINER_MDLL_insert_before(mdll,head,tail,other,element) do { \
1117 GNUNET_assert ( ( (element)->prev_##mdll == NULL) && ((head) != (element))); \
1118 GNUNET_assert ( ( (element)->next_##mdll == NULL) && ((tail) != (element))); \
1119 (element)->next_##mdll = (other); \
1120 if (NULL == other) \
1122 (element)->prev = (tail); \
1123 (tail) = (element); \
1127 (element)->prev_##mdll = (other)->prev_##mdll; \
1128 (other)->prev_##mdll = (element); \
1130 if (NULL == (element)->prev_##mdll) \
1131 (head) = (element); \
1133 (element)->prev_##mdll->next_##mdll = (element); } while (0)
1137 * Remove an element from a MDLL. Assumes
1138 * that head, tail and element are structs
1139 * with prev and next fields.
1141 * @param mdll suffix name for the next and prev pointers in the element
1142 * @param head pointer to the head of the MDLL
1143 * @param tail pointer to the tail of the MDLL
1144 * @param element element to remove
1146 #define GNUNET_CONTAINER_MDLL_remove(mdll,head,tail,element) do { \
1147 GNUNET_assert ( ( (element)->prev_##mdll != NULL) || ((head) == (element))); \
1148 GNUNET_assert ( ( (element)->next_##mdll != NULL) || ((tail) == (element))); \
1149 if ((element)->prev_##mdll == NULL) \
1150 (head) = (element)->next_##mdll; \
1152 (element)->prev_##mdll->next_##mdll = (element)->next_##mdll; \
1153 if ((element)->next_##mdll == NULL) \
1154 (tail) = (element)->prev_##mdll; \
1156 (element)->next_##mdll->prev_##mdll = (element)->prev_##mdll; \
1157 (element)->next_##mdll = NULL; \
1158 (element)->prev_##mdll = NULL; } while (0)
1162 /* ******************** Heap *************** */
1166 * Cost by which elements in a heap can be ordered.
1168 typedef uint64_t GNUNET_CONTAINER_HeapCostType;
1172 * Heap type, either max or min. Hopefully makes the
1173 * implementation more useful.
1175 enum GNUNET_CONTAINER_HeapOrder
1178 * Heap with the maximum cost at the root.
1180 GNUNET_CONTAINER_HEAP_ORDER_MAX,
1183 * Heap with the minimum cost at the root.
1185 GNUNET_CONTAINER_HEAP_ORDER_MIN
1192 struct GNUNET_CONTAINER_Heap;
1197 * Handle to a node in a heap.
1199 struct GNUNET_CONTAINER_HeapNode;
1203 * Create a new heap.
1205 * @param order how should the heap be sorted?
1206 * @return handle to the heap
1208 struct GNUNET_CONTAINER_Heap *
1209 GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder order);
1213 * Destroys the heap. Only call on a heap that
1216 * @param heap heap to destroy
1219 GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap);
1223 * Get element stored at root of heap.
1225 * @param heap heap to inspect
1226 * @return NULL if heap is empty
1229 GNUNET_CONTAINER_heap_peek (const struct GNUNET_CONTAINER_Heap *heap);
1233 * Get the current size of the heap
1235 * @param heap the heap to get the size of
1236 * @return number of elements stored
1239 GNUNET_CONTAINER_heap_get_size (const struct GNUNET_CONTAINER_Heap *heap);
1243 * Get the current cost of the node
1245 * @param node the node to get the cost of
1246 * @return cost of the node
1248 GNUNET_CONTAINER_HeapCostType
1249 GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode
1255 * @param cls closure
1256 * @param node internal node of the heap
1257 * @param element value stored at the node
1258 * @param cost cost associated with the node
1259 * @return GNUNET_YES if we should continue to iterate,
1262 typedef int (*GNUNET_CONTAINER_HeapIterator) (void *cls,
1263 struct GNUNET_CONTAINER_HeapNode *
1264 node, void *element,
1265 GNUNET_CONTAINER_HeapCostType
1270 * Iterate over all entries in the heap.
1272 * @param heap the heap
1273 * @param iterator function to call on each entry
1274 * @param iterator_cls closure for iterator
1277 GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap,
1278 GNUNET_CONTAINER_HeapIterator iterator,
1279 void *iterator_cls);
1282 * Perform a random walk of the tree. The walk is biased
1283 * towards elements closer to the root of the tree (since
1284 * each walk starts at the root and ends at a random leaf).
1285 * The heap internally tracks the current position of the
1288 * @param heap heap to walk
1289 * @return data stored at the next random node in the walk;
1290 * NULL if the tree is empty.
1293 GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap);
1297 * Inserts a new element into the heap.
1299 * @param heap heap to modify
1300 * @param element element to insert
1301 * @param cost cost for the element
1302 * @return node for the new element
1304 struct GNUNET_CONTAINER_HeapNode *
1305 GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, void *element,
1306 GNUNET_CONTAINER_HeapCostType cost);
1310 * Remove root of the heap.
1312 * @param heap heap to modify
1313 * @return element data stored at the root node
1316 GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap);
1320 * Removes a node from the heap.
1322 * @param node node to remove
1323 * @return element data stored at the node, NULL if heap is empty
1326 GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node);
1330 * Updates the cost of any node in the tree
1332 * @param heap heap to modify
1333 * @param node node for which the cost is to be changed
1334 * @param new_cost new cost for the node
1337 GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
1338 struct GNUNET_CONTAINER_HeapNode *node,
1339 GNUNET_CONTAINER_HeapCostType new_cost);
1342 /* ******************** Singly linked list *************** */
1345 * Possible ways for how data stored in the linked list
1346 * might be allocated.
1348 enum GNUNET_CONTAINER_SListDisposition
1351 * Single-linked list must copy the buffer.
1353 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT = 0,
1356 * Data is static, no need to copy or free.
1358 GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC = 2,
1361 * Data is dynamic, do not copy but free when done.
1363 GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC = 4
1369 * Handle to a singly linked list
1371 struct GNUNET_CONTAINER_SList;
1374 * Handle to a singly linked list iterator
1376 struct GNUNET_CONTAINER_SList_Iterator
1379 * Linked list that we are iterating over.
1381 struct GNUNET_CONTAINER_SList *list;
1384 * Last element accessed.
1386 struct GNUNET_CONTAINER_SList_Elem *last;
1389 * Current list element.
1391 struct GNUNET_CONTAINER_SList_Elem *elem;
1397 * Add a new element to the list
1399 * @param disp memory disposition
1400 * @param buf payload buffer
1401 * @param len length of the buffer
1404 GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
1405 enum GNUNET_CONTAINER_SListDisposition disp,
1406 const void *buf, size_t len);
1410 * Add a new element to the end of the list
1412 * @param disp memory disposition
1413 * @param buf payload buffer
1414 * @param len length of the buffer
1417 GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
1418 enum GNUNET_CONTAINER_SListDisposition disp,
1419 const void *buf, size_t len);
1423 * Append a singly linked list to another
1424 * @param dst list to append to
1428 GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst,
1429 struct GNUNET_CONTAINER_SList *src);
1433 * Create a new singly linked list
1434 * @return the new list
1436 struct GNUNET_CONTAINER_SList *
1437 GNUNET_CONTAINER_slist_create (void);
1441 * Destroy a singly linked list
1442 * @param l the list to be destroyed
1445 GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l);
1449 * Return the beginning of a list
1452 * @return iterator pointing to the beginning (by value! Either allocate the
1453 * structure on the stack, or use GNUNET_malloc() yourself! All other
1454 * functions do take pointer to this struct though)
1456 struct GNUNET_CONTAINER_SList_Iterator
1457 GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l);
1466 GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l);
1470 * Check if a list contains a certain element
1472 * @param buf payload buffer to find
1473 * @param len length of the payload (number of bytes in buf)
1475 * @return GNUNET_YES if found, GNUNET_NO otherwise
1478 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
1479 const void *buf, size_t len);
1482 * Check if a list contains a certain element using 'compare' function
1485 * @param buf payload buffer to find
1486 * @param len length of the payload (number of bytes in buf)
1487 * @param compare comparison function
1489 * @return NULL if the 'buf' could not be found, pointer to the
1490 * list element, if found
1493 GNUNET_CONTAINER_slist_contains2 (const struct GNUNET_CONTAINER_SList *l,
1494 const void *buf, size_t len,
1495 int (*compare)(const void *, const size_t, const void *, const size_t));
1497 * Count the elements of a list
1499 * @return number of elements in the list
1502 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l);
1506 * Remove an element from the list
1507 * @param i iterator that points to the element to be removed
1510 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i);
1514 * Insert an element into a list at a specific position
1515 * @param before where to insert the new element
1516 * @param disp memory disposition
1517 * @param buf payload buffer
1518 * @param len length of the payload
1521 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
1522 enum GNUNET_CONTAINER_SListDisposition disp,
1523 const void *buf, size_t len);
1527 * Advance an iterator to the next element
1529 * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
1532 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i);
1536 * Check if an iterator points beyond the end of a list
1538 * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
1539 * points to a valid element
1542 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i);
1546 * Retrieve the element at a specific position in a list
1549 * @param len set to the payload length
1553 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
1558 * Release an iterator
1562 GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator *i);
1565 #if 0 /* keep Emacsens' auto-indent happy */
1573 /* ifndef GNUNET_CONTAINER_LIB_H */
1575 /* end of gnunet_container_lib.h */