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

Functions

 CH_HEAP_AREA (luaExtraHeapArea, LUA_EXTRA_HEAP) SDRAM_OPTIONAL
 
static Heap luaExtraHeap (luaExtraHeapArea)
 
void luaHeapInit ()
 
void * luaHeapAlloc (void *, void *optr, size_t osize, size_t nsize)
 
size_t luaHeapUsed ()
 
void luaHeapReset ()
 
void luaHeapPrintInfo ()
 

Variables

static Heap luaOptionalHeap
 
static Heap luaCcmHeap
 
static size_t luaMemoryUsed = 0
 
uint8_t __heap_base__ []
 
uint8_t __heap_end__ []
 

Function Documentation

◆ CH_HEAP_AREA()

CH_HEAP_AREA ( luaExtraHeapArea  ,
LUA_EXTRA_HEAP   
)
Here is the call graph for this function:

◆ luaExtraHeap()

static Heap luaExtraHeap ( luaExtraHeapArea  )
static

Referenced by luaHeapAlloc(), luaHeapPrintInfo(), and luaHeapReset().

Here is the caller graph for this function:

◆ luaHeapAlloc()

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

Definition at line 138 of file lua_heap.cpp.

138 {
139 // If new size is zero, this is a free. Do not allocate.
140 if (nsize == 0) {
141 if (optr) {
142 size_t oldSize = chHeapGetSize(optr);
143 chHeapFree(optr);
144 luaMemoryUsed -= oldSize;
145 }
146 return nullptr;
147 }
148
149 void *nptr = nullptr;
150
151 // [tag:multi-step-lua-alloc]
152 // First try dedicated Lua heap(s)
153#if (LUA_EXTRA_HEAP > 0)
154 if (nptr == nullptr) {
155 nptr = luaExtraHeap.alloc(nsize);
156 }
157#endif
158#if defined(STM32F4)
159 if (nptr == nullptr) {
160 nptr = luaOptionalHeap.alloc(nsize);
161 }
162#endif
163#if MCU_HAS_CCM_RAM
164 if (nptr == nullptr) {
165 nptr = luaCcmHeap.alloc(nsize);
166 }
167#endif
168
169 // [tag:multi-step-lua-alloc]
170 // then try ChibiOS default heap
171 if (nptr == nullptr) {
172 nptr = chHeapAlloc(NULL, nsize);
173 }
174
175 size_t newSize = 0;
176 if (nptr) {
177 // Account for newly allocated memory by actual block size
178 newSize = chHeapGetSize(nptr);
179 chDbgAssert(newSize >= nsize, "Lua allocator returned smaller block than requested");
180 luaMemoryUsed += newSize;
181 }
182
183 if (optr) {
184 chDbgAssert(osize <= chHeapGetSize(optr), "Lua lost track of allocated mem");
185 // An old pointer was passed in. Only free it if we successfully allocated a new one.
186 size_t oldSize = chHeapGetSize(optr);
187 if (nptr != nullptr) {
188 // Copy the minimum of old and new block sizes
189 size_t copySize = (oldSize < newSize) ? oldSize : newSize;
190 memcpy(nptr, optr, copySize);
191 // chHeapFree will find correct heap to return memory to
192 chHeapFree(optr);
193 luaMemoryUsed -= oldSize;
194 } else {
195 if (nsize <= oldSize) {
196 return optr; // shrink must not fail per Lua's assumption
197 }
198 return nullptr; // grow failed
199 }
200 }
201
202 if (engineConfiguration->debugMode == DBG_LUA) {
204 }
205
206 return nptr;
207}
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:209
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 234 of file lua_heap.cpp.

234 {
235 size_t chMemTotal =
236 /* Chibios heap size */
237 #if CH_CFG_MEMCORE_SIZE == 0
239 #else
240 CH_CFG_MEMCORE_SIZE;
241 #endif
242 auto totalHeapSize =
243 chMemTotal +
244 #if (LUA_EXTRA_HEAP > 0)
245 luaExtraHeap.size() +
246 #endif
247 #if defined(STM32F4)
248 luaOptionalHeap.size() +
249 #endif
250 #if MCU_HAS_CCM_RAM
251 luaCcmHeap.size() +
252 #endif
253 0;
254
255 if (totalHeapSize) {
256 auto memoryUsed = luaHeapUsed();
257 float pct = 100.0f * memoryUsed / totalHeapSize;
258 efiPrintf("Lua total heap(s) usage: %d / %d bytes = %.1f%%", memoryUsed, totalHeapSize, pct);
259 } else {
260 efiPrintf("No heap available for Lua");
261 }
262
263 #if (LUA_EXTRA_HEAP > 0)
264 efiPrintf("Lua extra heap usage: %d / %d", luaExtraHeap.used(), luaExtraHeap.size());
265 #endif
266 #if defined(STM32F4)
267 efiPrintf("Lua optional heap usage: %d / %d", luaOptionalHeap.used(), luaOptionalHeap.size());
268 #endif
269 #if MCU_HAS_CCM_RAM
270 efiPrintf("Lua CCM heap usage: %d / %d", luaCcmHeap.used(), luaCcmHeap.size());
271 #endif
272
273 size_t chHeapFree = 0;
274 chHeapStatus(NULL, &chHeapFree, NULL);
275 /* total available for ChibiOS minus left free, plus free in Chibios Heap */
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);
279}
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 214 of file lua_heap.cpp.

215{
216#if (LUA_EXTRA_HEAP > 0)
217 luaExtraHeap.reset();
218#endif
219#if defined(STM32F4)
220 luaOptionalHeap.reset();
221#endif
222#if MCU_HAS_CCM_RAM
223 luaCcmHeap.reset();
224#endif
225
226 luaMemoryUsed = 0;
227}
Here is the call graph for this function:

◆ luaHeapUsed()

size_t luaHeapUsed ( )

Definition at line 209 of file lua_heap.cpp.

210{
211 return luaMemoryUsed;
212}

Referenced by luaHeapAlloc(), and luaHeapPrintInfo().

Here is the caller graph for this function:

Variable Documentation

◆ __heap_base__

uint8_t __heap_base__[]
extern

Referenced by luaHeapPrintInfo().

◆ __heap_end__

uint8_t __heap_end__[]
extern

Referenced by luaHeapPrintInfo().

◆ luaCcmHeap

Heap luaCcmHeap
static

Definition at line 106 of file lua_heap.cpp.

Referenced by luaHeapAlloc(), luaHeapInit(), luaHeapPrintInfo(), and luaHeapReset().

◆ luaMemoryUsed

size_t luaMemoryUsed = 0
static

Definition at line 136 of file lua_heap.cpp.

Referenced by luaHeapAlloc(), luaHeapReset(), and luaHeapUsed().

◆ luaOptionalHeap

Heap luaOptionalHeap
static

Definition at line 101 of file lua_heap.cpp.

Referenced by luaHeapAlloc(), luaHeapInit(), luaHeapPrintInfo(), and luaHeapReset().

Go to the source code of this file.