Memory API Reference
This section details the Memory API, providing functions for managing and accessing system memory.
Core Concepts
Understanding how memory is managed is crucial for developing efficient and stable applications. The Memory API provides low-level access to memory, allowing for fine-grained control over allocation, deallocation, and manipulation.
Memory Allocation Functions
AllocateMemory(size_t size, MemoryFlags flags)
Allocates a block of memory of the specified size.
Parameters:
size
: The number of bytes to allocate.flags
: A bitmask of flags to control allocation behavior (e.g.,MEM_COMMIT
,MEM_RESERVE
).
Returns: A pointer to the allocated memory block, or nullptr
if the allocation fails.
FreeMemory(void* ptr)
Deallocates a previously allocated block of memory.
Parameters:
ptr
: A pointer to the memory block to deallocate.
Returns: true
if the deallocation was successful, false
otherwise.
Memory Management Functions
ReadMemory(void* dest, const void* src, size_t size)
Copies a specified number of bytes from a source memory location to a destination memory location.
Parameters:
dest
: A pointer to the destination memory location.src
: A pointer to the source memory location.size
: The number of bytes to copy.
Returns: A pointer to the destination memory location.
WriteMemory(void* dest, const void* src, size_t size)
Copies a specified number of bytes from a source memory location to a destination memory location.
Parameters:
dest
: A pointer to the destination memory location.src
: A pointer to the source memory location.size
: The number of bytes to copy.
Returns: A pointer to the destination memory location.
CompareMemory(const void* ptr1, const void* ptr2, size_t size)
Compares the contents of two memory blocks.
Parameters:
ptr1
: Pointer to the first memory block.ptr2
: Pointer to the second memory block.size
: The number of bytes to compare.
Returns: An integer less than, equal to, or greater than zero, depending on whether the first memory block is less than, equal to, or greater than the second memory block.
Memory Information
GetMemoryUsage()
Retrieves information about the current system memory usage.
Returns: A structure containing details about total memory, free memory, used memory, etc.
// Example structure for GetMemoryUsage()
struct MemoryInfo {
size_t totalMemory;
size_t freeMemory;
size_t usedMemory;
size_t availableMemory;
};
Flags
The following flags can be used with memory allocation functions:
Flag | Description |
---|---|
MEM_COMMIT |
Allocates physical storage in memory or in the paging file for the specified region of pages. |
MEM_RESERVE |
Reserves a range of the process's virtual address space without any actual physical storage being allocated. |
MEM_RELEASE |
Decommits a whole region of pages. |
MEM_RESET |
Indicates that data in the memory range specified by lpAddress and dwSize is no longer of interest. |
Important Note
When working with raw memory, it is essential to ensure that pointers are valid and that memory is properly deallocated to prevent memory leaks and crashes.
Performance Tip
For frequent memory operations, consider using memory pools or custom allocators to improve performance and reduce fragmentation.
Security Warning
Accessing memory outside allocated regions can lead to security vulnerabilities. Always validate memory bounds and use appropriate access controls.