diff --git a/src/fs_io.cc b/src/fs_io.cc index b1df20b81fc..148a8935dda 100644 --- a/src/fs_io.cc +++ b/src/fs_io.cc @@ -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(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. @@ -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; }; @@ -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; } } @@ -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; } } @@ -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(const_cast(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; diff --git a/src/fs_io.h b/src/fs_io.h index 77a93e88151..c1493a7d398 100644 --- a/src/fs_io.h +++ b/src/fs_io.h @@ -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;