temporary allocations

This commit is contained in:
dzaima 2021-05-21 00:05:11 +03:00
parent ee9c4e2f9d
commit aa36531783
11 changed files with 41 additions and 27 deletions

View File

@ -5,11 +5,13 @@ typedef struct TmpFile { // to be turned into a proper I8Arr
FILE* file_open(B path, char* desc, char* mode) { // consumes path; can error
u64 plen = utf8lenB(path);
char p[plen+1];
TALLOC(char, p, plen+1);
toUTF8(path, p);
p[plen] = 0;
FILE* f = fopen(p, mode);
TFREE(p);
if (f==NULL) thrF("Couldn't %S file \"%R\"", desc, path);
dec(path);
return f;
}
@ -76,9 +78,10 @@ void file_write(B path, B x) { // consumes path
FILE* f = file_open(path, "write to", "w");
u64 len = utf8lenB(x);
char val[len];
TALLOC(char, val, len);
toUTF8(x, val);
fwrite(val, 1, len, f);
TFREE(val);
fclose(f);
}

View File

@ -234,7 +234,7 @@ B indexOf_c1(B t, B x) {
i32 dst = 1 + max-(i64)min;
if ((dst<xia*5 || dst<50) && min!=I32_MIN) {
i32* rp; B r = m_i32arrv(&rp, xia);
i32 tmp[dst];
TALLOC(i32, tmp, dst);
for (usz i = 0; i < dst; i++) tmp[i] = I32_MIN;
i32* tc = tmp-min;
i32 ctr = 0;
@ -243,7 +243,7 @@ B indexOf_c1(B t, B x) {
if (tc[c]==I32_MIN) tc[c] = ctr++;
rp[i] = tc[c];
}
dec(x);
dec(x); TFREE(tmp);
return r;
}
}

View File

@ -87,10 +87,10 @@ enum Type {
/*19*/ t_hslice, t_i8slice, t_i32slice, t_fillslice, t_c32slice, t_f64slice,
/*25*/ t_comp, t_block, t_body, t_scope,
/*29*/ t_ns, t_nsDesc, t_fldAlias, t_hashmap,
/*33*/ t_freed, t_harrPartial,
/*29*/ t_ns, t_nsDesc, t_fldAlias, t_hashmap, t_temp,
/*34*/ t_freed, t_harrPartial,
#ifdef RT_PERF
/*35*/ t_funPerf, t_md1Perf, t_md2Perf,
/*36*/ t_funPerf, t_md1Perf, t_md2Perf,
#endif
t_COUNT
};
@ -114,7 +114,7 @@ char* format_type(u8 u) {
case t_harr :return"harr" ; case t_i8arr :return"i8arr" ; case t_i32arr :return"i32arr" ; case t_fillarr :return"fillarr" ; case t_c32arr :return"c32arr" ; case t_f64arr :return"f64arr" ;
case t_hslice:return"hslice"; case t_i8slice:return"i8slice"; case t_i32slice:return"i32slice"; case t_fillslice:return"fillslice"; case t_c32slice:return"c32slice"; case t_f64slice:return"f64slice";
case t_comp:return"comp"; case t_block:return"block"; case t_body:return"body"; case t_scope:return"scope";
case t_ns:return"ns"; case t_nsDesc:return"nsDesc"; case t_fldAlias:return"alias"; case t_hashmap:return"hashmap";
case t_ns:return"ns"; case t_nsDesc:return"nsDesc"; case t_fldAlias:return"alias"; case t_hashmap:return"hashmap"; case t_temp:return"temporary";
case t_freed:return"(freed by GC)"; case t_harrPartial:return"partHarr";
#ifdef RT_PERF
case t_funPerf:return"perf fn"; case t_md1Perf:return"perf m1"; case t_md2Perf:return "perf m2";

View File

@ -55,10 +55,11 @@ u64 bqn_hash(B x, const u64 secret[4]) { // doesn't consume
r = wyhash(f64any_ptr(x), xia*8, shHash, secret);
} else {
assert(xe==el_B);
u64 data[xia];
TALLOC(u64, data, xia);
BS2B xgetU = TI(x).getU;
for (usz i = 0; i < xia; i++) data[i] = bqn_hash(xgetU(x, i), secret);
r = wyhash(data, xia*8, shHash, secret);
TFREE(data);
}
dec(x);
return r;

View File

@ -243,6 +243,7 @@ B flines_c1(B d, B x) { B f = c(Md1D,d)->f;
if (p[pos]=='\r' && pos+1<ia && p[pos+1]=='\n') pos+= 2;
else pos++;
}
ptr_dec(tf);
return harr_fv(r);
}
B import_c1(B d, B x) { B f = c(Md1D,d)->f;

View File

@ -81,7 +81,7 @@ B repeat_replace(B g, B* q) { // doesn't consume
i64 bound[2] = {0,0}; \
repeat_bounds(bound, g); \
u64 min=(u64)-bound[0]; u64 max=(u64)bound[1]; \
B all[min+max+1]; \
TALLOC(B, all, min+max+1); \
B* q = all+min; \
q[0] = inc(x); \
if (min) { \
@ -96,7 +96,7 @@ B repeat_replace(B g, B* q) { // doesn't consume
B r = repeat_replace(g, q); \
dec(g); \
for (u64 i = 0; i < min+max+1; i++) dec(all[i]); \
END; \
END; TFREE(all); \
return r;
B repeat_c1(B d, B x) { REPEAT_T(c1,{} ); }

View File

@ -651,8 +651,8 @@ B group_c2(B t, B w, B x) {
for (usz i = 0; i < xia; i++) if (wp[i]>ria) ria = wp[i];
if (ria>USZ_MAX-1) thrOOM();
ria++;
i32 len[ria];
i32 pos[ria];
TALLOC(i32, len, ria);
TALLOC(i32, pos, ria);
for (usz i = 0; i < ria; i++) len[i] = pos[i] = 0;
for (usz i = 0; i < xia; i++) { i32 n = wp[i]; if (n>=0) len[n]++; }
@ -689,7 +689,7 @@ B group_c2(B t, B w, B x) {
}
for (usz i = 0; i < ria; i++) { arr_shVec(rp[i], len[i]); }
}
dec(w); dec(x);
dec(w); dec(x); TFREE(len); TFREE(pos);
return r;
} else {
BS2B wgetU = TI(w).getU;
@ -701,8 +701,8 @@ B group_c2(B t, B w, B x) {
}
if (ria>USZ_MAX-1) thrOOM();
ria++;
i32 len[ria];
i32 pos[ria];
TALLOC(i32, len, ria);
TALLOC(i32, pos, ria);
for (usz i = 0; i < ria; i++) len[i] = pos[i] = 0;
for (usz i = 0; i < xia; i++) {
i64 n = o2i64u(wgetU(w, i));
@ -732,7 +732,7 @@ B group_c2(B t, B w, B x) {
if (n>=0) fillarr_ptr(rp[n])[pos[n]++] = xget(x, i);
}
for (usz i = 0; i < ria; i++) { arr_shVec(rp[i], len[i]); }
dec(w); dec(x);
dec(w); dec(x); TFREE(len); TFREE(pos);
return r;
}
}
@ -872,11 +872,13 @@ B select_ucw(B t, B o, B w, B x) {
B rep = c1(o, arg);
if (isAtm(rep) || rnk(rep)!=1 || a(rep)->ia != wia) thrF("𝔽⌾(a⊸⊏)𝕩: Result of 𝔽 must have the same shape as a⊏𝕩 (expected ⟨%s⟩, got %H)", wia, rep);
#if CHECK_VALID
bool set[xia];
TALLOC(bool, set, xia);
for (i64 i = 0; i < xia; i++) set[i] = false;
#define EQ(F) if (set[cw] && (F)) thrM("𝔽⌾(a⊸⊏): Incompatible result elements"); set[cw] = true;
#define FREE_CHECK TFREE(set)
#else
#define EQ(F)
#define FREE_CHECK
#endif
if (TI(w).elType==el_i32) {
i32* wp = i32any_ptr(w);
@ -890,7 +892,7 @@ B select_ucw(B t, B o, B w, B x) {
EQ(cr!=xp[cw]);
xp[cw] = cr;
}
dec(w); dec(rep);
dec(w); dec(rep); FREE_CHECK;
return x;
} else if (v(x)->type==t_harr) {
B* xp = harr_ptr(x);
@ -902,7 +904,7 @@ B select_ucw(B t, B o, B w, B x) {
dec(xp[cw]);
xp[cw] = cr;
}
dec(w); dec(rep);
dec(w); dec(rep); FREE_CHECK;
return x;
}
}
@ -916,7 +918,7 @@ B select_ucw(B t, B o, B w, B x) {
mut_rm(r, cw);
mut_setS(r, cw, cr);
}
dec(w); dec(rep);
dec(w); dec(rep); FREE_CHECK;
return mut_fcd(r, x);
}
MAKE_MUT(r, xia); mut_to(r, el_or(TI(x).elType, TI(rep).elType));
@ -929,9 +931,10 @@ B select_ucw(B t, B o, B w, B x) {
mut_rm(r, cw);
mut_set(r, cw, cr);
}
dec(w); dec(rep);
dec(w); dec(rep); FREE_CHECK;
return mut_fcd(r, x);
#undef EQ
#undef FREE_CHECK
}

View File

@ -30,10 +30,11 @@ B gradeUp_c1(B t, B x) {
// }
i32* rp; B r = m_i32arrv(&rp, ia);
i32 tmp[ia];
TALLOC(i32, tmp, ia);
for (usz i = 0; i < ia; i++) tmp[i] = rp[i] = i;
gradeUp_rec(xp, tmp, rp, 0, ia);
dec(x);
TFREE(tmp);
return r;
}
return c1(rt_gradeUp, x);

View File

@ -9,6 +9,12 @@
#define MAP_NORESERVE 0 // apparently needed for freebsd or something
#endif
typedef struct TAlloc {
struct Value;
u8 data[];
} TAlloc;
#define TALLOC(T,N,AM) TAlloc* N##_obj = mm_allocN(sizeof(TAlloc) + (AM)*sizeof(T) + 8, t_temp); T* N = (T*) N##_obj->data; // +8 so mm is happy
#define TFREE(N) mm_free((Value*)N##_obj);
void empty_free(Value* x) { err("FREEING EMPTY\n"); }
void builtin_free(Value* x) { err("FREEING BUILTIN\n"); }

View File

@ -91,7 +91,7 @@ B grOrd_c2(B t, B w, B x) { // assumes valid arguments
if (xia==0) { dec(w); return x; }
BS2B wgetU = TI(w).getU;
BS2B xgetU = TI(x).getU;
usz tmp[wia];
TALLOC(usz, tmp, wia);
tmp[0] = 0;
for (usz i = 1; i < wia; i++) tmp[i] = tmp[i-1]+o2su(wgetU(w,i-1));
usz ria = tmp[wia-1]+o2su(wgetU(w,wia-1));
@ -101,8 +101,7 @@ B grOrd_c2(B t, B w, B x) { // assumes valid arguments
i64 c = o2i64(xgetU(x,i));
if (c>=0) rp[tmp[c]++] = i;
}
dec(w);
dec(x);
dec(w); dec(x); TFREE(tmp);
return r;
}

View File

@ -64,7 +64,7 @@ u64 utf8lenB(B x) { // doesn't consume; may error as it verifies whether is all
}
return res;
}
void toUTF8(B x, char* p) { // doesn't consume; doesn't verify anything; p must have utf8lenB(x) bytes
void toUTF8(B x, char* p) { // doesn't consume; doesn't verify anything; p must have utf8lenB(x) bytes (calculating which should verify that this call is ok)
BS2B xgetU = TI(x).getU;
usz ia = a(x)->ia;
for (usz i = 0; i < ia; i++) {