AllocMem Function Documentation

A versatile memory allocation system for diverse computing environments

Overview

The AllocMem function is a custom memory allocation utility designed to provide flexible and robust memory management across various memory types, data types, and computing environments. It supports heap, stack, IPC, GPU, cloud, registry, and page memory, with fine-grained control over data types, array dimensions, and memory attributes.

The function is built to handle both simple allocations (similar to malloc) and complex, metadata-driven allocations via a MemoryAllocationRequest structure, making it suitable for applications ranging from embedded systems to high-performance computing.

Functionality

The AllocMem function allocates memory with customizable properties and returns a pointer to the allocated memory. It supports two modes of operation:

Signature

void* AllocMem(uint64_t tSize, ...)

Parameters

Parameter Description
tSize Size of memory to allocate (in bytes). If set to 0xffffffffffffffff, a MemoryAllocationRequest is expected.
... Variadic arguments, used to pass a MemoryAllocationRequest structure for advanced allocations.

Return Value

Versatility

The AllocMem function is highly versatile, making it suitable for a wide range of use cases. Its flexibility stems from the following features:

1. Multiple Memory Types

Supports allocation across diverse memory types, enabling compatibility with different hardware and software environments:

This allows AllocMem to be used in embedded systems, high-performance computing, distributed systems, and GPU-accelerated applications.

2. Flexible Data Types

Supports a variety of data types, from primitive types to complex objects:

Additionally, the DATA_SIGNED flag allows specification of signedness for primitives, and DATA_CONTAINED supports encapsulated objects.

3. Array Dimension Support

Handles multi-dimensional arrays with explicit dimension encoding:

This is particularly useful for scientific computing, image processing, or machine learning applications requiring multi-dimensional data structures.

4. Advanced Memory Flags

Provides fine-grained control over memory behavior through flags:

5. Packed Parameters

The DATA_PACK_PARAMETERS flag allows optimized storage of parameters, reducing memory overhead in specific use cases, such as parameter passing in distributed systems.

6. Memory Auditing and Naming

Automatically generates unique Unicode names for memory blocks if not provided, and integrates with a MemoryHandle structure for tracking allocation metadata (e.g., size, creation time, last access). Memory allocations are added to a memory list for auditing, enhancing debugging and resource management.

7. Cross-Platform Compatibility

By abstracting memory types and providing a unified interface, AllocMem can be adapted to different platforms, from CPUs to GPUs, and from local systems to cloud environments.

This versatility makes AllocMem a powerful tool for developers working on complex systems requiring precise control over memory allocation and management.

Usage Examples

Simple Allocation

Allocate 1024 bytes of heap memory:

void* ptr = AllocMem(1024);
if (ptr) {
    // Use memory
    // Memory is automatically named (e.g., "user_mem1")
}

Advanced Allocation

Allocate a 2D array of signed integers in GPU memory:

struct MemoryAllocationRequest request = {
    .ma_size = 1024 * sizeof(int),
    .ma_ram_type = GPU_MEMORY,
    .ma_data_type = DATA_INT | DATA_SIGNED,
    .ma_dimension_type = DATA_2D_ARRAY,
    .ma_flags = MEMORY_GPU_GLOBAL,
    .ma_name = L"gpu_int_array"
};
void* ptr = AllocMem(0xffffffffffffffff, &request);
if (ptr) {
    // Use GPU-accessible 2D integer array
}

Error Handling

Check for allocation failure:

void* ptr = AllocMem(0); // Invalid size
if (!ptr) {
    printf("Allocation failed: Invalid size\n");
}

Encoding Scheme

The AllocMem function uses a compact encoding scheme to store metadata about memory allocations. The encoding is managed through bit fields and decoded using the DECODE_CHANNEL_DATA macro.

Key Macros

Macro Description
MEMORY_TYPE(input) Extracts memory type (e.g., HEAP_MEMORY).
PRIMITIVE_TYPE(input) Extracts data type (e.g., DATA_INT).
IS_SIGNED(input) Checks if data is signed.
DIMENSION_TYPE(input) Extracts array dimension (e.g., DATA_2D_ARRAY).
IS_PACKED_PARAMETERS(input) Checks if parameters are packed.

Decoding Example

uint8_t memoryType, primitiveType, dimensionBits, packedParameters, isSigned;
int64_t input = (DATA_PACK_PARAMETERS << 24) | (DATA_2D_ARRAY << 16) | (DATA_INT << 8) | GPU_MEMORY;
DECODE_CHANNEL_DATA(input, memoryType, primitiveType, dimensionBits, packedParameters, isSigned);
// Results:
// memoryType = GPU_MEMORY
// primitiveType = DATA_INT
// dimensionBits = DATA_2D_ARRAY
// packedParameters = 1
// isSigned = 0