2 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 * Declaration of map, a binary tree.
44 #include <complib/cl_qmap.h>
45 #include <complib/cl_qpool.h>
48 # define BEGIN_C_DECLS extern "C" {
49 # define END_C_DECLS }
50 #else /* !__cplusplus */
51 # define BEGIN_C_DECLS
53 #endif /* __cplusplus */
56 /****h* Component Library/Map
61 * Map implements a binary tree that stores user objects. Each item stored
62 * in a map has a unique 64-bit key (duplicates are not allowed). Map
63 * provides the ability to efficiently search for an item given a key.
65 * Map may allocate memory when inserting objects, and can therefore fail
66 * operations due to insufficient memory. Use quick map in situations
67 * where such insertion failures cannot be tolerated.
69 * Map is not thread safe, and users must provide serialization when adding
70 * and removing items from the map.
72 * The map functions operates on a cl_map_t structure which should be treated
73 * as opaque and should be manipulated only through the provided functions.
80 * cl_map_t, cl_map_item_t, cl_map_obj_t
83 * cl_map_obj, cl_map_key
86 * cl_map_construct, cl_map_init, cl_map_destroy
89 * cl_map_end, cl_map_head, cl_map_tail, cl_map_next, cl_map_prev
92 * cl_map_insert, cl_map_get, cl_map_remove_item, cl_map_remove,
93 * cl_map_remove_all, cl_map_merge, cl_map_delta, cl_map_get_next
96 * cl_map_count, cl_is_map_empty, cl_is_map_inited
98 /****s* Component Library: Map/cl_map_t
103 * Quick map structure.
105 * The cl_map_t structure should be treated as opaque and should
106 * be manipulated only through the provided functions.
110 typedef struct _cl_map {
117 * Quick map object that maintains the map.
120 * Pool of cl_map_obj_t structures used to store user objects
127 /****d* Component Library: Map/cl_map_iterator_t
132 * Iterator type used to walk a map.
136 typedef const cl_map_item_t *cl_map_iterator_t;
139 * The iterator should be treated as opaque to prevent corrupting the map.
142 * Map, cl_map_head, cl_map_tail, cl_map_next, cl_map_prev, cl_map_key
145 /****f* Component Library: Map/cl_map_count
150 * The cl_map_count function returns the number of items stored
155 static inline size_t cl_map_count(IN const cl_map_t * const p_map)
158 return (cl_qmap_count(&p_map->qmap));
164 * [in] Pointer to a map whose item count to return.
167 * Returns the number of items stored in the map.
170 * Map, cl_is_map_empty
173 /****f* Component Library: Map/cl_is_map_empty
178 * The cl_is_map_empty function returns whether a map is empty.
182 static inline boolean_t cl_is_map_empty(IN const cl_map_t * const p_map)
185 return (cl_is_qmap_empty(&p_map->qmap));
191 * [in] Pointer to a map to test for emptiness.
194 * TRUE if the map is empty.
199 * Map, cl_map_count, cl_map_remove_all
202 /****f* Component Library: Map/cl_map_key
207 * The cl_map_key function retrieves the key value of a map item.
211 static inline uint64_t cl_map_key(IN const cl_map_iterator_t itor)
213 return (cl_qmap_key(itor));
219 * [in] Iterator for the item whose key to return.
222 * Returns the 64-bit key value for the specified iterator.
225 * The iterator specified by the itor parameter must have been retrived by
226 * a previous call to cl_map_head, cl_map_tail, cl_map_next, or cl_map_prev.
228 * The key value is set in a call to cl_map_insert.
231 * Map, cl_map_insert, cl_map_head, cl_map_tail, cl_map_next, cl_map_prev
234 /****f* Component Library: Map/cl_map_construct
239 * The cl_map_construct function constructs a map.
243 void cl_map_construct(IN cl_map_t * const p_map);
247 * [in] Pointer to a cl_map_t structure to construct.
250 * This function does not return a value.
253 * Allows calling cl_map_init, cl_map_destroy, and cl_is_map_inited.
255 * Calling cl_map_construct is a prerequisite to calling any other
256 * map function except cl_map_init.
259 * Map, cl_map_init, cl_map_destroy, cl_is_map_inited
262 /****f* Component Library: Event/cl_is_map_inited
267 * The cl_is_map_inited function returns whether a map was
268 * successfully initialized.
272 static inline boolean_t cl_is_map_inited(IN const cl_map_t * const p_map)
275 * The map's pool of map items is the last thing initialized.
276 * We can therefore use it to test for initialization.
278 return (cl_is_qpool_inited(&p_map->pool));
284 * [in] Pointer to a cl_map_t structure whose initialization state
288 * TRUE if the map was initialized successfully.
293 * Allows checking the state of a map to determine if invoking
294 * member functions is appropriate.
300 /****f* Component Library: Map/cl_map_init
305 * The cl_map_init function initialized a map for use.
309 cl_status_t cl_map_init(IN cl_map_t * const p_map, IN const uint32_t min_items);
313 * [in] Pointer to a cl_map_t structure to initialize.
316 * [in] Minimum number of items that can be stored. All necessary
317 * allocations to allow storing the minimum number of items is
318 * performed at initialization time.
321 * CL_SUCCESS if the map was initialized successfully.
324 * Allows calling map manipulation functions.
327 * Map, cl_map_destroy, cl_map_insert, cl_map_remove
330 /****f* Component Library: Map/cl_map_destroy
335 * The cl_map_destroy function destroys a map.
339 void cl_map_destroy(IN cl_map_t * const p_map);
343 * [in] Pointer to a map to destroy.
346 * This function does not return a value.
349 * Performs any necessary cleanup of the specified map. Further
350 * operations should not be attempted on the map. cl_map_destroy does
351 * not affect any of the objects stored in the map.
352 * This function should only be called after a call to cl_map_construct.
354 * In debug builds, cl_map_destroy asserts that the map is empty.
357 * Map, cl_map_construct, cl_map_init
360 /****f* Component Library: Map/cl_map_end
365 * The cl_map_end function returns the iterator for the end of a map.
369 static inline cl_map_iterator_t cl_map_end(IN const cl_map_t * const p_map)
372 return (cl_qmap_end(&p_map->qmap));
378 * [in] Pointer to a cl_map_t structure whose end to return.
381 * Iterator for the end of the map.
384 * cl_map_end is useful for determining the validity of map items returned
385 * by cl_map_head, cl_map_tail, cl_map_next, cl_map_prev. If the iterator
386 * by any of these functions compares to the end, the end of the map was
388 * When using cl_map_head or cl_map_tail, this condition indicates that
392 * Map, cl_qmap_head, cl_qmap_tail, cl_qmap_next, cl_qmap_prev
395 /****f* Component Library: Map/cl_map_head
400 * The cl_map_head function returns the map item with the lowest key
401 * value stored in a map.
405 static inline cl_map_iterator_t cl_map_head(IN const cl_map_t * const p_map)
408 return (cl_qmap_head(&p_map->qmap));
414 * [in] Pointer to a map whose item with the lowest key is returned.
417 * Iterator for the object with the lowest key in the map.
419 * Iterator for the map end if the map was empty.
422 * cl_map_head does not remove the object from the map.
425 * Map, cl_map_tail, cl_map_next, cl_map_prev, cl_map_end
428 /****f* Component Library: Map/cl_map_tail
433 * The cl_map_tail function returns the map item with the highest key
434 * value stored in a map.
438 static inline cl_map_iterator_t cl_map_tail(IN const cl_map_t * const p_map)
441 return (cl_qmap_tail(&p_map->qmap));
447 * [in] Pointer to a map whose item with the highest key
451 * Iterator for the object with the highest key in the map.
453 * Iterator for the map end if the map was empty.
456 * cl_map_end does no remove the object from the map.
459 * Map, cl_map_head, cl_map_next, cl_map_prev, cl_map_end
462 /****f* Component Library: Map/cl_map_next
467 * The cl_map_next function returns the map item with the next higher
468 * key value than a specified map item.
472 static inline cl_map_iterator_t cl_map_next(IN const cl_map_iterator_t itor)
475 return (cl_qmap_next(itor));
481 * [in] Iterator for an object in a map whose successor to return.
484 * Iterator for the object with the next higher key value in a map.
486 * Iterator for the map end if the specified object was the last item in
490 * The iterator must have been retrieved by a previous call to cl_map_head,
491 * cl_map_tail, cl_map_next, or cl_map_prev.
494 * Map, cl_map_head, cl_map_tail, cl_map_prev, cl_map_end
497 /****f* Component Library: Map/cl_map_prev
502 * The cl_map_prev function returns the map item with the next lower
503 * key value than a precified map item.
507 static inline cl_map_iterator_t cl_map_prev(IN const cl_map_iterator_t itor)
510 return (cl_qmap_prev(itor));
516 * [in] Iterator for an object in a map whose predecessor to return.
519 * Iterator for the object with the next lower key value in a map.
521 * Iterator for the map end if the specified object was the first item in
525 * The iterator must have been retrieved by a previous call to cl_map_head,
526 * cl_map_tail, cl_map_next, or cl_map_prev.
529 * Map, cl_map_head, cl_map_tail, cl_map_next, cl_map_end
532 /****f* Component Library: Map/cl_map_insert
537 * The cl_map_insert function inserts a map item into a map.
541 void *cl_map_insert(IN cl_map_t * const p_map,
542 IN const uint64_t key, IN const void *const p_object);
546 * [in] Pointer to a map into which to add the item.
549 * [in] Value to associate with the object.
552 * [in] Pointer to an object to insert into the map.
555 * Pointer to the object in the map with the specified key after the call
558 * NULL if there was not enough memory to insert the desired item.
561 * Insertion operations may cause the map to rebalance.
563 * If the map already contains an object already with the specified key,
564 * that object will not be replaced and the pointer to that object is
568 * Map, cl_map_remove, cl_map_item_t
571 /****f* Component Library: Map/cl_map_get
576 * The cl_map_get function returns the object associated with a key.
580 void *cl_map_get(IN const cl_map_t * const p_map, IN const uint64_t key);
584 * [in] Pointer to a map from which to retrieve the object with
588 * [in] Key value used to search for the desired object.
591 * Pointer to the object with the desired key value.
593 * NULL if there was no item with the desired key value stored in
597 * cl_map_get does not remove the item from the map.
600 * Map, cl_map_remove, cl_map_get_next
603 /****f* Component Library: Map/cl_map_get_next
608 * The cl_qmap_get_next function returns the first object associated with a
609 * key > the key specified.
613 void *cl_map_get_next(IN const cl_map_t * const p_map, IN const uint64_t key);
617 * [in] Pointer to a map from which to retrieve the object with
621 * [in] Key value used to search for the desired object.
624 * Pointer to the first object with a key > the desired key value.
626 * NULL if there was no item with a key > the desired key
627 * value stored in the map.
630 * cl_map_get does not remove the item from the map.
633 * Map, cl_map_remove, cl_map_get
636 /****f* Component Library: Map/cl_map_remove_item
641 * The cl_map_remove_item function removes the specified map item
647 cl_map_remove_item(IN cl_map_t * const p_map, IN const cl_map_iterator_t itor);
651 * [in] Pointer to a map from which to remove the object associated
652 * with the specified iterator.
655 * [in] Iterator for an object to remove from its map.
658 * This function does not return a value.
661 * Removes the object associated with the specifid iterator from its map.
663 * The specified iterator is no longer valid after the call completes.
665 * The iterator must have been retrieved by a previous call to cl_map_head,
666 * cl_map_tail, cl_map_next, or cl_map_prev.
669 * Map, cl_map_remove, cl_map_remove_all, cl_map_insert, cl_map_head,
670 * cl_map_tail, cl_map_next, cl_map_prev
673 /****f* Component Library: Map/cl_map_remove
678 * The cl_map_remove function removes the map item with the specified key
683 void *cl_map_remove(IN cl_map_t * const p_map, IN const uint64_t key);
687 * [in] Pointer to a cl_map_t structure from which to remove the
688 * item with the specified key.
691 * [in] Key value used to search for the object to remove.
694 * Pointer to the object associated with the specified key if
695 * it was found and removed.
697 * NULL if no object with the specified key exists in the map.
700 * Map, cl_map_remove_item, cl_map_remove_all, cl_map_insert
703 /****f* Component Library: Map/cl_map_remove_all
708 * The cl_map_remove_all function removes all objects from a map,
713 void cl_map_remove_all(IN cl_map_t * const p_map);
717 * [in] Pointer to a map to empty.
720 * This function does not return a value.
723 * Map, cl_map_remove, cl_map_remove_item
726 /****f* Component Library: Map/cl_map_obj
731 * The cl_map_obj function returns the object associated with an iterator.
735 static inline void *cl_map_obj(IN const cl_map_iterator_t itor)
737 return (cl_qmap_obj(PARENT_STRUCT(itor, cl_map_obj_t, item)));
743 * [in] Iterator whose object to return.
746 * Returns the value of the object pointer associated with the iterator.
748 * The iterator must have been retrieved by a previous call to cl_map_head,
749 * cl_map_tail, cl_map_next, or cl_map_prev.
752 * Map, cl_map_head, cl_map_tail, cl_map_next, cl_map_prev
755 /****f* Component Library: Map/cl_map_merge
760 * The cl_map_merge function moves all items from one map to another,
761 * excluding duplicates.
766 cl_map_merge(OUT cl_map_t * const p_dest_map,
767 IN OUT cl_map_t * const p_src_map);
771 * [out] Pointer to a cl_map_t structure to which items should be added.
774 * [in/out] Pointer to a cl_map_t structure whose items to add
778 * CL_SUCCESS if the operation succeeded.
780 * CL_INSUFFICIENT_MEMORY if there was not enough memory for the operation
784 * Items are evaluated based on their keys only.
786 * Upon return from cl_map_merge, the map referenced by p_src_map contains
787 * all duplicate items.
793 /****f* Component Library: Map/cl_map_delta
798 * The cl_map_delta function computes the differences between two maps.
803 cl_map_delta(IN OUT cl_map_t * const p_map1,
804 IN OUT cl_map_t * const p_map2,
805 OUT cl_map_t * const p_new, OUT cl_map_t * const p_old);
809 * [in/out] Pointer to the first of two cl_map_t structures whose
810 * differences to compute.
813 * [in/out] Pointer to the second of two cl_map_t structures whose
814 * differences to compute.
817 * [out] Pointer to an empty cl_map_t structure that contains the
818 * items unique to p_map2 upon return from the function.
821 * [out] Pointer to an empty cl_map_t structure that contains the
822 * items unique to p_map1 upon return from the function.
825 * CL_SUCCESS if the operation succeeded.
827 * CL_INSUFFICIENT_MEMORY if there was not enough memory for the operation
831 * Items are evaluated based on their keys. Items that exist in both
832 * p_map1 and p_map2 remain in their respective maps. Items that
833 * exist only p_map1 are moved to p_old. Likewise, items that exist only
834 * in p_map2 are moved to p_new. This function can be useful in evaluating
835 * changes between two maps.
837 * Both maps pointed to by p_new and p_old must be empty on input.
839 * Upon failure, all input maps are restored to their original state.
846 #endif /* _CL_MAP_H_ */