diff options
| author | eunhhu <[email protected]> | 2026-09-26 18:10:52 +0100 |
|---|---|---|
| committer | eunhhu <[email protected]> | 2026-09-26 18:10:52 +0100 |
| commit | 2fc9919508d283456deb440579955a631a572efe (patch) | |
| tree | 302e15cbcdfd27d6b0c9b51b7a81f696fd1b1770 /src | |
| parent | 9c2d1eb1d423625672cb469af6bf708a48d3cb71 (diff) | |
| download | vega-2fc9919508d283456deb440579955a631a572efe.tar.gz vega-2fc9919508d283456deb440579955a631a572efe.zip | |
Read module files of any size
A module may now be some 2 MiB; the buffer that keeps the last module's
bytes (for --watch's digest) grows to the file instead of 128 KiB.
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 20 |
1 files changed, 16 insertions, 4 deletions
@@ -43,15 +43,27 @@ static size_t entry_length; static bool typing; /* The module file last read, and a digest of its bytes. A loaded module - * keeps nothing of them, so one buffer serves every load. */ -static unsigned char module_bytes[1 << 17]; + * keeps nothing of them, so one buffer, grown to the largest, serves every load. */ +static unsigned char *module_bytes; +static size_t module_capacity; static uint64_t module_digest; /* Read a module file whole into module_bytes: its size, or -1. */ static long read_module(const char *path) { FILE *file = fopen(path, "rb"); - size_t size = file ? fread(module_bytes, 1, sizeof(module_bytes), file) : 0; - bool ok = file && !ferror(file) && size < sizeof(module_bytes); + size_t size = 0; + bool ok = file != NULL; + while (ok && !feof(file)) { + if (size == module_capacity) { + size_t capacity = module_capacity ? module_capacity * 2 : 1 << 16; + unsigned char *grown = capacity <= (8u << 20) ? realloc(module_bytes, capacity) : NULL; + if (!grown) { ok = false; break; } + module_bytes = grown; + module_capacity = capacity; + } + size += fread(module_bytes + size, 1, module_capacity - size, file); + ok = !ferror(file); + } if (file) fclose(file); if (!ok) return -1; uint64_t h = UINT64_C(0xcbf29ce484222325) ^ size; |
