Skip to content

Commit 08d7363

Browse files
committed
erasure-code/jerasure: fix memory leak in Galois field operations
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 <[email protected]>
1 parent 25803ec commit 08d7363

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/erasure-code/jerasure/jerasure_init.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,23 @@ extern "C" int jerasure_init(int count, int *words)
3535
}
3636
return 0;
3737
}
38+
39+
void jerasure_finish()
40+
{
41+
// jerasure based codings generate matrices using Galois field operations via
42+
// the Jerasure library. The underlying acceleration functions for different
43+
// word sizes are cached in global static variables and initialized lazily.
44+
// These cached functions must be explicitly freed after erasure coding
45+
// operations complete to prevent memory leaks.
46+
// Note:
47+
// - Operations for word sizes > 32 bits are not yet implemented
48+
// - Jerasure only supports word sizes that are power of 2.
49+
static const int words[] = {4, 8, 16, 32};
50+
for (auto w : words) {
51+
gf_t* gf = galois_get_field_ptr(w);
52+
if (gf) {
53+
gf_free(gf, 0);
54+
free(gf);
55+
}
56+
}
57+
}

src/erasure-code/jerasure/jerasure_init.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define CEPH_JERASURE_INIT_H
2020

2121
extern "C" int jerasure_init(int count, int *words);
22+
void jerasure_finish() __attribute__((destructor));
2223

2324
#endif
2425

0 commit comments

Comments
 (0)