Skip to content

Commit f55767a

Browse files
authored
Merge pull request neovim#29016 from bfredl/shadareader
refactor(shada): remove ShaDaReadDef secondary wrapper
2 parents efa4583 + b386334 commit f55767a

File tree

5 files changed

+114
-239
lines changed

5 files changed

+114
-239
lines changed

src/nvim/os/fileio.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ int file_open_fd(FileDescriptor *const ret_fp, const int fd, const int flags)
126126
ret_fp->rv->data = ret_fp;
127127
ret_fp->rv->full_cb = (rbuffer_callback)&file_rb_write_full_cb;
128128
}
129+
ret_fp->bytes_read = 0;
129130
return 0;
130131
}
131132

@@ -140,6 +141,18 @@ int file_open_stdin(FileDescriptor *fp)
140141
return error;
141142
}
142143

144+
/// opens buffer for reading
145+
void file_open_buffer(FileDescriptor *ret_fp, char *data, size_t len)
146+
{
147+
ret_fp->wr = false;
148+
ret_fp->non_blocking = false;
149+
ret_fp->fd = -1;
150+
ret_fp->eof = true;
151+
ret_fp->rv = rbuffer_new_wrap_buf(data, len);
152+
ret_fp->_error = 0;
153+
ret_fp->bytes_read = 0;
154+
}
155+
143156
/// Close file and free its buffer
144157
///
145158
/// @param[in,out] fp File to close.
@@ -149,6 +162,11 @@ int file_open_stdin(FileDescriptor *fp)
149162
int file_close(FileDescriptor *const fp, const bool do_fsync)
150163
FUNC_ATTR_NONNULL_ALL
151164
{
165+
if (fp->fd < 0) {
166+
rbuffer_free(fp->rv);
167+
return 0;
168+
}
169+
152170
const int flush_error = (do_fsync ? file_fsync(fp) : file_flush(fp));
153171
const int close_error = os_close(fp->fd);
154172
rbuffer_free(fp->rv);
@@ -294,6 +312,7 @@ ptrdiff_t file_read(FileDescriptor *const fp, char *const ret_buf, const size_t
294312
fp->non_blocking);
295313
if (r_ret >= 0) {
296314
read_remaining -= (size_t)r_ret;
315+
fp->bytes_read += (size - read_remaining);
297316
return (ptrdiff_t)(size - read_remaining);
298317
} else if (r_ret < 0) {
299318
return r_ret;
@@ -314,6 +333,7 @@ ptrdiff_t file_read(FileDescriptor *const fp, char *const ret_buf, const size_t
314333
called_read = true;
315334
}
316335
}
336+
fp->bytes_read += (size - read_remaining);
317337
return (ptrdiff_t)(size - read_remaining);
318338
}
319339

src/nvim/os/fileio_defs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <stdbool.h>
4+
#include <stdint.h>
45

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

1820
static inline bool file_eof(const FileDescriptor *fp)

src/nvim/rbuffer.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ RBuffer *rbuffer_new(size_t capacity)
2929
return rv;
3030
}
3131

32+
/// Creates a new `RBuffer` instance for reading from a buffer.
33+
///
34+
/// Must not be used with any write function like rbuffer_write_ptr or rbuffer_produced!
35+
RBuffer *rbuffer_new_wrap_buf(char *data, size_t len)
36+
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_RET
37+
{
38+
RBuffer *rv = xcalloc(1, sizeof(RBuffer));
39+
rv->full_cb = rv->nonfull_cb = NULL;
40+
rv->data = NULL;
41+
rv->size = len;
42+
rv->read_ptr = data;
43+
rv->write_ptr = data + len;
44+
rv->end_ptr = NULL;
45+
rv->temp = NULL;
46+
return rv;
47+
}
48+
3249
void rbuffer_free(RBuffer *buf) FUNC_ATTR_NONNULL_ALL
3350
{
3451
xfree(buf->temp);
@@ -129,7 +146,7 @@ void rbuffer_consumed(RBuffer *buf, size_t count)
129146
assert(count <= buf->size);
130147

131148
buf->read_ptr += count;
132-
if (buf->read_ptr >= buf->end_ptr) {
149+
if (buf->end_ptr && buf->read_ptr >= buf->end_ptr) {
133150
buf->read_ptr -= rbuffer_capacity(buf);
134151
}
135152

0 commit comments

Comments
 (0)