[fpc-devel] Aligned dynamic arrays
J. Gareth Moreton
gareth at moreton-family.com
Fri Mar 29 02:47:56 CET 2019
Just to add a minor thing... while usually you don't have to align memory
any more coarsely than 16 bytes for XMM, 32 bytes for YMM and 64 bytes for
ZMM, the 4 kB granularity is useful for paging, especially if you're
reading or writing to a swap file. An entire library of functions for
such memory management and alignment would definitely be beneficial.
Using FastMM4 if at all possible is even better.
And that record definition is pretty nice, I have to say!
Gareth aka. Kit
On Thu 28/03/19 22:40 , Anthony Walter sysrpl at gmail.com sent:
Here is a brief follow up. I am working on other projects at the moment,
but I am confident this a simple solution. Please tell me if this somehow
will not fit you needs.
Create a new type that is compatible with Array where Array = type array
of T.Define implcit conversions between the to types and similar
methodsCreate a Push and Pop methods, well as a read write Length property
and two indexers. Indexer named Reference is default and provides a memory
reference to TnIndexer named Item is available and provides a copy of Tn
So we would have ...
TAlignedArray = record
type TReference = ^T; type TValue = T; FPage: Pointer;
FLength: Integer; procedure SetLength(Value: Integer);
function GetReference(Index: Integer): TReference; function
GetItem(Index: Integer): TValue; procedure SetItem(Index: Integer;
const Value: TValue); public procedure Push(const Item: TValue);
procedure Pop: TValue; property Length: Integer read FLength write
SetLength; property Reference[Index: Integer]: TValue read
GetReference; default; property Item[Index: Integer]: TValue read
GetValue write SetValue;
Examples usages:
type TVertexArray = TAlignedArray;
// and later
var Vertices: TVertexArray;begin
Vertices.Length = 1000; // okay, request a page aligned memory
Vertices[0].X := X; // okay, we are working with references
Vertices.Item[1] := Vec3(X, Y, Z); // okay, we are setting a value
Vertices.Length = 0; // clean up
When the vertex array need to grow, it uses the POSIX posix_memalign (or
the _aligned_malloc on Windows) to request a 4 KB aligned and sized page
adequate to contain all the items. The newish allocate and release
mechanisms can further be used to simply the management of the
TAlignedArray type. _______________________________________________
fpc-devel maillist - fpc-devel at lists.freepascal.org [1]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
[2]">http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Links:
------
[1] mailto:fpc-devel at lists.freepascal.org
[2] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190329/99881f51/attachment.html>
More information about the fpc-devel
mailing list