Conversation
cfd6a0b to
f03373a
Compare
f03373a to
2f38858
Compare
apolyakov
left a comment
There was a problem hiding this comment.
Added comments about mmap
|
|
||
| // ================================================================================================ | ||
|
|
||
| class mmap_resource final : public sync_resource { |
There was a problem hiding this comment.
_resourceprefix seems unnecessary- To think about: maybe it should not be a resource
| // ================================================================================================ | ||
|
|
||
| class mmap_resource final : public sync_resource { | ||
| char* ptr{}; |
There was a problem hiding this comment.
I think std::byte* suits better here as it's more type safe
| char* ptr{}; | ||
| k2::descriptor m_descriptor{k2::INVALID_PLATFORM_DESCRIPTOR}; | ||
|
|
||
| explicit mmap_resource(char* ptr, k2::descriptor descriptor) noexcept |
|
|
||
| public: | ||
| mmap_resource(mmap_resource&& other) noexcept | ||
| : ptr{std::move(other.ptr)}, |
There was a problem hiding this comment.
It's safer to exchange other.ptr with nullptr
| mmap_resource& operator=(mmap_resource&& other) noexcept { | ||
| if (this != std::addressof(other)) { | ||
| std::ignore = close(); | ||
| ptr = other.ptr; |
There was a problem hiding this comment.
Same here with exchanging
| static auto mmap(void* addr, size_t length, int32_t prot, int32_t flags, k2::descriptor fd, | ||
| uint64_t offset) noexcept -> std::expected<mmap_resource, int32_t>; | ||
|
|
||
| auto as_ptr() noexcept -> char*&; |
There was a problem hiding this comment.
- Why do we need reference here? The method is called
as_ptr, notas_ref_to_ptr - Isn't it safer to return
std::span<std::byte>? Currently, your implementation looses the information about mmap's length which is quite dangerous
| k2::descriptor descriptor{k2::INVALID_PLATFORM_DESCRIPTOR}; | ||
| const auto ptr{k2::mmap(std::addressof(descriptor), addr, length, prot, flags, fd, offset)}; | ||
| if (ptr == nullptr) [[unlikely]] { | ||
| return std::unexpected{k2::errno_enodev}; |
There was a problem hiding this comment.
What do you think about returning k2::einval instead?
| if (m_descriptor == k2::INVALID_PLATFORM_DESCRIPTOR) [[unlikely]] { | ||
| return std::unexpected{k2::errno_enodev}; | ||
| } | ||
| k2::free_descriptor(std::exchange(m_descriptor, k2::INVALID_PLATFORM_DESCRIPTOR)); |
There was a problem hiding this comment.
Let's also write nullptr to ptr. It may help in debugging
| class file : public sync_resource { | ||
| bool m_eof{}; | ||
| k2::descriptor m_descriptor{k2::INVALID_PLATFORM_DESCRIPTOR}; | ||
| std::optional<kphp::fs::mmap_resource> m_mmap_resource; |
There was a problem hiding this comment.
nit: rename to m_opt_mmap?
|
|
||
| auto descriptor() const noexcept -> k2::descriptor; | ||
|
|
||
| auto has_mmap_resource() const noexcept -> bool; |
There was a problem hiding this comment.
I don't see a need for that as you already have a method returning expected<mmap*, int32_t>. Also, isn't it better to have it returning just std::optional<std::reference_wrapper<mmap>>?
No description provided.