TmpFile→I8Arr

This commit is contained in:
dzaima 2021-09-10 15:24:07 +03:00
parent 1d43e72a16
commit d8d56bbdd4
4 changed files with 9 additions and 14 deletions

View File

@ -500,9 +500,9 @@ B fchars_c2(B d, B w, B x) {
}
static NFnDesc* fBytesDesc;
B fbytes_c1(B d, B x) {
TmpFile* tf = file_bytes(path_resolve(nfn_objU(d), x));
I8Arr* tf = file_bytes(path_resolve(nfn_objU(d), x));
usz ia = tf->ia; u8* p = (u8*)tf->a;
u32* rp; B r = m_c32arrv(&rp, ia);
u8* rp; B r = m_c8arrv(&rp, ia);
for (i64 i = 0; i < ia; i++) rp[i] = p[i];
ptr_dec(tf);
return r;

View File

@ -197,13 +197,13 @@ static void* nvm_alloc(u64 sz) {
// void* r = mmap(NULL, sz, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON|MAP_32BIT, -1, 0);
// if (r==MAP_FAILED) thrM("JIT: Failed to allocate executable memory");
// return r;
TmpFile* src = mmX_allocN(fsizeof(TmpFile,a,u8,sz), t_i8arr);
I8Arr* src = mmX_allocN(fsizeof(I8Arr,a,u8,sz), t_i8arr);
src->ia = sz;
arr_shVec((Arr*)src);
return src->a;
}
void nvm_free(u8* ptr) {
if (!USE_PERF) mmX_free((Value*)RFLD(ptr, TmpFile, a));
if (!USE_PERF) mmX_free((Value*)RFLD(ptr, I8Arr, a));
}

View File

@ -25,25 +25,25 @@ static DIR* dir_open(B path) { // doesn't consume
return f;
}
TmpFile* file_bytes(B path) { // consumes
I8Arr* file_bytes(B path) { // consumes
FILE* f = file_open(path, "read", "r");
fseek(f, 0, SEEK_END);
u64 len = ftell(f);
fseek(f, 0, SEEK_SET);
TmpFile* src = m_arr(fsizeof(TmpFile,a,u8,len), t_i8arr, len); arr_shVec((Arr*)src);
I8Arr* src = m_arr(fsizeof(I8Arr,a,u8,len), t_i8arr, len); arr_shVec((Arr*)src);
if (fread((char*)src->a, 1, len, f)!=len) thrF("Error reading file \"%R\"", path);
dec(path);
fclose(f);
return src;
}
B file_chars(B path) { // consumes
TmpFile* c = file_bytes(path);
I8Arr* c = file_bytes(path);
B r = fromUTF8((char*)c->a, c->ia);
ptr_dec(c);
return r;
}
B file_lines(B path) { // consumes
TmpFile* tf = file_bytes(path);
I8Arr* tf = file_bytes(path);
usz ia = tf->ia; u8* p = (u8*)tf->a;
usz lineCount = 0;
for (usz i = 0; i < ia; i++) {

View File

@ -1,17 +1,12 @@
#pragma once
#include "utf.h"
typedef struct TmpFile { // to be turned into a proper I8Arr
struct Arr;
i8 a[];
} TmpFile;
B path_resolve(B base, B rel); // consumes rel; assumes base is a char vector or bi_N
B path_dir(B path); // consumes; returns directory part of file path, with trailing slash
B path_abs(B path); // consumes; returns absolute version of the path; propagates bi_N
FILE* file_open(B path, char* desc, char* mode); // doesn't consume path
TmpFile* file_bytes(B path); // consumes
I8Arr* file_bytes(B path); // consumes
B file_chars(B path); // consumes
B file_lines(B path); // consumes