record alignment

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

record alignment

Ryan Joseph
I have a struct I need to port from C but the size I’m getting in Pascal is wrong. From C sizeof(AAPLVertex) = 32 but in Pascal sizeof(AAPLVertex) = 24.

The alignment of the struct is on 32 bit bounds I think but how do I replicate this in Pascal so it behaves exactly like c? I tried using $align but it didn’t seem to have any effect.

{$align 32}
type
        TAAPLVertex = packed record
                position: vector_float2;
                color: vector_float4;
        end;


typedef struct
{
    vector_float2 position;
    vector_float4 color;
} AAPLVertex;


Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: record alignment

Karoly Balogh (Charlie/SGR)
Hi,

On Fri, 15 Jun 2018, Ryan Joseph wrote:

> I have a struct I need to port from C but the size I’m getting in Pascal
> is wrong. From C sizeof(AAPLVertex) = 32 but in Pascal
> sizeof(AAPLVertex) = 24.
>
> The alignment of the struct is on 32 bit bounds I think but how do I
> replicate this in Pascal so it behaves exactly like c? I tried using
> $align but it didn’t seem to have any effect.
>
> {$align 32}
> type
> TAAPLVertex = packed record
> position: vector_float2;
> color: vector_float4;
> end;
The whole idea of a packed record is to ignore alignment and "pack" the
fields ignoring padding and alignment. Use a normal record without
"packed", or add the padding fields manually, if you still have to use a
packed record for whatever reason.

BTW, $align specifies the alignment in *BYTES* not in bits. So to align to
32bits you need {$align 4}. But Free Pascal also supports {$packrecords
C}, where it will try to mimic the C compiler's alignment as native to the
target platform. In any unit which needs to interface to a C library, I'd
use {$packrecords C} and no {$align}

This is all documented BTW at the respective directives...

Charlie
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: record alignment

Ryan Joseph


> On Jun 15, 2018, at 3:45 PM, Karoly Balogh (Charlie/SGR) <[hidden email]> wrote:
>
> The whole idea of a packed record is to ignore alignment and "pack" the
> fields ignoring padding and alignment. Use a normal record without
> "packed", or add the padding fields manually, if you still have to use a
> packed record for whatever reason.
>
> BTW, $align specifies the alignment in *BYTES* not in bits. So to align to
> 32bits you need {$align 4}. But Free Pascal also supports {$packrecords
> C}, where it will try to mimic the C compiler's alignment as native to the
> target platform. In any unit which needs to interface to a C library, I'd
> use {$packrecords C} and no {$align}

Ok, I see now. I tried adding {$packrecords C} in all units but the size still comes back as 24 instead of 32. So what is the C compiler really doing I wonder? I found out through trial and error I need 2 floats in-between both fields to get proper alignment but why is this? I hate to be adding random space into records which may break on other machines or if built with different compilers (I’m using ppcx64 btw if that matters).

type
        TAAPLVertex = record
                position: vector_float2;

                padding_0: simd_float;
                padding_1: simd_float;

                color: vector_float4;
        end;





Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal