1 /*
2 Copyright (c) 2012, Broadcom Europe Ltd
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in the
11       documentation and/or other materials provided with the distribution.
12     * Neither the name of the copyright holder nor the
13       names of its contributors may be used to endorse or promote products
14       derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef _VC_CONTAINERS_LIST_H_
29 #define _VC_CONTAINERS_LIST_H_
30 
31 #include "containers/containers.h"
32 
33 /** List entry comparison prototype.
34  * Returns zero if items at a and b match, positive if a is "bigger" than b and
35  * negative if a is "smaller" than b. */
36 typedef int (*VC_CONTAINERS_LIST_COMPARATOR_T)(const void *a, const void *b);
37 
38 /** Sorted list type.
39  * Storage type providing efficient insertion and search via binary sub-division. */
40 typedef struct vc_containers_list_tag
41 {
42    uint32_t size;                               /**< Number of defined entries in list */
43    uint32_t capacity;                           /**< Capacity of list, in entries, or zero for read-only */
44    size_t entry_size;                           /**< Size of one entry, in bytes */
45    VC_CONTAINERS_LIST_COMPARATOR_T comparator;  /**< Entry comparison function */
46    void *entries;                               /**< Pointer to array of entries */
47 } VC_CONTAINERS_LIST_T;
48 
49 /** Macro to generate a static, read-only list from an array and comparator */
50 #define VC_CONTAINERS_STATIC_LIST(L, A, C)  static VC_CONTAINERS_LIST_T L = { countof(A), 0, sizeof(*(A)), (VC_CONTAINERS_LIST_COMPARATOR_T)(C), A }
51 
52 /** Create an empty list.
53  * The list is created based on the details provided, minimum capacity one entry.
54  *
55  * \param The initial capacity in entries.
56  * \param entry_size The size of each entry, in bytes.
57  * \param comparator A function for comparing two entries.
58  * \return The new list or NULL. */
59 VC_CONTAINERS_LIST_T *vc_containers_list_create(uint32_t capacity, size_t entry_size, VC_CONTAINERS_LIST_COMPARATOR_T comparator);
60 
61 /** Destroy a list.
62  * Has no effect on a static list.
63  *
64  * \param list The list to be destroyed. */
65 void vc_containers_list_destroy(VC_CONTAINERS_LIST_T *list);
66 
67 /** Reset a list to be empty.
68  * Has no effect on a static list.
69  *
70  * \param list The list to be reset. */
71 void vc_containers_list_reset(VC_CONTAINERS_LIST_T *list);
72 
73 /** Insert an entry into the list.
74  *
75  * \param list The list.
76  * \param new_entry The new entry to be inserted.
77  * \param allow_duplicates Determines whether to insert or overwrite if there
78  *    is an existing matching entry.
79  * \return True if the entry has successfully been inserted, false if the list
80  *    needed to be enlarged and the memory allocation failed. */
81 bool vc_containers_list_insert(VC_CONTAINERS_LIST_T *list, void *new_entry, bool allow_duplicates);
82 
83 /** Find an entry in the list and fill in the result.
84  * Searches for an entry in the list using the comparator and if found
85  * overwrites the one passed in with the one found.
86  *
87  * \param list The list to search.
88  * \param entry An entry with enough defined to find it in the list, filled in
89  *    with the rest if found.
90  * \return True if found, false if not. */
91 bool vc_containers_list_find_entry(const VC_CONTAINERS_LIST_T *list, void *entry);
92 
93 /** Validates a list pointer.
94  * Fields and contents of a list are checked and asserted to be correct. With a
95  * large list this may be slow, so it is recommended only to call this in debug
96  * builds.
97  *
98  * \param list The list to be validated. */
99 void vc_containers_list_validate(const VC_CONTAINERS_LIST_T *list);
100 
101 #endif /* _VC_CONTAINERS_LIST_H_ */
102