10#if EFI_PROD_CODE || EFI_SIMULATOR
12#ifndef MCU_HAS_CCM_RAM
13 #define MCU_HAS_CCM_RAM FALSE
17 #define LUA_EXTRA_HEAP 0
20#if (LUA_EXTRA_HEAP > 0)
22 #ifdef EFI_HAS_EXT_SDRAM
33 uint8_t* m_buffer =
nullptr;
35 void* alloc(
size_t n) {
36 if (m_buffer && m_size) {
37 return chHeapAlloc(&m_heap, n);
43 void free(
void* obj) {
48 template<
size_t TSize>
49 Heap(uint8_t (&
buffer)[TSize])
60 criticalAssertVoid(used() == 0,
"Too late to init Lua heap: already in use");
78 size_t lagestFree = 0;
79 chHeapStatus(&m_heap, &heapFree, &lagestFree);
81 return m_size - heapFree;
86 if (m_buffer && m_size) {
87 chHeapObjectInit(&m_heap, m_buffer, m_size);
94#if (LUA_EXTRA_HEAP > 0)
114#if !defined(EFI_IS_F42x)
121 extern uint8_t __ram3_base__[];
122 extern uint8_t __ram3_end__[];
130 extern uint8_t __heap_ccm_base__[];
131 extern uint8_t __heap_ccm_end__[];
132 luaCcmHeap.init(__heap_ccm_base__, __heap_ccm_end__ - __heap_ccm_base__);
142 size_t oldSize = chHeapGetSize(optr);
149 void *nptr =
nullptr;
153#if (LUA_EXTRA_HEAP > 0)
154 if (nptr ==
nullptr) {
159 if (nptr ==
nullptr) {
164 if (nptr ==
nullptr) {
171 if (nptr ==
nullptr) {
172 nptr = chHeapAlloc(NULL, nsize);
178 newSize = chHeapGetSize(nptr);
179 chDbgAssert(newSize >= nsize,
"Lua allocator returned smaller block than requested");
184 chDbgAssert(osize <= chHeapGetSize(optr),
"Lua lost track of allocated mem");
186 size_t oldSize = chHeapGetSize(optr);
187 if (nptr !=
nullptr) {
189 size_t copySize = (oldSize < newSize) ? oldSize : newSize;
190 memcpy(nptr, optr, copySize);
195 if (nsize <= oldSize) {
216#if (LUA_EXTRA_HEAP > 0)
229#if CH_CFG_MEMCORE_SIZE == 0
237 #if CH_CFG_MEMCORE_SIZE == 0
244 #if (LUA_EXTRA_HEAP > 0)
257 float pct = 100.0f * memoryUsed / totalHeapSize;
258 efiPrintf(
"Lua total heap(s) usage: %d / %d bytes = %.1f%%", memoryUsed, totalHeapSize, pct);
260 efiPrintf(
"No heap available for Lua");
263 #if (LUA_EXTRA_HEAP > 0)
273 size_t chHeapFree = 0;
274 chHeapStatus(NULL, &chHeapFree, NULL);
276 size_t chMemCoreUsed = chMemTotal - chCoreGetStatusX() - chHeapFree;
277 efiPrintf(
"Common ChibiOS heap: %d bytes free", chHeapFree);
278 efiPrintf(
"ChibiOS memcore usage: %d / %d", chMemCoreUsed, chMemTotal);
283void*
luaHeapAlloc(
void* ,
void* ptr,
size_t ,
size_t nsize) {
293 return realloc(ptr, nsize);
TunerStudioOutputChannels outputChannels
static EngineAccessor engine
static constexpr engine_configuration_s * engineConfiguration
static Heap luaExtraHeap(luaExtraHeapArea)
CH_HEAP_AREA(luaExtraHeapArea, LUA_EXTRA_HEAP) SDRAM_OPTIONAL
static size_t luaMemoryUsed
void * luaHeapAlloc(void *, void *optr, size_t osize, size_t nsize)
static Heap luaOptionalHeap
static BigBufferHandle buffer