Skip to content

Commit 98a17f4

Browse files
Remove some functions which were rendered trivial by xiaq's changes. Make io_file_t take its path directly. Make io_buffer_t no longer use a shared_ptr for its data.
1 parent f850c02 commit 98a17f4

File tree

5 files changed

+18
-75
lines changed

5 files changed

+18
-75
lines changed

exec.cpp

+8-15
Original file line numberDiff line numberDiff line change
@@ -332,20 +332,11 @@ static int has_fd(const io_chain_t &d, int fd)
332332
}
333333

334334
/**
335-
Free a transmogrified io chain. Only the chain itself and resources
336-
used by a transmogrified IO_FILE redirection are freed, since the
337-
original chain may still be needed.
335+
Close a list of fds.
338336
*/
339-
static void io_cleanup_chains(io_chain_t &chains, const std::vector<int> &opened_fds)
337+
static void io_cleanup_fds(const std::vector<int> &opened_fds)
340338
{
341-
/* Close all the fds */
342-
for (size_t idx = 0; idx < opened_fds.size(); idx++)
343-
{
344-
close(opened_fds.at(idx));
345-
}
346-
347-
/* Then delete all of the redirections we made */
348-
chains.destroy();
339+
std::for_each(opened_fds.begin(), opened_fds.end(), close);
349340
}
350341

351342
/**
@@ -446,7 +437,8 @@ static bool io_transmogrify(const io_chain_t &in_chain, io_chain_t &out_chain, s
446437
else
447438
{
448439
/* No dice - clean up */
449-
io_cleanup_chains(result_chain, opened_fds);
440+
result_chain.clear();
441+
io_cleanup_fds(opened_fds);
450442
}
451443
return success;
452444
}
@@ -488,7 +480,8 @@ static void internal_exec_helper(parser_t &parser,
488480

489481
signal_block();
490482

491-
io_cleanup_chains(morphed_chain, opened_fds);
483+
morphed_chain.clear();
484+
io_cleanup_fds(opened_fds);
492485
job_reap(0);
493486
is_block=is_block_old;
494487
}
@@ -563,7 +556,7 @@ void exec(parser_t &parser, job_t *j)
563556

564557
if (! parser.block_io.empty())
565558
{
566-
io_duplicate_prepend(parser.block_io, j->io);
559+
j->io.insert(j->io.begin(), parser.block_io.begin(), parser.block_io.end());
567560
}
568561

569562
const io_buffer_t *input_redirect = 0;

io.cpp

-22
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ io_buffer_t *io_buffer_t::create(bool is_input)
135135
{
136136
bool success = true;
137137
io_buffer_t *buffer_redirect = new io_buffer_t(is_input ? 0 : 1, is_input);
138-
buffer_redirect->out_buffer_create();
139138

140139
if (exec_pipe(buffer_redirect->pipe_fd) == -1)
141140
{
@@ -194,17 +193,6 @@ void io_chain_t::remove(const shared_ptr<const io_data_t> &element)
194193
}
195194
}
196195

197-
void io_chain_t::duplicate_prepend(const io_chain_t &src)
198-
{
199-
/* Prepend a duplicate of src before this. */
200-
this->insert(this->begin(), src.begin(), src.end());
201-
}
202-
203-
void io_chain_t::destroy()
204-
{
205-
this->clear();
206-
}
207-
208196
void io_remove(io_chain_t &list, const shared_ptr<const io_data_t> &element)
209197
{
210198
list.remove(element);
@@ -227,16 +215,6 @@ void io_print(const io_chain_t &chain)
227215
}
228216
}
229217

230-
void io_duplicate_prepend(const io_chain_t &src, io_chain_t &dst)
231-
{
232-
return dst.duplicate_prepend(src);
233-
}
234-
235-
void io_chain_destroy(io_chain_t &chain)
236-
{
237-
chain.destroy();
238-
}
239-
240218
/* Return the last IO for the given fd */
241219
shared_ptr<const io_data_t> io_chain_t::get_io_for_fd(int fd) const
242220
{

io.h

+9-35
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,15 @@ class io_file_t : public io_data_t
7171
{
7272
public:
7373
/** Filename, malloc'd. This needs to be used after fork, so don't use wcstring here. */
74-
const char *filename_cstr;
74+
const char * const filename_cstr;
7575
/** file creation flags to send to open */
7676
int flags;
77-
78-
/** Convenience to set filename_cstr via wcstring */
79-
void set_filename(const wcstring &str)
80-
{
81-
free((void *)filename_cstr);
82-
filename_cstr = wcs2str(str.c_str());
83-
}
84-
77+
8578
virtual void print() const;
8679

87-
io_file_t(int f, const char *fname = NULL, int fl = 0) :
80+
io_file_t(int f, const wcstring &fname, int fl = 0) :
8881
io_data_t(IO_FILE, f),
89-
filename_cstr(fname ? strdup(fname) : NULL),
82+
filename_cstr(wcs2str(fname)),
9083
flags(fl)
9184
{
9285
}
@@ -125,7 +118,7 @@ class io_buffer_t : public io_pipe_t
125118
{
126119
private:
127120
/** buffer to save output in */
128-
shared_ptr<std::vector<char> > out_buffer;
121+
std::vector<char> out_buffer;
129122

130123
io_buffer_t(int f, bool i):
131124
io_pipe_t(IO_BUFFER, f, i),
@@ -138,37 +131,27 @@ class io_buffer_t : public io_pipe_t
138131

139132
virtual ~io_buffer_t();
140133

141-
/** Function to create the output buffer */
142-
void out_buffer_create()
143-
{
144-
out_buffer.reset(new std::vector<char>);
145-
}
146-
147134
/** Function to append to the buffer */
148135
void out_buffer_append(const char *ptr, size_t count)
149136
{
150-
assert(out_buffer.get() != NULL);
151-
out_buffer->insert(out_buffer->end(), ptr, ptr + count);
137+
out_buffer.insert(out_buffer.end(), ptr, ptr + count);
152138
}
153139

154140
/** Function to get a pointer to the buffer */
155141
char *out_buffer_ptr(void)
156142
{
157-
assert(out_buffer.get() != NULL);
158-
return out_buffer->empty() ? NULL : &out_buffer->at(0);
143+
return out_buffer.empty() ? NULL : &out_buffer.at(0);
159144
}
160145

161146
const char *out_buffer_ptr(void) const
162147
{
163-
assert(out_buffer.get() != NULL);
164-
return out_buffer->empty() ? NULL : &out_buffer->at(0);
148+
return out_buffer.empty() ? NULL : &out_buffer.at(0);
165149
}
166150

167151
/** Function to get the size of the buffer */
168152
size_t out_buffer_size(void) const
169153
{
170-
assert(out_buffer.get() != NULL);
171-
return out_buffer->size();
154+
return out_buffer.size();
172155
}
173156

174157
/**
@@ -195,9 +178,6 @@ class io_chain_t : public std::vector<shared_ptr<io_data_t> >
195178
io_chain_t(const shared_ptr<io_data_t> &);
196179

197180
void remove(const shared_ptr<const io_data_t> &element);
198-
io_chain_t duplicate() const;
199-
void duplicate_prepend(const io_chain_t &src);
200-
void destroy();
201181

202182
shared_ptr<const io_data_t> get_io_for_fd(int fd) const;
203183
shared_ptr<io_data_t> get_io_for_fd(int fd);
@@ -209,12 +189,6 @@ class io_chain_t : public std::vector<shared_ptr<io_data_t> >
209189
*/
210190
void io_remove(io_chain_t &list, const shared_ptr<const io_data_t> &element);
211191

212-
/** Return a shallow copy of the specified chain of redirections that contains only the applicable redirections. That is, if there's multiple redirections for the same fd, only the second one is included. */
213-
io_chain_t io_unique(const io_chain_t &chain);
214-
215-
/** Prepends a copy of the specified 'src' chain of redirections to 'dst.' Uses operator new. */
216-
void io_duplicate_prepend(const io_chain_t &src, io_chain_t &dst);
217-
218192
/** Destroys an io_chain */
219193
void io_chain_destroy(io_chain_t &chain);
220194

parser.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -1619,8 +1619,7 @@ void parser_t::parse_job_argument_list(process_t *p,
16191619
break;
16201620

16211621
}
1622-
io_file_t *new_io_file = new io_file_t(fd, NULL, flags);
1623-
new_io_file->set_filename(target);
1622+
io_file_t *new_io_file = new io_file_t(fd, target, flags);
16241623
new_io.reset(new_io_file);
16251624
}
16261625
}

proc.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,6 @@ job_t::~job_t()
544544
{
545545
if (first_process != NULL)
546546
delete first_process;
547-
io_chain_destroy(this->io);
548547
release_job_id(job_id);
549548
}
550549

0 commit comments

Comments
 (0)