Final Platform Layer 0.9.8-beta
|
FPL provides a couple of functions to allocate/set/clear memory for you.
You are free to use it as you like, but you can use malloc(), free() as well if you want to.
To allocate a block of contiguous memory, you simply call fplMemoryAllocate() with the number of bytes you want to allocate.
This will return a pointer you can start to write into.
To free memory, you simply call fplMemoryFree() with the base pointer of your memory, and that's it.
Here you can see the full data-layout for a default/unaligned memory block:
0 or 4/8 bytes for x86/x64 | 0 or 4/8 bytes for x86/x64 | n-bytes |
---|---|---|
Optional size of the entire memory block | Optional padding | Memory of the data |
To allocate a custom aligned block of contiguous memory, you simply call fplMemoryAlignedAllocate() with the number of bytes you want to allocate and the custom alignment.
This will return a guaranteed aligned pointer you can start to write into.
To free aligned memory you simply call fplMemoryAlignedFree() with the base pointer of your aligned memory and that's it.
Here you can see the full data-layout for a aligned memory block:
4/8 bytes for x86/x64 | Alignment * 2 bytes | n-bytes |
---|---|---|
Address of the base-pointer | Alignment padding | Data layout |
Call fplMemoryClear() to clear a memory block starting from the base-pointer with a given size.
Call fplMemorySet() to overwrite a memory block starting from the base-pointer with a given size and the given value.
To copy (n)-bytes of memory you simply call fplMemoryCopy() with the source and destination pointer and the number of bytes you want to copy.
FPL provides a few macro functions useful for dealing with memory:
Name | What it does |
---|---|
fplZeroInit | Initializes a struct with zero |
fplStructInit | Initializes a struct with values |
fplStructSet | Overwrites a struct with the given value |
fplClearStruct | Clears the given struct pointer to zero |
fplCopyStruct | Copies the source struct data into the destination pointer |
fplArrayCount | Returns the number of elements in fixed sized array |
fplOffsetOf | Returns the byte-offset to a field in a struct |
fplKiloBytes | Converts the given kilobytes value into bytes |
fplMegaBytes | Converts the given megabytes value into bytes |
fplGigaBytes | Converts the given gigabytes value into bytes |
fplTeraBytes | Converts the given terabytes value into bytes |