// Raw.read: whole 32-bit cells between a file and a Bend array (see raw.bend). Runs on the event loop. static uint32_t* raw_read_cells(Env e, Term a, uint64_t need) { if (term_tag(a) != TAG_BUF) { fprintf(stderr, "Raw.read: not a 32-bit array\n"); exit(1); } if ((1ull << blk_cls(a)) < need) { fprintf(stderr, "Raw.read: array holds %llu cells, needs %llu\n", (unsigned long long)(1ull << blk_cls(a)), (unsigned long long)need); exit(1); } return (uint32_t*)(e.mem + blk_loc(e.mem, a)); } Term raw_read_run(Env e, Term* f, IoWork* w) { uint64_t len; char* path = io_cstr(e, f[0], &len); uint64_t cells = (uint32_t)f[2]; uint32_t* p = raw_read_cells(e, f[1], cells); FILE* fp = fopen(path, "rb"); if (fp == NULL) { fprintf(stderr, "Raw.read: cannot open %s\n", path); exit(1); } fseek(fp, 0, SEEK_END); long size = ftell(fp); fseek(fp, 0, SEEK_SET); if (size != (long)(cells * 4)) { fprintf(stderr, "Raw.read: %s has %ld bytes, expected %llu\n", path, size, (unsigned long long)(cells * 4)); exit(1); } if (fread(p, 4, cells, fp) != cells) { fprintf(stderr, "Raw.read: short read of %s\n", path); exit(1); } fclose(fp); free(path); return f[1]; } static void __attribute__((constructor)) raw_read_use(void) { #ifdef CID(Raw.read) io_eff(CID(Raw.read), raw_read_run); #endif }