Quickstart
Get started with eAlloc using a simple example.
Basic Usage (Host/PC Example)
1#include <eAlloc.hpp>
2#include <globalELock.hpp>
3
4// Define a memory pool (4KB)
5static char memory_pool[4096];
6elock::StdMutex mutex;
7dsa::eAlloc alloc(memory_pool, sizeof(memory_pool));
8alloc.setLock(&mutex);
9void* block = alloc.malloc(128);
10if (block) {
11 std::cout << "Allocated 128 bytes at " << block << std::endl;
12 alloc.free(block);
13} else {
14 std::cout << "Failed to allocate 128 bytes." << std::endl;
15}
16return 0;
ESP32 Example (MCU)
1#include <eAlloc.hpp>
2#include <globalELock.hpp>
3#include "freertos/FreeRTOS.h"
4#include "freertos/task.h"
5#include "esp_log.h"
6
7static const char *TAG = "eAlloc_ESP32";
8
9// Define a smaller memory pool for MCU (2KB)
10static char mcu_pool[2048];
11
12extern "C" void app_main(void) {
13 // Create a FreeRTOS mutex
14 elock::FreeRTOSMutex mutex(xSemaphoreCreateMutex());
15
16 dsa::eAlloc alloc(mcu_pool, sizeof(mcu_pool));
17 alloc.setLock(&mutex);
18
19 void* ptr = alloc.malloc(128);
20 if (ptr) {
21 ESP_LOGI(TAG, "Allocated 128 bytes at %p", ptr);
22 alloc.free(ptr);
23 } else {
24 ESP_LOGE(TAG, "Failed to allocate memory.");
25 }
26 // Delay to allow logs to flush
27 vTaskDelay(1000 / portTICK_PERIOD_MS);
28}
StackAllocator Example
1#include <StackAllocator.hpp>
2#include <vector>
3#include <iostream>
4
5int main() {
6 dsa::StackAllocator<int, 256> stack_alloc;
7 std::vector<int, dsa::StackAllocator<int, 256>> vec(stack_alloc);
8 for (int i = 0; i < 10; ++i) {
9 vec.push_back(i * 5);
10 }
11 std::cout << "StackAllocator Vector: ";
12 for (int val : vec) {
13 std::cout << val << " ";
14 }
15 std::cout << std::endl;
16 return 0;
17}