Skip to content

Commit bd09d18

Browse files
Merge pull request #1444 from alex-s168/more-safe-alloc
use safe-alloc
2 parents 954e198 + 96eab8c commit bd09d18

File tree

8 files changed

+37
-26
lines changed

8 files changed

+37
-26
lines changed

PLATFORM/C/LIB/array.lm

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ const-cons := λ: Blob(: v x)(: i L). (: (
1919
))
2020
) Array<x,CONST>);
2121

22-
close := λ(: x p). (: (
23-
(let r (as (malloc(sizeof p)) p[]))
24-
(set[]( r 0_u64 x ))
25-
r
26-
) p[]);
27-
2822
#declare-binop( mov, raw-type(base[CONST+array-len]), raw-type(base[CONST+array-len]), raw-type(Nil), (
2923
# l"({memcpy(&"; y;
3024
# l","; x;

PLATFORM/C/LIB/array.lsts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,9 @@ let safe-realloc(ptr: t[], len: U64, ty: Type<t>): t[] = (
6464
mark-memory-as-safe(new_ptr, len);
6565
new_ptr
6666
);
67+
68+
let close(x: p): p[] = (
69+
let r = safe-alloc(1_u64, type(p));
70+
r[0_u64] = x;
71+
r
72+
);

PLATFORM/C/LIB/io.lm

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,6 @@ fail := λ(: msg String). (: (
2424
(as (exit 1_u32) Never)
2525
) Never);
2626

27-
read-file := λ(: fp String). (: (
28-
(let buff-sz 0_u64)
29-
(let buff (as (malloc 1024_u64) U8[]))
30-
(let f (fopen( (as fp U8[]) (as 'r_s U8[]) )))
31-
(if (==( (as f U64) 0_u64 )) (
32-
(print 'Unable\sTo\sRead\sFrom\sFile:\s_s)(print fp)(print '\n_s)(exit 1_u64)
33-
) ())
34-
(let bytes-read 1_u64)
35-
(while bytes-read (
36-
(set bytes-read (fread( (+( buff buff-sz )) 1_u64 1023_u64 f )))
37-
(set buff-sz (+( buff-sz bytes-read )))
38-
(set buff (as (realloc( buff (+( buff-sz 1023_u64 )) )) U8[]))
39-
))
40-
(fclose f)
41-
(set[]( buff buff-sz 0_u8 ))
42-
(as buff String)
43-
) String);
44-
4527
write-file := λ(: fp String)(: contents String). (: (
4628
(let f (fopen( (as fp U8[]) (as 'w_s U8[]) )))
4729
(fwrite( (as contents U8[]) 1_u64 (.length contents) f ))

PLATFORM/C/LIB/io.lsts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,24 @@ let .file-extension(path: CString): CString = (
55
};
66
path
77
);
8+
9+
let read-binary-file-to(out: Vector<U8>, path: CString): Vector<U8> = (
10+
let fp = fopen(path as U8[], untern("rb") as U8[]);
11+
if (fp as U64) == 0_u64 {
12+
fail("Unable to read from file: \{path}");
13+
};
14+
let by = 1_u64;
15+
while by {
16+
let increment = 1024_u64;
17+
out = out.reserve-additional(increment);
18+
by = fread(out.ptr(out.length), 1_u64, increment, fp);
19+
out._length = (out.length + by) as U32;
20+
};
21+
fclose(fp);
22+
out
23+
);
24+
25+
let read-file(fp: CString): CString = (
26+
let v = read-binary-file-to(mk-vector(type(U8)), fp);
27+
v.into(type(CString))
28+
);

PLATFORM/C/LIB/smart-string.lm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ head-string := λ(: x SmartString). (: (
147147
# Smart Strings may allocated a hidden null byte after the data to protect against any accidental CString coercions
148148
+ := λ(: l SmartString)(: r SmartString). (: (
149149
(let length (+( (.length l) (.length r) )))
150+
# when porting this to LSTS, replace malloc with safe-alloc
150151
(let data (as (malloc( (+( length 1_u64 )) )) U8[]))
151152
(let li 0_u64)
152153
(while (<( li (.length l) )) (

PLATFORM/C/LIB/u8.lsts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ declare-unop( into-branch-conditional, raw-type(U8), raw-type(BranchConditional)
2727
let print(x: U8): Nil = print(x as U64);
2828

2929
let clone-rope(s: U8): CString = (
30-
let x = malloc(2) as U8[];
30+
let x = safe-alloc(2, type(U8));
3131
x[0] = s;
3232
x[1] = 0_u8;
3333
x as CString;

PLATFORM/C/LIB/umbra.lsts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ let new-umbra(length: U64): Umbra = (
209209
let cs = const-cons(0_u8, 4_l);
210210
Umbra(length as U32,
211211
UmbraLong(cs as U8[4],
212-
malloc(length) as U8[]))
212+
safe-alloc(length, type(U8))))
213213
}
214214
);
215215

PLATFORM/C/LIB/vector.lsts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,10 @@ let .into(in: Vector<U8>, res: Type<CString>): CString = (
208208
let .into(self: Vector<U8>, res: Type<SmartString>): SmartString = (
209209
intern(self.into(type(CString)))
210210
);
211+
212+
let .ptr(v: Vector<t>, toelem: U64): t[] = (
213+
if toelem >= v.capacity {
214+
fail("Vector.ptr() index out of capacity bounds");
215+
};
216+
((v.data as U64) + sizeof(t) * toelem) as t[]
217+
);

0 commit comments

Comments
 (0)