source: trunk/lib/range_list.c @ 148

Last change on this file since 148 was 148, checked in by tim, 16 years ago

integrated talloc into range_list

fixed some uninitialized structure values in the winsec library

  • Property svn:keywords set to Id
File size: 7.7 KB
Line 
1/*
2 * Copyright (C) 2008-2009 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.c 148 2009-02-22 23:22:59Z tim $
18 */
19
20#include "range_list.h"
21
22
23/*******************/
24/* Private symbols */
25/*******************/
26#define RANGE_LIST_ALLOC_SIZE 256
27
28#if 0 /* For debugging */
29#include <stdio.h>
30static void range_list_print(const range_list* rl)
31{
32  uint32_t i;
33  for(i=0; i<rl->size; i++)
34    fprintf(stderr, " %d=%p,%d,%d,%p", i, (void*)rl->elements[i],
35            rl->elements[i]->offset, rl->elements[i]->length,
36            rl->elements[i]->data);
37  fprintf(stderr, "\n");
38}
39#endif
40
41/*
42 * Inserts elem into rl at the specified index and updates rl->size.
43 * Memory reallocation of rl->elements is handled when necessary, and
44 * rl->elem_alloced is updated in this case..  Returns false if memory
45 * could not be allocated. 
46 */
47static bool range_list_insert(range_list* rl, range_list_element* elem, uint32_t index)
48{
49  uint32_t i;
50  range_list_element** tmp;
51
52  if(rl->size == rl->elem_alloced)
53  {
54    tmp = talloc_realloc(rl, rl->elements, range_list_element*, 
55                         (rl->elem_alloced+RANGE_LIST_ALLOC_SIZE));
56    if(tmp == NULL)
57      return false;
58    rl->elements = tmp;
59    rl->elem_alloced += RANGE_LIST_ALLOC_SIZE;
60  }
61
62  /* Do the shuffle to the right. */
63  for(i=rl->size; i > index; i--)
64    rl->elements[i] = rl->elements[i-1];
65  rl->elements[index] = elem;
66
67  rl->size++;
68  return true;
69}
70
71/*
72 * Finds the element with the closest offset to that provided, such that
73 * the element's offset <= the provided offset.  If no such element
74 * exists, this returns -1 which indicates that the provided offset
75 * appears before all elements.
76 */
77static int32_t range_list_find_previous(const range_list* rl, uint32_t offset)
78{
79  uint32_t h_idx, l_idx, cur_idx;
80  uint32_t h_val, l_val;
81  range_list_element* cur_elem;
82
83  if((rl->size == 0) || (offset < rl->elements[0]->offset))
84    return -1;
85
86  if(offset >= rl->elements[rl->size-1]->offset)
87    return rl->size-1;
88
89  h_idx = rl->size-1;
90  l_idx = 0;
91  while(h_idx != l_idx)
92  {
93    h_val = rl->elements[h_idx]->offset + rl->elements[h_idx]->length;
94    l_val = rl->elements[l_idx]->offset;
95    /* Make an educated guess as to the "middle" index based on the
96     * ratios of the offset and high/low values.
97     */
98    cur_idx = (uint32_t)ceil((((double)offset-l_val)/(h_val-l_val))*(h_idx-l_idx));
99    if(cur_idx > h_idx)
100      cur_idx = h_idx;
101    if(cur_idx < l_idx)
102      cur_idx = l_idx;
103    cur_elem = rl->elements[cur_idx];
104
105    if((offset >= cur_elem->offset) && (offset < rl->elements[cur_idx+1]->offset))
106      return cur_idx;
107   
108    if(offset < cur_elem->offset)
109      h_idx = cur_idx-1;
110    else
111      l_idx = cur_idx+1;
112  }
113
114  return h_idx;
115}
116
117
118/******************/
119/* Public symbols */
120/******************/
121range_list* range_list_new()
122{
123  range_list* rl;
124
125  rl = talloc(NULL, range_list);
126  if(rl == NULL)
127    return NULL;
128
129  rl->elements = talloc_array(rl, range_list_element*, RANGE_LIST_ALLOC_SIZE);
130  if(rl->elements == NULL)
131  {
132    talloc_free(rl);
133    return NULL;
134  }
135
136  rl->elem_alloced = RANGE_LIST_ALLOC_SIZE;
137  rl->size = 0;
138
139  return rl;
140}
141
142
143void range_list_free(range_list* rl)
144{
145  if(rl == NULL)
146    return;
147
148  talloc_free(rl);
149}
150
151
152uint32_t range_list_size(const range_list* rl)
153{
154  return rl->size;
155}
156
157
158
159bool range_list_add(range_list* rl, uint32_t offset, uint32_t length, void* data)
160{
161  uint32_t insert_index;
162  range_list_element* elem;
163  range_list_element* prev_elem;
164  /*fprintf(stderr, "DEBUG: rl->size=%d\n", rl->size);*/
165  /* Sorry, limited to 2**31-1 elements. */
166  if(rl->size >= 0x7FFFFFFF)
167    return false;
168
169  /* 0-length ranges aren't allowed. */
170  if(length == 0)
171    return false;
172 
173  /* Check for integer overflows */
174  if((uint32_t)(offset+length) < offset || (uint32_t)(offset+length) < length)
175    return false;
176
177  /* Find insertion point and validate there are no overlaps */
178  insert_index = range_list_find_previous(rl, offset)+1;
179 
180  /* Does the previous element overlap with this one? */
181  if(insert_index > 0)
182  {
183    prev_elem = rl->elements[insert_index-1];
184    if(offset < prev_elem->length + prev_elem->offset)
185      return false;
186  }
187
188  /* Does this new element overlap with the next one? */
189  if((insert_index+1 < rl->size) 
190     && (offset+length > rl->elements[insert_index+1]->offset))
191    return false;
192
193  elem = talloc(rl->elements, range_list_element);
194  if(elem == NULL)
195    return false;
196  elem->offset = offset;
197  elem->length = length;
198  elem->data = data;
199 
200  if(!range_list_insert(rl, elem, insert_index))
201  {
202    talloc_free(elem);
203    return false;
204  }
205
206  return true;
207}
208
209
210bool range_list_remove(range_list* rl, uint32_t index)
211{
212  uint32_t i;
213  range_list_element** tmp;
214
215  if(index >= rl->size)
216    return false;
217
218  talloc_free(rl->elements[index]);
219
220  /* Do the shuffle to the left. */
221  for(i=index; i < (rl->size-1); i++)
222    rl->elements[i] = rl->elements[i+1];
223  rl->elements[rl->size-1] = NULL;
224  rl->size--;
225
226  /* Try to keep memory usage down */
227  if(rl->size + 2 * RANGE_LIST_ALLOC_SIZE  < rl->elem_alloced)
228  {
229    tmp = talloc_realloc(rl, rl->elements, range_list_element*, 
230                         (rl->elem_alloced-2*RANGE_LIST_ALLOC_SIZE));
231    if(tmp != NULL)
232    {
233      rl->elements = tmp;
234      rl->elem_alloced -= 2*RANGE_LIST_ALLOC_SIZE;
235    }
236  }
237
238  return true;
239}
240
241
242const range_list_element* range_list_get(const range_list* rl, uint32_t index)
243{
244  if(index >= rl->size)
245    return NULL;
246
247  return rl->elements[index];
248}
249
250
251int32_t range_list_find(const range_list* rl, uint32_t offset)
252{
253  uint32_t prev_idx;
254  range_list_element* elem;
255
256  if(rl->size == 0)
257    return -1;
258
259  if((offset < rl->elements[0]->offset)
260     || (offset > rl->elements[rl->size-1]->offset
261         + rl->elements[rl->size-1]->length))
262    return -2;
263
264  prev_idx = range_list_find_previous(rl, offset);
265  elem = rl->elements[prev_idx];
266  if(offset < elem->offset+elem->length)
267    return prev_idx;
268
269  return -3;
270}
271
272
273void* range_list_find_data(const range_list* rl, uint32_t offset)
274{
275  int32_t index = range_list_find(rl, offset);
276  if(index < 0)
277    return NULL;
278
279  return rl->elements[index]->data;
280}
281
282
283bool range_list_split_element(range_list* rl, uint32_t index, uint32_t offset)
284{
285  range_list_element* cur_elem;
286  range_list_element* new_elem;
287
288  if(index >= rl->size)
289    return false;
290
291  cur_elem = rl->elements[index];
292  if((offset <= cur_elem->offset) 
293     || (offset >= cur_elem->offset+cur_elem->length))
294    return false;
295
296  new_elem = talloc(rl->elements, range_list_element);
297  if(new_elem == NULL)
298    return false;
299 
300  new_elem->offset = offset;
301  new_elem->length = cur_elem->offset + cur_elem->length - offset;
302  new_elem->data = cur_elem->data;
303 
304  if(!range_list_insert(rl, new_elem, index+1))
305  {
306    talloc_free(new_elem);
307    return false;
308  }
309
310  cur_elem->length = new_elem->offset - cur_elem->offset;
311
312  return true;
313}
314
315
316bool range_list_has_range(range_list* rl, uint32_t start, uint32_t length)
317{
318  int32_t idx1, idx2;
319
320  idx1 = range_list_find(rl, start);
321  if(idx1 < 0)
322    return false;
323
324  idx2 = range_list_find(rl, start+length);
325  if(idx2 < 0)
326    return false;
327
328  if(idx1 == idx2)
329    return true;
330
331  while(idx1 != idx2)
332  {
333    if(rl->elements[idx1]->offset + rl->elements[idx1]->length
334       != rl->elements[idx1+1]->offset)
335      return false;
336    idx1++;
337  }
338
339  return true;
340}
Note: See TracBrowser for help on using the repository browser.