aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2017-07-23 07:58:02 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2017-07-23 07:58:02 +0300
commit6ffdc2c388a93365c4ec0ee5cb37296b120b2e3c (patch)
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.
-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)
165void 165void
166capture_DESTROY(struct capture *cp) 166capture_DESTROY(struct capture *cp)
167{ 167{
168 free_argv(cp);
169 runcap_free(&cp->rc);
170
171 free(cp->closure[0].str);
172
173 if (cp->program != &PL_sv_undef) 168 if (cp->program != &PL_sv_undef)
174 SvREFCNT_dec(cp->program); 169 SvREFCNT_dec(cp->program);
175 if (cp->input != &PL_sv_undef) 170
171 if (cp->input != &PL_sv_undef) {
176 SvREFCNT_dec(cp->input); 172 SvREFCNT_dec(cp->input);
173 /* Make sure runcap_free won't free the input sc_base pointer
174 */
175 cp->rc.rc_cap[RUNCAP_STDIN].sc_base = NULL;
176 cp->rc.rc_cap[RUNCAP_STDIN].sc_fd = -1;
177 }
177 178
179 free(cp->closure[0].str);
178 if (cp->closure[0].cv != &PL_sv_undef) 180 if (cp->closure[0].cv != &PL_sv_undef)
179 SvREFCNT_dec(cp->closure[0].cv); 181 SvREFCNT_dec(cp->closure[0].cv);
180 182
181 free(cp->closure[1].str); 183 free(cp->closure[1].str);
182 if (cp->closure[1].cv != &PL_sv_undef) 184 if (cp->closure[1].cv != &PL_sv_undef)
183 SvREFCNT_dec(cp->closure[1].cv); 185 SvREFCNT_dec(cp->closure[1].cv);
186
187 free_argv(cp);
188 runcap_free(&cp->rc);
189
184 free(cp); 190 free(cp);
185} 191}
186 192
@@ -192,9 +198,9 @@ capture_set_input(struct capture *cp, SV *inp)
192 if (cp->input != &PL_sv_undef) { 198 if (cp->input != &PL_sv_undef) {
193 SvREFCNT_dec(cp->input); 199 SvREFCNT_dec(cp->input);
194 cp->input = &PL_sv_undef; 200 cp->input = &PL_sv_undef;
195 if (cp->rc.rc_cap[0].sc_base) { 201 if (cp->rc.rc_cap[RUNCAP_STDIN].sc_base) {
196 free(cp->rc.rc_cap[0].sc_base); 202 free(cp->rc.rc_cap[RUNCAP_STDIN].sc_base);
197 cp->rc.rc_cap[0].sc_base = NULL; 203 cp->rc.rc_cap[RUNCAP_STDIN].sc_base = NULL;
198 } 204 }
199 } 205 }
200 } 206 }
@@ -204,27 +210,18 @@ capture_set_input(struct capture *cp, SV *inp)
204 PerlIO *fh = IoIFP(sv_2io(inp)); 210 PerlIO *fh = IoIFP(sv_2io(inp));
205 PerlIO_flush(fh); 211 PerlIO_flush(fh);
206 PerlIO_rewind(fh); 212 PerlIO_rewind(fh);
207 cp->rc.rc_cap[0].sc_fd = PerlIO_fileno(fh); 213 cp->rc.rc_cap[RUNCAP_STDIN].sc_fd = PerlIO_fileno(fh);
208 if (cp->rc.rc_cap[0].sc_fd == -1) 214 if (cp->rc.rc_cap[RUNCAP_STDIN].sc_fd == -1)
209 croak("no file descriptor associated to hanlde"); 215 croak("no file descriptor associated to hanlde");
210 cp->rc.rc_cap[0].sc_base = NULL; 216 cp->rc.rc_cap[RUNCAP_STDIN].sc_base = NULL;
211 cp->rc.rc_cap[0].sc_size = 0; 217 cp->rc.rc_cap[RUNCAP_STDIN].sc_size = 0;
212 } else { 218 } else {
213 croak("argument must be a string or file handle"); 219 croak("argument must be a string or file handle");
214 } 220 }
215 } else { 221 } else {
216 char *s = SvPV(inp, cp->rc.rc_cap[0].sc_size); 222 cp->rc.rc_cap[RUNCAP_STDIN].sc_base
217 223 = SvPV(inp, cp->rc.rc_cap[RUNCAP_STDIN].sc_size);
218 /* FIXME: Unnecessary allocation due to a design flow 224 cp->rc.rc_cap[RUNCAP_STDIN].sc_fd = -1;
219 in runcap */
220 cp->rc.rc_cap[0].sc_base
221 = malloc(cp->rc.rc_cap[0].sc_size);
222 if (!cp->rc.rc_cap[0].sc_base)
223 croak_nomem();
224 memcpy(cp->rc.rc_cap[0].sc_base, s,
225 cp->rc.rc_cap[0].sc_size);
226
227 cp->rc.rc_cap[0].sc_fd = -1;
228 } 225 }
229 SvREFCNT_inc(inp); 226 SvREFCNT_inc(inp);
230 cp->input = inp; 227 cp->input = inp;

Return to:

Send suggestions and report system problems to the System administrator.