Skip to content

Commit

Permalink
bit: Add adbg_bits_ptr_outside
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed Jun 10, 2024
1 parent b5d5acb commit be2e7eb
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/adbg/utils/bit.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import adbg.platform;
import core.bitop : bswap;

// TODO: Helper for array of bits (checking/unchecking array of bytes)
// TODO: adbg_bit_listget
// Base pointer to item list (void*)
// Index to item list (size_t)
// Base range pointer (void*)
// Range size in item count (size_t)
// Size of one item (size_t)

extern (C):

Expand Down Expand Up @@ -166,4 +172,28 @@ unittest {
assert(adbg_alignup(7, ulong.sizeof) == 8);
assert(adbg_alignup(8, ulong.sizeof) == 8);
assert(adbg_alignup(9, ulong.sizeof) == 16);
}

/// Returns true if pointer is outside a sized range.
/// Params:
/// ptr = Pointer value to check.
/// base = Base pointer value of range.
/// size = Size of range in bytes.
/// Returns: False if ptr is inside range, true if outside range.
bool adbg_bits_ptr_outside(void* ptr, void* base, size_t size) {
return ptr < base || ptr >= base + size;
}
unittest {
// Within bounds
assert(adbg_bits_ptr_outside(cast(void*)0, cast(void*)0, 20) == false);
assert(adbg_bits_ptr_outside(cast(void*)1, cast(void*)0, 20) == false);
assert(adbg_bits_ptr_outside(cast(void*)10, cast(void*)0, 20) == false);
assert(adbg_bits_ptr_outside(cast(void*)11, cast(void*)0, 20) == false);
assert(adbg_bits_ptr_outside(cast(void*)19, cast(void*)0, 20) == false);
// Outside bounds
assert(adbg_bits_ptr_outside(cast(void*)20, cast(void*)0, 20));
assert(adbg_bits_ptr_outside(cast(void*)30, cast(void*)0, 20));
assert(adbg_bits_ptr_outside(cast(void*)40, cast(void*)0, 20));
assert(adbg_bits_ptr_outside(cast(void*)-1, cast(void*)0, 20));
assert(adbg_bits_ptr_outside(cast(void*)0, cast(void*)10, 20));
}

0 comments on commit be2e7eb

Please sign in to comment.