+void
+mark_value_bytes_unavailable (struct value *value, int offset, int length)
+{
+ range_s newr;
+ int i;
+
+ /* Insert the range sorted. If there's overlap or the new range
+ would be contiguous with an existing range, merge. */
+
+ newr.offset = offset;
+ newr.length = length;
+
+ /* Do a binary search for the position the given range would be
+ inserted if we only considered the starting OFFSET of ranges.
+ Call that position I. Since we also have LENGTH to care for
+ (this is a range afterall), we need to check if the _previous_
+ range overlaps the I range. E.g., calling R the new range:
+
+ #1 - overlaps with previous
+
+ R
+ |-...-|
+ |---| |---| |------| ... |--|
+ 0 1 2 N
+
+ I=1
+
+ In the case #1 above, the binary search would return `I=1',
+ meaning, this OFFSET should be inserted at position 1, and the
+ current position 1 should be pushed further (and become 2). But,
+ note that `0' overlaps with R, so we want to merge them.
+
+ A similar consideration needs to be taken if the new range would
+ be contiguous with the previous range:
+
+ #2 - contiguous with previous
+
+ R
+ |-...-|
+ |--| |---| |------| ... |--|
+ 0 1 2 N
+
+ I=1
+
+ If there's no overlap with the previous range, as in:
+
+ #3 - not overlapping and not contiguous
+
+ R
+ |-...-|
+ |--| |---| |------| ... |--|
+ 0 1 2 N
+
+ I=1
+
+ or if I is 0:
+
+ #4 - R is the range with lowest offset
+
+ R
+ |-...-|
+ |--| |---| |------| ... |--|
+ 0 1 2 N
+
+ I=0
+
+ ... we just push the new range to I.
+
+ All the 4 cases above need to consider that the new range may
+ also overlap several of the ranges that follow, or that R may be
+ contiguous with the following range, and merge. E.g.,
+
+ #5 - overlapping following ranges
+
+ R
+ |------------------------|
+ |--| |---| |------| ... |--|
+ 0 1 2 N
+
+ I=0
+
+ or:
+
+ R
+ |-------|
+ |--| |---| |------| ... |--|
+ 0 1 2 N
+
+ I=1
+
+ */
+
+ i = VEC_lower_bound (range_s, value->unavailable, &newr, range_lessthan);
+ if (i > 0)
+ {
+ struct range *bef = VEC_index (range_s, value->unavailable, i - 1);
+
+ if (ranges_overlap (bef->offset, bef->length, offset, length))
+ {
+ /* #1 */
+ ULONGEST l = min (bef->offset, offset);
+ ULONGEST h = max (bef->offset + bef->length, offset + length);
+
+ bef->offset = l;
+ bef->length = h - l;
+ i--;
+ }
+ else if (offset == bef->offset + bef->length)
+ {
+ /* #2 */
+ bef->length += length;
+ i--;
+ }
+ else
+ {
+ /* #3 */
+ VEC_safe_insert (range_s, value->unavailable, i, &newr);
+ }
+ }
+ else
+ {
+ /* #4 */
+ VEC_safe_insert (range_s, value->unavailable, i, &newr);
+ }
+
+ /* Check whether the ranges following the one we've just added or
+ touched can be folded in (#5 above). */
+ if (i + 1 < VEC_length (range_s, value->unavailable))
+ {
+ struct range *t;
+ struct range *r;
+ int removed = 0;
+ int next = i + 1;
+
+ /* Get the range we just touched. */
+ t = VEC_index (range_s, value->unavailable, i);
+ removed = 0;
+
+ i = next;
+ for (; VEC_iterate (range_s, value->unavailable, i, r); i++)
+ if (r->offset <= t->offset + t->length)
+ {
+ ULONGEST l, h;
+
+ l = min (t->offset, r->offset);
+ h = max (t->offset + t->length, r->offset + r->length);
+
+ t->offset = l;
+ t->length = h - l;
+
+ removed++;
+ }
+ else
+ {
+ /* If we couldn't merge this one, we won't be able to
+ merge following ones either, since the ranges are
+ always sorted by OFFSET. */
+ break;
+ }
+
+ if (removed != 0)
+ VEC_block_remove (range_s, value->unavailable, next, removed);
+ }
+}