1/*
2Copyright (c) 2012, Broadcom Europe Ltd
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, 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
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON 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
25SOFTWARE, 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. */
36typedef 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. */
40typedef 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
53/** Create an empty list.
54 * The list is created based on the details provided, minimum capacity one entry.
55 *
56 * \param The initial capacity in entries.
57 * \param entry_size The size of each entry, in bytes.
58 * \param comparator A function for comparing two entries.
59 * \return The new list or NULL. */
60VC_CONTAINERS_LIST_T *vc_containers_list_create(uint32_t capacity, size_t entry_size, VC_CONTAINERS_LIST_COMPARATOR_T comparator);
61
62/** Destroy a list.
63 * Has no effect on a static list.
64 *
65 * \param list The list to be destroyed. */
66void vc_containers_list_destroy(VC_CONTAINERS_LIST_T *list);
67
68/** Reset a list to be empty.
69 * Has no effect on a static list.
70 *
71 * \param list The list to be reset. */
72void vc_containers_list_reset(VC_CONTAINERS_LIST_T *list);
73
74/** Insert an entry into the list.
75 *
76 * \param list The list.
77 * \param new_entry The new entry to be inserted.
78 * \param allow_duplicates Determines whether to insert or overwrite if there
79 * is an existing matching entry.
80 * \return True if the entry has successfully been inserted, false if the list
81 * needed to be enlarged and the memory allocation failed. */
82bool vc_containers_list_insert(VC_CONTAINERS_LIST_T *list, void *new_entry, bool allow_duplicates);
83
84/** Find an entry in the list and fill in the result.
85 * Searches for an entry in the list using the comparator and if found
86 * overwrites the one passed in with the one found.
87 *
88 * \param list The list to search.
89 * \param entry An entry with enough defined to find it in the list, filled in
90 * with the rest if found.
91 * \return True if found, false if not. */
92bool vc_containers_list_find_entry(const VC_CONTAINERS_LIST_T *list, void *entry);
93
94/** Validates a list pointer.
95 * Fields and contents of a list are checked and asserted to be correct. With a
96 * large list this may be slow, so it is recommended only to call this in debug
97 * builds.
98 *
99 * \param list The list to be validated. */
100void vc_containers_list_validate(const VC_CONTAINERS_LIST_T *list);
101
102#endif /* _VC_CONTAINERS_LIST_H_ */
103