Static Memory Allocation

Aus ProjectWiki
Wechseln zu:Navigation, Suche

Index


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 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

Setup

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 2.0.8.1). 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).


Managing memory

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


Interface description

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

Define this constant (value doesn't matter) to keep track of the remaining available memory (see below).


Dim Available_memory As Word/Dword

If the constant Os_task_dbg_metrics is defined, the remaining memory is available to the application via this variable.


#if Os_big_ram = False
   Function Malloc(byval Size As Word) As Word
#else
   Function Malloc(byval Size As Dword) As Dword
#endif

Allocate a block of memory. Cannot be freed. Returns 0 if not enough free memory left to serve the request.

Samples

WIP


Download

Bascom OOP & Precompiler Download