MappedFile.h 905 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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 <AK/Result.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(String const& path);
  18. static ErrorOr<NonnullRefPtr<MappedFile>> map_from_fd_and_close(int fd, String const& path);
  19. ~MappedFile();
  20. void* data() { return m_data; }
  21. void const* data() const { return m_data; }
  22. size_t size() const { return m_size; }
  23. ReadonlyBytes bytes() const { return { m_data, m_size }; }
  24. private:
  25. explicit MappedFile(void*, size_t);
  26. void* m_data { nullptr };
  27. size_t m_size { 0 };
  28. };
  29. }