From 678febd75acbe46e48e9655d1e7c6345b1d04f0a Mon Sep 17 00:00:00 2001 From: Nicholas Clark Date: Thu, 11 Nov 2010 21:37:48 +0000 Subject: [PATCH 1/1] 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. --- sv.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sv.c b/sv.c index a1ca186..e811580 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 -- 1.8.3.1