Examples
This page provides practical examples of how to use eAlloc in various scenarios.
Tip
For thread safety in concurrent environments, always set an elock for your allocator instance using alloc.setLock(&mutex_adapter)
.
Use elock::StdMutex
for host/PC applications, or the appropriate adapter (e.g., elock::FreeRTOSMutex
, elock::CMSISMutex
) for your specific MCU/RTOS platform.
MCU Example 1: FreeRTOS
A minimal, thread-safe allocator for FreeRTOS:
1#include <eAlloc.hpp>
2#include <globalELock.hpp> // For elock::FreeRTOSMutex
3#include "freertos/FreeRTOS.h"
4#include "freertos/semphr.h" // For xSemaphoreCreateMutex
5
6// Define a memory pool (e.g., 4KB)
7static char memory_pool[4096];
8
9// Create a FreeRTOS mutex
10SemaphoreHandle_t my_mutex_handle = xSemaphoreCreateMutex();
11elock::FreeRTOSMutex freertos_mutex(my_mutex_handle);
12
13// Initialize the allocator
14dsa::eAlloc alloc(memory_pool, sizeof(memory_pool));
15alloc.setLock(&freertos_mutex); // Set the lock for thread safety
16
17// Usage
18void* block = alloc.malloc(128);
19if (block) {
20 // Use the allocated memory
21 alloc.free(block);
22}
MCU Example 2: ESP-IDF (ESP32)
A minimal, thread-safe allocator for ESP-IDF (ESP32/ESP8266):
1#include <eAlloc.hpp>
2#include <globalELock.hpp> // For elock::FreeRTOSMutex (ESP-IDF uses FreeRTOS)
3#include "freertos/FreeRTOS.h"
4#include "freertos/semphr.h"
5#include "esp_log.h"
6
7static const char *TAG_EX = "eAlloc_ESP_Example";
8
9// Define a memory pool (e.g., 2KB)
10static char esp_pool[2048];
11
12// Create a FreeRTOS mutex (as ESP-IDF uses FreeRTOS)
13SemaphoreHandle_t esp_mutex_handle = xSemaphoreCreateMutex();
14elock::FreeRTOSMutex esp_freertos_mutex(esp_mutex_handle);
15
16// Initialize the allocator
17dsa::eAlloc esp_alloc(esp_pool, sizeof(esp_pool));
18esp_alloc.setLock(&esp_freertos_mutex); // Set the lock
19
20// Usage within an ESP-IDF task
21void some_esp_task(void *pvParameters) {
22 void* ptr = esp_alloc.malloc(64);
23 if (ptr) {
24 ESP_LOGI(TAG_EX, "Allocated 64 bytes at %p", ptr);
25 // Use memory
26 esp_alloc.free(ptr);
27 } else {
28 ESP_LOGE(TAG_EX, "Failed to allocate memory.");
29 }
30 vTaskDelete(NULL);
31}
Host/PC Example
A minimal, thread-safe allocator for host/PC applications:
1#include <eAlloc.hpp>
2#include <globalELock.hpp>
3#include <iostream>
4
5// Define a memory pool (e.g., 8KB)
6static char pc_pool[8192];
7elock::StdMutex std_mutex;
8dsa::eAlloc host_alloc(pc_pool, sizeof(pc_pool));
9host_alloc.setLock(&std_mutex);
10
11int main() {
12 alloc2.setLock(&pc2_lock);
13
14 // Using calloc: allocate memory for 10 integers, initialized to zero
15 int* arr = (int*)alloc2.calloc(10, sizeof(int));
16 if (arr) {
17 std::cout << "calloc allocated 10 ints at " << arr << std::endl;
18 bool all_zero = true;
19 for (size_t i = 0; i < 10; ++i) {
20 if (arr[i] != 0) {
21 all_zero = false;
22 break;
23 }
24 }
25 std::cout << "Memory is " << (all_zero ? "" : "NOT ") << "zero-initialized by calloc." << std::endl;
26
27 // Using realloc: resize the block to hold 20 integers
28 int* resized_arr = (int*)alloc2.realloc(arr, 20 * sizeof(int));
29 if (resized_arr) {
30 arr = resized_arr; // arr might have moved
31 std::cout << "realloc resized block to 20 ints at " << arr << std::endl;
32 // Initialize the new part
33 for (size_t i = 10; i < 20; ++i) arr[i] = i;
34 std::cout << "arr[15] = " << arr[15] << std::endl;
35 }
36 alloc2.free(arr); // Free the final block
37 } else {
38 std::cout << "calloc failed." << std::endl;
39 }
40 return 0;
41}
StackAllocator Example (STL-compatible)
Use STL containers (e.g., std::vector
) with a fixed-size stack buffer, achieving zero heap allocations for the container’s elements:
1#include <StackAllocator.hpp>
2#include <vector>
3#include <iostream>
4#include <string>
5
6int main() {
7 // StackAllocator for 10 integers (approx 40 bytes)
8 dsa::StackAllocator<int, 10 * sizeof(int)> int_alloc;
9 std::vector<int, dsa::StackAllocator<int, 10 * sizeof(int)>> int_vec(int_alloc);
10
11 for (int i = 0; i < 5; ++i) {
12 int_vec.push_back(i * 10);
13 }
14 std::cout << "StackAllocator int_vec: ";
15 for (int val : int_vec) {
16 std::cout << val << " ";
17 }
18 std::cout << std::endl;
19
20 // StackAllocator for a few small strings (e.g., 256 bytes buffer)
21 // Note: std::string itself might do heap allocations for large strings internally,
22 // but the vector's control structures and small string optimization (SSO)
23 // can benefit from the StackAllocator.
24 dsa::StackAllocator<std::string, 256> string_alloc;
25 std::vector<std::string, dsa::StackAllocator<std::string, 256>> str_vec(string_alloc);
26
27 str_vec.push_back("Hello");
28 str_vec.push_back("Stack");
29 str_vec.push_back("World");
30
31 std::cout << "StackAllocator str_vec: ";
32 for (const auto& s : str_vec) {
33 std::cout << "\"" << s << "\" ";
34 }
35 std::cout << std::endl;
36
37 return 0;
38}
See also the API Reference for eAlloc page for full details on StackAllocator
and other classes.