Make io_buffer_t::create return a shared_ptr

Eliminates some manual memory management
This commit is contained in:
ridiculousfish 2017-01-22 00:44:04 -08:00
parent 009a677e0d
commit 6bdab62358
3 changed files with 6 additions and 7 deletions

View file

@ -642,7 +642,7 @@ void exec_job(parser_t &parser, job_t *j) {
if (p->next) {
// Be careful to handle failure, e.g. too many open fds.
block_output_io_buffer.reset(io_buffer_t::create(STDOUT_FILENO, all_ios));
block_output_io_buffer = io_buffer_t::create(STDOUT_FILENO, all_ios);
if (block_output_io_buffer.get() == NULL) {
exec_error = true;
job_mark_process_as_failed(j, p);
@ -668,7 +668,7 @@ void exec_job(parser_t &parser, job_t *j) {
case INTERNAL_BLOCK_NODE: {
if (p->next) {
block_output_io_buffer.reset(io_buffer_t::create(STDOUT_FILENO, all_ios));
block_output_io_buffer = io_buffer_t::create(STDOUT_FILENO, all_ios);
if (block_output_io_buffer.get() == NULL) {
// We failed (e.g. no more fds could be created).
exec_error = true;

View file

@ -76,10 +76,10 @@ bool io_buffer_t::avoid_conflicts_with_io_chain(const io_chain_t &ios) {
return result;
}
io_buffer_t *io_buffer_t::create(int fd, const io_chain_t &conflicts) {
shared_ptr<io_buffer_t> io_buffer_t::create(int fd, const io_chain_t &conflicts) {
bool success = true;
assert(fd >= 0);
io_buffer_t *buffer_redirect = new io_buffer_t(fd);
shared_ptr<io_buffer_t> buffer_redirect(new io_buffer_t(fd));
if (exec_pipe(buffer_redirect->pipe_fd) == -1) {
debug(1, PIPE_ERROR);
@ -95,8 +95,7 @@ io_buffer_t *io_buffer_t::create(int fd, const io_chain_t &conflicts) {
}
if (!success) {
delete buffer_redirect;
buffer_redirect = NULL;
buffer_redirect.reset();
}
return buffer_redirect;
}

View file

@ -133,7 +133,7 @@ class io_buffer_t : public io_pipe_t {
/// \param fd the fd that will be mapped in the child process, typically STDOUT_FILENO
/// \param conflicts A set of IO redirections. The function ensures that any pipe it makes does
/// not conflict with an fd redirection in this list.
static io_buffer_t *create(int fd, const io_chain_t &conflicts);
static shared_ptr<io_buffer_t> create(int fd, const io_chain_t &conflicts);
};
class io_chain_t : public std::vector<shared_ptr<io_data_t> > {