JFLib

plat/modabi.rs#

Source: lib/plat/modabi.rs

modabi -- the ABI of loadable modules (jxl-module): the records the host (modhost.rs) and a
module (the module runtime modules/modrt, which has a copy of this file) exchange
                                                                      [jxl-module: no asm]

A module is a no_std cdylib linked against nothing (no imports; built and checked as
build_rs.py's sections jxl-module and "loader checks" say) that exports exactly one function

    <name>_module(host: *const HostTable) -> *const ModTable          rt::plugin_abi!

(`jxlenc_module` for the JPEG XL writer).  The host keeps the module loaded for the rest of
the process; the module keeps the HostTable pointer.  Null = the module refuses the host
(another MOD_VERSION).  Every function pointer has the plugin ABI (rt::plugin_abi!: win64 on
x86-64, also on Linux; C elsewhere), so the records are the same on Windows and Linux.

Memory: the module's global allocator is the host's (HostTable::alloc / free = the host's
GlobalAlloc with the same Layout), so blocks move between both sides as Vec<u8> raw parts
(JxlReq::rgba to the module, JxlRes to the host) without a copy.
Threads: parallel_for runs item(ctx, index, job) on the host's worker pool (and the calling
thread); thr_count / thr_self are the pool's, so per-thread arrays of the module work as in
the host.  A panic of the module ends in HostTable::panic: the host's normal crash report
(stderr "panic: jxlenc: ...", the crash hook, exit code 3); a fault inside the module code
goes to the host's crash handlers like any other.

Version 1.  Layouts are asserted below.
/// ModTable::magic ("MODM" little endian)
MOD_MAGIC: u32 = 0x4D44_4F4D
/// the ABI version of this file
MOD_VERSION: u32 = 1
/// ModTable::kind of the JPEG XL writer
MOD_JXLENC: u32 = 1
/// JxlReq::blobs: which of the blobs are present (None vs Some)
JXB_ICC: u32 = 1
JXB_EXIF: u32 = 2
JXB_XMP: u32 = 4
/// JxlReq::test: crash tests of the module (plat::jxlenc_*: jfgui_JXLENC_TEST=panic / fault)
JXT_PANIC: u32 = 1
JXT_FAULT: u32 = 2
  • type ItemFn = rt::plugin_abi!(fn(ctx: *const c_void, index: usize, job: *const c_void)) one item of a parallel loop: item(ctx, index, job)

  • struct HostTable the host's functions (the module keeps the pointer)

    • size: u32 -- size_of::<HostTable>()
    • version: u32 -- MOD_VERSION
    • cpu: u32 -- the host's rt::cpu::features() (--simd sse2: 0, the module then uses SSE2 too)
    • threads: u32 -- workers of the host's pool
    • alloc: rt::plugin_abi!(fn(size: usize, align: usize, zeroed: u32) -> *mut u8) -- a block of size bytes aligned to align from the host's allocator (zeroed != 0: cleared), null when out of memory
    • free: rt::plugin_abi!(fn(p: *mut u8, size: usize, align: usize)) -- free a block of alloc with its size and alignment
    • parallel_for: rt::plugin_abi!(fn(count: usize, ctx: *const c_void, item: ItemFn) -> u32) -- item(ctx, i, job) for i in 0..count on the pool; 0 = done, 1 = cancelled
    • job_cancel: rt::plugin_abi!(fn(job: *const c_void)) -- job.cancel() of the job an item got
    • job_cancelled: rt::plugin_abi!(fn(job: *const c_void) -> u32) -- job.cancelled()
    • thr_count: rt::plugin_abi!(fn() -> u32) -- the pool's thr_count() / thr_self()
    • thr_self: rt::plugin_abi!(fn() -> usize)
    • yield_now: rt::plugin_abi!(fn()) -- give the time slice away (spin locks)
    • log: rt::plugin_abi!(fn(msg: *const u8, len: usize)) -- one line on stderr ("<module>: msg")
    • panic: rt::plugin_abi!(fn(msg: *const u8, len: usize) -> !) -- a panic of the module (UTF-8 message): the host's crash report, exit code 3
  • struct ModTable what a module offers

    • size: u32 -- size_of::<ModTable>()
    • version: u32 -- MOD_VERSION
    • magic: u32 -- MOD_MAGIC
    • kind: u32 -- MOD_JXLENC
    • jxl_encode: rt::plugin_abi!(fn(req: *const JxlReq, res: *mut JxlRes) -> i32) -- the JPEG XL writer: 0 and res filled, or ERR_* (res empty)
  • struct JxlReq a JPEG XL encode: the samples of img_jxl::writer::image_of and the writer's options

    • size: u32 -- size_of::<JxlReq>()
    • w: u32
    • h: u32
    • gray: u32 -- Px::gray
    • alpha: u32 -- Px::alpha
    • effort: u32
    • quality: u32
    • flags: u32 -- IO_FLAGS
    • blobs: u32 -- JXB_*: which blobs are present
    • test: u32 -- JXT_* (0)
    • rgba: *mut u8 -- Px::rgba (R, G, B, A per pixel) as Vec raw parts: the module owns it from now on
    • rgba_len: usize
    • rgba_cap: usize
    • icc: *const u8
    • icc_len: usize
    • exif: *const u8
    • exif_len: usize
    • xmp: *const u8
    • xmp_len: usize
    • cancel: *const u32 -- the cancel flag (an AtomicU32), null = none
  • struct JxlRes the file the writer made, as Vec raw parts (the host owns it from now on)

    • data: *mut u8
    • len: usize
    • cap: usize

Source: docs/API.md in the JFLib repository.