-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1056 from andrew-johnson-4/fix-buffer-deleted
add back buffer to fix regressions
- Loading branch information
Showing
2 changed files
with
42 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
|
||
type Buffer (Buffer( ptr:U8[] , allocated:U64 , capacity:U64 )); | ||
|
||
new-buffer := λ(: capacity U64). (: ( | ||
(let ptr (malloc capacity)) | ||
(Buffer( (as ptr U8[]) 0_u64 capacity )) | ||
) Buffer); | ||
|
||
.calculate-extension-size := λ(: min-size U64). (: ( | ||
(let try-size 1024_u64) | ||
(while (<( try-size min-size )) ( | ||
(set try-size (*( try-size 4_u64 ))) | ||
)) | ||
try-size | ||
) U64); | ||
|
||
.extend := λ(: buff Buffer)(: sz U64). (: ( | ||
(if (<( (+( (.allocated buff) sz )) (.capacity buff) )) ( | ||
(set buff (Buffer( (.ptr buff) (+( (.allocated buff) sz )) (.capacity buff) ))) | ||
) ( | ||
(let new-sz (.calculate-extension-size( (+( (.allocated buff) sz )) ))) | ||
(let new-ptr (realloc( (.ptr buff) new-sz ))) | ||
(set buff (Buffer( (as new-ptr U8[]) (+( (.allocated buff) sz )) new-sz ))) | ||
)) | ||
buff | ||
) Buffer); | ||
|
||
.write := λ(: buff Buffer)(: data U8). (: ( | ||
(let out-buff (.extend( buff 1_u64 ))) | ||
(set[]( (as (.ptr out-buff) U8[]) (.allocated buff) data )) | ||
out-buff | ||
) Buffer); | ||
|