1 | /*
|
---|
2 | * Copyright (C) 2008-2010 Timothy D. Morgan
|
---|
3 | *
|
---|
4 | * This program is free software; you can redistribute it and/or modify
|
---|
5 | * it under the terms of the GNU General Public License as published by
|
---|
6 | * the Free Software Foundation; version 3 of the License.
|
---|
7 | *
|
---|
8 | * This program is distributed in the hope that it will be useful,
|
---|
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
11 | * GNU General Public License for more details.
|
---|
12 | *
|
---|
13 | * You should have received a copy of the GNU General Public License
|
---|
14 | * along with this program; if not, write to the Free Software
|
---|
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
16 | *
|
---|
17 | * $Id: range_list.h 253 2011-06-13 02:27:42Z tim $
|
---|
18 | */
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * @file
|
---|
22 | *
|
---|
23 | * A data structure which stores a list of address ranges.
|
---|
24 | *
|
---|
25 | * range_lists support basic in-place modifications and maintain the address
|
---|
26 | * space in sorted order. Looking up a range_list_element is implemented
|
---|
27 | * through binary search.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #ifndef _RANGE_LIST_H
|
---|
31 | #define _RANGE_LIST_H
|
---|
32 |
|
---|
33 | #include <stdlib.h>
|
---|
34 | #include <stdbool.h>
|
---|
35 | #include <stdint.h>
|
---|
36 | #include <string.h>
|
---|
37 | #include <math.h>
|
---|
38 | #include <talloc.h>
|
---|
39 |
|
---|
40 | #include "compat.h"
|
---|
41 |
|
---|
42 | typedef struct _range_list_element
|
---|
43 | {
|
---|
44 | uint32_t offset;
|
---|
45 | uint32_t length;
|
---|
46 | void* data;
|
---|
47 | } range_list_element;
|
---|
48 |
|
---|
49 |
|
---|
50 | /** XXX: document this. */
|
---|
51 | typedef struct _range_list
|
---|
52 | {
|
---|
53 | range_list_element** elements;
|
---|
54 | uint32_t elem_alloced;
|
---|
55 | uint32_t size;
|
---|
56 | } range_list;
|
---|
57 |
|
---|
58 |
|
---|
59 | /** Allocates a new range_list.
|
---|
60 | *
|
---|
61 | * @return A newly allocated range_list, or NULL if an error occurred.
|
---|
62 | */
|
---|
63 | _EXPORT()
|
---|
64 | range_list* range_list_new();
|
---|
65 |
|
---|
66 |
|
---|
67 | /** Frees the memory associated with a range_list, including the elements, but
|
---|
68 | * not any data parameters referenced by those elements.
|
---|
69 | *
|
---|
70 | * If rl is NULL, does nothing.
|
---|
71 | *
|
---|
72 | * @param rl the range_list to be free()d.
|
---|
73 | */
|
---|
74 | _EXPORT()
|
---|
75 | void range_list_free(range_list* rl);
|
---|
76 |
|
---|
77 |
|
---|
78 | /** Query the current number of elements on a range_list
|
---|
79 | *
|
---|
80 | * @param rl the range_list to query
|
---|
81 | *
|
---|
82 | * @return The number of elements currently in the list.
|
---|
83 | */
|
---|
84 | _EXPORT()
|
---|
85 | uint32_t range_list_size(const range_list* rl);
|
---|
86 |
|
---|
87 |
|
---|
88 | /** Adds an element to the range_list.
|
---|
89 | *
|
---|
90 | * The new element must not overlap with others.
|
---|
91 | * NOTE: this is a slow operation.
|
---|
92 | *
|
---|
93 | * @param rl the range list to update
|
---|
94 | * @param offset the starting point for the range
|
---|
95 | * @param length the length of the range
|
---|
96 | * @param data misc data associated with this range element
|
---|
97 | *
|
---|
98 | * @return true on success, false on failure.
|
---|
99 | *
|
---|
100 | * Failures can occur due to memory limitations, max_size limitations,
|
---|
101 | * or if the submitted range overlaps with an existing element. Other
|
---|
102 | * errors may also be possible.
|
---|
103 | */
|
---|
104 | _EXPORT()
|
---|
105 | bool range_list_add(range_list* rl, uint32_t offset, uint32_t length, void* data);
|
---|
106 |
|
---|
107 |
|
---|
108 | /** Removes an element from the list.
|
---|
109 | *
|
---|
110 | * The element data structure will be freed, but the data property will not be.
|
---|
111 | *
|
---|
112 | * @param rl the range_list to modify
|
---|
113 | * @param index the element index to remove
|
---|
114 | *
|
---|
115 | * @return true if the element was successfully removed, false otherwise.
|
---|
116 | */
|
---|
117 | _EXPORT()
|
---|
118 | bool range_list_remove(range_list* rl, uint32_t index);
|
---|
119 |
|
---|
120 |
|
---|
121 | /** Retrieves the element for a given index.
|
---|
122 | *
|
---|
123 | * @param rl the range_list being queried.
|
---|
124 | * @param index the element index desired.
|
---|
125 | *
|
---|
126 | * @return The element for a given index, or NULL if the element is not
|
---|
127 | * available.
|
---|
128 | */
|
---|
129 | _EXPORT()
|
---|
130 | const range_list_element* range_list_get(const range_list* rl, uint32_t index);
|
---|
131 |
|
---|
132 |
|
---|
133 | /** Attempts to find the unique element whose range encompasses offset.
|
---|
134 | *
|
---|
135 | * @param rl the range_list being queried.
|
---|
136 | * @param offset the location for which an element is desired.
|
---|
137 | *
|
---|
138 | * @return A matching element index or a negative value if none could be found.
|
---|
139 | */
|
---|
140 | _EXPORT()
|
---|
141 | int32_t range_list_find(const range_list* rl, uint32_t offset);
|
---|
142 |
|
---|
143 |
|
---|
144 | /** Same as range_list_find(), but returns the data associated with an element.
|
---|
145 | *
|
---|
146 | * @param rl the range_list being queried.
|
---|
147 | * @param offset the address to search for in the ranges
|
---|
148 | *
|
---|
149 | * @return The data element of the matching element index or NULL if none could
|
---|
150 | * be found.
|
---|
151 | *
|
---|
152 | * NOTE: May also return NULL if an element matched but the data
|
---|
153 | * element was never set.
|
---|
154 | */
|
---|
155 | _EXPORT()
|
---|
156 | void* range_list_find_data(const range_list* rl, uint32_t offset);
|
---|
157 |
|
---|
158 |
|
---|
159 | /** Splits an existing element into two elements in place.
|
---|
160 | *
|
---|
161 | * The resulting list will contain an additional element whose offset
|
---|
162 | * is the one provided and whose length extends to the end of the old element
|
---|
163 | * (the one identified by the index). The original element's offset will
|
---|
164 | * remain the same while it's length is shortened such that it is contiguous
|
---|
165 | * with the newly created element. The newly created element will have an index
|
---|
166 | * of one more than the current element.
|
---|
167 | *
|
---|
168 | * Both the original element and the newly created element will reference the
|
---|
169 | * original element's data.
|
---|
170 | *
|
---|
171 | * @param rl the range_list to modify
|
---|
172 | * @param index the index of the element to be split
|
---|
173 | * @param offset the at which the element will be split
|
---|
174 | *
|
---|
175 | * @return true if the element was successfully split, false otherwise.
|
---|
176 | */
|
---|
177 | _EXPORT()
|
---|
178 | bool range_list_split_element(range_list* rl, uint32_t index, uint32_t offset);
|
---|
179 |
|
---|
180 |
|
---|
181 | /** Determines whether or not a specified range exists contiguously within the
|
---|
182 | * range_list.
|
---|
183 | *
|
---|
184 | * @param rl the range_list to search
|
---|
185 | * @param start the offset at the beginning of the range
|
---|
186 | * @param length the length of the range
|
---|
187 | *
|
---|
188 | * @return true if the specified range exists and is complete, false otherwise.
|
---|
189 | */
|
---|
190 | _EXPORT()
|
---|
191 | bool range_list_has_range(range_list* rl, uint32_t start, uint32_t length);
|
---|
192 |
|
---|
193 | #endif
|
---|