[RT #92738] improve XS for i_io_read()/i_io_raw_read()
authorTony Cook <tony@develop-help.com>
Mon, 31 Mar 2014 09:20:07 +0000 (20:20 +1100)
committerTony Cook <tony@develop-help.com>
Mon, 31 Mar 2014 09:20:07 +0000 (20:20 +1100)
Previously it would call sv_setpvn() which did all the right things,
but we can avoid the sv_utf8_downgrade() and some of the other work
done by sv_setpvn() with the new code.

Imager.xs

index ac80434..2b757fe 100644 (file)
--- a/Imager.xs
+++ b/Imager.xs
@@ -30,6 +30,10 @@ extern "C" {
 
 #include "imperl.h"
 
+#ifndef SV_COW_DROP_PV
+#define SV_COW_DROP_PV
+#endif
+
 /*
 
 Context object management
@@ -1280,15 +1284,9 @@ i_io_raw_read(ig, buffer_sv, size)
       PPCODE:
         if (size <= 0)
          croak("size negative in call to i_io_raw_read()");
-        /* prevent an undefined value warning if they supplied an 
-          undef buffer.
-           Orginally conditional on !SvOK(), but this will prevent the
-          downgrade from croaking */
-       sv_setpvn(buffer_sv, "", 0);
-#ifdef SvUTF8
-       if (SvUTF8(buffer_sv))
-          sv_utf8_downgrade(buffer_sv, FALSE);
-#endif
+       if (SvTHINKFIRST(buffer_sv))
+         sv_force_normal_flags(buffer_sv, SV_COW_DROP_PV);
+       SvUPGRADE(buffer_sv, SVt_PV);
        buffer = SvGROW(buffer_sv, size+1);
         result = i_io_raw_read(ig, buffer, size);
         if (result >= 0) {
@@ -1411,15 +1409,9 @@ i_io_read(ig, buffer_sv, size)
       PPCODE:
         if (size <= 0)
          croak("size negative in call to i_io_read()");
-        /* prevent an undefined value warning if they supplied an 
-          undef buffer.
-           Orginally conditional on !SvOK(), but this will prevent the
-          downgrade from croaking */
-       sv_setpvn(buffer_sv, "", 0);
-#ifdef SvUTF8
-       if (SvUTF8(buffer_sv))
-          sv_utf8_downgrade(buffer_sv, FALSE);
-#endif
+       if (SvTHINKFIRST(buffer_sv))
+         sv_force_normal_flags(buffer_sv, SV_COW_DROP_PV);
+       SvUPGRADE(buffer_sv, SVt_PV);
        buffer = SvGROW(buffer_sv, size+1);
         result = i_io_read(ig, buffer, size);
         if (result >= 0) {