Skip to content

Commit

Permalink
Maintenance: Upgrade MEM_DWRITE_Q memory pool
Browse files Browse the repository at this point in the history
 ... to a MEMPROXY class pool.

Replacing memory allocate and destruct with C++ new/delete
and in-class initialization.
  • Loading branch information
yadij committed Sep 22, 2024
1 parent c56edb4 commit b7cc30a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 40 deletions.
42 changes: 11 additions & 31 deletions src/fs_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,11 @@ diskCombineWrites(_fde_disk *fdd)
for (dwrite_q *q = fdd->write_q; q != nullptr; q = q->next)
len += q->len - q->buf_offset;

dwrite_q *wq = (dwrite_q *)memAllocate(MEM_DWRITE_Q);

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

wq->len = 0;

wq->buf_offset = 0;

wq->next = nullptr;

wq->free_func = cxx_xfree;

while (fdd->write_q != nullptr) {
dwrite_q *q = fdd->write_q;

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;
Expand All @@ -161,7 +151,7 @@ diskCombineWrites(_fde_disk *fdd)
if (q->free_func)
q->free_func(q->buf);

memFree(q, MEM_DWRITE_Q);
delete q;
};

fdd->write_q_tail = wq;
Expand All @@ -178,11 +168,10 @@ diskHandleWrite(int fd, void *)
fde *F = &fd_table[fd];

_fde_disk *fdd = &F->disk;
dwrite_q *q = fdd->write_q;
int status = DISK_OK;
bool do_close;

if (nullptr == q)
if (!fdd->write_q)
return;

debugs(6, 3, "diskHandleWrite: FD " << fd);
Expand Down Expand Up @@ -246,23 +235,20 @@ diskHandleWrite(int fd, void *)
* repeated write failures for the same FD because of
* the queued data.
*/
do {
while (auto *q = fdd->write_q) {
fdd->write_q = q->next;

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

if (q) {
memFree(q, MEM_DWRITE_Q);
q = nullptr;
}
} while ((q = fdd->write_q));
delete q;
}
}

len = 0;
}

if (q != nullptr) {
if (auto *q = fdd->write_q) {
/* q might become NULL from write failure above */
q->buf_offset += len;

Expand All @@ -280,10 +266,7 @@ diskHandleWrite(int fd, void *)
if (q->free_func)
q->free_func(q->buf);

if (q) {
memFree(q, MEM_DWRITE_Q);
q = nullptr;
}
delete q;
}
}

Expand Down Expand Up @@ -331,17 +314,14 @@ file_write(int fd,
void *handle_data,
FREE * free_func)
{
dwrite_q *wq = nullptr;
fde *F = &fd_table[fd];
assert(fd >= 0);
assert(F->flags.open);
/* if we got here. Caller is eligible to write. */
wq = (dwrite_q *)memAllocate(MEM_DWRITE_Q);
auto *wq = new dwrite_q;
wq->file_offset = file_offset;
wq->buf = (char *)ptr_to_buf;
wq->len = len;
wq->buf_offset = 0;
wq->next = nullptr;
wq->free_func = free_func;

if (!F->disk.wrt_handle_data) {
Expand Down
15 changes: 8 additions & 7 deletions src/fs_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ class dread_ctrl
void *client_data;
};

// POD
class dwrite_q
{
MEMPROXY_CLASS(dwrite_q);

public:
off_t file_offset;
char *buf;
size_t len;
size_t buf_offset;
dwrite_q *next;
FREE *free_func;
off_t file_offset = 0;
char *buf = nullptr;
size_t len = 0;
size_t buf_offset = 0;
dwrite_q *next = nullptr;
FREE *free_func = {}; // function to free 'buf' (if any)
};

int file_open(const char *path, int mode);
Expand Down
1 change: 0 additions & 1 deletion src/mem/forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ typedef enum {
MEM_32K_BUF,
MEM_64K_BUF,
MEM_DREAD_CTRL,
MEM_DWRITE_Q,
MEM_MD5_DIGEST,
MEM_MAX
} mem_type;
Expand Down
1 change: 0 additions & 1 deletion src/mem/old_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ Mem::Init(void)
memDataInit(MEM_64K_BUF, "64K Buffer", 65536, 10, false);
// TODO: Carefully stop zeroing these objects memory and drop the doZero parameter
memDataInit(MEM_DREAD_CTRL, "dread_ctrl", sizeof(dread_ctrl), 0, true);
memDataInit(MEM_DWRITE_Q, "dwrite_q", sizeof(dwrite_q), 0, true);
memDataInit(MEM_MD5_DIGEST, "MD5 digest", SQUID_MD5_DIGEST_LENGTH, 0, true);
GetPool(MEM_MD5_DIGEST)->setChunkSize(512 * 1024);

Expand Down

0 comments on commit b7cc30a

Please sign in to comment.