-
Notifications
You must be signed in to change notification settings - Fork 13
Memory handling
- Default/Unaligned memory allocation
- Custom aligned memory allocation
- Memory operations
- Useful macro functions
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.
// Allocate 4MB of memory
void *myMemory = ((4));
// ...
// or you can allocate the type directly
// Allocate a bunch of int's
int *intArray = (int *)(sizeof(int) * 100000);
intArray[0] = 42;
// ...Note: This function does nothing more than calling the proper operating-system function such as
VirtualAlloc() or
mmap() .
Attention: The memory is not guaranteed to be aligned, but depending on the OS it may be aligned to something.
Warning: Do not write before this pointer, otherwise, you may overwrite meta-information required for releasing the memory later on.
To free memory, you simply call fplMemoryFree() with the base pointer of your memory, and that's it.
// Free a memory block allocated by fplMemoryAllocate
(myMemory);Warning: Do not call this function with a pointer allocated from
fplMemoryAlignedAllocate() , otherwise, you will corrupt memory!
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 |
Note: On Linux/Unix the size and small padding are stored before the actual data, because
munmap() requires a size as a parameter as well.
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.
// Allocate a bunch of 4x4 matrices aligned by 16-bytes
typedef struct Mat4f {
float m[16];
} Vec4f;
Mat4f *matrices = (Mat4f *)(sizeof(Mat4f) * 128, 16);
// ...Note: The size of the memory block may be greater than the size you requested, due to alignment padding and meta-information.
Attention: The alignment parameter must be a power-of-two based value, otherwise, the function will fail.
Warning: Do not write before this pointer, otherwise you will overwrite meta-information required for releasing the memory later on.
To free aligned memory you simply call fplMemoryAlignedFree() with the base pointer of your aligned memory and that's it.
Warning: Do not call this function with a pointer allocated from
fplMemoryAllocate() , otherwise, you will corrupt memory!
Here you can see the full data-layout for an 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.
// Clear 4-KB of memory to zero, starting by the given memory address
(myMemory, 4096);
// or
// Clear a struct
(myStructPtr, sizeof(*myStructPtr));
// or
// Clear a fixed sized array
(myArray, sizeof(myArray));Note: This operation is executed in 8-bytes, 4-bytes, 2-bytes sized chunks to improve performance.
Call fplMemorySet() to overwrite a memory block starting from the base-pointer with a given size and the given value.
// Overwrites 1000 bytes of memory with a 8-bit value
(myMemory, 0xAB, 1000);Note: This operation is executed in 8-bytes, 4-bytes, 2-bytes sized chunks to improve performance.
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.
// Copy parts of memory into another memory
// void *destMemory was already allocated before
size_t numberOfBytesToCopy = (10);
(sourceMemory, numberOfBytesToCopy, destMemory);Note: This operation is executed in 8-bytes, 4-bytes, 2-bytes sized chunks to improve performance.
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 |
| fplStructField | Defines a single field in a struct |
| 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 |
| fplGetAlignmentOffset | Returns the offset needed to satisfy an alignment boundary |
| fplGetAlignedSize | Extends a size to satisfy an alignment boundary |
| fplIsAligned | Returns true when a pointer address is aligned |
| fplStackAllocate | Allocates the given number of bytes on the stack |
| 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 |
- Assertion & Debug
- Atomic operations
- Audio functions
- Clipboard functions
- Console functions
- Constants
- Display/Monitor functions
- Dynamic library loading
- Error Handling
- Files/IO functions
- Function macros
- Hardware Infos
- Input types and functions
- Localization functions
- Logging
- Memory Macros
- Memory functions
- Operating system Infos
- Path functions
- Platform functions
- Session Infos
- Settings & Configurations
- Storage class identifiers
- String functions
- Threading and synchronizations routines
- Timing functions
- Video functions
- Window events
- Window functions
- fplARMCPUCapabilities
- fplAudioChannelMap
- fplAudioDeviceID
- fplAudioDeviceInfo
- fplAudioFormat
- fplAudioSettings
- fplColor32
- fplConditionVariable
- fplConsoleSettings
- fplCPUCapabilities
- fplCPUIDLeaf
- fplDateTime
- fplDateTimeCreationResult
- fplDateTimeResult
- fplDisplayInfo
- fplDisplayMode
- fplDynamicLibraryHandle
- fplEndianess
- fplEvent
- fplFileEntry
- fplFileHandle
- fplFilePermissions
- fplFileTimeStamps
- fplGamepadButton
- fplGamepadData
- fplGamepadEvent
- fplGamepadInfo
- fplGamepadInputBinding
- fplGamepadMapping
- fplGamepadSettings
- fplGamepadState
- fplGamepadStates
- fplGraphicsApiSettings
- fplImageSource
- fplInputBackendMask
- fplInputBackendSupport
- fplInputDevice
- fplInputDeviceGuid
- fplInputSettings
- fplInternalConditionVariable
- fplInternalDynamicLibraryHandle
- fplInternalFileEntryHandle
- fplInternalFileHandle
- fplInternalFileRootInfo
- fplInternalMutexHandle
- fplInternalSemaphoreHandle
- fplInternalSignalHandle
- fplInternalThreadHandle
- fplKeyboardEvent
- fplKeyboardState
- fplLogSettings
- fplLogWriter
- fplLogWriterConsole
- fplLogWriterCustom
- fplMemoryAllocationSettings
- fplMemoryBlock
- fplMemoryInfos
- fplMemorySettings
- fplMouseEvent
- fplMouseState
- fplMutexHandle
- fplOpenGLSettings
- fplOSVersionInfos
- fplSemaphoreHandle
- fplSettings
- fplSignalHandle
- fplSpecificAudioSettings
- fplThreadHandle
- fplThreadParameters
- fplTimestamp
- fplVersionInfo
- fplVideoBackBuffer
- fplVideoRect
- fplVideoRequirements
- fplVideoRequirementsVulkan
- fplVideoSettings
- fplVideoSurface
- fplVideoSurfaceOpenGL
- fplVideoSurfaceVulkan
- fplVideoWindow
- fplVulkanSettings
- fplWindowCallbacks
- fplWindowDropFiles
- fplWindowEvent
- fplWindowPosition
- fplWindowSettings
- fplWindowSize
- fplX86CPUCapabilities