refactor(mt): move final job-table teardown order to Rust

Normal MT context teardown still let the C wrapper directly sequence
per-job synchronization destruction before Rust released the opaque job-table
storage, while failed create and expansion transactions already used a Rust
helper for that same order. Route the normal teardown through a repr(C)
projection so Rust owns the destroy-before-storage-free policy consistently.
The synchronization callback, descriptor storage, and custom allocator remain
opaque C-owned operations. Add matching C/Rust layout assertions and a focused
callback-order test.

Test Plan:
- `git diff --cached --check` -- passed
- Cargo, make, native tests, and heavy verification were not run per request
This commit is contained in:
2026-07-21 09:15:05 +02:00
parent 19c3414095
commit 10b8b25865
2 changed files with 164 additions and 5 deletions
+34 -2
View File
@@ -1024,6 +1024,30 @@ typedef int (*ZSTDMT_jobTableInitFn)(void* jobTable, unsigned nbJobs,
size_t jobSize);
typedef void (*ZSTDMT_jobTableDestroyFn)(void* jobTable, unsigned nbJobs,
size_t jobSize);
typedef struct {
void* jobTable;
unsigned nbJobs;
size_t jobSize;
ZSTD_customMem cMem;
ZSTDMT_jobTableDestroyFn destroySync;
} ZSTDMT_RustFreeJobsTableProjection;
typedef char ZSTDMT_rust_free_jobs_table_projection_layout[
(sizeof(ZSTDMT_jobTableDestroyFn) == sizeof(void*)
&& offsetof(ZSTDMT_RustFreeJobsTableProjection, jobTable) == 0
&& offsetof(ZSTDMT_RustFreeJobsTableProjection, nbJobs)
== sizeof(void*)
&& offsetof(ZSTDMT_RustFreeJobsTableProjection, jobSize)
== (sizeof(void*) == 8 ? 16 : 8)
&& offsetof(ZSTDMT_RustFreeJobsTableProjection, cMem)
== offsetof(ZSTDMT_RustFreeJobsTableProjection, jobSize)
+ sizeof(size_t)
&& offsetof(ZSTDMT_RustFreeJobsTableProjection, destroySync)
== offsetof(ZSTDMT_RustFreeJobsTableProjection, cMem)
+ sizeof(ZSTD_customMem)
&& sizeof(ZSTDMT_RustFreeJobsTableProjection)
== offsetof(ZSTDMT_RustFreeJobsTableProjection, destroySync)
+ sizeof(ZSTDMT_jobTableDestroyFn))
? 1 : -1];
void* ZSTDMT_rust_createJobsTable(
unsigned* nbJobsPtr, size_t jobSize, ZSTD_customMem cMem,
ZSTDMT_jobTableInitFn initSync, ZSTDMT_jobTableDestroyFn destroySync);
@@ -1032,6 +1056,8 @@ size_t ZSTDMT_rust_expandJobsTable(
size_t jobSize, ZSTD_customMem cMem,
ZSTDMT_jobTableInitFn initSync,
ZSTDMT_jobTableDestroyFn destroySync);
void ZSTDMT_rust_freeJobsTable(
const ZSTDMT_RustFreeJobsTableProjection* projection);
typedef struct ZSTDMT_bufferPool_s {
ZSTDMT_RustBufferPool* rustPool;
@@ -2214,8 +2240,14 @@ static void ZSTDMT_projectJob(void* opaque, unsigned jobID,
static void ZSTDMT_freeJobsTable(ZSTDMT_jobDescription* jobTable, U32 nbJobs, ZSTD_customMem cMem)
{
if (jobTable == NULL) return;
ZSTDMT_job_table_destroy_sync(jobTable, nbJobs, sizeof(*jobTable));
ZSTDMT_rust_job_table_free(jobTable, nbJobs, sizeof(*jobTable), cMem);
ZSTDMT_RustFreeJobsTableProjection const projection = {
jobTable,
nbJobs,
sizeof(*jobTable),
cMem,
ZSTDMT_job_table_destroy_sync
};
ZSTDMT_rust_freeJobsTable(&projection);
}
int ZSTDMT_job_table_init_sync(void* jobTable, unsigned nbJobs, size_t jobSize)