File: range_list.c

package info (click to toggle)
reglookup 1.0.1+svn287-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 952 kB
  • sloc: ansic: 6,676; python: 3,762; makefile: 40; sh: 27
file content (342 lines) | stat: -rw-r--r-- 7,933 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * Copyright (C) 2008-2010 Timothy D. Morgan
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
 *
 * $Id$
 */

/**
 * @file
 */

#include "range_list.h"


/*******************/
/* Private symbols */
/*******************/
#define RANGE_LIST_ALLOC_SIZE 256

#if 0 /* For debugging */
#include <stdio.h>
static void range_list_print(const range_list* rl)
{
  uint32_t i;
  for(i=0; i<rl->size; i++)
    fprintf(stderr, " %d=%p,%d,%d,%p", i, (void*)rl->elements[i],
	    rl->elements[i]->offset, rl->elements[i]->length, 
	    rl->elements[i]->data);
  fprintf(stderr, "\n");
}
#endif

/*
 * Inserts elem into rl at the specified index and updates rl->size. 
 * Memory reallocation of rl->elements is handled when necessary, and
 * rl->elem_alloced is updated in this case..  Returns false if memory
 * could not be allocated.  
 */
static bool range_list_insert(range_list* rl, range_list_element* elem, uint32_t index)
{
  uint32_t i;
  range_list_element** tmp;

  if(rl->size == rl->elem_alloced)
  {
    tmp = talloc_realloc(rl, rl->elements, range_list_element*, 
			 (rl->elem_alloced+RANGE_LIST_ALLOC_SIZE));
    if(tmp == NULL)
      return false;
    rl->elements = tmp;
    rl->elem_alloced += RANGE_LIST_ALLOC_SIZE;
  }

  /* Do the shuffle to the right. */
  for(i=rl->size; i > index; i--)
    rl->elements[i] = rl->elements[i-1];
  rl->elements[index] = elem;

  rl->size++;
  return true;
}

/*
 * Finds the element with the closest offset to that provided, such that
 * the element's offset <= the provided offset.  If no such element
 * exists, this returns -1 which indicates that the provided offset
 * appears before all elements.
 */
static int32_t range_list_find_previous(const range_list* rl, uint32_t offset)
{
  uint32_t h_idx, l_idx, cur_idx;
  uint32_t h_val, l_val;
  range_list_element* cur_elem;

  if((rl->size == 0) || (offset < rl->elements[0]->offset))
    return -1;

  if(offset >= rl->elements[rl->size-1]->offset)
    return rl->size-1;

  h_idx = rl->size-1;
  l_idx = 0;
  while(h_idx != l_idx)
  {
    h_val = rl->elements[h_idx]->offset + rl->elements[h_idx]->length;
    l_val = rl->elements[l_idx]->offset;
    /* Make an educated guess as to the "middle" index based on the
     * ratios of the offset and high/low values.
     */
    cur_idx = (uint32_t)ceil((((double)offset-l_val)/(h_val-l_val))*(h_idx-l_idx));
    if(cur_idx > h_idx)
      cur_idx = h_idx;
    if(cur_idx < l_idx)
      cur_idx = l_idx;
    cur_elem = rl->elements[cur_idx];

    if((offset >= cur_elem->offset) && (offset < rl->elements[cur_idx+1]->offset))
      return cur_idx;
    
    if(offset < cur_elem->offset)
      h_idx = cur_idx-1;
    else
      l_idx = cur_idx+1;
  }

  return h_idx;
}


/******************/
/* Public symbols */
/******************/
range_list* range_list_new()
{
  range_list* rl;

  rl = talloc(NULL, range_list);
  if(rl == NULL)
    return NULL;

  rl->elements = talloc_array(rl, range_list_element*, RANGE_LIST_ALLOC_SIZE);
  if(rl->elements == NULL)
  {
    talloc_free(rl);
    return NULL;
  }

  rl->elem_alloced = RANGE_LIST_ALLOC_SIZE;
  rl->size = 0;

  return rl;
}


void range_list_free(range_list* rl)
{
  if(rl != NULL)
    talloc_free(rl);
}


uint32_t range_list_size(const range_list* rl)
{
  return rl->size;
}



bool range_list_add(range_list* rl, uint32_t offset, uint32_t length, void* data)
{
  uint32_t insert_index;
  range_list_element* elem;
  range_list_element* prev_elem;
  /*fprintf(stderr, "DEBUG: rl->size=%d\n", rl->size);*/
  /* Sorry, limited to 2**31-1 elements. */
  if(rl->size >= 0x7FFFFFFF)
    return false;

  /* 0-length ranges aren't allowed. */
  if(length == 0)
    return false;
  
  /* Check for integer overflows */
  if((uint32_t)(offset+length) < offset || (uint32_t)(offset+length) < length)
    return false;

  /* Find insertion point and validate there are no overlaps */
  insert_index = range_list_find_previous(rl, offset)+1;
  
  /* Does the previous element overlap with this one? */
  if(insert_index > 0)
  {
    prev_elem = rl->elements[insert_index-1];
    if(offset < prev_elem->length + prev_elem->offset)
      return false;
  }

  /* Does this new element overlap with the next one? */
  if((insert_index+1 < rl->size) 
     && (offset+length > rl->elements[insert_index+1]->offset))
    return false;

  elem = talloc(rl->elements, range_list_element);
  if(elem == NULL)
    return false;
  elem->offset = offset;
  elem->length = length;
  elem->data = data;
  
  if(!range_list_insert(rl, elem, insert_index))
  {
    talloc_free(elem);
    return false;
  }

  return true;
}


bool range_list_remove(range_list* rl, uint32_t index)
{
  uint32_t i;
  range_list_element** tmp;

  if(index >= rl->size)
    return false;

  talloc_free(rl->elements[index]);

  /* Do the shuffle to the left. */
  for(i=index; i < (rl->size-1); i++)
    rl->elements[i] = rl->elements[i+1];
  rl->elements[rl->size-1] = NULL;
  rl->size--;

  /* Try to keep memory usage down */
  if(rl->size + 2 * RANGE_LIST_ALLOC_SIZE  < rl->elem_alloced)
  {
    tmp = talloc_realloc(rl, rl->elements, range_list_element*, 
			 (rl->elem_alloced-2*RANGE_LIST_ALLOC_SIZE));
    if(tmp != NULL)
    {
      rl->elements = tmp;
      rl->elem_alloced -= 2*RANGE_LIST_ALLOC_SIZE;
    }
  }

  return true;
}


const range_list_element* range_list_get(const range_list* rl, uint32_t index)
{
  if(index >= rl->size)
    return NULL;

  return rl->elements[index];
}


int32_t range_list_find(const range_list* rl, uint32_t offset)
{
  uint32_t prev_idx;
  range_list_element* elem;

  if(rl->size == 0)
    return -1;

  if((offset < rl->elements[0]->offset)
     || (offset > rl->elements[rl->size-1]->offset 
	 + rl->elements[rl->size-1]->length))
    return -2;

  prev_idx = range_list_find_previous(rl, offset);
  elem = rl->elements[prev_idx];
  if(offset < elem->offset+elem->length)
    return prev_idx;

  return -3;
}


void* range_list_find_data(const range_list* rl, uint32_t offset)
{
  int32_t index = range_list_find(rl, offset);
  if(index < 0)
    return NULL;

  return rl->elements[index]->data;
}


bool range_list_split_element(range_list* rl, uint32_t index, uint32_t offset)
{
  range_list_element* cur_elem;
  range_list_element* new_elem;

  if(index >= rl->size)
    return false;

  cur_elem = rl->elements[index];
  if((offset <= cur_elem->offset) 
     || (offset >= cur_elem->offset+cur_elem->length))
    return false;

  new_elem = talloc(rl->elements, range_list_element);
  if(new_elem == NULL)
    return false;
  
  new_elem->offset = offset;
  new_elem->length = cur_elem->offset + cur_elem->length - offset;
  new_elem->data = cur_elem->data;
  
  if(!range_list_insert(rl, new_elem, index+1))
  {
    talloc_free(new_elem);
    return false;
  }

  cur_elem->length = new_elem->offset - cur_elem->offset;

  return true;
}


bool range_list_has_range(range_list* rl, uint32_t start, uint32_t length)
{
  int32_t idx1, idx2;

  idx1 = range_list_find(rl, start);
  if(idx1 < 0)
    return false;

  idx2 = range_list_find(rl, start+length);
  if(idx2 < 0)
    return false;

  if(idx1 == idx2)
    return true;

  while(idx1 != idx2)
  {
    if(rl->elements[idx1]->offset + rl->elements[idx1]->length 
       != rl->elements[idx1+1]->offset)
      return false;
    idx1++;
  }

  return true;
}