Skip to content

Commit 1e3e016

Browse files
authored
Change struct/obj alloc size (#682)
1 parent 63763dc commit 1e3e016

File tree

6 files changed

+32
-21
lines changed

6 files changed

+32
-21
lines changed

libs/directx/dx/Dx12.hx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,7 @@ enum abstract InputClassification(Int) {
10441044
@:struct class InputLayoutDesc {
10451045
public var inputElementDescs : hl.CArray<InputElementDesc>;
10461046
public var numElements : Int;
1047+
public var __padding : Int; // largest element
10471048
public function new() {
10481049
}
10491050
}
@@ -1094,7 +1095,6 @@ enum abstract PipelineStateFlags(Int) {
10941095
@:packed public var rasterizerState(default,null) : RasterizerDesc;
10951096
@:packed public var depthStencilDesc(default,null) : DepthStencilDesc;
10961097
@:packed public var inputLayout(default,null) : InputLayoutDesc;
1097-
var __padding : Int; // ?
10981098
public var ibStripCutValue : IndexBufferStripCutValue;
10991099
public var primitiveTopologyType : PrimitiveTopologyType;
11001100
public var numRenderTargets : Int;

src/gc.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -1271,16 +1271,13 @@ vdynamic *hl_alloc_dynbool( bool b ) {
12711271

12721272
vdynamic *hl_alloc_obj( hl_type *t ) {
12731273
vobj *o;
1274-
int size;
12751274
int i;
12761275
hl_runtime_obj *rt = t->obj->rt;
12771276
if( rt == NULL || rt->methods == NULL ) rt = hl_get_obj_proto(t);
1278-
size = rt->size;
1279-
if( size & (HL_WSIZE-1) ) size += HL_WSIZE - (size & (HL_WSIZE-1));
12801277
if( t->kind == HSTRUCT ) {
1281-
o = (vobj*)hl_gc_alloc_gen(t, size, (rt->hasPtr ? MEM_KIND_RAW : MEM_KIND_NOPTR) | MEM_ZERO);
1278+
o = (vobj*)hl_gc_alloc_gen(t, rt->size, (rt->hasPtr ? MEM_KIND_RAW : MEM_KIND_NOPTR) | MEM_ZERO);
12821279
} else {
1283-
o = (vobj*)hl_gc_alloc_gen(t, size, (rt->hasPtr ? MEM_KIND_DYNAMIC : MEM_KIND_NOPTR) | MEM_ZERO);
1280+
o = (vobj*)hl_gc_alloc_gen(t, rt->size, (rt->hasPtr ? MEM_KIND_DYNAMIC : MEM_KIND_NOPTR) | MEM_ZERO);
12841281
o->t = t;
12851282
}
12861283
for(i=0;i<rt->nbindings;i++) {

src/hl.h

+2
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,8 @@ struct hl_runtime_obj {
533533
int size;
534534
int nmethods;
535535
int nbindings;
536+
unsigned char pad_size;
537+
unsigned char largest_field;
536538
bool hasPtr;
537539
void **methods;
538540
int *fields_indexes;

src/jit.c

-1
Original file line numberDiff line numberDiff line change
@@ -3822,7 +3822,6 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
38223822
else {
38233823
hl_runtime_obj *rt = hl_get_obj_rt(dst->t);
38243824
osize = rt->size;
3825-
if( osize & (HL_WSIZE-1) ) osize += HL_WSIZE - (osize & (HL_WSIZE-1));
38263825
}
38273826
preg *idx = alloc_cpu(ctx, rb, true);
38283827
op64(ctx, IMUL, idx, pconst(&p,osize));

src/std/array.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,11 @@ HL_PRIM void *hl_alloc_carray( hl_type *at, int size ) {
5757

5858
hl_runtime_obj *rt = at->obj->rt;
5959
if( rt == NULL || rt->methods == NULL ) rt = hl_get_obj_proto(at);
60-
int osize = rt->size;
61-
if( osize & (HL_WSIZE-1) ) osize += HL_WSIZE - (osize & (HL_WSIZE-1));
62-
char *arr = hl_gc_alloc_gen(at, size * osize, (rt->hasPtr ? MEM_KIND_RAW : MEM_KIND_NOPTR) | MEM_ZERO);
60+
char *arr = hl_gc_alloc_gen(at, size * rt->size, (rt->hasPtr ? MEM_KIND_RAW : MEM_KIND_NOPTR) | MEM_ZERO);
6361
if( at->kind == HOBJ || rt->nbindings ) {
6462
int i,k;
6563
for(k=0;k<size;k++) {
66-
char *o = arr + osize * k;
64+
char *o = arr + rt->size * k;
6765
if( at->kind == HOBJ )
6866
((vobj*)o)->t = at;
6967
for(i=0;i<rt->nbindings;i++) {

src/std/obj.c

+25-10
Original file line numberDiff line numberDiff line change
@@ -236,18 +236,21 @@ HL_PRIM hl_runtime_obj *hl_get_obj_rt( hl_type *ot ) {
236236
start = p->nfields;
237237
memcpy(t->fields_indexes, p->fields_indexes, sizeof(int)*p->nfields);
238238
}
239-
size = p ? p->size : (ot->kind == HSTRUCT ? 0 : HL_WSIZE); // hl_type*
239+
size = p ? p->size - p->pad_size : (ot->kind == HSTRUCT ? 0 : HL_WSIZE); // hl_type*
240240
nlookup = 0;
241+
int largest_field = p ? p->largest_field : size;
241242
for(i=0;i<o->nfields;i++) {
242243
hl_type *ft = o->fields[i].t;
243-
hl_type *pad = ft;
244-
while( pad->kind == HPACKED ) {
245-
// align on first field
246-
pad = pad->tparam;
247-
while( pad->obj->super && hl_get_obj_rt(pad->obj->super)->nfields ) pad = pad->obj->super;
248-
pad = pad->obj->fields[0].t;
249-
}
250-
size += hl_pad_struct(size,pad);
244+
if( ft->kind == HPACKED ) {
245+
// align on packed largest field
246+
int large = hl_get_obj_rt(ft->tparam)->largest_field;
247+
int pad = size % large;
248+
if( pad != 0 )
249+
size += large - pad;
250+
if( large > largest_field )
251+
largest_field = large;
252+
} else
253+
size += hl_pad_struct(size,ft);
251254
t->fields_indexes[i+start] = size;
252255
if( *o->fields[i].name )
253256
hl_lookup_insert(t->lookup,nlookup++,o->fields[i].hashed_name,o->fields[i].t,size);
@@ -259,10 +262,22 @@ HL_PRIM hl_runtime_obj *hl_get_obj_rt( hl_type *ot ) {
259262
if( rts->hasPtr ) t->hasPtr = true;
260263
continue;
261264
}
262-
size += hl_type_size(ft);
265+
int sz = hl_type_size(ft);
266+
size += sz;
267+
if( sz > largest_field ) largest_field = sz;
263268
if( !t->hasPtr && hl_is_ptr(ft) ) t->hasPtr = true;
264269
}
265270
t->size = size;
271+
t->pad_size = 0;
272+
// align size on largest field size to match C, so arr[i].field is always aligned
273+
if( largest_field > 0 ) {
274+
int pad = size % largest_field;
275+
if( pad != 0 ) {
276+
t->pad_size = largest_field - pad;
277+
t->size += t->pad_size;
278+
}
279+
}
280+
t->largest_field = largest_field;
266281
t->nmethods = p ? p->nmethods : o->nproto;
267282
t->methods = NULL;
268283
o->rt = t;

0 commit comments

Comments
 (0)