aboutsummaryrefslogtreecommitdiff
path: root/src/serialization.cpp
diff options
context:
space:
mode:
authorsfan5 <sfan5@live.de>2022-01-30 22:44:29 +0100
committersfan5 <sfan5@live.de>2022-01-30 22:57:44 +0100
commit484a4b518f6025f2cdec9eb89fdf3883eb57fc28 (patch)
treebafe1a32ce30f161f6eb74e7cdeb418c17d823e2 /src/serialization.cpp
parent5e4a01f2debd173058963f3c19dd954fcd039660 (diff)
downloadminetest-484a4b518f6025f2cdec9eb89fdf3883eb57fc28.tar.gz
minetest-484a4b518f6025f2cdec9eb89fdf3883eb57fc28.tar.bz2
minetest-484a4b518f6025f2cdec9eb89fdf3883eb57fc28.zip
Add another very awful workaround to prevent a crash on Mingw32
This appears to be the same issue as 70df3d54f37c280f7afe60f6e964b8406577f39f. Hopefully the next MinGW update will remove the need for this.
Diffstat (limited to 'src/serialization.cpp')
-rw-r--r--src/serialization.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/serialization.cpp b/src/serialization.cpp
index b6ce3b37f..d4d7b5f6e 100644
--- a/src/serialization.cpp
+++ b/src/serialization.cpp
@@ -208,11 +208,31 @@ struct ZSTD_Deleter {
}
};
+#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
thread_local std::unique_ptr<ZSTD_CStream, ZSTD_Deleter> stream(ZSTD_createCStream());
+#endif
+
ZSTD_initCStream(stream.get(), level);
@@ -256,9 +276,14 @@ void compressZstd(const std::string &data, std::ostream &os, int 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
thread_local std::unique_ptr<ZSTD_DStream, ZSTD_Deleter> stream(ZSTD_createDStream());
+#endif
ZSTD_initDStream(stream.get());