+int
+value_bytes_available (const struct value *value, int offset, int length)
+{
+ gdb_assert (!value->lazy);
+
+ return !ranges_contain (value->unavailable, offset, length);
+}
+
+int
+value_entirely_available (struct value *value)
+{
+ /* We can only tell whether the whole value is available when we try
+ to read it. */
+ if (value->lazy)
+ value_fetch_lazy (value);
+
+ if (VEC_empty (range_s, value->unavailable))
+ return 1;
+ return 0;
+}
+
+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);
+ }
+}
+
+/* Find the first range in RANGES that overlaps the range defined by
+ OFFSET and LENGTH, starting at element POS in the RANGES vector,
+ Returns the index into RANGES where such overlapping range was
+ found, or -1 if none was found. */
+
+static int
+find_first_range_overlap (VEC(range_s) *ranges, int pos,
+ int offset, int length)
+{
+ range_s *r;
+ int i;
+
+ for (i = pos; VEC_iterate (range_s, ranges, i, r); i++)
+ if (ranges_overlap (r->offset, r->length, offset, length))
+ return i;
+
+ return -1;
+}
+
+int
+value_available_contents_eq (const struct value *val1, int offset1,
+ const struct value *val2, int offset2,
+ int length)
+{
+ int idx1 = 0, idx2 = 0;
+
+ /* This routine is used by printing routines, where we should
+ already have read the value. Note that we only know whether a
+ value chunk is available if we've tried to read it. */
+ gdb_assert (!val1->lazy && !val2->lazy);
+
+ while (length > 0)
+ {
+ range_s *r1, *r2;
+ ULONGEST l1, h1;
+ ULONGEST l2, h2;
+
+ idx1 = find_first_range_overlap (val1->unavailable, idx1,
+ offset1, length);
+ idx2 = find_first_range_overlap (val2->unavailable, idx2,
+ offset2, length);
+
+ /* The usual case is for both values to be completely available. */
+ if (idx1 == -1 && idx2 == -1)
+ return (memcmp (val1->contents + offset1,
+ val2->contents + offset2,
+ length) == 0);
+ /* The contents only match equal if the available set matches as
+ well. */
+ else if (idx1 == -1 || idx2 == -1)
+ return 0;
+
+ gdb_assert (idx1 != -1 && idx2 != -1);
+
+ r1 = VEC_index (range_s, val1->unavailable, idx1);
+ r2 = VEC_index (range_s, val2->unavailable, idx2);
+
+ /* Get the unavailable windows intersected by the incoming
+ ranges. The first and last ranges that overlap the argument
+ range may be wider than said incoming arguments ranges. */
+ l1 = max (offset1, r1->offset);
+ h1 = min (offset1 + length, r1->offset + r1->length);
+
+ l2 = max (offset2, r2->offset);
+ h2 = min (offset2 + length, r2->offset + r2->length);
+
+ /* Make them relative to the respective start offsets, so we can
+ compare them for equality. */
+ l1 -= offset1;
+ h1 -= offset1;
+
+ l2 -= offset2;
+ h2 -= offset2;
+
+ /* Different availability, no match. */
+ if (l1 != l2 || h1 != h2)
+ return 0;
+
+ /* Compare the _available_ contents. */
+ if (memcmp (val1->contents + offset1,
+ val2->contents + offset2,
+ l1) != 0)
+ return 0;
+
+ length -= h1;
+ offset1 += h1;
+ offset2 += h1;
+ }
+
+ return 1;
+}
+
+/* Prototypes for local functions. */