aboutsummaryrefslogtreecommitdiffstats
path: root/field.c
diff options
context:
space:
mode:
authorAndrew J. Schorr <aschorr@telemetry-investments.com>2016-07-05 21:55:46 -0400
committerAndrew J. Schorr <aschorr@telemetry-investments.com>2016-07-05 21:55:46 -0400
commit166a253ca87240fe7ec463223af0b25a3e0f3d8a (patch)
tree3c10055520aee77ba235780b056fa8a801c77ea0 /field.c
parentf421a5fbef014040712a7c89e8863c7196f6ab93 (diff)
downloadegawk-166a253ca87240fe7ec463223af0b25a3e0f3d8a.tar.gz
egawk-166a253ca87240fe7ec463223af0b25a3e0f3d8a.tar.bz2
egawk-166a253ca87240fe7ec463223af0b25a3e0f3d8a.zip
When rebuilding $0, do not bother to copy malloc'ed nodes.
Diffstat (limited to 'field.c')
-rw-r--r--field.c53
1 files changed, 21 insertions, 32 deletions
diff --git a/field.c b/field.c
index 823e7a79..308f33cd 100644
--- a/field.c
+++ b/field.c
@@ -194,41 +194,30 @@ rebuild_record()
*/
for (cops = ops, i = 1; i <= NF; i++) {
NODE *r = fields_arr[i];
- if (r->stlen > 0) {
+ /*
+ * I see no reason to copy malloc'ed fields to point into
+ * the new $0 buffer, but that's how previous versions did it.
+ * It seems faster to leave the malloc'ed fields in place.
+ */
+ if (r->stlen > 0 && (r->flags & MALLOC) == 0) {
NODE *n;
getnode(n);
- if ((r->flags & MALLOC) != 0) {
- *n = *Null_field;
- n->stlen = r->stlen;
- if ((r->flags & (NUMCUR|NUMBER)) != 0) {
- n->flags |= (r->flags & (MPFN|MPZN|NUMCUR|NUMBER));
-#ifdef HAVE_MPFR
- if (is_mpg_float(r)) {
- mpfr_init(n->mpg_numbr);
- mpfr_set(n->mpg_numbr, r->mpg_numbr, ROUND_MODE);
- } else if (is_mpg_integer(r)) {
- mpz_init(n->mpg_i);
- mpz_set(n->mpg_i, r->mpg_i);
- } else
-#endif
- n->numbr = r->numbr;
- }
- } else {
- *n = *r;
- if (r->valref > 1) {
- /*
- * XXX This probably should never
- * happen, but we can't leave r with
- * stptr pointing into the old $0
- * buffer. Perhaps we should issue a
- * warning message about memory
- * corruption...
- */
- emalloc(r->stptr, char *, r->stlen + 1, "rebuild_record");
- memcpy(r->stptr, cops, r->stlen);
- r->stptr[r->stlen] = '\0';
- }
+ *n = *r;
+ if (r->valref > 1) {
+ /*
+ * I don't think this should happen, since it
+ * was not considered by previous versions of
+ * this function. But it seems clear to me that
+ * we can't leave r's stptr pointing into the
+ * old $0 buffer that we are about to unref.
+ * It's not obvious to me that valref must be
+ * 1 in all cases, so it seems wise to suppport
+ * this corner case.
+ */
+ emalloc(r->stptr, char *, r->stlen + 1, "rebuild_record");
+ memcpy(r->stptr, cops, r->stlen);
+ r->stptr[r->stlen] = '\0';
}
n->stptr = cops;