Serenity Operating System
1/*
2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/OwnPtr.h>
10#include <Kernel/Memory/Region.h>
11#include <Kernel/PhysicalAddress.h>
12
13namespace Kernel::Memory {
14
15class MappedROM {
16public:
17 u8 const* base() const { return region->vaddr().offset(offset).as_ptr(); }
18 u8 const* end() const { return base() + size; }
19 OwnPtr<Region> region;
20 size_t size { 0 };
21 size_t offset { 0 };
22 PhysicalAddress paddr;
23
24 Optional<PhysicalAddress> find_chunk_starting_with(StringView prefix, size_t chunk_size) const
25 {
26 auto prefix_length = prefix.length();
27 if (size < prefix_length)
28 return {};
29 for (auto* candidate = base(); candidate <= end() - prefix_length; candidate += chunk_size) {
30 if (!__builtin_memcmp(prefix.characters_without_null_termination(), candidate, prefix.length()))
31 return paddr_of(candidate);
32 }
33 return {};
34 }
35
36 PhysicalAddress paddr_of(u8 const* ptr) const { return paddr.offset(ptr - this->base()); }
37};
38
39}