Ever wanted to align any type on a certain boundary, and you couldn't, because Delphi doesn't have a STACKALIGN or DATAALIGN directive that allows you to align a type on the boundary?

Since the undocumented record alignment directive was introduced, you can already align any record onto a boundary of 1, 2, 4, 8 or 16 bytes, whether it is allocated on the stack, or on the heap.

From the link, we know that a record declaration can be decorated with an align directive, in order to force it to be aligned on a boundary of the given parameter.

With a little bit of innovation on my part, and generics, now, you can align anything on the stack. Let's begin with a little experiment.

For our little experiment, let's ensure you turn off alignment with {$ALIGN OFF}, or set it to align on a different boundary, for example, 2, 4 or 8 bytes.

Then, let's declare a generic record:

type
  TAligned<T> = packed record
    AlignedRec: T;
  end align 16;

What the above record does is align any record or class onto a 16-byte aligned position.  You can also declare various generic records with the alignment being set to any of the allowed values, so that you can have your desired type aligned on a specific boundary.  So... given the following code:

type
  PFoo = ^TFoo;
  TFoo = record
    B: Byte;
    D1, D2: Int32;
  end;

If you declare

var
  AlignedFoo: TAligned<TFoo>;

you'd get an TFoo that's aligned at a 16-byte boundary due to it being declared as TAligned<TFoo>.

This can be proven with the following code:

var
  B: Byte;
  AlignFoo: TAligned<TFoo>;
begin
  ShowMessage(Format('B: %p, Foo: %p %p', [@B, @AlignFoo, @AlignFoo.AlignedRec]));
end;

Now, let's add two implicit class operators, so that an instance of TFoo can be assigned to an instance of TAligned<TFoo> and vice versa.

    class operator Implicit(const AT: TAligned<T>): TFoo; inline;
    class operator Implicit(const AT: TFoo): TAligned<T>; inline;
...
class operator TAligned<T>.Implicit(const AT: T): TAligned<T>;
begin
  Result.AlignedRec := AT;
end;

class operator TAligned<T>.Implicit(const AT: TAligned<T>): T;
begin
  Result := AT.AlignedRec;
end;

Without the implicit class operators, you can only assign an instance of TAligned<TFoo> to another instance of TAligned<TFoo>, or explicitly assign an instance of TFoo to the AlignedRec field, ie, AlignedFoo1.AlignedRec := LFoo1;

Now, let's test that it works, by declaring 2 test types:

type
  TMyBytes = record
    A: Byte;
    B: string;
  end;

  TFoo = record
    B: Byte;
    D1, D2: Int32;
  end;

and then, let's test it with the code:

procedure Main;
var
  B: Byte;
  AlignFoo: TAligned<TFoo>;
  B2: Byte;
  AlignBytes: TAligned<TMyBytes>;
begin
  ShowMessage(Format('B: %p, Foo: %p, %p; AlignBytes: %p, %p',
    [@B, @AlignFoo, @AlignFoo.AlignedRec, @AlignBytes, @AlignBytes.AlignedRec]));
end;

Running the Main procedure listed above gives us:

'B: 0018FF6F, B2: 0018FF6E, Foo: 0018FF50, 0018FF50; AlignBytes: 0018FF40, 0018FF40'

which means, we've managed to achieved alignment!

But what if you wanted to align on larger values, like 32 bytes, or 64 bytes? Delphi's record alignment directive currently doesn't support a parameter value larger than 16, so we'll need to get creative.
We can align records and types on 32 bytes or 64 bytes with generic variations like the following:

type
  TAligned32<T> = packed record
  private
    A1: array[0..31] of Byte;
  public
    AlignedRec: T;
  // ... other class operator code as above
  end;

  TAligned64<T> = record
  private
    A1: array[0..63] of Byte;
  public
    AlignedRec: T;
  // ... other class operator code as above
  end;

Unfortunately, we'll not be able to declare pointers to these types like: PAligned64<TFoo> = ^TAligned64<TFoo>, as the compiler will produce the error "E2508 Type parameters not allowed on this type".

I discussed with another MVP, Stefan, and he said the following:

From the comments of https://stackoverflow.com/questions/4635945/are-pointers-to-generic-types-supported-in-delphi-xe by the former Delphi compiler engineer Barry Kelly:

it is perfectly possible in principle to have generic pointers in Delphi (though they’re not implemented).

I read that as: “no technical reason - we simply did not do it”

Here is another answer from him: https://stackoverflow.com/a/511384/587106

And even though the question is a different one he mentions the topic here as well: https://stackoverflow.com/a/793821/587106

An alternative is to declare a pointer to an instantiated type, like so:

type
  TAlignedFoo = TAligned<TFoo>
  PAlignedFoo = ^TAlignedFoo;

So, there we go! Aligning records or native types on the stack, on a 16-byte aligned memory.

I wanted to expand this further, and develop a class using Delphi's generics, so that I could specify the size to align on as a generic parameter, like so:

type
  TAligned<T, AlignSize> = packed record
    FPadding: array[1..AlignSize] of Byte;  
    AlignedRec: T;
  end align 16;

Where I wanted to use it as such:

  var AlignedFoo: TAligned<TFoo, 256>;

Unfortunately, as everyone knows, Delphi's generics constraints is quite limited, and as such, it doesn't allow anything except a type to be specified as a generic parameter.

So, as a result, how I eventually implemented this, is by using code templates with the alignment being part of the code template.