Skip to content

Commit

Permalink
Reviewer required changes
Browse files Browse the repository at this point in the history
NO logic change to buffers yet. Just moving
existing logic into constructor/destructor.
  • Loading branch information
yadij committed Sep 24, 2024
1 parent b7cc30a commit 50e4b3e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
39 changes: 19 additions & 20 deletions src/fs_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ static void cxx_xfree(void *ptr)
xfree(ptr);
}

dwrite_q::dwrite_q(const size_t aSize, char *aBuffer, FREE *aFree) :
buf(aBuffer),
len(aSize),
free_func(aFree)
{
if (!buf) {
buf = static_cast<char *>(xmalloc(len));
free_func = cxx_xfree; // dwrite_q buffer xfree()
}
}

dwrite_q::~dwrite_q()
{
if (free_func)
free_func(buf);
}

/*
* opens a disk file specified by 'path'. This function always
* blocks! There is no callback.
Expand Down Expand Up @@ -138,19 +155,12 @@ diskCombineWrites(_fde_disk *fdd)
for (dwrite_q *q = fdd->write_q; q != nullptr; q = q->next)
len += q->len - q->buf_offset;

auto *wq = new dwrite_q;
wq->buf = (char *)xmalloc(len);
wq->free_func = cxx_xfree; // dwrite_q buffer xfree()

auto *wq = new dwrite_q(len);
while (auto *q = fdd->write_q) {
len = q->len - q->buf_offset;
memcpy(wq->buf + wq->len, q->buf + q->buf_offset, len);
wq->len += len;
fdd->write_q = q->next;

if (q->free_func)
q->free_func(q->buf);

delete q;
};

Expand Down Expand Up @@ -237,10 +247,6 @@ diskHandleWrite(int fd, void *)
*/
while (auto *q = fdd->write_q) {
fdd->write_q = q->next;

if (q->free_func)
q->free_func(q->buf);

delete q;
}
}
Expand All @@ -262,10 +268,6 @@ diskHandleWrite(int fd, void *)
if (q->buf_offset == q->len) {
/* complete write */
fdd->write_q = q->next;

if (q->free_func)
q->free_func(q->buf);

delete q;
}
}
Expand Down Expand Up @@ -318,11 +320,8 @@ file_write(int fd,
assert(fd >= 0);
assert(F->flags.open);
/* if we got here. Caller is eligible to write. */
auto *wq = new dwrite_q;
auto *wq = new dwrite_q(len, static_cast<char *>(const_cast<void *>(ptr_to_buf)), free_func);
wq->file_offset = file_offset;
wq->buf = (char *)ptr_to_buf;
wq->len = len;
wq->free_func = free_func;

if (!F->disk.wrt_handle_data) {
F->disk.wrt_handle = handle;
Expand Down
6 changes: 5 additions & 1 deletion src/fs_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ class dread_ctrl
class dwrite_q
{
MEMPROXY_CLASS(dwrite_q);

public:
dwrite_q() = default;
dwrite_q(const size_t, char * = nullptr, FREE * = nullptr);
dwrite_q(dwrite_q &&) = delete; // no copying or moving of any kind
~dwrite_q();

off_t file_offset = 0;
char *buf = nullptr;
size_t len = 0;
Expand Down

0 comments on commit 50e4b3e

Please sign in to comment.