Tactility/TactilityKernel/source/memory_esp32.cpp
Ken Van Hoeylandt 5707c27882 Improvements and fixes
- custom lvgl alloc
- keyboard drivers now uses codepoints only (no more hardwired LV_KEY_*)
- fix lvgl file lock when lvgl is stopped
2026-08-22 22:50:58 +02:00

88 lines
3.3 KiB
C++

// SPDX-License-Identifier: Apache-2.0
#ifdef ESP_PLATFORM
#include <tactility/memory.h>
#include <esp_heap_caps.h>
namespace {
uint32_t toHeapCaps(uint16_t capabilityFlags) {
uint32_t caps = 0;
if (capabilityFlags & MEMORY_CAPABILITY_INTERNAL) caps |= MALLOC_CAP_INTERNAL;
if (capabilityFlags & MEMORY_CAPABILITY_EXTERNAL) caps |= MALLOC_CAP_SPIRAM;
if (capabilityFlags & MEMORY_CAPABILITY_EXECUTABLE) caps |= MALLOC_CAP_EXEC;
if (capabilityFlags & MEMORY_CAPABILITY_DMA) caps |= MALLOC_CAP_DMA;
if (capabilityFlags & MEMORY_CAPABILITY_SIMD) caps |= MALLOC_CAP_SIMD;
return caps;
}
} // namespace
extern "C" {
void* memory_alloc_with_policy(size_t size, const struct MemoryPolicy* policy) {
uint32_t required_caps = toHeapCaps(policy->required);
uint32_t desired_caps = toHeapCaps(policy->desired);
// heap_caps matches heaps via (heap->caps[prio] & caps) != 0 - a caps value of 0 (e.g.
// required_caps when policy->required wasn't set) can never match any heap, so the fallback
// must OR in MALLOC_CAP_DEFAULT to actually reach a general-purpose heap, same as ESP-IDF's
// own heap_caps_malloc_default() does.
void* ptr;
if (policy->alignment > 0) {
ptr = heap_caps_aligned_alloc(policy->alignment, size, required_caps | desired_caps);
if (ptr == nullptr && desired_caps != 0) {
// Desired caps couldn't be satisfied alongside the required ones - retry with
// required only, since desired is explicitly optional.
ptr = heap_caps_aligned_alloc(policy->alignment, size, required_caps | MALLOC_CAP_DEFAULT);
}
} else {
ptr = heap_caps_malloc(size, required_caps | desired_caps);
if (ptr == nullptr && desired_caps != 0) {
ptr = heap_caps_malloc(size, required_caps | MALLOC_CAP_DEFAULT);
}
}
return ptr;
}
void* memory_realloc_with_policy(void* ptr, size_t size, const struct MemoryPolicy* policy) {
uint32_t required_caps = toHeapCaps(policy->required);
uint32_t desired_caps = toHeapCaps(policy->desired);
// No aligned-realloc counterpart in the heap_caps API - policy->alignment is only honored
// on fresh allocations (memory_alloc_with_policy/memory_calloc_with_policy).
void* result = heap_caps_realloc(ptr, size, required_caps | desired_caps);
if (result == nullptr && desired_caps != 0) {
result = heap_caps_realloc(ptr, size, required_caps | MALLOC_CAP_DEFAULT);
}
return result;
}
void* memory_calloc_with_policy(size_t count, size_t size, const struct MemoryPolicy* policy) {
uint32_t required_caps = toHeapCaps(policy->required);
uint32_t desired_caps = toHeapCaps(policy->desired);
void* ptr;
if (policy->alignment > 0) {
ptr = heap_caps_aligned_calloc(policy->alignment, count, size, required_caps | desired_caps);
if (ptr == nullptr && desired_caps != 0) {
ptr = heap_caps_aligned_calloc(policy->alignment, count, size, required_caps | MALLOC_CAP_DEFAULT);
}
} else {
ptr = heap_caps_calloc(count, size, required_caps | desired_caps);
if (ptr == nullptr && desired_caps != 0) {
ptr = heap_caps_calloc(count, size, required_caps | MALLOC_CAP_DEFAULT);
}
}
return ptr;
}
void memory_free(void* ptr) {
heap_caps_free(ptr);
}
} // extern "C"
#endif // ESP_PLATFORM