Memory Manager Usage Scenarios#

Scenario 1: Basic Dynamic Allocation#

Use Case: Allocating temporary buffers for data processing

Implementation:

#include "sl_memory_manager.h"

void process_data_buffer(void) {
  // Allocate buffer for data processing using short-term allocation
  uint8_t *buffer;
  sl_status_t status = sl_memory_alloc(1024, BLOCK_TYPE_SHORT_TERM, (void**)&buffer);
  if (status == SL_STATUS_OK) {
    // Process data
    memset(buffer, 0, 1024);
    // ... data processing ...

    // Free buffer when done
    sl_free(buffer);
  }
}

Expected Behavior:

  • Buffer allocated from short-term heap section

  • Memory cleared and available for processing

  • Buffer freed back to heap when processing complete

Scenario 2: Memory Pool for Real-Time Applications#

Use Case: Deterministic memory allocation for real-time tasks

Implementation:

#include "sl_memory_manager.h"

// Global pool handle
static sl_memory_pool_t rt_pool = { 0 };

void init_rt_memory_pool(void) {
  // Create pool with 20 blocks of 64 bytes each
  sl_status_t status = sl_memory_create_pool(64, 20, &rt_pool);
  if (status != SL_STATUS_OK) {
    // Handle pool creation failure
  }
}

void rt_task_allocate_buffer(void) {
  void *buffer;
  sl_status_t status = sl_memory_pool_alloc(&rt_pool, &buffer);
  if (status == SL_STATUS_OK) {
    // Use buffer for real-time processing
    // ... processing ...

    // Return buffer to pool
    sl_memory_pool_free(&rt_pool, buffer);
  }
}

Expected Behavior:

  • Deterministic allocation timing

  • Guaranteed availability of blocks

  • No heap fragmentation concerns

Scenario 3: Advanced Allocation with Alignment#

Use Case: Allocating buffers for DMA operations requiring specific alignment

Implementation:

#include "sl_memory_manager.h"

void allocate_dma_buffer(void) {
  void *dma_buffer;
  sl_status_t status = sl_memory_alloc_advanced(512, SL_MEMORY_BLOCK_ALIGN_64_BYTES,
                                               BLOCK_TYPE_SHORT_TERM, &dma_buffer);
  if (status == SL_STATUS_OK) {
    // Buffer is aligned to 64-byte boundary for DMA
    // ... DMA operations ...

    sl_memory_free(dma_buffer);
  }
}

Expected Behavior:

  • Buffer allocated with 64-byte alignment

  • Suitable for DMA operations

  • Short-term allocation for efficient use once the DMA buffer has been consumed

Scenario 4: C++ Integration#

Use Case: Using Memory Manager with C++ applications

Implementation:

#include "sl_memory_manager.h"

class DataProcessor {
private:
    uint8_t* buffer;
    size_t buffer_size;

public:
    DataProcessor(size_t size) : buffer_size(size) {
        // Constructor allocates via overloaded new
        buffer = new uint8_t[buffer_size];
    }

    ~DataProcessor() {
        // Destructor frees via overloaded delete
        delete[] buffer;
    }

    void process() {
        if (buffer != nullptr) {
            // Process data
            memset(buffer, 0, buffer_size);
        }
    }
};

void cpp_memory_example(void) {
    DataProcessor processor(1024);
    processor.process();
    // Destructor automatically frees memory
}

Expected Behavior:

  • C++ new/delete operators redirected to Memory Manager

  • Automatic memory management through RAII

  • Exception-safe memory handling

Scenario 5: Memory Usage Monitoring#

Use Case: Monitoring heap usage for optimization and debugging

Implementation:

#include "sl_memory_manager.h"

void monitor_heap_usage(void) {
  sl_memory_heap_info_t heap_info;
  sl_status_t status = sl_memory_get_heap_info(&heap_info);

  if (status == SL_STATUS_OK) {
    printf("Heap Statistics:\n");
    printf("  Total Size: %zu bytes\n", heap_info.total_size);
    printf("  Used Size: %zu bytes\n", heap_info.used_size);
    printf("  Free Size: %zu bytes\n", heap_info.free_size);
    printf("  Free Blocks: %zu\n", heap_info.free_block_count);
    printf("  Largest Free Block: %zu bytes\n", heap_info.free_block_largest_size);
    printf("  High Watermark: %zu bytes\n", sl_memory_get_heap_high_watermark());
  }
}

Expected Behavior:

  • Detailed heap usage information

  • Fragmentation analysis

  • High watermark tracking for peak usage