MappedFile.h 1010 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Error.h>
  8. #include <AK/Noncopyable.h>
  9. #include <AK/NonnullRefPtr.h>
  10. #include <AK/RefCounted.h>
  11. #include <LibCore/Forward.h>
  12. namespace Core {
  13. class MappedFile : public RefCounted<MappedFile> {
  14. AK_MAKE_NONCOPYABLE(MappedFile);
  15. AK_MAKE_NONMOVABLE(MappedFile);
  16. public:
  17. static ErrorOr<NonnullRefPtr<MappedFile>> map(StringView path);
  18. static ErrorOr<NonnullRefPtr<MappedFile>> map_from_file(NonnullOwnPtr<Core::File>, StringView path);
  19. static ErrorOr<NonnullRefPtr<MappedFile>> map_from_fd_and_close(int fd, StringView path);
  20. ~MappedFile();
  21. void* data() { return m_data; }
  22. void const* data() const { return m_data; }
  23. size_t size() const { return m_size; }
  24. ReadonlyBytes bytes() const { return { m_data, m_size }; }
  25. private:
  26. explicit MappedFile(void*, size_t);
  27. void* m_data { nullptr };
  28. size_t m_size { 0 };
  29. };
  30. }