Skip to content

[k2] add fgetcsv#1516

Draft
denisichh wants to merge 2 commits intomasterfrom
dzubarev/k2-fgets-fgetcsv
Draft

[k2] add fgetcsv#1516
denisichh wants to merge 2 commits intomasterfrom
dzubarev/k2-fgets-fgetcsv

Conversation

@denisichh
Copy link
Contributor

No description provided.

@denisichh denisichh self-assigned this Feb 3, 2026
@denisichh denisichh added the k2 k2 related label Feb 3, 2026
@denisichh denisichh marked this pull request as draft February 3, 2026 14:43
@denisichh denisichh force-pushed the dzubarev/k2-fgets-fgetcsv branch from cfd6a0b to f03373a Compare February 3, 2026 14:47
@denisichh denisichh force-pushed the dzubarev/k2-fgets-fgetcsv branch from f03373a to 2f38858 Compare February 3, 2026 15:13
@denisichh denisichh marked this pull request as ready for review February 3, 2026 15:35
Copy link
Contributor

@apolyakov apolyakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comments about mmap


// ================================================================================================

class mmap_resource final : public sync_resource {
Copy link
Contributor

@apolyakov apolyakov Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. _resource prefix seems unnecessary
  2. To think about: maybe it should not be a resource

// ================================================================================================

class mmap_resource final : public sync_resource {
char* ptr{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for explicit


public:
mmap_resource(mmap_resource&& other) noexcept
: ptr{std::move(other.ptr)},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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*&;
Copy link
Contributor

@apolyakov apolyakov Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Why do we need reference here? The method is called as_ptr, not as_ref_to_ptr
  2. 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};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: rename to m_opt_mmap?


auto descriptor() const noexcept -> k2::descriptor;

auto has_mmap_resource() const noexcept -> bool;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>>?

@denisichh denisichh changed the title [k2] add fgets, fgetcsv [k2] add fgetcsv Feb 5, 2026
@denisichh denisichh marked this pull request as draft February 5, 2026 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

k2 k2 related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants