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
RevLine 
[169]1/*
2 * Copyright (C) 2008-2010 Timothy D. Morgan
[168]3 *
[98]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 *
[122]17 * $Id: range_list.h 169 2010-03-03 19:24:58Z tim $
[98]18 */
19
[169]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
[148]30#ifndef _RANGE_LIST_H
31#define _RANGE_LIST_H
32
[98]33#include <stdlib.h>
34#include <stdbool.h>
35#include <stdint.h>
36#include <string.h>
[148]37#include <math.h>
38#include "talloc.h"
[98]39
40typedef struct _range_list_element
41{
42  uint32_t offset;
43  uint32_t length;
44  void* data;
45} range_list_element;
46
47
[169]48/** XXX: document this. */
[98]49typedef struct _range_list
50{
51  range_list_element** elements;
52  uint32_t elem_alloced;
53  uint32_t size;
54} range_list;
55
56
[169]57/** Allocates a new range_list.
[98]58 *
[169]59 * @return A newly allocated range_list, or NULL if an error occurred.
[98]60 */
61range_list* range_list_new();
62
63
[169]64/** Frees the memory associated with a range_list, including the elements, but
65 *  not any data parameters referenced by those elements. 
[98]66 *
[169]67 * If rl is NULL, does nothing.
68 *
69 * @param rl the range_list to be free()d.
[98]70 */
71void range_list_free(range_list* rl);
72
73
[169]74/** Query the current number of elements on a range_list
[98]75 *
[169]76 * @param rl the range_list to query
[98]77 *
[169]78 * @return The number of elements currently in the list.
[98]79 */
80uint32_t range_list_size(const range_list* rl);
81
82
[169]83/** Adds an element to the range_list. 
[98]84 *
[169]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]98 */
99bool range_list_add(range_list* rl, uint32_t offset, uint32_t length, void* data);
100
101
[169]102/** Removes an element from the list. 
[98]103 *
[169]104 * The element data structure will be freed, but the data property will not be.
[98]105 *
[169]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.
[98]110 */
111bool range_list_remove(range_list* rl, uint32_t index);
112
113
[169]114/** Retrieves the element for a given index.
[98]115 *
[169]116 * @param rl    the range_list being queried.
117 * @param index the element index desired.
[98]118 *
[169]119 * @return The element for a given index, or NULL if the element is not
120 *         available.
[98]121 */
122const range_list_element* range_list_get(const range_list* rl, uint32_t index);
123
124
[169]125/** Attempts to find the unique element whose range encompasses offset.
[98]126 *
[169]127 * @param rl     the range_list being queried.
128 * @param offset the location for which an element is desired.
[98]129 *
[169]130 * @return A matching element index or a negative value if none could be found.
[98]131 */
132int32_t range_list_find(const range_list* rl, uint32_t offset);
133
134
[169]135/** Same as range_list_find(), but returns the data associated with an element.
[98]136 *
[169]137 * @param rl     the range_list being queried.
138 * @param offset the address to search for in the ranges
[98]139 *
[169]140 * @return The data element of the matching element index or NULL if none could
141 *         be found.
[98]142 *
[169]143 *  NOTE: May also return NULL if an element matched but the data
[98]144 *        element was never set.
145 */
146void* range_list_find_data(const range_list* rl, uint32_t offset);
147
148
[169]149/**  Splits an existing element into two elements in place.
[98]150 *
[169]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.
[98]157 *
[169]158 * Both the original element and the newly created element will reference the
159 * original element's data.
[98]160 *
[169]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
[98]164 *
[169]165 * @return true if the element was successfully split, false otherwise.
[98]166 */
167bool range_list_split_element(range_list* rl, uint32_t index, uint32_t offset);
168
[113]169
[169]170/** Determines whether or not a specified range exists contiguously within the
[113]171 *  range_list.
172 *
[169]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
[113]176 *
[169]177 * @return true if the specified range exists and is complete, false otherwise.
[113]178 */
179bool range_list_has_range(range_list* rl, uint32_t start, uint32_t length);
180
[98]181#endif
Note: See TracBrowser for help on using the repository browser.