[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 253 2011-06-13 02:27:42Z 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> |
---|
[201] | 38 | #include <talloc.h> |
---|
[98] | 39 | |
---|
[253] | 40 | #include "compat.h" |
---|
[201] | 41 | |
---|
[98] | 42 | typedef struct _range_list_element |
---|
| 43 | { |
---|
| 44 | uint32_t offset; |
---|
| 45 | uint32_t length; |
---|
| 46 | void* data; |
---|
| 47 | } range_list_element; |
---|
| 48 | |
---|
| 49 | |
---|
[169] | 50 | /** XXX: document this. */ |
---|
[98] | 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 | |
---|
[169] | 59 | /** Allocates a new range_list. |
---|
[98] | 60 | * |
---|
[169] | 61 | * @return A newly allocated range_list, or NULL if an error occurred. |
---|
[98] | 62 | */ |
---|
[253] | 63 | _EXPORT() |
---|
[98] | 64 | range_list* range_list_new(); |
---|
| 65 | |
---|
| 66 | |
---|
[169] | 67 | /** Frees the memory associated with a range_list, including the elements, but |
---|
| 68 | * not any data parameters referenced by those elements. |
---|
[98] | 69 | * |
---|
[169] | 70 | * If rl is NULL, does nothing. |
---|
| 71 | * |
---|
| 72 | * @param rl the range_list to be free()d. |
---|
[98] | 73 | */ |
---|
[253] | 74 | _EXPORT() |
---|
[98] | 75 | void range_list_free(range_list* rl); |
---|
| 76 | |
---|
| 77 | |
---|
[169] | 78 | /** Query the current number of elements on a range_list |
---|
[98] | 79 | * |
---|
[169] | 80 | * @param rl the range_list to query |
---|
[98] | 81 | * |
---|
[169] | 82 | * @return The number of elements currently in the list. |
---|
[98] | 83 | */ |
---|
[253] | 84 | _EXPORT() |
---|
[98] | 85 | uint32_t range_list_size(const range_list* rl); |
---|
| 86 | |
---|
| 87 | |
---|
[169] | 88 | /** Adds an element to the range_list. |
---|
[98] | 89 | * |
---|
[169] | 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. |
---|
[98] | 103 | */ |
---|
[253] | 104 | _EXPORT() |
---|
[98] | 105 | bool range_list_add(range_list* rl, uint32_t offset, uint32_t length, void* data); |
---|
| 106 | |
---|
| 107 | |
---|
[169] | 108 | /** Removes an element from the list. |
---|
[98] | 109 | * |
---|
[169] | 110 | * The element data structure will be freed, but the data property will not be. |
---|
[98] | 111 | * |
---|
[169] | 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. |
---|
[98] | 116 | */ |
---|
[253] | 117 | _EXPORT() |
---|
[98] | 118 | bool range_list_remove(range_list* rl, uint32_t index); |
---|
| 119 | |
---|
| 120 | |
---|
[169] | 121 | /** Retrieves the element for a given index. |
---|
[98] | 122 | * |
---|
[169] | 123 | * @param rl the range_list being queried. |
---|
| 124 | * @param index the element index desired. |
---|
[98] | 125 | * |
---|
[169] | 126 | * @return The element for a given index, or NULL if the element is not |
---|
| 127 | * available. |
---|
[98] | 128 | */ |
---|
[253] | 129 | _EXPORT() |
---|
[98] | 130 | const range_list_element* range_list_get(const range_list* rl, uint32_t index); |
---|
| 131 | |
---|
| 132 | |
---|
[169] | 133 | /** Attempts to find the unique element whose range encompasses offset. |
---|
[98] | 134 | * |
---|
[169] | 135 | * @param rl the range_list being queried. |
---|
| 136 | * @param offset the location for which an element is desired. |
---|
[98] | 137 | * |
---|
[169] | 138 | * @return A matching element index or a negative value if none could be found. |
---|
[98] | 139 | */ |
---|
[253] | 140 | _EXPORT() |
---|
[98] | 141 | int32_t range_list_find(const range_list* rl, uint32_t offset); |
---|
| 142 | |
---|
| 143 | |
---|
[169] | 144 | /** Same as range_list_find(), but returns the data associated with an element. |
---|
[98] | 145 | * |
---|
[169] | 146 | * @param rl the range_list being queried. |
---|
| 147 | * @param offset the address to search for in the ranges |
---|
[98] | 148 | * |
---|
[169] | 149 | * @return The data element of the matching element index or NULL if none could |
---|
| 150 | * be found. |
---|
[98] | 151 | * |
---|
[169] | 152 | * NOTE: May also return NULL if an element matched but the data |
---|
[98] | 153 | * element was never set. |
---|
| 154 | */ |
---|
[253] | 155 | _EXPORT() |
---|
[98] | 156 | void* range_list_find_data(const range_list* rl, uint32_t offset); |
---|
| 157 | |
---|
| 158 | |
---|
[169] | 159 | /** Splits an existing element into two elements in place. |
---|
[98] | 160 | * |
---|
[169] | 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. |
---|
[98] | 167 | * |
---|
[169] | 168 | * Both the original element and the newly created element will reference the |
---|
| 169 | * original element's data. |
---|
[98] | 170 | * |
---|
[169] | 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 |
---|
[98] | 174 | * |
---|
[169] | 175 | * @return true if the element was successfully split, false otherwise. |
---|
[98] | 176 | */ |
---|
[253] | 177 | _EXPORT() |
---|
[98] | 178 | bool range_list_split_element(range_list* rl, uint32_t index, uint32_t offset); |
---|
| 179 | |
---|
[113] | 180 | |
---|
[169] | 181 | /** Determines whether or not a specified range exists contiguously within the |
---|
[113] | 182 | * range_list. |
---|
| 183 | * |
---|
[169] | 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 |
---|
[113] | 187 | * |
---|
[169] | 188 | * @return true if the specified range exists and is complete, false otherwise. |
---|
[113] | 189 | */ |
---|
[253] | 190 | _EXPORT() |
---|
[113] | 191 | bool range_list_has_range(range_list* rl, uint32_t start, uint32_t length); |
---|
| 192 | |
---|
[98] | 193 | #endif |
---|