summaryrefslogtreecommitdiff
path: root/sv.c
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2010-11-11 21:37:48 +0000
committerNicholas Clark <nick@ccl4.org>2010-11-11 21:37:48 +0000
commit678febd75acbe46e48e9655d1e7c6345b1d04f0a (patch)
tree619f06ee1b50fda759b435f2d04e619d1c01fb1d /sv.c
parentaf16de9fb95af5577c9af62f59b791ea3a98d2b9 (diff)
downloadperl-678febd75acbe46e48e9655d1e7c6345b1d04f0a.tar.gz
In newSVhek(), use sv_usepvn_flags() to avoid a malloc()/copy/free()
The return value of bytes_to_utf8() is a malloc()ed string, which we can donate to the scalar to use as its buffer. Previously the code free()d this memory, after using it as a parameter to newSVpvn(), which allocates a new buffer and copies to it.
Diffstat (limited to 'sv.c')
-rw-r--r--sv.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/sv.c b/sv.c
index a1ca186b6f..e81158013f 100644
--- a/sv.c
+++ b/sv.c
@@ -8214,11 +8214,11 @@ Perl_newSVhek(pTHX_ const HEK *const hek)
Andreas would like keys he put in as utf8 to come back as utf8
*/
STRLEN utf8_len = HEK_LEN(hek);
- const U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
- SV * const sv = newSVpvn ((const char*)as_utf8, utf8_len);
-
+ SV * const sv = newSV_type(SVt_PV);
+ char *as_utf8 = (char *)bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
+ /* bytes_to_utf8() allocates a new string, which we can repurpose: */
+ sv_usepvn_flags(sv, as_utf8, utf8_len, SV_HAS_TRAILING_NUL);
SvUTF8_on (sv);
- Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
return sv;
} else if (flags & (HVhek_REHASH|HVhek_UNSHARED)) {
/* We don't have a pointer to the hv, so we have to replicate the