1 | /* |
---|
2 | * Copyright (C) 2008 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 122 2008-08-09 20:24:01Z tim $ |
---|
18 | */ |
---|
19 | |
---|
20 | #include <stdlib.h> |
---|
21 | #include <stdbool.h> |
---|
22 | #include <stdint.h> |
---|
23 | #include <string.h> |
---|
24 | |
---|
25 | #ifndef _RANGE_LIST_H |
---|
26 | #define _RANGE_LIST_H |
---|
27 | |
---|
28 | |
---|
29 | typedef struct _range_list_element |
---|
30 | { |
---|
31 | uint32_t offset; |
---|
32 | uint32_t length; |
---|
33 | void* data; |
---|
34 | } range_list_element; |
---|
35 | |
---|
36 | |
---|
37 | typedef struct _range_list |
---|
38 | { |
---|
39 | range_list_element** elements; |
---|
40 | uint32_t elem_alloced; |
---|
41 | uint32_t size; |
---|
42 | } range_list; |
---|
43 | |
---|
44 | |
---|
45 | /* range_list_new(): |
---|
46 | * Allocates a new range_list. |
---|
47 | * |
---|
48 | * Returns: |
---|
49 | * A newly allocated range_list, or NULL if an error occurred. |
---|
50 | */ |
---|
51 | range_list* range_list_new(); |
---|
52 | |
---|
53 | |
---|
54 | /* range_list_free(): |
---|
55 | * Frees the memory associated with a range_list, including the elements, but |
---|
56 | * not any data parameters referenced by those elements. If rl is NULL, does |
---|
57 | * nothing. |
---|
58 | * |
---|
59 | * Arguments: |
---|
60 | * rl -- the range_list to be free()d. |
---|
61 | */ |
---|
62 | void range_list_free(range_list* rl); |
---|
63 | |
---|
64 | |
---|
65 | /* range_list_size(): |
---|
66 | * Query the current number of elements on a range_list |
---|
67 | * |
---|
68 | * Arguments: |
---|
69 | * rl -- the range_list to query |
---|
70 | * |
---|
71 | * Returns: |
---|
72 | * The number of elements currently in the list. |
---|
73 | */ |
---|
74 | uint32_t range_list_size(const range_list* rl); |
---|
75 | |
---|
76 | |
---|
77 | /* range_list_add(): |
---|
78 | * Adds an element to the range_list. |
---|
79 | * The new element must not overlap with others. |
---|
80 | * NOTE: this is a slow operation. |
---|
81 | * |
---|
82 | * Arguments: |
---|
83 | * rl -- the range list to update |
---|
84 | * offset -- the starting point for the range |
---|
85 | * length -- the length of the range |
---|
86 | * data -- misc data associated with this range element |
---|
87 | * Returns: |
---|
88 | * true on success, false on failure. |
---|
89 | * Failures can occur due to memory limitations, max_size limitations, |
---|
90 | * or if the submitted range overlaps with an existing element. Other |
---|
91 | * errors may also be possible. |
---|
92 | */ |
---|
93 | bool range_list_add(range_list* rl, uint32_t offset, uint32_t length, void* data); |
---|
94 | |
---|
95 | |
---|
96 | /* range_list_remove(): |
---|
97 | * Removes an element from the list. The element data structure will be |
---|
98 | * freed, but the data property will not be. |
---|
99 | * |
---|
100 | * Arguments: |
---|
101 | * rl -- the range_list to modify |
---|
102 | * index -- the element index to remove |
---|
103 | * |
---|
104 | * Returns: |
---|
105 | * true if the element was successfully removed, false otherwise. |
---|
106 | */ |
---|
107 | bool range_list_remove(range_list* rl, uint32_t index); |
---|
108 | |
---|
109 | |
---|
110 | /* range_list_get(): |
---|
111 | * Retrieves the element for a given index. |
---|
112 | * |
---|
113 | * Arguments: |
---|
114 | * rl -- the range_list being queried. |
---|
115 | * index -- the element index desired. |
---|
116 | * |
---|
117 | * Returns: |
---|
118 | * The element for a given index, or NULL if the element is not available. |
---|
119 | */ |
---|
120 | const range_list_element* range_list_get(const range_list* rl, uint32_t index); |
---|
121 | |
---|
122 | |
---|
123 | /* range_list_find(): |
---|
124 | * Attempts to find the unique element whose range encompasses offset. |
---|
125 | * |
---|
126 | * Arguments: |
---|
127 | * rl -- the range_list being queried. |
---|
128 | * offset -- the location for which an element is desired. |
---|
129 | * |
---|
130 | * Returns: |
---|
131 | * A matching element index or a negative value if none could be found. |
---|
132 | */ |
---|
133 | int32_t range_list_find(const range_list* rl, uint32_t offset); |
---|
134 | |
---|
135 | |
---|
136 | /* range_list_find_data(): |
---|
137 | * Same as range_list_find(), but returns the data associated with an element. |
---|
138 | * |
---|
139 | * Arguments: |
---|
140 | * rl -- the range_list being queried. |
---|
141 | * offset -- the address to search for in the ranges |
---|
142 | * |
---|
143 | * Returns: |
---|
144 | * The data element of the matching element index or NULL if none could |
---|
145 | * be found. |
---|
146 | * |
---|
147 | * NOTE: May also return NULL if an element matched but if the data |
---|
148 | * element was never set. |
---|
149 | */ |
---|
150 | void* range_list_find_data(const range_list* rl, uint32_t offset); |
---|
151 | |
---|
152 | |
---|
153 | /* range_list_split_element(): |
---|
154 | * Splits an existing element into two elements in place. |
---|
155 | * |
---|
156 | * The resulting list will contain an additional element whose offset |
---|
157 | * is the one provided and whose length extends to the end of the old element |
---|
158 | * (the one identified by the index). The original element's offset will |
---|
159 | * remain the same while it's length is shortened such that it is contiguous |
---|
160 | * with the newly created element. The newly created element will have an index |
---|
161 | * of one more than the current element. |
---|
162 | * |
---|
163 | * Both the original element and the newly created element will reference the |
---|
164 | * original element's data. |
---|
165 | * |
---|
166 | * Arguments: |
---|
167 | * rl -- the range_list to modify |
---|
168 | * index -- the index of the element to be split |
---|
169 | * offset -- the at which the element will be split |
---|
170 | * |
---|
171 | * Returns: |
---|
172 | * true if the element was successfully split, false otherwise. |
---|
173 | * |
---|
174 | * |
---|
175 | */ |
---|
176 | bool range_list_split_element(range_list* rl, uint32_t index, uint32_t offset); |
---|
177 | |
---|
178 | |
---|
179 | /* range_list_has_range(): |
---|
180 | * Determines whether or not a specified range exists contiguously within the |
---|
181 | * range_list. |
---|
182 | * |
---|
183 | * Arguments: |
---|
184 | * rl -- the range_list to search |
---|
185 | * start -- the offset at the beginning of the range |
---|
186 | * length -- the length of the range |
---|
187 | * |
---|
188 | * Returns: |
---|
189 | * true if the specified range exists and is complete, false otherwise. |
---|
190 | */ |
---|
191 | bool range_list_has_range(range_list* rl, uint32_t start, uint32_t length); |
---|
192 | |
---|
193 | #endif |
---|