source: trunk/include/range_list.h @ 216

Last change on this file since 216 was 201, checked in by tim, 14 years ago

changed symbol visibility to hidden by default and explicitly exported API functions

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