char output_buffer[bufsize];
int status = 0;
int ret;
- int bytes_read = 0;
int bytes_written = 0;
int input_buffer_len = 0;
z.avail_in = 0;
- //dstream<<"initial fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
-
for(;;)
{
int output_size = bufsize;
is.read(input_buffer, bufsize);
input_buffer_len = is.gcount();
z.avail_in = input_buffer_len;
- //dstream<<"read fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
}
if(z.avail_in == 0)
{
- //dstream<<"z.avail_in == 0"<<std::endl;
break;
}
- //dstream<<"1 z.avail_in="<<z.avail_in<<std::endl;
status = inflate(&z, Z_NO_FLUSH);
- //dstream<<"2 z.avail_in="<<z.avail_in<<std::endl;
- bytes_read += is.gcount() - z.avail_in;
- //dstream<<"bytes_read="<<bytes_read<<std::endl;
if(status == Z_NEED_DICT || status == Z_DATA_ERROR
|| status == Z_MEM_ERROR)
throw SerializationError("decompressZlib: inflate failed");
}
int count = output_size - z.avail_out;
- //dstream<<"count="<<count<<std::endl;
if(count)
os.write(output_buffer, count);
bytes_written += count;
if(status == Z_STREAM_END)
{
- //dstream<<"Z_STREAM_END"<<std::endl;
-
- //dstream<<"z.avail_in="<<z.avail_in<<std::endl;
- //dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
// Unget all the data that inflate didn't take
is.clear(); // Just in case EOF is set
for(u32 i=0; i < z.avail_in; i++)
}
};
-#if defined(__MINGW32__) && !defined(__MINGW64__)
-/*
- * This is exactly as dumb as it looks.
- * Yes, this is a memory leak. No, we don't have better solution right now.
- */
-template<typename T> class leaky_ptr
-{
- T *value;
-public:
- leaky_ptr(T *value) : value(value) {};
- T *get() { return value; }
-};
-#endif
-
void compressZstd(const u8 *data, size_t data_size, std::ostream &os, int level)
{
-#if defined(__MINGW32__) && !defined(__MINGW64__)
- // leaks one context per thread but doesn't crash :shrug:
- thread_local leaky_ptr<ZSTD_CStream> stream(ZSTD_createCStream());
-#else
// reusing the context is recommended for performance
- // it will destroyed when the thread ends
+ // it will be destroyed when the thread ends
thread_local std::unique_ptr<ZSTD_CStream, ZSTD_Deleter> stream(ZSTD_createCStream());
-#endif
ZSTD_initCStream(stream.get(), level);
void decompressZstd(std::istream &is, std::ostream &os)
{
-#if defined(__MINGW32__) && !defined(__MINGW64__)
- // leaks one context per thread but doesn't crash :shrug:
- thread_local leaky_ptr<ZSTD_DStream> stream(ZSTD_createDStream());
-#else
// reusing the context is recommended for performance
- // it will destroyed when the thread ends
+ // it will be destroyed when the thread ends
thread_local std::unique_ptr<ZSTD_DStream, ZSTD_Deleter> stream(ZSTD_createDStream());
-#endif
ZSTD_initDStream(stream.get());