]> iEval git - io-compress-brotli.git/blobdiff - Brotli.xs
Don't leak memory on encoder DESTROY
[io-compress-brotli.git] / Brotli.xs
index dff0a63f3b6017564e51b6582f683e28bcb19053..10beb1c6fd891e2903f44539ef189002c4f01726 100644 (file)
--- a/Brotli.xs
+++ b/Brotli.xs
@@ -200,17 +200,42 @@ _mode(self, mode)
     RETVAL
 
 SV*
-_compress(self, in, op)
+_compress(self, in = &PL_sv_undef)
     IO::Compress::Brotli self
     SV* in
-    U8 op
+  ALIAS:
+    compress = 1
+    flush = 2
+    finish = 3
   PREINIT:
     uint8_t *next_in, *next_out, *buffer;
     size_t available_in, available_out;
     BROTLI_BOOL result;
+    BrotliEncoderOperation op;
   CODE:
-    next_in = (uint8_t*) SvPV(in, available_in);
+    switch(ix) {
+    case 0:
+        croak("_compress may not be called directly");
+        break;
+    case 1:
+        op = BROTLI_OPERATION_PROCESS;
+        break;
+    case 2:
+        op = BROTLI_OPERATION_FLUSH;
+        break;
+    case 3:
+        op = BROTLI_OPERATION_FINISH;
+        break;
+    default:
+        croak("Impossible ix in _compress");
+        break;
+    }
+
     Newx(buffer, BUFFER_SIZE, uint8_t);
+    if(in == &PL_sv_undef)
+        next_in = (uint8_t*) buffer, available_in = 0;
+    else
+        next_in = (uint8_t*) SvPV(in, available_in);
     RETVAL = newSVpv("", 0);
     while(1) {
         next_out = buffer;
@@ -245,6 +270,7 @@ DESTROY(self)
     IO::Compress::Brotli self
   CODE:
     BrotliEncoderDestroyInstance(self->encoder);
+    Safefree(self);
 
 void
 set_dictionary(self, dict)
@@ -256,4 +282,3 @@ set_dictionary(self, dict)
   CODE:
     data = SvPV(dict, size);
     BrotliEncoderSetCustomDictionary(self->encoder, size, data);
-
This page took 0.023374 seconds and 4 git commands to generate.