Patching Dec 9, 2021 6-7a CST- All GitLab services may be unavailable for 5-10 minutes

Commit c14c052b authored by Roland Haas's avatar Roland Haas
Browse files

async_writer: allow per-write free() function

parent 26c70690
......@@ -43,7 +43,7 @@ async_writer::~async_writer()
assert(!thread_active);
}
void async_writer::write(const void* buf, size_t count)
void async_writer::write(const void* buf, size_t count, void (*free_func)(void*))
{
assert(thread_active);
......@@ -51,6 +51,7 @@ void async_writer::write(const void* buf, size_t count)
write_cmd.write_block.cmd = CMD_WRITE;
write_cmd.write_block.count = count;
write_cmd.write_block.buf = buf;
write_cmd.write_block.free_func = free_func;
pthread_mutex_lock(&bytes_lock);
while(bytes_queued >= max_bytes_queued)
......@@ -147,7 +148,8 @@ void async_writer::writer()
cmd_block.write_block.count, strerror(errno));
exit(1);
}
free(const_cast<void*>(cmd_block.write_block.buf));
if(cmd_block.write_block.free_func)
cmd_block.write_block.free_func(const_cast<void*>(cmd_block.write_block.buf));
pthread_mutex_lock(&bytes_lock);
bytes_queued -= written;
......
......@@ -3,6 +3,7 @@
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <pthread.h>
#include <queue>
......@@ -20,9 +21,9 @@ class async_writer
async_writer& operator=(const async_writer& other);
public:
// write buf to file, free() memory once done ie. buf must be obtained from
// malloc()
void write(const void* buf, size_t count);
// write buf to file, call free_func() on block of memory once done
// it is NULL)
void write(const void* buf, size_t count, void (*free_func)(void*) = free);
// seek to location offset in the file
void seek(long offset);
// flush command queue and wait for writer to finish
......@@ -43,6 +44,7 @@ class async_writer
cmd_name cmd;
const void *buf;
size_t count;
void (*free_func)(void*);
} write_block;
struct seek_block_t {
cmd_name cmd;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment