rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Functions
lua_heap.h File Reference

Functions

void luaHeapInit ()
 
void * luaHeapAlloc (void *, void *optr, size_t osize, size_t nsize)
 
size_t luaHeapUsed ()
 
void luaHeapReset ()
 
void luaHeapPrintInfo ()
 

Function Documentation

◆ luaHeapAlloc()

void * luaHeapAlloc ( void *  ,
void *  optr,
size_t  osize,
size_t  nsize 
)

Definition at line 138 of file lua_heap.cpp.

138 {
139 void *nptr = nullptr;
140
141 if (nsize) {
142 // [tag:multi-step-lua-alloc]
143 // First try dedicated Lua heap(s)
144 #if (LUA_EXTRA_HEAP > 0)
145 if (nptr == nullptr) {
146 nptr = luaExtraHeap.alloc(nsize);
147 }
148 #endif
149 #if defined(STM32F4)
150 if (nptr == nullptr) {
151 nptr = luaOptionalHeap.alloc(nsize);
152 }
153 #endif
154 #if MCU_HAS_CCM_RAM
155 if (nptr == nullptr) {
156 nptr = luaCcmHeap.alloc(nsize);
157 }
158 #endif
159
160 // [tag:multi-step-lua-alloc]
161 // then try ChibiOS default heap
162 if (nptr == nullptr) {
163 nptr = chHeapAlloc(NULL, nsize);
164 }
165 }
166
167 if (nptr) {
168 luaMemoryUsed += nsize;
169 }
170
171 if (optr) {
172 // An old pointer was passed in, copy the old data in, then free
173 if (nptr != nullptr) {
174 memcpy(nptr, optr, chHeapGetSize(optr) > nsize ? nsize : chHeapGetSize(optr));
175 }
176 // chHeapFree will find correct heap to return memory to
177 chHeapFree(optr);
178 luaMemoryUsed -= osize;
179 }
180
181 if (engineConfiguration->debugMode == DBG_LUA) {
183 }
184
185 return nptr;
186}
TunerStudioOutputChannels outputChannels
Definition engine.h:109
static EngineAccessor engine
Definition engine.h:413
static constexpr engine_configuration_s * engineConfiguration
static Heap luaExtraHeap(luaExtraHeapArea)
size_t luaHeapUsed()
Definition lua_heap.cpp:188
static size_t luaMemoryUsed
Definition lua_heap.cpp:136
static Heap luaCcmHeap
Definition lua_heap.cpp:106
static Heap luaOptionalHeap
Definition lua_heap.cpp:101

Referenced by runScript(), and testLuaExecString().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ luaHeapInit()

void luaHeapInit ( )

Definition at line 109 of file lua_heap.cpp.

110{
111 // stm32f4xx can have optional ram3 region
112#if defined(STM32F4)
113 // Some boads can be equiped with STM32F42x only, in this case we allow linker to take care of ram3
114#if !defined(EFI_IS_F42x)
115 // cute hack: let's check at runtime if you are a lucky owner of board with extra RAM and use that extra RAM for extra Lua
116 // we need this on microRusEFI for sure
117 // definitely should NOT have this on Proteus
118 // on Hellen a bit of open question what's the best track
119 if (isStm32F42x()) {
120 // This is safe to use section base and end as we define ram3 for all F4 chips
121 extern uint8_t __ram3_base__[];
122 extern uint8_t __ram3_end__[];
123 luaOptionalHeap.init(__ram3_base__, __ram3_end__ - __ram3_base__);
124 }
125#endif // !EFI_IS_F42x
126#endif // STM32F4
127
128 // stm32f4xx have CCM memory that may have some leftovers
129#if MCU_HAS_CCM_RAM
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__);
133#endif
134}
bool isStm32F42x(void)

Referenced by startLua().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ luaHeapPrintInfo()

void luaHeapPrintInfo ( )

Definition at line 213 of file lua_heap.cpp.

213 {
214 size_t chMemTotal =
215 /* Chibios heap size */
216 #if CH_CFG_MEMCORE_SIZE == 0
218 #else
219 CH_CFG_MEMCORE_SIZE;
220 #endif
221 auto totalHeapSize =
222 chMemTotal +
223 #if (LUA_EXTRA_HEAP > 0)
224 luaExtraHeap.size() +
225 #endif
226 #if defined(STM32F4)
227 luaOptionalHeap.size() +
228 #endif
229 #if MCU_HAS_CCM_RAM
230 luaCcmHeap.size() +
231 #endif
232 0;
233
234 if (totalHeapSize) {
235 auto memoryUsed = luaHeapUsed();
236 float pct = 100.0f * memoryUsed / totalHeapSize;
237 efiPrintf("Lua total heap(s) usage: %d / %d bytes = %.1f%%", memoryUsed, totalHeapSize, pct);
238 } else {
239 efiPrintf("No heap available for Lua");
240 }
241
242 #if (LUA_EXTRA_HEAP > 0)
243 efiPrintf("Lua extra heap usage: %d / %d", luaExtraHeap.used(), luaExtraHeap.size());
244 #endif
245 #if defined(STM32F4)
246 efiPrintf("Lua optional heap usage: %d / %d", luaOptionalHeap.used(), luaOptionalHeap.size());
247 #endif
248 #if MCU_HAS_CCM_RAM
249 efiPrintf("Lua CCM heap usage: %d / %d", luaCcmHeap.used(), luaCcmHeap.size());
250 #endif
251
252 size_t chHeapFree = 0;
253 chHeapStatus(NULL, &chHeapFree, NULL);
254 /* total available for ChibiOS minus left free, plus free in Chibios Heap */
255 size_t chMemCoreUsed = chMemTotal - chCoreGetStatusX() - chHeapFree;
256 efiPrintf("Common ChibiOS heap: %d bytes free", chHeapFree);
257 efiPrintf("ChibiOS memcore usage: %d / %d", chMemCoreUsed, chMemTotal);
258}
uint8_t __heap_end__[]
uint8_t __heap_base__[]

Referenced by loadScript(), and startLua().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ luaHeapReset()

void luaHeapReset ( )

Definition at line 193 of file lua_heap.cpp.

194{
195#if (LUA_EXTRA_HEAP > 0)
196 luaExtraHeap.reset();
197#endif
198#if defined(STM32F4)
199 luaOptionalHeap.reset();
200#endif
201#if MCU_HAS_CCM_RAM
202 luaCcmHeap.reset();
203#endif
204
205 luaMemoryUsed = 0;
206}
Here is the call graph for this function:

◆ luaHeapUsed()

size_t luaHeapUsed ( )

Definition at line 188 of file lua_heap.cpp.

189{
190 return luaMemoryUsed;
191}

Referenced by luaHeapAlloc(), and luaHeapPrintInfo().

Here is the caller graph for this function:

Go to the source code of this file.