Static Memory Allocation: Unterschied zwischen den Versionen
(Die Seite wurde neu angelegt: „== Overview == In most applications, dynamic memory allocation is not needed, instead, it is allocated one time (either at the compile-stage or during the boot…“)
Version vom 28. Juni 2019, 01:42 Uhr
In most applications, dynamic memory allocation is not needed, instead, it is allocated one time (either at the compile-stage or during the boot process) and unchanged during run-time. Where dynamic memory is not needed, but to maintain compatibility, this static allocation library is used.
The remaining memory from the end of global declared variables to the start of the stack spaces and also external memory (Xram), if available, is managed by the library (configurable).
How to use it
Create an inc subfolder to your project and copy the files os_common.inc, os_memory.inc and os_malloc_static.inc into the folder.
Include the library:
' any const option must be placed above the file include Const Os_mem_free_start = ### ' mandatory $include "inc\os_malloc_static.inc"
The start adress of the free memory area can not be determined automatically (Bascom-AVR 220.127.116.11). It can be found by examining the compile report (with setting Options->Compiler->Output->Show internal variables enabled), the last variable (with the highest adress) plus the byte size is the adress of the first unused byte (refer to TLSF Dynamic Memory Allocation).
The library maintains a pointer to the first free memory position, memory requests are served in a consecutive order (incrementing the pointer by the requested size). If the request can't be fullfilled, the value 0 is returned. The application has to check for that return value and react accordingly, it is also responsible not to write outside of the requested block of memory. Allocated memory is NOT cleared (set to 0) by the library. Releasing memory for use in further requests is not possible.
With no external memory, 16 bit adress width (Word variable type) is used, with external memory 24 bits are used (DWord type).
Dim Myblockofmemory As [Word/Dword] Myblockofmemory = Malloc([Size]) If Myblockofmemory = 0 Then ' memory allocation error handling (not enough free memory) End If
The following constants have to be defined prior to including the library. The values shown here are default values if the constant is not defined by the user.
Const Os_mem_free_start = [&H200/with XRAM: &H2000]
MANDATORY parameter, as described in Setup. For reasons, it has also default values.
Const Os_task_dbg_metrics = 1
Dim Available_memory As Word
#if Os_big_ram = False Function Malloc(byval Size As Word) As Word #else Function Malloc(byval Size As Dword) As Dword #endif
Version 0.1 (##.##.2019)
- Initial release.