Memory Management

See Memory Management. The managment routine allows you to dynamically split SRAM into blocks of different sizes. Luna calls such a block Memory Block and is an Object. Memory Management is dynamic and automatically cleans up memory Garbage Collection. Memory management comes in whenever you use strings or string functions or when memory is allocated. Memory management requires at least 64 Byte of available memory. Do not use strings or string functions if this is not the case.

Typical Memory Allocation in Luna

Memory Block Structure

Every memory block requires a 5 Byte to manage the block (Header).

Name Value Typ Description
Magic 0xAA byte ID
Length Number word Number of data byte
VarAddr Address word Address of linked variable, or Nil
Data Your data

If memory is very limited then you can manage it yourself. Use direct access commands to the memory object sram.

Example

Assuming that you have dimensioned some strings:

dim date,time as string

When allocating a string or string function, the result will be stored in a memory block and its starting address (Pointer) is returned to the respective variable. The Pointer points to the data starting address and not the block address to avoid havint to take the header size into account. This would be inefficient. Lets also assume that you RAM starts at Address 100 (decimal). The variables date and time are 16 Bit pointer and require 2 Byte each:

Memory →
100 101 102 103
Variable date Variable time
0 (nil) 0 (nil)

The available memory starts right behind these variables and ends at stack end. 1). As long as you have not assigned data to the string, it will point to nil. Allocating data to the string variable will create a memory block in your RAM:

dim date,time as string
date="13.11.1981"

The memory now looks like this:

Memory →
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
109 0 (nil) 0xAA 11 100 10 '1' '3' '.' '1' '1' '.' '1' '9' '8' '1'
Variable date Variable tme Header Memory Block Data Memory Block

As you can see zeit is not allocated, date now has the starting address of your data in the memory block. The string data is stored as a Pascal-String to allow string functions to read the string length. This is important because only the background memory management may access the memory block's header data. You will also notice that only the variable's address is stored in the memory block header. This is important because when the memory block is released, (defragmentation/compression), all addresses of the following blocks will change (they will move up). Memory management will now adjust all addresses of variables that have a pointer to a memory block.

Pro and Con

Without memory management, the programmer himself must carefully look after his memeory allocation. This would also include the disadvantage that all strings would have to have a fixed length (statis Strings). If a string mit sometime grow upto 60 Byte, then you would have to reserve 60 Byte, even if most of the time the string might only need 10 Byte. This is advantageous if you only have a few short string operations and you have limited memory available. Memory management ist advantageous is more efficient and even saves memory inspite of the header dta required if you have to manage many strings an blocks. It is also easier to allocate dynamic data. If memory is limited (less than 128 Byte), it will become inefficient due to the header data. Luna tries to combine both by allowing you to use Strukturen to allocate static strings or data blocks. If you limit your usage to in- / output and avoid string functions like Left(), Right() or Mid() etc., or define your own memory structure, both types of memory management will be combined.

1) The stack grows from the RAM end towards RAM start. The definition avr.Stack defines the number of bytes reservedfor the stack