armpack fixes

This commit is contained in:
luboslenco
2026-01-12 16:41:25 +01:00
parent 8cb28a1777
commit 213a85a2d6
7 changed files with 39 additions and 105 deletions
+28 -16
View File
@@ -31,9 +31,9 @@ static void store_u8(uint8_t u8) {
}
/*static void store_i16(int16_t i16) {
di += pad(di, 2);
*(int16_t *)(decoded + di) = i16;
di += 2;
di += pad(di, 2);
*(int16_t *)(decoded + di) = i16;
di += 2;
}*/
static void store_u32(uint32_t u32) {
@@ -155,29 +155,35 @@ static uint32_t traverse(int di, bool count_arrays) {
break;
case 0xca: // Typed f32
ei += 4 * count;
len += 4 * count;
if (4 * count <= 4096)
len += 4 * count;
break;
case 0xd2: // Typed i32
ei += 4 * count;
len += 4 * count;
if (4 * count <= 4096)
len += 4 * count;
break;
case 0xd1: // Typed i16
ei += 2 * count;
len += 2 * count;
if (2 * count <= 4096)
len += 2 * count;
break;
case 0xc4: // Typed u8
ei += count;
len += count;
if (count <= 4096)
len += count;
break;
case 0xc2: // Bool array
case 0xc2: // Deprecated: Bool array
ei -= 1;
ei += count;
len += count;
if (count <= 4096)
len += count;
break;
case 0xc3: // Bool array
case 0xc3: // Deprecated: Bool array
ei -= 1;
ei += count;
len += count;
if (count <= 4096)
len += count;
break;
default: // Dynamic (type (array / map / string) - value)
ei -= 1; // Undo flag2 read
@@ -254,12 +260,18 @@ static uint8_t flag_to_byte_size(uint8_t flag) {
static void store_typed_array(uint8_t flag, uint32_t count) {
uint32_t size = flag_to_byte_size(flag) * count;
memcpy(decoded + di, encoded + ei, size);
if (size > 4096) {
gc_cut(decoded, di, size);
void *data = gc_alloc(size);
memcpy(data, encoded + ei, size);
gc_leaf(data);
*(uintptr_t *)(decoded + di - 4 - 4 - PTR_SIZE) = (uintptr_t)data; // Set buffer ptr
*(uint32_t *)(decoded + di - 4) = count; // Set capacity
}
else {
memcpy(decoded + di, encoded + ei, size);
di += size;
}
ei += size;
di += size;
}
static void read_store_array(uint32_t count) { // Store in any/i32/../_array_t format
@@ -350,7 +362,7 @@ static void read_store_array(uint32_t count) { // Store in any/i32/../_array_t f
for (uint32_t i = 0; i < count; ++i) {
/*uint8_t flag =*/read_u8();
read_store_array(read_i32());
read_store_array(read_u32());
}
}
// Structs
@@ -371,7 +383,7 @@ static void read_store_array(uint32_t count) { // Store in any/i32/../_array_t f
for (uint32_t i = 0; i < count; ++i) {
di = pad(di, PTR_SIZE) + di;
/*uint8_t flag =*/read_u8();
read_store_map(read_i32());
read_store_map(read_u32());
}
}
}
-5
View File
@@ -35,9 +35,6 @@ static void array_alloc(void *a, uint8_t element_size) {
if (element_size < 8) {
gc_leaf(tmp->buffer);
}
else {
gc_array(tmp->buffer, &tmp->length);
}
}
}
@@ -131,13 +128,11 @@ void f32_array_resize(f32_array_t *a, uint32_t size) {
void any_array_resize(any_array_t *a, uint32_t size) {
a->capacity = size;
a->buffer = gc_realloc(a->buffer, a->capacity * sizeof(void *));
gc_array(a->buffer, &a->length);
}
void char_ptr_array_resize(char_ptr_array_t *a, uint32_t size) {
a->capacity = size;
a->buffer = gc_realloc(a->buffer, a->capacity * sizeof(void *));
gc_array(a->buffer, &a->length);
}
void buffer_resize(buffer_t *b, uint32_t size) {
-18
View File
@@ -22,15 +22,10 @@ void *gc_alloc(size_t size) {
return &heap[old_top];
}
void gc_array(void *ptr, uint32_t *length) {}
void gc_leaf(void *ptr) {}
void gc_root(void *ptr) {}
void gc_unroot(void *ptr) {}
void *gc_cut(void *ptr, size_t pos, size_t size) {
return NULL;
}
void *gc_realloc(void *ptr, size_t size) {
void *new_ptr = gc_alloc(size);
if (ptr != NULL) {
@@ -59,15 +54,10 @@ void *gc_alloc(size_t size) {
return calloc(size, 1);
}
void gc_array(void *ptr, uint32_t *length) {}
void gc_leaf(void *ptr) {}
void gc_root(void *ptr) {}
void gc_unroot(void *ptr) {}
void *gc_cut(void *ptr, size_t pos, size_t size) {
return NULL;
}
void *gc_realloc(void *ptr, size_t size) {
return realloc(ptr, size);
}
@@ -90,10 +80,6 @@ void *gc_alloc(size_t size) {
return _gc_calloc(size, sizeof(uint8_t));
}
void gc_array(void *ptr, uint32_t *length) {
_gc_array(ptr, length);
}
void gc_leaf(void *ptr) {
_gc_leaf(ptr);
}
@@ -106,10 +92,6 @@ void gc_unroot(void *ptr) {
_gc_unroot(ptr);
}
void *gc_cut(void *ptr, size_t pos, size_t size) {
return _gc_cut(ptr, pos, size);
}
void *gc_realloc(void *ptr, size_t size) {
return ptr == NULL ? gc_alloc(size) : _gc_realloc(ptr, size);
}
-2
View File
@@ -7,11 +7,9 @@
#define GC_ALLOC_INIT(type, ...) (type *)memcpy(gc_alloc(sizeof(type)), (type[]){__VA_ARGS__}, sizeof(type))
void *gc_alloc(size_t size);
void gc_array(void *ptr, uint32_t *length);
void gc_leaf(void *ptr);
void gc_root(void *ptr);
void gc_unroot(void *ptr);
void *gc_cut(void *ptr, size_t pos, size_t size);
void *gc_realloc(void *ptr, size_t size);
void gc_free(void *ptr);
void gc_pause();
+10 -62
View File
@@ -35,20 +35,17 @@
#define GC_TAG_LEAF 0x1
#define GC_TAG_MARK 0x2
#define GC_TAG_ROOT 0x4
#define GC_TAG_CUT 0x8
#if defined(_MSC_VER) && !defined(__clang__)
#define __builtin_frame_address(x) ((void)(x), _AddressOfReturnAddress())
#endif
typedef struct gc_allocation {
void *ptr; // mem pointer
size_t size; // allocated size in bytes
int *array_length; // if this alloc is an array
char tag; // the tag for mark-and-sweep
char roots; // number of root users
struct gc_allocation *next; // separate chaining
struct gc_allocation *cut;
void *ptr; // mem pointer
size_t size; // allocated size in bytes
char tag; // the tag for mark-and-sweep
char roots; // number of root users
struct gc_allocation *next; // separate chaining
} gc_allocation_t;
typedef struct gc_allocation_map {
@@ -76,11 +73,9 @@ static gc_allocation_t *gc_allocation_new(void *ptr, size_t size) {
gc_allocation_t *a = (gc_allocation_t *)malloc(sizeof(gc_allocation_t));
a->ptr = ptr;
a->size = size;
a->array_length = NULL;
a->tag = GC_TAG_NONE;
a->roots = 0;
a->next = NULL;
a->cut = NULL;
return a;
}
@@ -277,13 +272,6 @@ static void *gc_allocate(size_t count, size_t size) {
return ptr;
}
void _gc_array(void *ptr, uint32_t *length) {
gc_allocation_t *alloc = gc_allocation_map_get(gc->allocs, ptr);
if (alloc) {
// alloc->array_length = length;
}
}
void _gc_leaf(void *ptr) {
gc_allocation_t *alloc = gc_allocation_map_get(gc->allocs, ptr);
if (alloc) {
@@ -313,35 +301,6 @@ static inline uint64_t pad(int di, int n) {
return (n - (di % n)) % n;
}
// Cut out a leaf out of an existing allocation
void *_gc_cut(void *ptr, size_t pos, size_t size) {
size += pad(size, 8);
// Start
gc_allocation_t *start = gc_allocation_map_get(gc->allocs, ptr);
while (start->cut) {
start = start->cut;
}
size_t start_size = start->size;
pos -= (char *)start->ptr - (char *)ptr;
start->size = pos;
// Middle
gc_allocation_t *middle = gc_allocation_new((char *)start->ptr + pos, size);
middle->tag |= GC_TAG_CUT;
middle->tag |= GC_TAG_LEAF;
start->cut = middle;
gc_allocation_map_put(gc->allocs, middle);
// End
gc_allocation_t *end = gc_allocation_new((char *)start->ptr + pos + size, start_size - size - pos);
end->tag |= GC_TAG_CUT;
middle->cut = end;
gc_allocation_map_put(gc->allocs, end);
return middle->ptr;
}
static void gc_mark_alloc(void *ptr) {
gc_allocation_t *alloc = gc_allocation_map_get(gc->allocs, ptr);
/* Mark if alloc exists and is not tagged already, otherwise skip */
@@ -349,10 +308,8 @@ static void gc_mark_alloc(void *ptr) {
alloc->tag |= GC_TAG_MARK;
if (!(alloc->tag & GC_TAG_LEAF)) { // Skip contents
/* Iterate over allocation contents and mark them as well */
int size = alloc->array_length ? (*alloc->array_length) * PTRSIZE : alloc->size;
for (char *p = (char *)alloc->ptr; p <= (char *)alloc->ptr + size - PTRSIZE; ++p) {
gc_mark_alloc(*(void **)p);
for (void **p = (void **)alloc->ptr; (char *)p <= (char *)alloc->ptr + alloc->size - PTRSIZE; ++p) {
gc_mark_alloc(*p);
}
}
}
@@ -363,8 +320,8 @@ static void gc_mark_stack() {
void *bos = gc->bos;
/* The stack grows towards smaller memory addresses, hence we scan tos->bos.
* Stop scanning once the distance between tos & bos is too small to hold a valid pointer */
for (char *p = (char *)tos; p <= (char *)bos - PTRSIZE; ++p) {
gc_mark_alloc(*(void **)p);
for (void **p = (void **)tos; (char *)p <= (char *)bos - PTRSIZE; ++p) {
gc_mark_alloc(*p);
}
}
@@ -404,22 +361,13 @@ static size_t gc_sweep() {
chunk->tag &= ~GC_TAG_MARK;
chunk = chunk->next;
}
else if (chunk->tag & GC_TAG_CUT) {
chunk = chunk->next;
}
else {
/* no reference to this chunk, hence delete it */
total += chunk->size;
free(chunk->ptr);
/* and remove it from the bookkeeping */
next = chunk->next;
gc_allocation_t *cut = chunk->cut;
next = chunk->next;
gc_allocation_map_remove(gc->allocs, chunk->ptr, false);
while (cut) {
gc_allocation_t *next = cut->cut;
gc_allocation_map_remove(gc->allocs, cut->ptr, false);
cut = next;
}
chunk = next;
}
}
-2
View File
@@ -12,10 +12,8 @@ void _gc_pause();
void _gc_resume();
size_t _gc_run();
void *_gc_calloc(size_t count, size_t size);
void _gc_array(void *ptr, uint32_t *length);
void _gc_leaf(void *ptr);
void _gc_root(void *ptr);
void _gc_unroot(void *ptr);
void *_gc_cut(void *ptr, size_t pos, size_t size);
void *_gc_realloc(void *ptr, size_t size);
void _gc_free(void *ptr);
+1
View File
@@ -308,6 +308,7 @@ function project_set_default_envmap() {
scene_world._.irradiance = context_raw.default_irradiance;
scene_world.strength = 2.0;
context_raw.envmap_angle = 0.0;
project_raw.envmap = null;
}
function project_import_material() {