Skip to content

Commit

Permalink
change struct/obj alloc size based on largest field size alignment (m…
Browse files Browse the repository at this point in the history
…atch C spec)
  • Loading branch information
ncannasse committed May 13, 2024
1 parent 63763dc commit ef3eda5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
7 changes: 2 additions & 5 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1271,16 +1271,13 @@ vdynamic *hl_alloc_dynbool( bool b ) {

vdynamic *hl_alloc_obj( hl_type *t ) {
vobj *o;
int size;
int i;
hl_runtime_obj *rt = t->obj->rt;
if( rt == NULL || rt->methods == NULL ) rt = hl_get_obj_proto(t);
size = rt->size;
if( size & (HL_WSIZE-1) ) size += HL_WSIZE - (size & (HL_WSIZE-1));
if( t->kind == HSTRUCT ) {
o = (vobj*)hl_gc_alloc_gen(t, size, (rt->hasPtr ? MEM_KIND_RAW : MEM_KIND_NOPTR) | MEM_ZERO);
o = (vobj*)hl_gc_alloc_gen(t, rt->size, (rt->hasPtr ? MEM_KIND_RAW : MEM_KIND_NOPTR) | MEM_ZERO);
} else {
o = (vobj*)hl_gc_alloc_gen(t, size, (rt->hasPtr ? MEM_KIND_DYNAMIC : MEM_KIND_NOPTR) | MEM_ZERO);
o = (vobj*)hl_gc_alloc_gen(t, rt->size, (rt->hasPtr ? MEM_KIND_DYNAMIC : MEM_KIND_NOPTR) | MEM_ZERO);
o->t = t;
}
for(i=0;i<rt->nbindings;i++) {
Expand Down
2 changes: 2 additions & 0 deletions src/hl.h
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,8 @@ struct hl_runtime_obj {
int size;
int nmethods;
int nbindings;
unsigned char pad_size;
unsigned char largest_field;
bool hasPtr;
void **methods;
int *fields_indexes;
Expand Down
1 change: 0 additions & 1 deletion src/jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -3822,7 +3822,6 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
else {
hl_runtime_obj *rt = hl_get_obj_rt(dst->t);
osize = rt->size;
if( osize & (HL_WSIZE-1) ) osize += HL_WSIZE - (osize & (HL_WSIZE-1));
}
preg *idx = alloc_cpu(ctx, rb, true);
op64(ctx, IMUL, idx, pconst(&p,osize));
Expand Down
6 changes: 2 additions & 4 deletions src/std/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,11 @@ HL_PRIM void *hl_alloc_carray( hl_type *at, int size ) {

hl_runtime_obj *rt = at->obj->rt;
if( rt == NULL || rt->methods == NULL ) rt = hl_get_obj_proto(at);
int osize = rt->size;
if( osize & (HL_WSIZE-1) ) osize += HL_WSIZE - (osize & (HL_WSIZE-1));
char *arr = hl_gc_alloc_gen(at, size * osize, (rt->hasPtr ? MEM_KIND_RAW : MEM_KIND_NOPTR) | MEM_ZERO);
char *arr = hl_gc_alloc_gen(at, size * rt->size, (rt->hasPtr ? MEM_KIND_RAW : MEM_KIND_NOPTR) | MEM_ZERO);
if( at->kind == HOBJ || rt->nbindings ) {
int i,k;
for(k=0;k<size;k++) {
char *o = arr + osize * k;
char *o = arr + rt->size * k;
if( at->kind == HOBJ )
((vobj*)o)->t = at;
for(i=0;i<rt->nbindings;i++) {
Expand Down
23 changes: 21 additions & 2 deletions src/std/obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,9 @@ HL_PRIM hl_runtime_obj *hl_get_obj_rt( hl_type *ot ) {
start = p->nfields;
memcpy(t->fields_indexes, p->fields_indexes, sizeof(int)*p->nfields);
}
size = p ? p->size : (ot->kind == HSTRUCT ? 0 : HL_WSIZE); // hl_type*
size = p ? p->size - p->pad_size : (ot->kind == HSTRUCT ? 0 : HL_WSIZE); // hl_type*
nlookup = 0;
int largest_field = p ? p->largest_field : size;
for(i=0;i<o->nfields;i++) {
hl_type *ft = o->fields[i].t;
hl_type *pad = ft;
Expand All @@ -259,10 +260,28 @@ HL_PRIM hl_runtime_obj *hl_get_obj_rt( hl_type *ot ) {
if( rts->hasPtr ) t->hasPtr = true;
continue;
}
size += hl_type_size(ft);
int sz = hl_type_size(ft);
size += sz;
if( sz > largest_field ) largest_field = sz;
if( !t->hasPtr && hl_is_ptr(ft) ) t->hasPtr = true;
}
t->size = size;
t->pad_size = 0;
// align size on largest field size to match C, so arr[i].field is always aligned
if( largest_field > 0 ) {
int pad = size % largest_field;
if( pad != 0 ) {
t->pad_size = largest_field - pad;
t->size += t->pad_size;
}
}
{
int sz2 = size;
if( sz2 & (HL_WSIZE-1) ) sz2 += HL_WSIZE - (sz2 & (HL_WSIZE-1));
if( sz2 != t->size )
uprintf(USTR("%s(%d --> %d)\n"), o->name, sz2, t->size);
}
t->largest_field = largest_field;
t->nmethods = p ? p->nmethods : o->nproto;
t->methods = NULL;
o->rt = t;
Expand Down

0 comments on commit ef3eda5

Please sign in to comment.