Fix a memory leak where Galois field acceleration functions created by
ErasureCodeJerasure::prepare() were never freed. ASan detected this as
a one-time leak when the plugin was unloaded.
Add jerasure_finish() destructor function in jerasure_init.cc to free
the allocated Galois field operations. Since jerasure_init.cc and
galois.c are built into the same object library, jerasure_finish() can
access and clean up the global static acceleration functions defined
in galois.c.
The destructor function is automatically called when the shared library
(plugin) is unloaded, ensuring proper cleanup without requiring explicit
calls from client code.
Signed-off-by: Kefu Chai <tchaikov@gmail.com>
}
return 0;
}
+
+void jerasure_finish()
+{
+ // jerasure based codings generate matrices using Galois field operations via
+ // the Jerasure library. The underlying acceleration functions for different
+ // word sizes are cached in global static variables and initialized lazily.
+ // These cached functions must be explicitly freed after erasure coding
+ // operations complete to prevent memory leaks.
+ // Note:
+ // - Operations for word sizes > 32 bits are not yet implemented
+ // - Jerasure only supports word sizes that are power of 2.
+ static const int words[] = {4, 8, 16, 32};
+ for (auto w : words) {
+ gf_t* gf = galois_get_field_ptr(w);
+ if (gf) {
+ gf_free(gf, 0);
+ free(gf);
+ }
+ }
+}
#define CEPH_JERASURE_INIT_H
extern "C" int jerasure_init(int count, int *words);
+void jerasure_finish() __attribute__((destructor));
#endif