Kernel: Add a Range::intersect(other) helper

This commit is contained in:
Hendiadyoin1 2021-02-26 11:16:03 +01:00 committed by Andreas Kling
parent 7b22fb70a2
commit 7874b89426
Notes: sideshowbarker 2024-07-18 21:26:48 +09:00
2 changed files with 13 additions and 0 deletions

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -43,5 +44,15 @@ Vector<Range, 2> Range::carve(const Range& taken) const
parts.append({ taken.end(), end().get() - taken.end().get() });
return parts;
}
Range Range::intersect(const Range& other) const
{
if (*this == other) {
return *this;
}
auto new_base = max(base(), other.base());
auto new_end = min(end(), other.end());
VERIFY(new_base < new_end);
return Range(new_base, (new_end - new_base).get());
}
}

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -67,6 +68,7 @@ public:
}
Vector<Range, 2> carve(const Range&) const;
Range intersect(const Range&) const;
private:
VirtualAddress m_base;