Commit f4ff48f4
f4ff48f4c210Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
Move the File metadata predicates to libspinel_rt.a
Following the sp_io.c handle-ops slimming, move the File metadata predicates out of sp_runtime.h into lib/sp_io.c (+ prototypes in sp_io.h): File.directory?/file?/exist?/delete and the Windows sp_file_attributes helper. They touch only libc / WinAPI (stat, GetFileAttributesW, fopen, remove), allocate no spinel strings, and reference no shared mutable state, so they move byte-for-byte and stay perf-neutral (none were inline-hot).
The string-returning / raising ops stay inline in sp_runtime.h: sp_file_read / write / mtime / binread allocate via the hot static sp_str_alloc (per-TU string heap) or build an sp_Time; they call the now-extern sp_file_directory, which resolves from the library copy. The compiler doesn't use any of these (it has its own file reader), so this is bootstrap-safe.
sp_io.c gains <sys/stat.h> (POSIX) / <windows.h> (Win) for the predicate bodies. Gate green: test 846, optcarrot checksum 59662, bench 57, clang build clean.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- f4ff48f4c210
- Parent
- 80894e3a7b34
lib/sp_io.cmodifiedView file
#include <stdlib.h>#include <string.h>#ifdef _WIN32#include <io.h> /* _pipe */#include <fcntl.h> /* _O_BINARY */#include <io.h> /* _pipe */#include <fcntl.h> /* _O_BINARY */#include <windows.h> /* GetFileAttributesW for the File predicates */#else#include <unistd.h> /* pipe */#include <unistd.h> /* pipe */#include <sys/stat.h> /* stat() for the File predicates */#endif/* Provided by the generated TU / libspinel_rt.a. */ ungetc(c, f->fp); return FALSE;}/* ---- File metadata predicates ---- libc / WinAPI only, no spinel-string allocation and no shared mutable state, so they live here rather than inline in sp_runtime.h. */#ifdef _WIN32static DWORD sp_file_attributes(const char *path) { if (!path) return INVALID_FILE_ATTRIBUTES; int len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, NULL, 0); if (len <= 0) return INVALID_FILE_ATTRIBUTES; wchar_t *wpath = (wchar_t *)malloc(sizeof(wchar_t) * (size_t)len); if (!wpath) return INVALID_FILE_ATTRIBUTES; if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, wpath, len) <= 0) { free(wpath); return INVALID_FILE_ATTRIBUTES; } DWORD attrs = GetFileAttributesW(wpath); free(wpath); return attrs;}mrb_bool sp_file_directory(const char *path) { DWORD attrs = sp_file_attributes(path); return attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY);}mrb_bool sp_file_file(const char *path) { DWORD attrs = sp_file_attributes(path); return attrs != INVALID_FILE_ATTRIBUTES && !(attrs & FILE_ATTRIBUTE_DIRECTORY);}#elsemrb_bool sp_file_directory(const char *path) { struct stat st; return path && stat(path, &st) == 0 && S_ISDIR(st.st_mode);}mrb_bool sp_file_file(const char *path) { struct stat st; return path && stat(path, &st) == 0 && S_ISREG(st.st_mode);}#endifmrb_bool sp_file_exist(const char *path) { FILE *f = fopen(path, "r"); if (f) { fclose(f); return TRUE; } return FALSE; }void sp_file_delete(const char *path) { remove(path); }lib/sp_io.hmodifiedView file
Loading diff…
lib/sp_runtime.hmodifiedView file
Loading diff…