mirror of
https://github.com/Ed94/perfaware.git
synced 2026-09-14 03:39:23 +00:00
decoder table generator (metaprogram) works... will test rest tmrw.
This commit is contained in:
+12
-2
@@ -48,7 +48,8 @@ os_layer File file_open(FArena* scratch, AccessFlags flags, Str8 path);
|
||||
#define file_scope(scratch, flags, path) scope_info(Scope_FileInfo, {.f = file_open(scratch, flags, path)}, file_close(info.f))
|
||||
|
||||
os_layer FileProperties properties_from_file(File file);
|
||||
os_layer U8 file_read(File file, R1_U8 rng, U1* out_data);
|
||||
os_layer U8 file_read (File file, R1_U8 rng, U1* out_data);
|
||||
os_layer U8 file_write(File file, R1_U8 rng, U1* data);
|
||||
|
||||
I_ Slice_U1
|
||||
data_from_file_range(FArena* arena, File file, R1_U8 range) {
|
||||
@@ -56,7 +57,8 @@ data_from_file_range(FArena* arena, File file, R1_U8 range) {
|
||||
U8 len = span_r1u8(range);
|
||||
Slice_U1 result = farena_push_array(arena, U1, len);
|
||||
U8 actual_read_size = file_read(file, range, result.ptr); if (actual_read_size < result.len) {
|
||||
farena_rewind(arena, pre_pos + actual_read_size); result.len = actual_read_size;
|
||||
U8 committed = align_pow2(actual_read_size, MEM_ALIGNMENT_DEFAULT);
|
||||
farena_rewind(arena, pre_pos + committed); result.len = actual_read_size;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -67,4 +69,12 @@ data_from_file_path(FArena* arena, Str8 path, FArena* scratch) { file_scope(scra
|
||||
Slice_U1 data = data_from_file_range(arena, info.f, r1u8(0, props.size)); return data;
|
||||
} unreachable(); }
|
||||
|
||||
I_ B4
|
||||
write_data_to_file_path(Str8 path, Str8 data, FArena* scratch) {
|
||||
B4 good = false; file_scope(scratch, AccessFlag_Write, path) if (file_match(info.f, file_zero()) == false) {
|
||||
U8 bytes_written = file_write(info.f, r1u8(0, data.len), data.ptr); good = bytes_written == data.len;
|
||||
}
|
||||
return good;
|
||||
}
|
||||
|
||||
CLANG_OPTIMIZE_ENABLE
|
||||
|
||||
@@ -37,6 +37,7 @@ FI_ U8 mem_compare(U8 a, U8 b, U8 len) { return (U8)(__builtin_memcmp((void cons
|
||||
FI_ B4 mem_match (U8 a, U8 b, U8 z) { return mem_compare(a, b, z) == 0; }
|
||||
|
||||
#define mem_match_struct(a,b) mem_match(C_(U8,a), C_(U8,b), S_((a)[0]))
|
||||
#define mem_zero_struct(s) mem_zero(u8_(& s), S_(s))
|
||||
|
||||
#pragma region DAG
|
||||
|
||||
@@ -126,9 +127,9 @@ I_ Slice farena_push(FArena_R arena, U8 amount, Opt_farena o) {
|
||||
return (Slice){ ptr, to_commit };
|
||||
}
|
||||
FI_ void farena_reset (FArena_R arena) { arena->used = 0; }
|
||||
FI_ void farena_rewind(FArena_R arena, U4 save_point) {
|
||||
U8 end = arena->start + arena->used; assert_bounds(save_point, arena->start, end);
|
||||
arena->used -= save_point - arena->start;
|
||||
FI_ void farena_rewind(FArena_R arena, U8 save_point) {
|
||||
assert(save_point <= arena->used);
|
||||
arena->used = save_point;
|
||||
}
|
||||
FI_ U8 farena_save(FArena arena) { return arena.used; }
|
||||
#define farena_push_(arena, amount, ...) farena_push((arena), (amount), opt_(farena, __VA_ARGS__))
|
||||
|
||||
+7
-4
@@ -196,8 +196,8 @@ typedef Struct_(Str8Gen) { UTF8* ptr; U8 cap, len; };
|
||||
FI_ Slice str8gen_buf(Str8Gen_R gen) { return (Slice){u8_(gen->ptr) + gen->len, gen->cap - gen->len}; }
|
||||
|
||||
FI_ void str8gen_append_str8(Str8Gen_R gen, Str8 str) { assert(gen != nullptr);
|
||||
mem_bump_u8(u8_(gen->ptr), gen->cap, & gen->len, str.len);
|
||||
U8 ptr = u8_(gen->ptr) + gen->len;
|
||||
mem_bump_u8(u8_(gen->ptr), gen->cap, & gen->len, str.len);
|
||||
mem_copy(ptr, u8_(str.ptr), str.len);
|
||||
}
|
||||
FI_ void str8gen_append_fmt(Str8Gen_R gen, Str8 fmt, KTL_Str8 tbl) {
|
||||
@@ -216,8 +216,9 @@ typedef Str16 Slice_UTF16;
|
||||
internal Str16
|
||||
str16_from_8(FArena* arena, Str8 in) {
|
||||
Str16 result = {0}; if (in.len) {
|
||||
U8 cap = in.len * 2;
|
||||
Slice_U2 str = farena_push_array(arena, U2, cap + 1);
|
||||
U8 pre_pos = farena_save(arena[0]);
|
||||
U8 cap = in.len * 2;
|
||||
Slice_U2 str = farena_push_array(arena, U2, cap + 1);
|
||||
U1* ptr = in.ptr;
|
||||
U1* opl = ptr + in.len;
|
||||
U8 size = 0;
|
||||
@@ -227,7 +228,9 @@ str16_from_8(FArena* arena, Str8 in) {
|
||||
consume = utf8_decode(ptr, opl - ptr);
|
||||
size += utf16_encode(str.ptr + size, consume.codepoint);
|
||||
}
|
||||
str.ptr[size] = 0; farena_rewind(arena, (cap - size) * 2);
|
||||
str.ptr[size] = 0;
|
||||
U8 committed = align_pow2((size + 1) * S_(U2), MEM_ALIGNMENT_DEFAULT);
|
||||
farena_rewind(arena, pre_pos + committed);
|
||||
result = str16(C_(UTF16*, str.ptr), size);
|
||||
}
|
||||
return result;
|
||||
|
||||
+20
-2
@@ -271,8 +271,9 @@ properties_from_file(File file) {
|
||||
|
||||
internal File
|
||||
file_open(FArena* scratch, AccessFlags flags, Str8 path) {
|
||||
File result = {0};
|
||||
Str16 path16 = str16_from_8(scratch, path);
|
||||
File result = {0};
|
||||
U8 scratch_at = farena_save(scratch[0]);
|
||||
Str16 path16 = str16_from_8(scratch, path);
|
||||
U4 access_flags = 0;
|
||||
U4 share_mode = 0;
|
||||
U4 creation_disposition = MS_OPEN_EXISTING;
|
||||
@@ -290,6 +291,7 @@ file_open(FArena* scratch, AccessFlags flags, Str8 path) {
|
||||
else {
|
||||
U4 err = ms_get_last_error(); (void)err;
|
||||
}
|
||||
farena_rewind(scratch, scratch_at);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -314,4 +316,20 @@ internal U8 file_read(File file, R1_U8 rng, U1* out_data) {
|
||||
}
|
||||
U8 total_read_size = off - rng.p0; return total_read_size;
|
||||
}
|
||||
|
||||
internal U8 file_write(File file, R1_U8 rng, U1* data) {
|
||||
if (file_match(file, file_zero())) { return 0; }
|
||||
MS_Handle* handle = C_(MS_Handle*,file.ptr[0]);
|
||||
U1 const* ptr = data;
|
||||
U8 off = rng.p0; while (off != rng.p1) {
|
||||
U8 amt64 = rng.p1 - off;
|
||||
U4 amt32 = C_(U4, min(mega(32), amt64));
|
||||
U4 write_size = 0;
|
||||
MS_OVERLAPPED overlapped = { .Offset = C_(U4,off), .OffsetHigh = C_(U4,off >> 32) };
|
||||
if ( ! ms_write_file(handle, ptr, amt32, & write_size, & overlapped) || write_size == 0) { break; }
|
||||
ptr += write_size;
|
||||
off += write_size;
|
||||
}
|
||||
return off - rng.p0;
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user