skip to content
==> include/mesh/exec_wasm_cache.h <==
1/* @ajax/mesh-exec-wasm-cache: meshd's `--wasm-cache DIR` (mesh-host/src/wasm.rs `Rt::module`,
2 * `evict`): a compiled module is kept as `<wasm fnv1a64>-<engine hash>.cwasm` (16 hex digits each),
3 * written to a temp file and renamed, and the directory is kept to at most a cap of bytes by
4 * deleting the oldest `.cwasm` files (by modification time). */
5#ifndef MESH_EXEC_WASM_CACHE_H
6#define MESH_EXEC_WASM_CACHE_H
7
8#include <stdbool.h>
9#include <stddef.h>
10#include <stdint.h>
11
12/* The compiled-module cache's size cap (the Mac's disk is tight). */
13#define MESH_WASM_CACHE_CAP (256ull << 20)
14
15/* The path of the cached module for wasm under dir (malloc'd). */
16char *mesh_wasm_cache_path(const char *dir, const uint8_t *wasm, size_t n, uint64_t engine_hash);
17/* Creates the directory (and its parents). False with *err: "--wasm-cache DIR: <why>". */
18bool mesh_wasm_cache_dir(const char *dir, char **err);
19/* Writes bytes to path through path.tmp and a rename. False with *err. */
20bool mesh_wasm_cache_store(const char *path, const uint8_t *bytes, size_t n, char **err);
21/* Reads a cached file: true with *out (malloc'd) and *n. */
22bool mesh_wasm_cache_load(const char *path, uint8_t **out, size_t *n);
23/* Deletes the oldest .cwasm files of dir until they total at most cap bytes. Returns how many
24 * were evicted (and calls on_evict for each path, if given). */
25size_t mesh_wasm_cache_evict(const char *dir, uint64_t cap, void (*on_evict)(const char *path));
26
27#endif