static PyObject * compress_helper(void * input, size_t nbytes, size_t typesize, int clevel, int shuffle, char *cname){ int cbytes; PyObject *output = NULL; /* Alloc memory for compression */ if (!(output = PyBytes_FromStringAndSize(NULL, nbytes+BLOSC_MAX_OVERHEAD))) return NULL; /* Select compressor */ if (blosc_set_compressor(cname) < 0) { /* The compressor is not available (should never happen here) */ blosc_error(-1, "compressor not available"); return NULL; } /* Compress */ cbytes = blosc_compress(clevel, shuffle, typesize, nbytes, input, PyBytes_AS_STRING(output), nbytes+BLOSC_MAX_OVERHEAD); if (cbytes < 0) { blosc_error(cbytes, "while compressing data"); Py_XDECREF(output); return NULL; } /* Attempt to resize, if it's much smaller, a copy is required. */ if (_PyBytes_Resize(&output, cbytes) < 0){ /* the memory exception will have been set, hopefully */ return NULL; } return output; }
/* Read blosc header from input and fetch the uncompressed size into nbytes. * Also makes sure that value of the compressed bytes from the header is the * same as the cbytes provided by the input. * * Returns 1 on success and 0 on failure with a Python exception set. * * */ static int get_nbytes(void * input, size_t cbytes, size_t * nbytes) { size_t cbytes2, blocksize; /* Get the length of the uncompressed buffer */ blosc_cbuffer_sizes(input, nbytes, &cbytes2, &blocksize); if ((size_t)cbytes != cbytes2) { blosc_error((int)cbytes, ": not a Blosc buffer or header info is corrupted"); return 0; } return 1; }
/* Decompress nbytes from input into output. * * Returns 1 on success and 0 on failure with a Python exception set. * * */ static int decompress_helper(void * input, size_t nbytes, void * output) { int err; /* Do the decompression */ err = blosc_decompress(input, output, nbytes); if (err < 0 || err != (int)nbytes) { blosc_error(err, "while decompressing data"); return 0; } return 1; }
/* Decompress nbytes from input into output. * * Returns 1 on success and 0 on failure with a Python exception set. * * */ static int decompress_helper(void * input, size_t nbytes, void * output) { int err, nthreads; PyThreadState *_save = NULL; /* Do the decompression */ // int blosc_decompress_ctx(const void *src, void *dest, size_t destsize, // int numinternalthreads) if( RELEASEGIL ) { _save = PyEval_SaveThread(); nthreads = blosc_get_nthreads(); err = blosc_decompress_ctx(input, output, nbytes, nthreads); PyEval_RestoreThread(_save); _save = NULL; } else { // Run while holding the GIL err = blosc_decompress(input, output, nbytes); } if (err < 0) { blosc_error(err, "while decompressing data"); return 0; } else if (err != (int)nbytes) { PyErr_Format(BloscError, "expected %d bytes of decompressed data, got %d", (int) nbytes, err); return 0; } return 1; }
static PyObject * compress_helper(void * input, size_t nbytes, size_t typesize, int clevel, int shuffle, char *cname){ int cbytes, blocksize, nthreads; PyObject *output; char *output_ptr; PyThreadState *_save = NULL; /* Alloc memory for compression */ if (!(output = PyBytes_FromStringAndSize(NULL, nbytes+BLOSC_MAX_OVERHEAD))) return NULL; /* Select compressor */ if (blosc_set_compressor(cname) < 0) { /* The compressor is not available (should never happen here) */ blosc_error(-1, "compressor not available"); Py_DECREF(output); return NULL; } /* Compress */ // This macro probably doesn't require the Python interpreter but let's leave it outside for safety output_ptr = PyBytes_AS_STRING(output); if( RELEASEGIL ) { // Run with GIL released, tiny overhead penalty from this (although it // may be significant for smaller chunks.) _save = PyEval_SaveThread(); blocksize = blosc_get_blocksize(); // if blocksize==0, blosc_compress_ctx will try to auto-optimize it. nthreads = blosc_get_nthreads(); cbytes = blosc_compress_ctx(clevel, shuffle, typesize, nbytes, input, output_ptr, nbytes+BLOSC_MAX_OVERHEAD, cname, blocksize, nthreads); PyEval_RestoreThread(_save); _save = NULL; } else { // Hold onto the Python GIL while compressing cbytes = blosc_compress(clevel, shuffle, typesize, nbytes, input, output_ptr, nbytes+BLOSC_MAX_OVERHEAD); } if (cbytes < 0) { blosc_error(cbytes, "while compressing data"); Py_DECREF(output); return NULL; } /* Attempt to resize, if it's much smaller, a copy is required. */ if (_PyBytes_Resize(&output, cbytes) < 0){ /* the memory exception will have been set, hopefully */ Py_DECREF(output); return NULL; } return output; }