source: trunk/include/range_list.h @ 177

Last change on this file since 177 was 169, checked in by tim, 15 years ago

filled in additional, minimal documentation

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