summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org.ua>2017-07-23 04:58:02 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2017-07-23 04:58:02 (GMT)
commit6ffdc2c388a93365c4ec0ee5cb37296b120b2e3c (patch) (side-by-side diff)
tree72d0b8c08f9d8d2642d08b56cacfa6ff5f12fc33
parent7f5cbd2332df055d8b9e365974f8743f82ad4886 (diff)
downloadposixruncapture-6ffdc2c388a93365c4ec0ee5cb37296b120b2e3c.tar.gz
posixruncapture-6ffdc2c388a93365c4ec0ee5cb37296b120b2e3c.tar.bz2
Avoid unnecessary memory allocation
* capture.c (capture_set_input): Don't allocate input sc_base. (capture_DESTROY): Reset sc_base to NULL.
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--capture.c47
1 files changed, 22 insertions, 25 deletions
diff --git a/capture.c b/capture.c
index 67f2bcd..f22fc9f 100644
--- a/capture.c
+++ b/capture.c
@@ -165,22 +165,28 @@ capture_new(SV *program, ARGV argv, unsigned timeout, SV *cb[2], SV *input)
void
capture_DESTROY(struct capture *cp)
{
- free_argv(cp);
- runcap_free(&cp->rc);
-
- free(cp->closure[0].str);
-
if (cp->program != &PL_sv_undef)
SvREFCNT_dec(cp->program);
- if (cp->input != &PL_sv_undef)
+
+ if (cp->input != &PL_sv_undef) {
SvREFCNT_dec(cp->input);
+ /* Make sure runcap_free won't free the input sc_base pointer
+ */
+ cp->rc.rc_cap[RUNCAP_STDIN].sc_base = NULL;
+ cp->rc.rc_cap[RUNCAP_STDIN].sc_fd = -1;
+ }
+ free(cp->closure[0].str);
if (cp->closure[0].cv != &PL_sv_undef)
SvREFCNT_dec(cp->closure[0].cv);
free(cp->closure[1].str);
if (cp->closure[1].cv != &PL_sv_undef)
SvREFCNT_dec(cp->closure[1].cv);
+
+ free_argv(cp);
+ runcap_free(&cp->rc);
+
free(cp);
}
@@ -192,9 +198,9 @@ capture_set_input(struct capture *cp, SV *inp)
if (cp->input != &PL_sv_undef) {
SvREFCNT_dec(cp->input);
cp->input = &PL_sv_undef;
- if (cp->rc.rc_cap[0].sc_base) {
- free(cp->rc.rc_cap[0].sc_base);
- cp->rc.rc_cap[0].sc_base = NULL;
+ if (cp->rc.rc_cap[RUNCAP_STDIN].sc_base) {
+ free(cp->rc.rc_cap[RUNCAP_STDIN].sc_base);
+ cp->rc.rc_cap[RUNCAP_STDIN].sc_base = NULL;
}
}
}
@@ -204,27 +210,18 @@ capture_set_input(struct capture *cp, SV *inp)
PerlIO *fh = IoIFP(sv_2io(inp));
PerlIO_flush(fh);
PerlIO_rewind(fh);
- cp->rc.rc_cap[0].sc_fd = PerlIO_fileno(fh);
- if (cp->rc.rc_cap[0].sc_fd == -1)
+ cp->rc.rc_cap[RUNCAP_STDIN].sc_fd = PerlIO_fileno(fh);
+ if (cp->rc.rc_cap[RUNCAP_STDIN].sc_fd == -1)
croak("no file descriptor associated to hanlde");
- cp->rc.rc_cap[0].sc_base = NULL;
- cp->rc.rc_cap[0].sc_size = 0;
+ cp->rc.rc_cap[RUNCAP_STDIN].sc_base = NULL;
+ cp->rc.rc_cap[RUNCAP_STDIN].sc_size = 0;
} else {
croak("argument must be a string or file handle");
}
} else {
- char *s = SvPV(inp, cp->rc.rc_cap[0].sc_size);
-
- /* FIXME: Unnecessary allocation due to a design flow
- in runcap */
- cp->rc.rc_cap[0].sc_base
- = malloc(cp->rc.rc_cap[0].sc_size);
- if (!cp->rc.rc_cap[0].sc_base)
- croak_nomem();
- memcpy(cp->rc.rc_cap[0].sc_base, s,
- cp->rc.rc_cap[0].sc_size);
-
- cp->rc.rc_cap[0].sc_fd = -1;
+ cp->rc.rc_cap[RUNCAP_STDIN].sc_base
+ = SvPV(inp, cp->rc.rc_cap[RUNCAP_STDIN].sc_size);
+ cp->rc.rc_cap[RUNCAP_STDIN].sc_fd = -1;
}
SvREFCNT_inc(inp);
cp->input = inp;

Return to:

Send suggestions and report system problems to the System administrator.