Skip to content

Commit

Permalink
Merge pull request neovim#29016 from bfredl/shadareader
Browse files Browse the repository at this point in the history
refactor(shada): remove ShaDaReadDef secondary wrapper
  • Loading branch information
bfredl committed May 29, 2024
2 parents efa4583 + b386334 commit f55767a
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 239 deletions.
20 changes: 20 additions & 0 deletions src/nvim/os/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ int file_open_fd(FileDescriptor *const ret_fp, const int fd, const int flags)
ret_fp->rv->data = ret_fp;
ret_fp->rv->full_cb = (rbuffer_callback)&file_rb_write_full_cb;
}
ret_fp->bytes_read = 0;
return 0;
}

Expand All @@ -140,6 +141,18 @@ int file_open_stdin(FileDescriptor *fp)
return error;
}

/// opens buffer for reading
void file_open_buffer(FileDescriptor *ret_fp, char *data, size_t len)
{
ret_fp->wr = false;
ret_fp->non_blocking = false;
ret_fp->fd = -1;
ret_fp->eof = true;
ret_fp->rv = rbuffer_new_wrap_buf(data, len);
ret_fp->_error = 0;
ret_fp->bytes_read = 0;
}

/// Close file and free its buffer
///
/// @param[in,out] fp File to close.
Expand All @@ -149,6 +162,11 @@ int file_open_stdin(FileDescriptor *fp)
int file_close(FileDescriptor *const fp, const bool do_fsync)
FUNC_ATTR_NONNULL_ALL
{
if (fp->fd < 0) {
rbuffer_free(fp->rv);
return 0;
}

const int flush_error = (do_fsync ? file_fsync(fp) : file_flush(fp));
const int close_error = os_close(fp->fd);
rbuffer_free(fp->rv);
Expand Down Expand Up @@ -294,6 +312,7 @@ ptrdiff_t file_read(FileDescriptor *const fp, char *const ret_buf, const size_t
fp->non_blocking);
if (r_ret >= 0) {
read_remaining -= (size_t)r_ret;
fp->bytes_read += (size - read_remaining);
return (ptrdiff_t)(size - read_remaining);
} else if (r_ret < 0) {
return r_ret;
Expand All @@ -314,6 +333,7 @@ ptrdiff_t file_read(FileDescriptor *const fp, char *const ret_buf, const size_t
called_read = true;
}
}
fp->bytes_read += (size - read_remaining);
return (ptrdiff_t)(size - read_remaining);
}

Expand Down
2 changes: 2 additions & 0 deletions src/nvim/os/fileio_defs.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <stdbool.h>
#include <stdint.h>

#include "nvim/func_attr.h"
#include "nvim/rbuffer_defs.h"
Expand All @@ -13,6 +14,7 @@ typedef struct {
bool wr; ///< True if file is in write mode.
bool eof; ///< True if end of file was encountered.
bool non_blocking; ///< True if EAGAIN should not restart syscalls.
uint64_t bytes_read; ///< total bytes read so far
} FileDescriptor;

static inline bool file_eof(const FileDescriptor *fp)
Expand Down
19 changes: 18 additions & 1 deletion src/nvim/rbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ RBuffer *rbuffer_new(size_t capacity)
return rv;
}

/// Creates a new `RBuffer` instance for reading from a buffer.
///
/// Must not be used with any write function like rbuffer_write_ptr or rbuffer_produced!
RBuffer *rbuffer_new_wrap_buf(char *data, size_t len)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_RET
{
RBuffer *rv = xcalloc(1, sizeof(RBuffer));
rv->full_cb = rv->nonfull_cb = NULL;
rv->data = NULL;
rv->size = len;
rv->read_ptr = data;
rv->write_ptr = data + len;
rv->end_ptr = NULL;
rv->temp = NULL;
return rv;
}

void rbuffer_free(RBuffer *buf) FUNC_ATTR_NONNULL_ALL
{
xfree(buf->temp);
Expand Down Expand Up @@ -129,7 +146,7 @@ void rbuffer_consumed(RBuffer *buf, size_t count)
assert(count <= buf->size);

buf->read_ptr += count;
if (buf->read_ptr >= buf->end_ptr) {
if (buf->end_ptr && buf->read_ptr >= buf->end_ptr) {
buf->read_ptr -= rbuffer_capacity(buf);
}

Expand Down
Loading

0 comments on commit f55767a

Please sign in to comment.