-fix indent
[oweals/gnunet.git] / src / include / gnunet_container_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2001-2013 Christian Grothoff (and other contributing authors)
4
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 3, or (at your
8      option) any later version.
9
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.
14
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.
19 */
20
21 /**
22  * @file include/gnunet_container_lib.h
23  * @brief container classes for GNUnet
24  * @author Christian Grothoff
25  * @author Nils Durner
26  * @defgroup hashmap multi hash-map
27  * @defgroup heap min- or max-heap with arbitrary element removal
28  * @defgroup bloomfilter Bloom filter (probabilistic set tests)
29  * @defgroup dll Doubly-linked list
30  * @defgroup metadata Meta data (GNU libextractor key-value pairs)
31  */
32
33 #ifndef GNUNET_CONTAINER_LIB_H
34 #define GNUNET_CONTAINER_LIB_H
35
36 /* add error and config prototypes */
37 #include "gnunet_crypto_lib.h"
38 #include <extractor.h>
39
40 #ifndef EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME
41 /* hack for LE < 0.6.3 */
42 #define EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME 180
43 #endif
44
45 #ifdef __cplusplus
46 extern "C"
47 {
48 #if 0                           /* keep Emacsens' auto-indent happy */
49 }
50 #endif
51 #endif
52
53
54 /* ******************* bloomfilter ***************** */
55
56 /**
57  * @brief bloomfilter representation (opaque)
58  * @ingroup bloomfilter
59  */
60 struct GNUNET_CONTAINER_BloomFilter;
61
62 /**
63  * @ingroup bloomfilter
64  * Iterator over `struct GNUNET_HashCode`.
65  *
66  * @param cls closure
67  * @param next set to the next hash code
68  * @return #GNUNET_YES if next was updated
69  *         #GNUNET_NO if there are no more entries
70  */
71 typedef int (*GNUNET_CONTAINER_HashCodeIterator) (void *cls,
72                                         struct GNUNET_HashCode *next);
73
74
75 /**
76  * @ingroup bloomfilter
77  * Load a Bloom filter from a file.
78  *
79  * @param filename the name of the file (or the prefix)
80  * @param size the size of the bloom-filter (number of
81  *        bytes of storage space to use); will be rounded up
82  *        to next power of 2
83  * @param k the number of #GNUNET_CRYPTO_hash-functions to apply per
84  *        element (number of bits set per element in the set)
85  * @return the bloomfilter
86  */
87 struct GNUNET_CONTAINER_BloomFilter *
88 GNUNET_CONTAINER_bloomfilter_load (const char *filename,
89                                    size_t size,
90                                    unsigned int k);
91
92
93 /**
94  * @ingroup bloomfilter
95  * Create a Bloom filter from raw bits.
96  *
97  * @param data the raw bits in memory (maybe NULL,
98  *        in which case all bits should be considered
99  *        to be zero).
100  * @param size the size of the bloom-filter (number of
101  *        bytes of storage space to use); also size of @a data
102  *        -- unless data is NULL.  Must be a power of 2.
103  * @param k the number of #GNUNET_CRYPTO_hash-functions to apply per
104  *        element (number of bits set per element in the set)
105  * @return the bloomfilter
106  */
107 struct GNUNET_CONTAINER_BloomFilter *
108 GNUNET_CONTAINER_bloomfilter_init (const char *data,
109                                    size_t size,
110                                    unsigned int k);
111
112
113 /**
114  * @ingroup bloomfilter
115  * Copy the raw data of this Bloom filter into
116  * the given data array.
117  *
118  * @param data where to write the data
119  * @param size the size of the given @a data array
120  * @return #GNUNET_SYSERR if the data array of the wrong size
121  */
122 int
123 GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct GNUNET_CONTAINER_BloomFilter *bf,
124                                            char *data, size_t size);
125
126
127 /**
128  * @ingroup bloomfilter
129  * Test if an element is in the filter.
130  *
131  * @param e the element
132  * @param bf the filter
133  * @return #GNUNET_YES if the element is in the filter, #GNUNET_NO if not
134  */
135 int
136 GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter *bf,
137                                    const struct GNUNET_HashCode *e);
138
139
140 /**
141  * @ingroup bloomfilter
142  * Add an element to the filter.
143  *
144  * @param bf the filter
145  * @param e the element
146  */
147 void
148 GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
149                                   const struct GNUNET_HashCode *e);
150
151
152 /**
153  * @ingroup bloomfilter
154  * Remove an element from the filter.
155  *
156  * @param bf the filter
157  * @param e the element to remove
158  */
159 void
160 GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
161                                      const struct GNUNET_HashCode *e);
162
163
164 /**
165  * @ingroup bloomfilter
166  * Create a copy of a bloomfilter.
167  *
168  * @param bf the filter
169  * @return copy of bf
170  */
171 struct GNUNET_CONTAINER_BloomFilter *
172 GNUNET_CONTAINER_bloomfilter_copy (const struct GNUNET_CONTAINER_BloomFilter *bf);
173
174
175
176 /**
177  * @ingroup bloomfilter
178  * Free the space associcated with a filter
179  * in memory, flush to drive if needed (do not
180  * free the space on the drive).
181  *
182  * @param bf the filter
183  */
184 void
185 GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf);
186
187
188 /**
189  * Get the number of the addresses set per element in the bloom filter.
190  *
191  * @param bf the filter
192  * @return addresses set per element in the bf
193  */
194 size_t
195 GNUNET_CONTAINER_bloomfilter_get_element_addresses (const struct GNUNET_CONTAINER_BloomFilter *bf);
196
197 /**
198  * @ingroup bloomfilter
199  * Get size of the bloom filter.
200  *
201  * @param bf the filter
202  * @return number of bytes used for the data of the bloom filter
203  */
204 size_t
205 GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter *bf);
206
207
208 /**
209  * @ingroup bloomfilter
210  * Reset a Bloom filter to empty.
211  *
212  * @param bf the filter
213  */
214 void
215 GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf);
216
217
218 /**
219  * @ingroup bloomfilter
220  * "or" the entries of the given raw data array with the
221  * data of the given Bloom filter.  Assumes that
222  * the @a size of the @a data array and the current filter
223  * match.
224  *
225  * @param bf the filter
226  * @param data data to OR-in
227  * @param size size of @a data
228  * @return #GNUNET_OK on success
229  */
230 int
231 GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
232                                  const char *data, size_t size);
233
234
235 /**
236  * @ingroup bloomfilter
237  * "or" the entries of the given raw data array with the
238  * data of the given Bloom filter.  Assumes that
239  * the size of the two filters matches.
240  *
241  * @param bf the filter
242  * @param to_or the bloomfilter to or-in
243  * @return #GNUNET_OK on success
244  */
245 int
246 GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
247                                   const struct GNUNET_CONTAINER_BloomFilter *to_or);
248
249 /**
250  * @ingroup bloomfilter
251  * Resize a bloom filter.  Note that this operation
252  * is pretty costly.  Essentially, the Bloom filter
253  * needs to be completely re-build.
254  *
255  * @param bf the filter
256  * @param iterator an iterator over all elements stored in the BF
257  * @param iterator_cls closure for @a iterator
258  * @param size the new size for the filter
259  * @param k the new number of #GNUNET_CRYPTO_hash-function to apply per element
260  */
261 void
262 GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
263                                      GNUNET_CONTAINER_HashCodeIterator iterator,
264                                      void *iterator_cls, size_t size,
265                                      unsigned int k);
266
267
268 /* ****************** metadata ******************* */
269
270 /**
271  * @ingroup metadata
272  * Meta data to associate with a file, directory or namespace.
273  */
274 struct GNUNET_CONTAINER_MetaData;
275
276 /**
277  * @ingroup metadata
278  * Create a fresh meta data container.
279  *
280  * @return empty meta-data container
281  */
282 struct GNUNET_CONTAINER_MetaData *
283 GNUNET_CONTAINER_meta_data_create (void);
284
285
286 /**
287  * @ingroup metadata
288  * Duplicate a MetaData token.
289  *
290  * @param md what to duplicate
291  * @return duplicate meta-data container
292  */
293 struct GNUNET_CONTAINER_MetaData *
294 GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData
295                                       *md);
296
297 /**
298  * @ingroup metadata
299  * Free meta data.
300  *
301  * @param md what to free
302  */
303 void
304 GNUNET_CONTAINER_meta_data_destroy (struct GNUNET_CONTAINER_MetaData *md);
305
306
307 /**
308  * @ingroup metadata
309  * Test if two MDs are equal. We consider them equal if
310  * the meta types, formats and content match (we do not
311  * include the mime types and plugins names in this
312  * consideration).
313  *
314  * @param md1 first value to check
315  * @param md2 other value to check
316  * @return #GNUNET_YES if they are equal
317  */
318 int
319 GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData *md1,
320                                        const struct GNUNET_CONTAINER_MetaData *md2);
321
322
323 /**
324  * @ingroup metadata
325  * Extend metadata.
326  *
327  * @param md metadata to extend
328  * @param plugin_name name of the plugin that produced this value;
329  *        special values can be used (i.e. '&lt;zlib&gt;' for zlib being
330  *        used in the main libextractor library and yielding
331  *        meta data).
332  * @param type libextractor-type describing the meta data
333  * @param format basic format information about data
334  * @param data_mime_type mime-type of data (not of the original file);
335  *        can be NULL (if mime-type is not known)
336  * @param data actual meta-data found
337  * @param data_size number of bytes in data
338  * @return #GNUNET_OK on success, #GNUNET_SYSERR if this entry already exists
339  *         data_mime_type and plugin_name are not considered for "exists" checks
340  */
341 int
342 GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
343                                    const char *plugin_name,
344                                    enum EXTRACTOR_MetaType type,
345                                    enum EXTRACTOR_MetaFormat format,
346                                    const char *data_mime_type, const char *data,
347                                    size_t data_size);
348
349
350 /**
351  * @ingroup metadata
352  * Extend metadata.  Merges the meta data from the second argument
353  * into the first, discarding duplicate key-value pairs.
354  *
355  * @param md metadata to extend
356  * @param in metadata to merge
357  */
358 void
359 GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md,
360                                   const struct GNUNET_CONTAINER_MetaData *in);
361
362
363 /**
364  * @ingroup metadata
365  * Remove an item.
366  *
367  * @param md metadata to manipulate
368  * @param type type of the item to remove
369  * @param data specific value to remove, NULL to remove all
370  *        entries of the given type
371  * @param data_size number of bytes in data
372  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the item does not exist in md
373  */
374 int
375 GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
376                                    enum EXTRACTOR_MetaType type,
377                                    const char *data, size_t data_size);
378
379
380 /**
381  * @ingroup metadata
382  * Remove all items in the container.
383  *
384  * @param md metadata to manipulate
385  */
386 void
387 GNUNET_CONTAINER_meta_data_clear (struct GNUNET_CONTAINER_MetaData *md);
388
389
390 /**
391  * @ingroup metadata
392  * Add the current time as the publication date
393  * to the meta-data.
394  *
395  * @param md metadata to modify
396  */
397 void
398 GNUNET_CONTAINER_meta_data_add_publication_date (struct
399                                                  GNUNET_CONTAINER_MetaData *md);
400
401
402 /**
403  * @ingroup metadata
404  * Iterate over MD entries.
405  *
406  * @param md metadata to inspect
407  * @param iter function to call on each entry, return 0 to continue to iterate
408  *             and 1 to abort iteration in this function (GNU libextractor API!)
409  * @param iter_cls closure for @a iter
410  * @return number of entries
411  */
412 int
413 GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData *md,
414                                     EXTRACTOR_MetaDataProcessor iter,
415                                     void *iter_cls);
416
417
418 /**
419  * @ingroup metadata
420  * Get the first MD entry of the given type.  Caller
421  * is responsible for freeing the return value.
422  * Also, only meta data items that are strings (0-terminated)
423  * are returned by this function.
424  *
425  * @param md metadata to inspect
426  * @param type type to look for
427  * @return NULL if no entry was found
428  */
429 char *
430 GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData
431                                         *md, enum EXTRACTOR_MetaType type);
432
433
434 /**
435  * @ingroup metadata
436  * Get the first matching MD entry of the given types. Caller is
437  * responsible for freeing the return value.  Also, only meta data
438  * items that are strings (0-terminated) are returned by this
439  * function.
440  *
441  * @param md metadata to inspect
442  * @param ... -1-terminated list of types
443  * @return NULL if we do not have any such entry,
444  *  otherwise client is responsible for freeing the value!
445  */
446 char *
447 GNUNET_CONTAINER_meta_data_get_first_by_types (const struct
448                                                GNUNET_CONTAINER_MetaData *md,
449                                                ...);
450
451 /**
452  * @ingroup metadata
453  * Get a thumbnail from the meta-data (if present).  Only matches meta
454  * data with mime type "image" and binary format.
455  *
456  * @param md metadata to inspect
457  * @param thumb will be set to the thumbnail data.  Must be
458  *        freed by the caller!
459  * @return number of bytes in thumbnail, 0 if not available
460  */
461 size_t
462 GNUNET_CONTAINER_meta_data_get_thumbnail (const struct GNUNET_CONTAINER_MetaData
463                                           *md, unsigned char **thumb);
464
465
466
467 /**
468  * @ingroup metadata
469  * Options for metadata serialization.
470  */
471 enum GNUNET_CONTAINER_MetaDataSerializationOptions
472 {
473   /**
474    * @ingroup metadata
475    * Serialize all of the data.
476    */
477   GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL = 0,
478
479   /**
480    * @ingroup metadata
481    * If not enough space is available, it is acceptable
482    * to only serialize some of the metadata.
483    */
484   GNUNET_CONTAINER_META_DATA_SERIALIZE_PART = 1,
485
486   /**
487    * @ingroup metadata
488    * Speed is of the essence, do not allow compression.
489    */
490   GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS = 2
491 };
492
493
494 /**
495  * @ingroup metadata
496  * Serialize meta-data to target.
497  *
498  * @param md metadata to serialize
499  * @param target where to write the serialized metadata;
500  *         *target can be NULL, in which case memory is allocated
501  * @param max maximum number of bytes available
502  * @param opt is it ok to just write SOME of the
503  *        meta-data to match the size constraint,
504  *        possibly discarding some data?
505  * @return number of bytes written on success,
506  *         -1 on error (typically: not enough
507  *         space)
508  */
509 ssize_t
510 GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData
511                                       *md, char **target, size_t max,
512                                       enum
513                                       GNUNET_CONTAINER_MetaDataSerializationOptions
514                                       opt);
515
516
517 /**
518  * @ingroup metadata
519  * Get the size of the full meta-data in serialized form.
520  *
521  * @param md metadata to inspect
522  * @return number of bytes needed for serialization, -1 on error
523  */
524 ssize_t
525 GNUNET_CONTAINER_meta_data_get_serialized_size (const struct
526                                                 GNUNET_CONTAINER_MetaData *md);
527
528
529 /**
530  * @ingroup metadata
531  * Deserialize meta-data.  Initializes md.
532  *
533  * @param input serialized meta-data.
534  * @param size number of bytes available
535  * @return MD on success, NULL on error (i.e.
536  *         bad format)
537  */
538 struct GNUNET_CONTAINER_MetaData *
539 GNUNET_CONTAINER_meta_data_deserialize (const char *input, size_t size);
540
541
542 /* ******************************* HashMap **************************** */
543
544 /**
545  * @ingroup hashmap
546  * Opaque handle for a HashMap.
547  */
548 struct GNUNET_CONTAINER_MultiHashMap;
549
550 /**
551  * @ingroup hashmap
552  * Opaque handle to an iterator over
553  * a multihashmap.
554  */
555 struct GNUNET_CONTAINER_MultiHashMapIterator;
556
557 /**
558  * @ingroup hashmap
559  * Options for storing values in the HashMap.
560  */
561 enum GNUNET_CONTAINER_MultiHashMapOption
562 {
563
564   /**
565    * @ingroup hashmap
566    * If a value with the given key exists, replace it.  Note that the
567    * old value would NOT be freed by replace (the application has to
568    * make sure that this happens if required).
569    */
570   GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE,
571
572   /**
573    * @ingroup hashmap
574    * Allow multiple values with the same key.
575    */
576   GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE,
577
578   /**
579    * @ingroup hashmap
580    * There must only be one value per key; storing a value should fail
581    * if a value under the same key already exists.
582    */
583   GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY,
584
585   /**
586    * @ingroup hashmap There must only be one value per key, but don't
587    * bother checking if a value already exists (faster than
588    * #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY; implemented
589    * just like #GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE but this
590    * option documents better what is intended if
591    * #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY is what is
592    * desired).
593    */
594   GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
595 };
596
597
598 /**
599  * @ingroup hashmap
600  * Iterator over hash map entries.
601  *
602  * @param cls closure
603  * @param key current key code
604  * @param value value in the hash map
605  * @return #GNUNET_YES if we should continue to
606  *         iterate,
607  *         #GNUNET_NO if not.
608  */
609 typedef int (*GNUNET_CONTAINER_HashMapIterator) (void *cls,
610                                                  const struct GNUNET_HashCode *key,
611                                                  void *value);
612
613
614 /**
615  * @ingroup hashmap
616  * Create a multi hash map.
617  *
618  * @param len initial size (map will grow as needed)
619  * @param do_not_copy_keys #GNUNET_NO is always safe and should be used by default;
620  *                         #GNUNET_YES means that on 'put', the 'key' does not have
621  *                         to be copied as the destination of the pointer is
622  *                         guaranteed to be life as long as the value is stored in
623  *                         the hashmap.  This can significantly reduce memory
624  *                         consumption, but of course is also a recipie for
625  *                         heap corruption if the assumption is not true.  Only
626  *                         use this if (1) memory use is important in this case and
627  *                         (2) you have triple-checked that the invariant holds
628  * @return NULL on error
629  */
630 struct GNUNET_CONTAINER_MultiHashMap *
631 GNUNET_CONTAINER_multihashmap_create (unsigned int len,
632                                       int do_not_copy_keys);
633
634
635 /**
636  * @ingroup hashmap
637  * Destroy a hash map.  Will not free any values
638  * stored in the hash map!
639  *
640  * @param map the map
641  */
642 void
643 GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap *map);
644
645
646 /**
647  * @ingroup hashmap
648  * Given a key find a value in the map matching the key.
649  *
650  * @param map the map
651  * @param key what to look for
652  * @return NULL if no value was found; note that
653  *   this is indistinguishable from values that just
654  *   happen to be NULL; use "contains" to test for
655  *   key-value pairs with value NULL
656  */
657 void *
658 GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap *map,
659                                    const struct GNUNET_HashCode *key);
660
661
662 /**
663  * @ingroup hashmap
664  * Remove the given key-value pair from the map.  Note that if the
665  * key-value pair is in the map multiple times, only one of the pairs
666  * will be removed.
667  *
668  * @param map the map
669  * @param key key of the key-value pair
670  * @param value value of the key-value pair
671  * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
672  *  is not in the map
673  */
674 int
675 GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
676                                       const struct GNUNET_HashCode *key,
677                                       const void *value);
678
679 /**
680  * @ingroup hashmap
681  * Remove all entries for the given key from the map.
682  * Note that the values would not be "freed".
683  *
684  * @param map the map
685  * @param key identifies values to be removed
686  * @return number of values removed
687  */
688 int
689 GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap *map,
690                                           const struct GNUNET_HashCode *key);
691
692
693 /**
694  * @ingroup hashmap
695  * Check if the map contains any value under the given
696  * key (including values that are NULL).
697  *
698  * @param map the map
699  * @param key the key to test if a value exists for it
700  * @return #GNUNET_YES if such a value exists,
701  *         #GNUNET_NO if not
702  */
703 int
704 GNUNET_CONTAINER_multihashmap_contains (const struct GNUNET_CONTAINER_MultiHashMap *map,
705                                         const struct GNUNET_HashCode * key);
706
707
708 /**
709  * @ingroup hashmap
710  * Check if the map contains the given value under the given
711  * key.
712  *
713  * @param map the map
714  * @param key the key to test if a value exists for it
715  * @param value value to test for
716  * @return #GNUNET_YES if such a value exists,
717  *         #GNUNET_NO if not
718  */
719 int
720 GNUNET_CONTAINER_multihashmap_contains_value (const struct GNUNET_CONTAINER_MultiHashMap *map,
721                                               const struct GNUNET_HashCode *key,
722                                               const void *value);
723
724
725 /**
726  * @ingroup hashmap
727  * Store a key-value pair in the map.
728  *
729  * @param map the map
730  * @param key key to use
731  * @param value value to use
732  * @param opt options for put
733  * @return #GNUNET_OK on success,
734  *         #GNUNET_NO if a value was replaced (with REPLACE)
735  *         #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
736  *                       value already exists
737  */
738 int
739 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
740                                    const struct GNUNET_HashCode *key,
741                                    void *value,
742                                    enum GNUNET_CONTAINER_MultiHashMapOption
743                                    opt);
744
745 /**
746  * @ingroup hashmap
747  * Get the number of key-value pairs in the map.
748  *
749  * @param map the map
750  * @return the number of key value pairs
751  */
752 unsigned int
753 GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap
754                                     *map);
755
756
757 /**
758  * @ingroup hashmap
759  * Iterate over all entries in the map.
760  *
761  * @param map the map
762  * @param it function to call on each entry
763  * @param it_cls extra argument to @a it
764  * @return the number of key value pairs processed,
765  *         #GNUNET_SYSERR if it aborted iteration
766  */
767 int
768 GNUNET_CONTAINER_multihashmap_iterate (const struct
769                                        GNUNET_CONTAINER_MultiHashMap *map,
770                                        GNUNET_CONTAINER_HashMapIterator it,
771                                        void *it_cls);
772
773
774 /**
775  * @ingroup hashmap
776  * Create an iterator for a multihashmap.
777  * The iterator can be used to retrieve all the elements in the multihashmap
778  * one by one, without having to handle all elements at once (in contrast to
779  * #GNUNET_CONTAINER_multihashmap_iterate).  Note that the iterator can not be
780  * used anymore if elements have been removed from 'map' after the creation of
781  * the iterator, or 'map' has been destroyed.  Adding elements to 'map' may
782  * result in skipped or repeated elements.
783  *
784  * @param map the map to create an iterator for
785  * @return an iterator over the given multihashmap @a map
786  */
787 struct GNUNET_CONTAINER_MultiHashMapIterator *
788 GNUNET_CONTAINER_multihashmap_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap *map);
789
790
791 /**
792  * @ingroup hashmap
793  * Retrieve the next element from the hash map at the iterator's
794  * position.  If there are no elements left, #GNUNET_NO is returned,
795  * and @a key and @a value are not modified.  This operation is only
796  * allowed if no elements have been removed from the multihashmap
797  * since the creation of @a iter, and the map has not been destroyed.
798  * Adding elements may result in repeating or skipping elements.
799  *
800  * @param iter the iterator to get the next element from
801  * @param key pointer to store the key in, can be NULL
802  * @param value pointer to store the value in, can be NULL
803  * @return #GNUNET_YES we returned an element,
804  *         #GNUNET_NO if we are out of elements
805  */
806 int
807 GNUNET_CONTAINER_multihashmap_iterator_next (struct GNUNET_CONTAINER_MultiHashMapIterator *iter,
808                                              struct GNUNET_HashCode *key,
809                                              const void **value);
810
811
812 /**
813  * @ingroup hashmap
814  * Destroy a multihashmap iterator.
815  *
816  * @param iter the iterator to destroy
817  */
818 void
819 GNUNET_CONTAINER_multihashmap_iterator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter);
820
821
822 /**
823  * @ingroup hashmap
824  * Iterate over all entries in the map that match a particular key.
825  *
826  * @param map the map
827  * @param key key that the entries must correspond to
828  * @param it function to call on each entry
829  * @param it_cls extra argument to @a it
830  * @return the number of key value pairs processed,
831  *         #GNUNET_SYSERR if it aborted iteration
832  */
833 int
834 GNUNET_CONTAINER_multihashmap_get_multiple (const struct GNUNET_CONTAINER_MultiHashMap *map,
835                                             const struct GNUNET_HashCode *key,
836                                             GNUNET_CONTAINER_HashMapIterator it,
837                                             void *it_cls);
838
839
840 /* ***************** Version of Multihashmap for peer identities ****************** */
841
842 /**
843  * @ingroup hashmap
844  * Iterator over hash map entries.
845  *
846  * @param cls closure
847  * @param key current public key
848  * @param value value in the hash map
849  * @return #GNUNET_YES if we should continue to
850  *         iterate,
851  *         #GNUNET_NO if not.
852  */
853 typedef int (*GNUNET_CONTAINER_PeerMapIterator) (void *cls,
854                                                  const struct GNUNET_PeerIdentity *key,
855                                                  void *value);
856
857
858 struct GNUNET_CONTAINER_MultiPeerMap;
859 /**
860  * @ingroup hashmap
861  * Create a multi peer map (hash map for public keys of peers).
862  *
863  * @param len initial size (map will grow as needed)
864  * @param do_not_copy_keys #GNUNET_NO is always safe and should be used by default;
865  *                         #GNUNET_YES means that on 'put', the 'key' does not have
866  *                         to be copied as the destination of the pointer is
867  *                         guaranteed to be life as long as the value is stored in
868  *                         the hashmap.  This can significantly reduce memory
869  *                         consumption, but of course is also a recipie for
870  *                         heap corruption if the assumption is not true.  Only
871  *                         use this if (1) memory use is important in this case and
872  *                         (2) you have triple-checked that the invariant holds
873  * @return NULL on error
874  */
875 struct GNUNET_CONTAINER_MultiPeerMap *
876 GNUNET_CONTAINER_multipeermap_create (unsigned int len,
877                                       int do_not_copy_keys);
878
879
880 /**
881  * @ingroup hashmap
882  * Destroy a hash map.  Will not free any values
883  * stored in the hash map!
884  *
885  * @param map the map
886  */
887 void
888 GNUNET_CONTAINER_multipeermap_destroy (struct GNUNET_CONTAINER_MultiPeerMap *map);
889
890
891 /**
892  * @ingroup hashmap
893  * Given a key find a value in the map matching the key.
894  *
895  * @param map the map
896  * @param key what to look for
897  * @return NULL if no value was found; note that
898  *   this is indistinguishable from values that just
899  *   happen to be NULL; use "contains" to test for
900  *   key-value pairs with value NULL
901  */
902 void *
903 GNUNET_CONTAINER_multipeermap_get (const struct GNUNET_CONTAINER_MultiPeerMap *map,
904                                    const struct GNUNET_PeerIdentity *key);
905
906
907 /**
908  * @ingroup hashmap
909  * Remove the given key-value pair from the map.  Note that if the
910  * key-value pair is in the map multiple times, only one of the pairs
911  * will be removed.
912  *
913  * @param map the map
914  * @param key key of the key-value pair
915  * @param value value of the key-value pair
916  * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
917  *  is not in the map
918  */
919 int
920 GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map,
921                                       const struct GNUNET_PeerIdentity * key,
922                                       const void *value);
923
924 /**
925  * @ingroup hashmap
926  * Remove all entries for the given key from the map.
927  * Note that the values would not be "freed".
928  *
929  * @param map the map
930  * @param key identifies values to be removed
931  * @return number of values removed
932  */
933 int
934 GNUNET_CONTAINER_multipeermap_remove_all (struct GNUNET_CONTAINER_MultiPeerMap *map,
935                                           const struct GNUNET_PeerIdentity *key);
936
937
938 /**
939  * @ingroup hashmap
940  * Check if the map contains any value under the given
941  * key (including values that are NULL).
942  *
943  * @param map the map
944  * @param key the key to test if a value exists for it
945  * @return #GNUNET_YES if such a value exists,
946  *         #GNUNET_NO if not
947  */
948 int
949 GNUNET_CONTAINER_multipeermap_contains (const struct GNUNET_CONTAINER_MultiPeerMap *map,
950                                         const struct GNUNET_PeerIdentity *key);
951
952
953 /**
954  * @ingroup hashmap
955  * Check if the map contains the given value under the given
956  * key.
957  *
958  * @param map the map
959  * @param key the key to test if a value exists for it
960  * @param value value to test for
961  * @return #GNUNET_YES if such a value exists,
962  *         #GNUNET_NO if not
963  */
964 int
965 GNUNET_CONTAINER_multipeermap_contains_value (const struct GNUNET_CONTAINER_MultiPeerMap *map,
966                                               const struct GNUNET_PeerIdentity * key,
967                                               const void *value);
968
969
970 /**
971  * @ingroup hashmap
972  * Store a key-value pair in the map.
973  *
974  * @param map the map
975  * @param key key to use
976  * @param value value to use
977  * @param opt options for put
978  * @return #GNUNET_OK on success,
979  *         #GNUNET_NO if a value was replaced (with REPLACE)
980  *         #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
981  *                       value already exists
982  */
983 int
984 GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map,
985                                    const struct GNUNET_PeerIdentity *key,
986                                    void *value,
987                                    enum GNUNET_CONTAINER_MultiHashMapOption opt);
988
989
990 /**
991  * @ingroup hashmap
992  * Get the number of key-value pairs in the map.
993  *
994  * @param map the map
995  * @return the number of key value pairs
996  */
997 unsigned int
998 GNUNET_CONTAINER_multipeermap_size (const struct GNUNET_CONTAINER_MultiPeerMap *map);
999
1000
1001 /**
1002  * @ingroup hashmap
1003  * Iterate over all entries in the map.
1004  *
1005  * @param map the map
1006  * @param it function to call on each entry
1007  * @param it_cls extra argument to @a it
1008  * @return the number of key value pairs processed,
1009  *         #GNUNET_SYSERR if it aborted iteration
1010  */
1011 int
1012 GNUNET_CONTAINER_multipeermap_iterate (const struct GNUNET_CONTAINER_MultiPeerMap *map,
1013                                        GNUNET_CONTAINER_PeerMapIterator it,
1014                                        void *it_cls);
1015
1016
1017 struct GNUNET_CONTAINER_MultiPeerMapIterator;
1018 /**
1019  * @ingroup hashmap
1020  * Create an iterator for a multihashmap.
1021  * The iterator can be used to retrieve all the elements in the multihashmap
1022  * one by one, without having to handle all elements at once (in contrast to
1023  * #GNUNET_CONTAINER_multipeermap_iterate).  Note that the iterator can not be
1024  * used anymore if elements have been removed from @a map after the creation of
1025  * the iterator, or 'map' has been destroyed.  Adding elements to @a map may
1026  * result in skipped or repeated elements.
1027  *
1028  * @param map the map to create an iterator for
1029  * @return an iterator over the given multihashmap @a map
1030  */
1031 struct GNUNET_CONTAINER_MultiPeerMapIterator *
1032 GNUNET_CONTAINER_multipeermap_iterator_create (const struct GNUNET_CONTAINER_MultiPeerMap *map);
1033
1034
1035 /**
1036  * @ingroup hashmap
1037  * Retrieve the next element from the hash map at the iterator's
1038  * position.  If there are no elements left, #GNUNET_NO is returned,
1039  * and @a key and @a value are not modified.  This operation is only
1040  * allowed if no elements have been removed from the multihashmap
1041  * since the creation of @a iter, and the map has not been destroyed.
1042  * Adding elements may result in repeating or skipping elements.
1043  *
1044  * @param iter the iterator to get the next element from
1045  * @param key pointer to store the key in, can be NULL
1046  * @param value pointer to store the value in, can be NULL
1047  * @return #GNUNET_YES we returned an element,
1048  *         #GNUNET_NO if we are out of elements
1049  */
1050 int
1051 GNUNET_CONTAINER_multipeermap_iterator_next (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter,
1052                                              struct GNUNET_PeerIdentity *key,
1053                                              const void **value);
1054
1055
1056 /**
1057  * @ingroup hashmap
1058  * Destroy a multipeermap iterator.
1059  *
1060  * @param iter the iterator to destroy
1061  */
1062 void
1063 GNUNET_CONTAINER_multipeermap_iterator_destroy (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter);
1064
1065
1066 /**
1067  * @ingroup hashmap
1068  * Iterate over all entries in the map that match a particular key.
1069  *
1070  * @param map the map
1071  * @param key public key that the entries must correspond to
1072  * @param it function to call on each entry
1073  * @param it_cls extra argument to @a it
1074  * @return the number of key value pairs processed,
1075  *         #GNUNET_SYSERR if it aborted iteration
1076  */
1077 int
1078 GNUNET_CONTAINER_multipeermap_get_multiple (const struct GNUNET_CONTAINER_MultiPeerMap *map,
1079                                             const struct GNUNET_PeerIdentity *key,
1080                                             GNUNET_CONTAINER_PeerMapIterator it,
1081                                             void *it_cls);
1082
1083
1084
1085 /* Version of multihashmap with 32 bit keys */
1086
1087 /**
1088  * @ingroup hashmap
1089  * Opaque handle for the 32-bit key HashMap.
1090  */
1091 struct GNUNET_CONTAINER_MultiHashMap32;
1092
1093
1094 /**
1095  * @ingroup hashmap
1096  * Opaque handle to an iterator over
1097  * a 32-bit key multihashmap.
1098  */
1099 struct GNUNET_CONTAINER_MultiHashMap32Iterator;
1100
1101
1102 /**
1103  * @ingroup hashmap
1104  * Iterator over hash map entries.
1105  *
1106  * @param cls closure
1107  * @param key current key code
1108  * @param value value in the hash map
1109  * @return #GNUNET_YES if we should continue to
1110  *         iterate,
1111  *         #GNUNET_NO if not.
1112  */
1113 typedef int (*GNUNET_CONTAINER_HashMapIterator32) (void *cls,
1114                                                    uint32_t key,
1115                                                    void *value);
1116
1117
1118 /**
1119  * @ingroup hashmap
1120  * Create a 32-bit key multi hash map.
1121  *
1122  * @param len initial size (map will grow as needed)
1123  * @return NULL on error
1124  */
1125 struct GNUNET_CONTAINER_MultiHashMap32 *
1126 GNUNET_CONTAINER_multihashmap32_create (unsigned int len);
1127
1128
1129 /**
1130  * @ingroup hashmap
1131  * Destroy a 32-bit key hash map.  Will not free any values
1132  * stored in the hash map!
1133  *
1134  * @param map the map
1135  */
1136 void
1137 GNUNET_CONTAINER_multihashmap32_destroy (struct GNUNET_CONTAINER_MultiHashMap32
1138                                          *map);
1139
1140
1141 /**
1142  * @ingroup hashmap
1143  * Get the number of key-value pairs in the map.
1144  *
1145  * @param map the map
1146  * @return the number of key value pairs
1147  */
1148 unsigned int
1149 GNUNET_CONTAINER_multihashmap32_size (const struct
1150                                       GNUNET_CONTAINER_MultiHashMap32 *map);
1151
1152
1153 /**
1154  * @ingroup hashmap
1155  * Given a key find a value in the map matching the key.
1156  *
1157  * @param map the map
1158  * @param key what to look for
1159  * @return NULL if no value was found; note that
1160  *   this is indistinguishable from values that just
1161  *   happen to be NULL; use "contains" to test for
1162  *   key-value pairs with value NULL
1163  */
1164 void *
1165 GNUNET_CONTAINER_multihashmap32_get (const struct
1166                                      GNUNET_CONTAINER_MultiHashMap32 *map,
1167                                      uint32_t key);
1168
1169
1170 /**
1171  * @ingroup hashmap
1172  * Iterate over all entries in the map.
1173  *
1174  * @param map the map
1175  * @param it function to call on each entry
1176  * @param it_cls extra argument to @a it
1177  * @return the number of key value pairs processed,
1178  *         #GNUNET_SYSERR if it aborted iteration
1179  */
1180 int
1181 GNUNET_CONTAINER_multihashmap32_iterate (const struct
1182                                          GNUNET_CONTAINER_MultiHashMap32 *map,
1183                                          GNUNET_CONTAINER_HashMapIterator32 it,
1184                                          void *it_cls);
1185
1186
1187 /**
1188  * @ingroup hashmap
1189  * Remove the given key-value pair from the map.  Note that if the
1190  * key-value pair is in the map multiple times, only one of the pairs
1191  * will be removed.
1192  *
1193  * @param map the map
1194  * @param key key of the key-value pair
1195  * @param value value of the key-value pair
1196  * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
1197  *  is not in the map
1198  */
1199 int
1200 GNUNET_CONTAINER_multihashmap32_remove (struct GNUNET_CONTAINER_MultiHashMap32 *map,
1201                                         uint32_t key,
1202                                         const void *value);
1203
1204
1205 /**
1206  * @ingroup hashmap
1207  * Remove all entries for the given key from the map.
1208  * Note that the values would not be "freed".
1209  *
1210  * @param map the map
1211  * @param key identifies values to be removed
1212  * @return number of values removed
1213  */
1214 int
1215 GNUNET_CONTAINER_multihashmap32_remove_all (struct GNUNET_CONTAINER_MultiHashMap32 *map,
1216                                             uint32_t key);
1217
1218
1219 /**
1220  * @ingroup hashmap
1221  * Check if the map contains any value under the given
1222  * key (including values that are NULL).
1223  *
1224  * @param map the map
1225  * @param key the key to test if a value exists for it
1226  * @return #GNUNET_YES if such a value exists,
1227  *         #GNUNET_NO if not
1228  */
1229 int
1230 GNUNET_CONTAINER_multihashmap32_contains (const struct GNUNET_CONTAINER_MultiHashMap32 *map,
1231                                           uint32_t key);
1232
1233
1234 /**
1235  * @ingroup hashmap
1236  * Check if the map contains the given value under the given
1237  * key.
1238  *
1239  * @param map the map
1240  * @param key the key to test if a value exists for it
1241  * @param value value to test for
1242  * @return #GNUNET_YES if such a value exists,
1243  *         #GNUNET_NO if not
1244  */
1245 int
1246 GNUNET_CONTAINER_multihashmap32_contains_value (const struct GNUNET_CONTAINER_MultiHashMap32 *map,
1247                                                 uint32_t key,
1248                                                 const void *value);
1249
1250
1251 /**
1252  * @ingroup hashmap
1253  * Store a key-value pair in the map.
1254  *
1255  * @param map the map
1256  * @param key key to use
1257  * @param value value to use
1258  * @param opt options for put
1259  * @return #GNUNET_OK on success,
1260  *         #GNUNET_NO if a value was replaced (with REPLACE)
1261  *         #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
1262  *                       value already exists
1263  */
1264 int
1265 GNUNET_CONTAINER_multihashmap32_put (struct GNUNET_CONTAINER_MultiHashMap32 *map,
1266                                      uint32_t key,
1267                                      void *value,
1268                                      enum GNUNET_CONTAINER_MultiHashMapOption opt);
1269
1270
1271 /**
1272  * @ingroup hashmap
1273  * Iterate over all entries in the map that match a particular key.
1274  *
1275  * @param map the map
1276  * @param key key that the entries must correspond to
1277  * @param it function to call on each entry
1278  * @param it_cls extra argument to @a it
1279  * @return the number of key value pairs processed,
1280  *         #GNUNET_SYSERR if it aborted iteration
1281  */
1282 int
1283 GNUNET_CONTAINER_multihashmap32_get_multiple (const struct GNUNET_CONTAINER_MultiHashMap32 *map,
1284                                               uint32_t key,
1285                                               GNUNET_CONTAINER_HashMapIterator32 it,
1286                                               void *it_cls);
1287
1288
1289 /**
1290  * Create an iterator for a 32-bit multihashmap.
1291  * The iterator can be used to retrieve all the elements in the multihashmap
1292  * one by one, without having to handle all elements at once (in contrast to
1293  * #GNUNET_CONTAINER_multihashmap32_iterate).  Note that the iterator can not be
1294  * used anymore if elements have been removed from 'map' after the creation of
1295  * the iterator, or 'map' has been destroyed.  Adding elements to 'map' may
1296  * result in skipped or repeated elements.
1297  *
1298  * @param map the map to create an iterator for
1299  * @return an iterator over the given multihashmap map
1300  */
1301 struct GNUNET_CONTAINER_MultiHashMap32Iterator *
1302 GNUNET_CONTAINER_multihashmap32_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap32 *map);
1303
1304
1305 /**
1306  * Retrieve the next element from the hash map at the iterator's position.
1307  * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
1308  * are not modified.
1309  * This operation is only allowed if no elements have been removed from the
1310  * multihashmap since the creation of 'iter', and the map has not been destroyed.
1311  * Adding elements may result in repeating or skipping elements.
1312  *
1313  * @param iter the iterator to get the next element from
1314  * @param key pointer to store the key in, can be NULL
1315  * @param value pointer to store the value in, can be NULL
1316  * @return #GNUNET_YES we returned an element,
1317  *         #GNUNET_NO if we are out of elements
1318  */
1319 int
1320 GNUNET_CONTAINER_multihashmap32_iterator_next (struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter,
1321                                                uint32_t *key,
1322                                                const void **value);
1323
1324
1325 /**
1326  * Destroy a 32-bit multihashmap iterator.
1327  *
1328  * @param iter the iterator to destroy
1329  */
1330 void
1331 GNUNET_CONTAINER_multihashmap32_iterator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter);
1332
1333
1334 /* ******************** doubly-linked list *************** */
1335 /* To avoid mistakes: head->prev == tail->next == NULL     */
1336
1337 /**
1338  * @ingroup dll
1339  * Insert an element at the head of a DLL. Assumes that head, tail and
1340  * element are structs with prev and next fields.
1341  *
1342  * @param head pointer to the head of the DLL
1343  * @param tail pointer to the tail of the DLL
1344  * @param element element to insert
1345  */
1346 #define GNUNET_CONTAINER_DLL_insert(head,tail,element) do { \
1347   GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
1348   GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
1349   (element)->next = (head); \
1350   (element)->prev = NULL; \
1351   if ((tail) == NULL) \
1352     (tail) = element; \
1353   else \
1354     (head)->prev = element; \
1355   (head) = (element); } while (0)
1356
1357
1358 /**
1359  * @ingroup dll
1360  * Insert an element at the tail of a DLL. Assumes that head, tail and
1361  * element are structs with prev and next fields.
1362  *
1363  * @param head pointer to the head of the DLL
1364  * @param tail pointer to the tail of the DLL
1365  * @param element element to insert
1366  */
1367 #define GNUNET_CONTAINER_DLL_insert_tail(head,tail,element) do { \
1368   GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
1369   GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
1370   (element)->prev = (tail); \
1371   (element)->next = NULL; \
1372   if ((head) == NULL) \
1373     (head) = element; \
1374   else \
1375     (tail)->next = element; \
1376   (tail) = (element); } while (0)
1377
1378
1379 /**
1380  * @ingroup dll
1381  * Insert an element into a DLL after the given other element.  Insert
1382  * at the head if the other element is NULL.
1383  *
1384  * @param head pointer to the head of the DLL
1385  * @param tail pointer to the tail of the DLL
1386  * @param other prior element, NULL for insertion at head of DLL
1387  * @param element element to insert
1388  */
1389 #define GNUNET_CONTAINER_DLL_insert_after(head,tail,other,element) do { \
1390   GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
1391   GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
1392   (element)->prev = (other); \
1393   if (NULL == other) \
1394     { \
1395       (element)->next = (head); \
1396       (head) = (element); \
1397     } \
1398   else \
1399     { \
1400       (element)->next = (other)->next; \
1401       (other)->next = (element); \
1402     } \
1403   if (NULL == (element)->next) \
1404     (tail) = (element); \
1405   else \
1406     (element)->next->prev = (element); } while (0)
1407
1408
1409 /**
1410  * @ingroup dll
1411  * Insert an element into a DLL before the given other element.  Insert
1412  * at the tail if the other element is NULL.
1413  *
1414  * @param head pointer to the head of the DLL
1415  * @param tail pointer to the tail of the DLL
1416  * @param other prior element, NULL for insertion at head of DLL
1417  * @param element element to insert
1418  */
1419 #define GNUNET_CONTAINER_DLL_insert_before(head,tail,other,element) do { \
1420   GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
1421   GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
1422   (element)->next = (other); \
1423   if (NULL == other) \
1424     { \
1425       (element)->prev = (tail); \
1426       (tail) = (element); \
1427     } \
1428   else \
1429     { \
1430       (element)->prev = (other)->prev; \
1431       (other)->prev = (element); \
1432     } \
1433   if (NULL == (element)->prev) \
1434     (head) = (element); \
1435   else \
1436     (element)->prev->next = (element); } while (0)
1437
1438
1439 /**
1440  * @ingroup dll
1441  * Remove an element from a DLL. Assumes that head, tail and
1442  * element point to structs with prev and next fields.
1443  *
1444  * Using the head or tail pointer as the element
1445  * argument does NOT work with this macro.
1446  * Make sure to store head/tail in another pointer
1447  * and use it to remove the head or tail of the list.
1448  *
1449  * @param head pointer to the head of the DLL
1450  * @param tail pointer to the tail of the DLL
1451  * @param element element to remove
1452  */
1453 #define GNUNET_CONTAINER_DLL_remove(head,tail,element) do { \
1454   GNUNET_assert ( ( (element)->prev != NULL) || ((head) == (element))); \
1455   GNUNET_assert ( ( (element)->next != NULL) || ((tail) == (element))); \
1456   if ((element)->prev == NULL) \
1457     (head) = (element)->next;  \
1458   else \
1459     (element)->prev->next = (element)->next; \
1460   if ((element)->next == NULL) \
1461     (tail) = (element)->prev;  \
1462   else \
1463     (element)->next->prev = (element)->prev; \
1464   (element)->next = NULL; \
1465   (element)->prev = NULL; } while (0)
1466
1467
1468 /* ************ Multi-DLL interface, allows DLL elements to be
1469    in multiple lists at the same time *********************** */
1470
1471 /**
1472  * @ingroup dll
1473  * Insert an element at the head of a MDLL. Assumes that head, tail and
1474  * element are structs with prev and next fields.
1475  *
1476  * @param mdll suffix name for the next and prev pointers in the element
1477  * @param head pointer to the head of the MDLL
1478  * @param tail pointer to the tail of the MDLL
1479  * @param element element to insert
1480  */
1481 #define GNUNET_CONTAINER_MDLL_insert(mdll,head,tail,element) do {       \
1482   GNUNET_assert ( ( (element)->prev_##mdll == NULL) && ((head) != (element))); \
1483   GNUNET_assert ( ( (element)->next_##mdll == NULL) && ((tail) != (element))); \
1484   (element)->next_##mdll = (head); \
1485   (element)->prev_##mdll = NULL; \
1486   if ((tail) == NULL) \
1487     (tail) = element; \
1488   else \
1489     (head)->prev_##mdll = element; \
1490   (head) = (element); } while (0)
1491
1492
1493 /**
1494  * @ingroup dll
1495  * Insert an element at the tail of a MDLL. Assumes that head, tail and
1496  * element are structs with prev and next fields.
1497  *
1498  * @param mdll suffix name for the next and prev pointers in the element
1499  * @param head pointer to the head of the MDLL
1500  * @param tail pointer to the tail of the MDLL
1501  * @param element element to insert
1502  */
1503 #define GNUNET_CONTAINER_MDLL_insert_tail(mdll,head,tail,element) do {  \
1504   GNUNET_assert ( ( (element)->prev_##mdll == NULL) && ((head) != (element))); \
1505   GNUNET_assert ( ( (element)->next_##mdll == NULL) && ((tail) != (element))); \
1506   (element)->prev_##mdll = (tail); \
1507   (element)->next_##mdll = NULL; \
1508   if ((head) == NULL) \
1509     (head) = element; \
1510   else \
1511     (tail)->next_##mdll = element; \
1512   (tail) = (element); } while (0)
1513
1514
1515 /**
1516  * @ingroup dll
1517  * Insert an element into a MDLL after the given other element.  Insert
1518  * at the head if the other element is NULL.
1519  *
1520  * @param mdll suffix name for the next and prev pointers in the element
1521  * @param head pointer to the head of the MDLL
1522  * @param tail pointer to the tail of the MDLL
1523  * @param other prior element, NULL for insertion at head of MDLL
1524  * @param element element to insert
1525  */
1526 #define GNUNET_CONTAINER_MDLL_insert_after(mdll,head,tail,other,element) do { \
1527   GNUNET_assert ( ( (element)->prev_##mdll == NULL) && ((head) != (element))); \
1528   GNUNET_assert ( ( (element)->next_##mdll == NULL) && ((tail) != (element))); \
1529   (element)->prev_##mdll = (other); \
1530   if (NULL == other) \
1531     { \
1532       (element)->next_##mdll = (head); \
1533       (head) = (element); \
1534     } \
1535   else \
1536     { \
1537       (element)->next_##mdll = (other)->next_##mdll; \
1538       (other)->next_##mdll = (element); \
1539     } \
1540   if (NULL == (element)->next_##mdll) \
1541     (tail) = (element); \
1542   else \
1543     (element)->next->prev_##mdll = (element); } while (0)
1544
1545
1546 /**
1547  * @ingroup dll
1548  * Insert an element into a MDLL before the given other element.  Insert
1549  * at the tail if the other element is NULL.
1550  *
1551  * @param mdll suffix name for the next and prev pointers in the element
1552  * @param head pointer to the head of the MDLL
1553  * @param tail pointer to the tail of the MDLL
1554  * @param other prior element, NULL for insertion at head of MDLL
1555  * @param element element to insert
1556  */
1557 #define GNUNET_CONTAINER_MDLL_insert_before(mdll,head,tail,other,element) do { \
1558   GNUNET_assert ( ( (element)->prev_##mdll == NULL) && ((head) != (element))); \
1559   GNUNET_assert ( ( (element)->next_##mdll == NULL) && ((tail) != (element))); \
1560   (element)->next_##mdll = (other); \
1561   if (NULL == other) \
1562     { \
1563       (element)->prev = (tail); \
1564       (tail) = (element); \
1565     } \
1566   else \
1567     { \
1568       (element)->prev_##mdll = (other)->prev_##mdll; \
1569       (other)->prev_##mdll = (element); \
1570     } \
1571   if (NULL == (element)->prev_##mdll) \
1572     (head) = (element); \
1573   else \
1574     (element)->prev_##mdll->next_##mdll = (element); } while (0)
1575
1576
1577 /**
1578  * @ingroup dll
1579  * Remove an element from a MDLL. Assumes
1580  * that head, tail and element are structs
1581  * with prev and next fields.
1582  *
1583  * @param mdll suffix name for the next and prev pointers in the element
1584  * @param head pointer to the head of the MDLL
1585  * @param tail pointer to the tail of the MDLL
1586  * @param element element to remove
1587  */
1588 #define GNUNET_CONTAINER_MDLL_remove(mdll,head,tail,element) do {       \
1589   GNUNET_assert ( ( (element)->prev_##mdll != NULL) || ((head) == (element))); \
1590   GNUNET_assert ( ( (element)->next_##mdll != NULL) || ((tail) == (element))); \
1591   if ((element)->prev_##mdll == NULL) \
1592     (head) = (element)->next_##mdll;  \
1593   else \
1594     (element)->prev_##mdll->next_##mdll = (element)->next_##mdll; \
1595   if ((element)->next_##mdll == NULL) \
1596     (tail) = (element)->prev_##mdll;  \
1597   else \
1598     (element)->next_##mdll->prev_##mdll = (element)->prev_##mdll; \
1599   (element)->next_##mdll = NULL; \
1600   (element)->prev_##mdll = NULL; } while (0)
1601
1602
1603
1604 /* ******************** Heap *************** */
1605
1606
1607 /**
1608  * @ingroup heap
1609  * Cost by which elements in a heap can be ordered.
1610  */
1611 typedef uint64_t GNUNET_CONTAINER_HeapCostType;
1612
1613
1614 /**
1615  * @ingroup heap
1616  * Heap type, either max or min.
1617  */
1618 enum GNUNET_CONTAINER_HeapOrder
1619 {
1620   /**
1621    * @ingroup heap
1622    * Heap with the maximum cost at the root.
1623    */
1624   GNUNET_CONTAINER_HEAP_ORDER_MAX,
1625
1626   /**
1627    * @ingroup heap
1628    * Heap with the minimum cost at the root.
1629    */
1630   GNUNET_CONTAINER_HEAP_ORDER_MIN
1631 };
1632
1633
1634 /**
1635  * @ingroup heap
1636  * Handle to a Heap.
1637  */
1638 struct GNUNET_CONTAINER_Heap;
1639
1640
1641 /**
1642  * @ingroup heap
1643  * Handle to a node in a heap.
1644  */
1645 struct GNUNET_CONTAINER_HeapNode;
1646
1647
1648 /**
1649  * @ingroup heap
1650  * Create a new heap.
1651  *
1652  * @param order how should the heap be sorted?
1653  * @return handle to the heap
1654  */
1655 struct GNUNET_CONTAINER_Heap *
1656 GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder order);
1657
1658
1659 /**
1660  * @ingroup heap
1661  * Destroys the heap.  Only call on a heap that
1662  * is already empty.
1663  *
1664  * @param heap heap to destroy
1665  */
1666 void
1667 GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap);
1668
1669
1670 /**
1671  * @ingroup heap
1672  * Get element stored at the root of @a heap.
1673  *
1674  * @param heap  Heap to inspect.
1675  * @return Element at the root, or NULL if heap is empty.
1676  */
1677 void *
1678 GNUNET_CONTAINER_heap_peek (const struct GNUNET_CONTAINER_Heap *heap);
1679
1680
1681 /**
1682  * Get @a element and @a cost stored at the root of @a heap.
1683  *
1684  * @param[in]  heap     Heap to inspect.
1685  * @param[out] element  Root element is returned here.
1686  * @param[out] cost     Cost of @a element is returned here.
1687  * @return #GNUNET_YES if an element is returned,
1688  *         #GNUNET_NO  if the heap is empty.
1689  */
1690 int
1691 GNUNET_CONTAINER_heap_peek2 (const struct GNUNET_CONTAINER_Heap *heap,
1692                              void **element,
1693                              GNUNET_CONTAINER_HeapCostType *cost);
1694
1695
1696 /**
1697  * @ingroup heap
1698  * Get the current size of the heap
1699  *
1700  * @param heap the heap to get the size of
1701  * @return number of elements stored
1702  */
1703 unsigned int
1704 GNUNET_CONTAINER_heap_get_size (const struct GNUNET_CONTAINER_Heap *heap);
1705
1706
1707 /**
1708  * @ingroup heap
1709  * Get the current cost of the node
1710  *
1711  * @param node the node to get the cost of
1712  * @return cost of the node
1713  */
1714 GNUNET_CONTAINER_HeapCostType
1715 GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode
1716                                      *node);
1717
1718 /**
1719  * @ingroup heap
1720  * Iterator for heap
1721  *
1722  * @param cls closure
1723  * @param node internal node of the heap
1724  * @param element value stored at the node
1725  * @param cost cost associated with the node
1726  * @return #GNUNET_YES if we should continue to iterate,
1727  *         #GNUNET_NO if not.
1728  */
1729 typedef int (*GNUNET_CONTAINER_HeapIterator) (void *cls,
1730                                               struct GNUNET_CONTAINER_HeapNode *
1731                                               node, void *element,
1732                                               GNUNET_CONTAINER_HeapCostType
1733                                               cost);
1734
1735
1736 /**
1737  * @ingroup heap
1738  * Iterate over all entries in the heap.
1739  *
1740  * @param heap the heap
1741  * @param iterator function to call on each entry
1742  * @param iterator_cls closure for @a iterator
1743  */
1744 void
1745 GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap,
1746                                GNUNET_CONTAINER_HeapIterator iterator,
1747                                void *iterator_cls);
1748
1749 /**
1750  * @ingroup heap
1751  * Perform a random walk of the tree.  The walk is biased
1752  * towards elements closer to the root of the tree (since
1753  * each walk starts at the root and ends at a random leaf).
1754  * The heap internally tracks the current position of the
1755  * walk.
1756  *
1757  * @param heap heap to walk
1758  * @return data stored at the next random node in the walk;
1759  *         NULL if the tree is empty.
1760  */
1761 void *
1762 GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap);
1763
1764
1765 /**
1766  * @ingroup heap
1767  * Inserts a new element into the heap.
1768  *
1769  * @param heap heap to modify
1770  * @param element element to insert
1771  * @param cost cost for the element
1772  * @return node for the new element
1773  */
1774 struct GNUNET_CONTAINER_HeapNode *
1775 GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, void *element,
1776                               GNUNET_CONTAINER_HeapCostType cost);
1777
1778
1779 /**
1780  * @ingroup heap
1781  * Remove root of the heap.
1782  *
1783  * @param heap heap to modify
1784  * @return element data stored at the root node
1785  */
1786 void *
1787 GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap);
1788
1789
1790 /**
1791  * @ingroup heap
1792  * Removes a node from the heap.
1793  *
1794  * @param node node to remove
1795  * @return element data stored at the node, NULL if heap is empty
1796  */
1797 void *
1798 GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node);
1799
1800
1801 /**
1802  * @ingroup heap
1803  * Updates the cost of any node in the tree
1804  *
1805  * @param heap heap to modify
1806  * @param node node for which the cost is to be changed
1807  * @param new_cost new cost for the node
1808  */
1809 void
1810 GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
1811                                    struct GNUNET_CONTAINER_HeapNode *node,
1812                                    GNUNET_CONTAINER_HeapCostType new_cost);
1813
1814
1815 /* ******************** Singly linked list *************** */
1816
1817 /**
1818  * Possible ways for how data stored in the linked list
1819  * might be allocated.
1820  * @deprecated use DLL macros
1821  */
1822 enum GNUNET_CONTAINER_SListDisposition
1823 {
1824   /**
1825    * Single-linked list must copy the buffer.
1826    * @deprecated use DLL macros
1827    */
1828   GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT = 0,
1829
1830   /**
1831    * Data is static, no need to copy or free.
1832    * @deprecated use DLL macros
1833    */
1834   GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC = 2,
1835
1836   /**
1837    * Data is dynamic, do not copy but free when done.
1838    * @deprecated use DLL macros
1839    */
1840   GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC = 4
1841 };
1842
1843 struct GNUNET_CONTAINER_SList_Elem;
1844
1845
1846 /**
1847  * Handle to a singly linked list
1848  * @deprecated use DLL macros
1849  */
1850 struct GNUNET_CONTAINER_SList;
1851
1852 /**
1853  * Handle to a singly linked list iterator
1854  * @deprecated use DLL macros
1855  */
1856 struct GNUNET_CONTAINER_SList_Iterator
1857 {
1858   /**
1859    * Linked list that we are iterating over.
1860    */
1861   struct GNUNET_CONTAINER_SList *list;
1862
1863   /**
1864    * Last element accessed.
1865    */
1866   struct GNUNET_CONTAINER_SList_Elem *last;
1867
1868   /**
1869    * Current list element.
1870    */
1871   struct GNUNET_CONTAINER_SList_Elem *elem;
1872 };
1873
1874
1875
1876 /**
1877  * Add a new element to the list
1878  * @param l list
1879  * @param disp memory disposition
1880  * @param buf payload buffer
1881  * @param len length of the buffer
1882  * @deprecated use DLL macros
1883  */
1884 void
1885 GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
1886                             enum GNUNET_CONTAINER_SListDisposition disp,
1887                             const void *buf, size_t len);
1888
1889
1890 /**
1891  * Add a new element to the end of the list
1892  * @param l list
1893  * @param disp memory disposition
1894  * @param buf payload buffer
1895  * @param len length of the buffer
1896  * @deprecated use DLL macros
1897  */
1898 void
1899 GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
1900                                 enum GNUNET_CONTAINER_SListDisposition disp,
1901                                 const void *buf, size_t len);
1902
1903
1904 /**
1905  * Append a singly linked list to another
1906  * @param dst list to append to
1907  * @param src source
1908  * @deprecated use DLL macros
1909  */
1910 void
1911 GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst,
1912                                struct GNUNET_CONTAINER_SList *src);
1913
1914
1915 /**
1916  * Create a new singly linked list
1917  * @return the new list
1918  * @deprecated use DLL macros
1919  */
1920 struct GNUNET_CONTAINER_SList *
1921 GNUNET_CONTAINER_slist_create (void);
1922
1923
1924 /**
1925  * Destroy a singly linked list
1926  * @param l the list to be destroyed
1927  * @deprecated use DLL macros
1928  */
1929 void
1930 GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l);
1931
1932
1933 /**
1934  * Return the beginning of a list
1935  *
1936  * @param l list
1937  * @return iterator pointing to the beginning (by value! Either allocate the
1938  *   structure on the stack, or use GNUNET_malloc() yourself! All other
1939  *   functions do take pointer to this struct though)
1940  * @deprecated use DLL macros
1941  */
1942 struct GNUNET_CONTAINER_SList_Iterator
1943 GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l);
1944
1945
1946 /**
1947  * Clear a list
1948  *
1949  * @param l list
1950  * @deprecated use DLL macros
1951  */
1952 void
1953 GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l);
1954
1955
1956 /**
1957  * Check if a list contains a certain element
1958  * @param l list
1959  * @param buf payload buffer to find
1960  * @param len length of the payload (number of bytes in buf)
1961  * @return GNUNET_YES if found, GNUNET_NO otherwise
1962  * @deprecated use DLL macros
1963  */
1964 int
1965 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
1966                                  const void *buf, size_t len);
1967
1968 /**
1969  * Check if a list contains a certain element using 'compare' function
1970  *
1971  * @param l list
1972  * @param buf payload buffer to find
1973  * @param len length of the payload (number of bytes in buf)
1974  * @param compare comparison function
1975  *
1976  * @return NULL if the 'buf' could not be found, pointer to the
1977  *         list element, if found
1978  * @deprecated use DLL macros
1979  */
1980 void *
1981 GNUNET_CONTAINER_slist_contains2 (const struct GNUNET_CONTAINER_SList *l,
1982                                   const void *buf, size_t len,
1983                                   int (*compare)(const void *, const size_t, const void *, const size_t));
1984 /**
1985  * Count the elements of a list
1986  * @param l list
1987  * @return number of elements in the list
1988  * @deprecated use DLL macros
1989  */
1990 int
1991 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l);
1992
1993
1994 /**
1995  * Remove an element from the list
1996  * @param i iterator that points to the element to be removed
1997  * @deprecated use DLL macros
1998  */
1999 void
2000 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i);
2001
2002
2003 /**
2004  * Insert an element into a list at a specific position
2005  * @param before where to insert the new element
2006  * @param disp memory disposition
2007  * @param buf payload buffer
2008  * @param len length of the payload
2009  * @deprecated use DLL macros
2010  */
2011 void
2012 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
2013                                enum GNUNET_CONTAINER_SListDisposition disp,
2014                                const void *buf, size_t len);
2015
2016
2017 /**
2018  * Advance an iterator to the next element
2019  * @param i iterator
2020  * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
2021  * @deprecated use DLL macros
2022  */
2023 int
2024 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i);
2025
2026
2027 /**
2028  * Check if an iterator points beyond the end of a list
2029  * @param i iterator
2030  * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
2031  *         points to a valid element
2032  * @deprecated use DLL macros
2033  */
2034 int
2035 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i);
2036
2037
2038 /**
2039  * Retrieve the element at a specific position in a list
2040  *
2041  * @param i iterator
2042  * @param len set to the payload length
2043  * @return payload
2044  * @deprecated use DLL macros
2045  */
2046 void *
2047 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
2048                             size_t *len);
2049
2050
2051 /**
2052  * Release an iterator
2053  * @param i iterator
2054  * @deprecated use DLL macros
2055  */
2056 void
2057 GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator *i);
2058
2059
2060 #if 0                           /* keep Emacsens' auto-indent happy */
2061 {
2062 #endif
2063 #ifdef __cplusplus
2064 }
2065 #endif
2066
2067
2068 /* ifndef GNUNET_CONTAINER_LIB_H */
2069 #endif
2070 /* end of gnunet_container_lib.h */