ChronOS Memory Management: Unterschied zwischen den Versionen

Aus ProjectWiki
Wechseln zu:Navigation, Suche
(Created page with "== Segments == The remaining free memory (starting at the end of the global variables and ending at Bascom's Frame start) is managed by the OS using segments. A segment is a line...")
 
K (Free RAM start address)
 
(4 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
== Segments ==
+
== Common Settings ==
The remaining free memory (starting at the end of the global variables and ending at Bascom's Frame start) is managed by the OS using segments. A segment is a linear block of memory of any size fitting the available memory. They can be created and killed during runtime. On creation, the memory manager searches a free block big enough to hold the new segment.
 
  
Because this happens in a random order, external fragmentation could occur. This means, that there are small blocks of free memory between the segments, not big enough to hold new segments. In the end, a new segment cannot be created, although the free memory in addition is big enough.
+
=== Allocation Algorithm ===
To avoid this, the memory manager cleans up the memory by moving all segments together.
 
Addresses, which are pointing in a moved segment, become invalid. To take care of this, each pointer has to be registered in the OS and will then be adjusted if a segment is moved. However, this cannot be done with the task segments, because the pointers in the stacks are not registered. So these segments are static and are not moveable.
 
  
== Pipes ==
+
You can choose between two allocation algorithms.
Pipes are simple LIFO buffers, so data will be written on the top of the stack and read from the bottom.
+
System default is the static algorithm.
They are available for byte and word values. Each pipe is stored in an own segment. If you store a pointer in the pipe, it has to be registered.
+
<pre>
 +
Const Os_malloc_policy = Os_malloc_policy_static
 +
</pre>
 +
To use the dynamic TLSF algorithm:
 +
<pre>
 +
Const Os_malloc_policy = Os_malloc_policy_tlsf
 +
</pre>
 +
 
 +
=== Free RAM start address ===
 +
Because the start address cannot be calculated with some constants, you have to set this yourself. To do this, check "Show internal variables" in Options->Compiler->Output, look into the compiler report and search for the address of the first byte not occupied by a variable.
 +
Obviously this is a very important step, otherwise the values of global variables would be overwritten.
 +
<pre>
 +
Const Free_ram_start = [address]
 +
</pre>
 +
 
 +
=== Optional: Free RAM End ===
 +
It is also possible to set the end address of the free memory area yourself, system default is below the stack area.
 +
<pre>
 +
Const Free_ram_end = [address]
 +
</pre>
 +
 
 +
=== Compiler Report Example ===
 +
Here is a (shortened) example of a compiler report, and where the address could be found.
 +
In this example, the RAM start address should be set to at least 169 dec.
 +
<pre>
 +
Report      : chronos
 +
Date        : 05-20-2013
 +
Time        : 01:54:06
 +
 
 +
Compiler    : BASCOM-AVR LIBRARY V 2.0.7.6
 +
Processor    : M32
 +
SRAM        : 800 hex
 +
EEPROM      : 400 hex
 +
ROMSIZE      : 8000 hex
 +
 
 +
...
 +
 
 +
--------------------------------------------------------------------------------
 +
Variable                        Type            Address(hex)  Address(dec)
 +
--------------------------------------------------------------------------------
 +
 
 +
...
 +
 
 +
OS_CRITICAL_NESTING_LEVEL        Byte              0060          96
 +
OS_EVENT_TASK                    Word              0061          97
 +
MALLOC_FREE_POINTER              Word              0063          99
 +
OS_SCHED_RUNNING_WEIGHT          Byte              0065          101
 +
OS_SCHED_LIST_HEAD              Word              0066          102
 +
OS_SCHED_PREEMPTED_TASK          Word              0068          104
 +
OS_TASK_ACTIVE                  Word              006A          106
 +
OS_TASK_TEMPBYTE_ISR            Byte              006C          108
 +
OS_TASK_TEMPWORD_ISR            Word              006D          109
 +
OS_TASK_TEMPWORD2_ISR            Word              006F          111
 +
OS_TIMER_SYSTEMTIME              Dword            0071          113
 +
OS_TIMER_LISTHEAD                Word              0075          117
 +
OS_TIMER_NEXT_EVENT              Word              0077          119
 +
OS_TIMER_ELAPSED_TIME            Word              0079          121
 +
OS_TIMER_ENABLE_KERNEL          Byte              007B          123
 +
OS_INT_TABLE                    Word (20)        007C          124
 +
TASKOBJECT                      Word              00A4          164
 +
TASK_1_COUNTER                  Byte              00A6          166
 +
TASK_2_COUNTER                  Byte              00A7          167
 +
___LCDCOUNTER                    Byte              00A8          168    <---
 +
 
 +
--------------------------------------------------------------------------------
 +
Constant                        Value
 +
--------------------------------------------------------------------------------
 +
SREG                            &H3F
 +
SPH                              &H3E
 +
SPL                              &H3D
 +
 
 +
...
 +
</pre>
 +
 
 +
 
 +
== Programming Interface ==
 +
 
 +
=== Malloc: Request memory from free memory pool ===
 +
<pre>
 +
Dim Object As Word
 +
Object = Malloc(Byval Size As Word)
 +
</pre>
 +
If there is a risk running out of memory (always be aware of that when using a dynamic allocation algorithm), a check if Malloc() returns 0 and a proper error handling is important.
 +
 
 +
=== Free: Return memory to the pool ===
 +
<pre>
 +
Free Object
 +
</pre>
 +
 
 +
[[Category:ChronOS]]

Aktuelle Version vom 21. Mai 2013, 23:21 Uhr

Common Settings

Allocation Algorithm

You can choose between two allocation algorithms. System default is the static algorithm.

Const Os_malloc_policy = Os_malloc_policy_static

To use the dynamic TLSF algorithm:

Const Os_malloc_policy = Os_malloc_policy_tlsf

Free RAM start address

Because the start address cannot be calculated with some constants, you have to set this yourself. To do this, check "Show internal variables" in Options->Compiler->Output, look into the compiler report and search for the address of the first byte not occupied by a variable. Obviously this is a very important step, otherwise the values of global variables would be overwritten.

Const Free_ram_start = [address]

Optional: Free RAM End

It is also possible to set the end address of the free memory area yourself, system default is below the stack area.

Const Free_ram_end = [address]

Compiler Report Example

Here is a (shortened) example of a compiler report, and where the address could be found. In this example, the RAM start address should be set to at least 169 dec.

Report       : chronos
Date         : 05-20-2013
Time         : 01:54:06

Compiler     : BASCOM-AVR LIBRARY V 2.0.7.6
Processor    : M32
SRAM         : 800 hex
EEPROM       : 400 hex
ROMSIZE      : 8000 hex

...

--------------------------------------------------------------------------------
Variable                         Type            Address(hex)   Address(dec)
--------------------------------------------------------------------------------

...

OS_CRITICAL_NESTING_LEVEL        Byte              0060          96
OS_EVENT_TASK                    Word              0061          97
MALLOC_FREE_POINTER              Word              0063          99
OS_SCHED_RUNNING_WEIGHT          Byte              0065          101
OS_SCHED_LIST_HEAD               Word              0066          102
OS_SCHED_PREEMPTED_TASK          Word              0068          104
OS_TASK_ACTIVE                   Word              006A          106
OS_TASK_TEMPBYTE_ISR             Byte              006C          108
OS_TASK_TEMPWORD_ISR             Word              006D          109
OS_TASK_TEMPWORD2_ISR            Word              006F          111
OS_TIMER_SYSTEMTIME              Dword             0071          113
OS_TIMER_LISTHEAD                Word              0075          117
OS_TIMER_NEXT_EVENT              Word              0077          119
OS_TIMER_ELAPSED_TIME            Word              0079          121
OS_TIMER_ENABLE_KERNEL           Byte              007B          123
OS_INT_TABLE                     Word (20)         007C          124
TASKOBJECT                       Word              00A4          164
TASK_1_COUNTER                   Byte              00A6          166
TASK_2_COUNTER                   Byte              00A7          167
___LCDCOUNTER                    Byte              00A8          168     <---

--------------------------------------------------------------------------------
Constant                         Value
--------------------------------------------------------------------------------
SREG                             &H3F
SPH                              &H3E
SPL                              &H3D

...


Programming Interface

Malloc: Request memory from free memory pool

Dim Object As Word
Object = Malloc(Byval Size As Word)

If there is a risk running out of memory (always be aware of that when using a dynamic allocation algorithm), a check if Malloc() returns 0 and a proper error handling is important.

Free: Return memory to the pool

Free Object