TLSF Dynamic Memory Allocation: Unterschied zwischen den Versionen

Aus ProjectWiki
Wechseln zu:Navigation, Suche
K
K (Index)
 
(6 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
 +
== Index ==
 +
* [OOP 1a/3] Dynamic Memory Allocation using TLSF
 +
* [[Static Memory Allocation|''[OOP 1b/3] Static Memory Allocation'']]
 +
* [[Object Oriented Programming in Bascom|''[OOP 2/3] Object Oriented Programming in Bascom'']]
 +
* [[Bascom Precompiler|''[OOP 3/3] Bascom-Precompiler'']]
 +
 +
 +
* [https://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&p=83584#83584 Forum thread]
 +
 
== Overview ==
 
== Overview ==
 
[http://www.gii.upv.es/tlsf/ TLSF] (Two-Level Segregated Fit) is a dynamic memory allocator suited for real-time systems.
 
[http://www.gii.upv.es/tlsf/ TLSF] (Two-Level Segregated Fit) is a dynamic memory allocator suited for real-time systems.
Zeile 15: Zeile 24:
 
Include the library:
 
Include the library:
 
<pre>' any const option must be placed above the file include
 
<pre>' any const option must be placed above the file include
Const Os_mem_free_start = ###                        ' mandatory
+
Const Os_mem_free_size = ###                        ' mandatory
 
$include "inc\os_malloc_tlsf.inc"</pre>
 
$include "inc\os_malloc_tlsf.inc"</pre>
  
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.
+
The start adress of the free memory area can not be determined automatically (Bascom-AVR 2.0.8.6). It can be found by examining the compile report, fill in the value from "Space Left".
  
 
==== Compiler report example ====
 
==== 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.
+
Here is a (shortened) example of a compiler report, and where the remaining free memory size could be found. In this example, there are 7477 bytes left.
 
<pre>
 
<pre>
Report      : chronos
+
Report      : tlsf-xmem-sample
Date        : 05-20-2013
+
Date        : 11-03-2023
Time        : 01:54:06
+
Time        : 02:51:23
 
 
Compiler    : BASCOM-AVR LIBRARY V 2.0.7.6
 
Processor    : M32
 
SRAM        : 800 hex
 
EEPROM      : 400 hex
 
ROMSIZE      : 8000 hex
 
  
...
+
Compiler    : BASCOM-AVR LIBRARY V 2.0.8.6
 +
Processor    : XM128A1U
 +
SRAM        : 2000 hex
 +
EEPROM      : 800 hex
 +
ROMSIZE      : 22000 hex
  
--------------------------------------------------------------------------------
+
ROMIMAGE    : 324A hex  -> Will fit into ROM
Variable                        Type            Address(hex)   Address(dec)
+
ROMIMAGE    :  12874 dec
--------------------------------------------------------------------------------
+
FLASH USED  :  9  %
 +
UART1        :   0.01 % ERROR
 +
XTAL        : 32000000 Hz
  
...
+
XRAM        : 800000 hex
 +
Stack start  : 3FFF hex
 +
Stack size  : 80 hex
 +
S-Stacksize  : 80 hex
 +
S-Stackstart : 3F80 hex
 +
Framesize    : 80 hex
 +
Framestart  : 3E80 hex
 +
Space left  :  7477  dec          <------
  
OS_CRITICAL_NESTING_LEVEL        Byte              0060          96
+
LCD DB7      : PORTB.7
OS_EVENT_TASK                    Word              0061          97
+
LCD DB6      : PORTB.6
MALLOC_FREE_POINTER              Word              0063          99
+
LCD DB5      : PORTB.5
OS_SCHED_RUNNING_WEIGHT          Byte              0065          101
+
LCD DB4      : PORTB.4
OS_SCHED_LIST_HEAD              Word              0066          102
+
LCD E        : PORTB.3
OS_SCHED_PREEMPTED_TASK          Word              0068          104
+
LCD RS      : PORTB.2
OS_TASK_ACTIVE                  Word              006A          106
+
LCD mode     :  4  bit
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
+
Variable                         Type            Address(hex)  Address(dec)
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
SREG                            &H3F
+
DACA0                            Word              0318          792
SPH                              &H3E
 
SPL                              &H3D
 
 
 
 
...</pre>
 
...</pre>
  
Zeile 85: Zeile 85:
 
When the block is no longer needed, it must be returned to the free memory pool by calling:
 
When the block is no longer needed, it must be returned to the free memory pool by calling:
 
<pre>Free Myblockofmemory</pre>
 
<pre>Free Myblockofmemory</pre>
 
  
 
== Interface description ==
 
== 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.
 
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.
  
 
+
<pre>Const Os_mem_free_size = [Compile Report->Space Left]</pre>
 
<pre>Const Os_mem_free_start = [&H200/with XRAM: &H2000]</pre>
 
<pre>Const Os_mem_free_start = [&H200/with XRAM: &H2000]</pre>
 
MANDATORY parameter, as described in [[#Setup|Setup]]. For reasons, it has also default values.
 
MANDATORY parameter, as described in [[#Setup|Setup]]. For reasons, it has also default values.
 +
Set either one, the other will be calculated. Setting ''Os_mem_free_size'' is the recommended method.
  
  
Zeile 165: Zeile 165:
 
Initializes the allocator. This is done when the library is included, in standard applications, it is not needed to call. However, to reset the allocator, use ''Os_malloc_clear'' before calling ''Os_malloc_init''.
 
Initializes the allocator. This is done when the library is included, in standard applications, it is not needed to call. However, to reset the allocator, use ''Os_malloc_clear'' before calling ''Os_malloc_init''.
  
 +
== Samples ==
 +
=== M32 internal SRAM (runs in Simulator) ===
 +
<pre>$regfile = "m32def.dat"
 +
$crystal = 1000000
 +
$hwstack = 32
 +
$swstack = 32
 +
$framesize = 64
 +
$baud = 9600
 +
Config Submode = New
 +
 +
 +
 +
' Set start address of the free memory pool manually (examine compile report, "show internal variables" - setting enabled)
 +
Const Os_mem_size_free = 1810
 +
Const Os_task_dbg_metrics = 1                              ' constant needs to be defined if we want to see the available memory
 +
 +
' include needed libraries
 +
$include "..\..\src\inc\os_common.inc"                      ' per default os_common.inc and os_memory.inc are included by the memory allocator and are expected
 +
$include "..\..\src\inc\os_memory.inc"                      ' in the subdirectory "inc". To accomodate for the different directory structure of the samples,
 +
                                                            ' we need to override the default includes
 +
$include "..\..\src\inc\os_malloc_tlsf.inc"
 +
 +
 +
 +
Dim Memoryblock1 As Word
 +
Dim Memoryblock2 As Word
 +
 +
Print "Free Memory: " ; Available_memory
 +
Memoryblock1 = Malloc(512)
 +
Memoryblock2 = Malloc(45)
 +
Print "adr1: " ; Memoryblock1 ; "  adr2: " ; Memoryblock2
 +
Print "Free Memory: " ; Available_memory
 +
Free Memoryblock1
 +
Free Memoryblock2
 +
Print "Free Memory: " ; Available_memory
 +
 +
End</pre>
 +
 +
Output:
 +
<pre>Free Memory: 1804
 +
adr1: 210  adr2: 726
 +
Free Memory: 1236
 +
Free Memory: 1804</pre>
  
== Samples ==
+
=== ATXMega128A1-Xplained (8Mb ext. SRAM) ===
WIP
+
<pre>$regfile = "xm128a1udef.dat"
 +
$crystal = 32000000
 +
$baud = 115200
 +
$hwstack = 128
 +
$swstack = 128
 +
$framesize = 128
 +
Config Submode = New
 +
 
 +
Config Osc = Disabled , 32mhzosc = Enabled
 +
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1
 +
 
 +
Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
 +
Open "COM1:" For Binary As #1
 +
 
 +
 
 +
 
 +
' =========== XRAM ============
 +
'the XPLAINED has a 64 MBit SDRAM which is 8 MByte, it is connected in 3 port, 4 bit databus mode
 +
'in the PDF of the SDRAM you can see it is connected as 16 Meg x 4. Refreshcount is 4K and the row address is A0-A11, column addressing is A0-A9
 +
$xramsize = 8388608                                      ' 8 MByte
 +
Config Xram = 3port , Sdbus = 4 , Sdcol = 10 , Sdcas = 3 , Sdrow = 12 , Refresh = 500 , Initdelay = 3200 , Modedelay = 2 , Rowcycledelay = 7 , Rowprechargedelay = 7 , Wrdelay = 1 , Esrdelay = 7 , Rowcoldelay = 7 , Modesel3 = Sdram , Adrsize3 = 8m , Baseadr3 = &H0000
 +
'the config above will set the port registers correct. it will also wait for Ebi_cs3_ctrlb.7
 +
 
 +
 
 +
 
 +
' =========== MEMORY ALLOCATOR ============
 +
' Set size of the free memory pool manually (examine compile report, -> "Space Left")
 +
Const Os_mem_size_free = 7476                              ' activated debug info needs an additional byte
 +
 
 +
Const Os_task_dbg_metrics = 1                              ' constant needs to be defined if we want to see the available memory (value doesn't matter)
 +
Const Debug_level_tlsf = 0                                  ' print out additional debug info, possible values: 0 (no info), 1, 2 and 3 (full info)
 +
 
 +
' include needed libraries
 +
$include "..\..\src\inc\os_common.inc"                      ' per default os_common.inc and os_memory.inc are included by the memory allocator and are expected
 +
$include "..\..\src\inc\os_memory.inc"                      ' in the subdirectory "inc". To accomodate for the different directory structure of the samples,
 +
                                                            ' we need to override the default includes
 +
$include "..\..\src\inc\os_malloc_tlsf.inc"
 +
 
 +
 
 +
 
 +
' =========== DEMO ============
 +
Dim Memoryblock1 As Dword
 +
Dim Memoryblock2 As Dword
 +
Dim Memoryblock3 As Dword
 +
Dim Memoryblock4 As Dword
 +
Dim Blocksize As Dword
 +
 
 +
Print "Start,        free memory: " ; Available_memory
 +
 
 +
' allocate some smaller blocks (will be served from internal sram first)
 +
Memoryblock1 = Malloc(123)
 +
Blocksize = Os_malloc_blocksize(memoryblock1)
 +
Print "Alloc 123,    free memory: " ; Available_memory
 +
Print "Address 1: " ; Memoryblock1 ; " Size: " ; Blocksize
 +
 
 +
Memoryblock2 = Malloc(48)
 +
Blocksize = Os_malloc_blocksize(memoryblock2)
 +
Print "Alloc 48,      free memory: " ; Available_memory
 +
Print "Address 2: " ; Memoryblock2 ; " Size: " ; Blocksize
 +
 
 +
' allocate bigger blocks to force allocation from external memory
 +
Memoryblock3 = Malloc(10000)
 +
Blocksize = Os_malloc_blocksize(memoryblock3)
 +
Print "Alloc 10000,  free memory: " ; Available_memory
 +
Print "Address 3: " ; Memoryblock3 ; " Size: " ; Blocksize
 +
 
 +
Memoryblock4 = Malloc(12345)
 +
Blocksize = Os_malloc_blocksize(memoryblock4)
 +
Print "Alloc 12345,  free memory: " ; Available_memory
 +
Print "Address 4: " ; Memoryblock4 ; " Size: " ; Blocksize
 +
 
 +
' free up allocated memory
 +
Free Memoryblock1
 +
Print "Freed block 1, free memory: " ; Available_memory
 +
 
 +
Free Memoryblock2
 +
Print "Freed block 2, free memory: " ; Available_memory
 +
 
 +
Free Memoryblock3
 +
Print "Freed block 3, free memory: " ; Available_memory
 +
 
 +
Free Memoryblock4
 +
Print "Freed block 4, free memory: " ; Available_memory
 +
 
 +
End</pre>
  
 +
Output:
 +
<pre>Start,        free memory: 8396068
 +
Alloc 123,    free memory: 8395932
 +
Address 1: 8531 Size: 128
 +
Alloc 48,      free memory: 8395876
 +
Address 2: 8667 Size: 48
 +
Alloc 10000,  free memory: 8385868
 +
Address 3: 16392 Size: 10000
 +
Alloc 12345,  free memory: 8373512
 +
Address 4: 26400 Size: 12348
 +
Freed block 1, free memory: 8373640
 +
Freed block 2, free memory: 8373704
 +
Freed block 3, free memory: 8383704
 +
Freed block 4, free memory: 8396068</pre>
  
 
== Download ==
 
== Download ==
* [http://www.braunecker.at/downloads/memory_allocators.zip Memory Allocators version 0.1]
 
 
  
== Change log ==
+
* [http://www.braunecker.at/downloads/memory-allocators/memory-allocators-1-0.zip Memory Allocators & Precompiler Package 1.0]
=== Version 0.1 (##.##.2019) ===
 
* Initial release.
 

Aktuelle Version vom 5. November 2023, 16:51 Uhr

Index


Overview

TLSF (Two-Level Segregated Fit) is a dynamic memory allocator suited for real-time systems. It is a good-fit algorithm with a bound response time O(1) for any number of managed memory blocks.

Free blocks of memory are organized in lists each containing blocks of a specific size range. Requested blocks are searched in the list with sizes equal or greater the requested size, remaining memory is split up if neccessary and sorted back into the list. Freed blocks are merged with neighboring free blocks and also put back into the list.

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_tlsf.inc into the folder.

Include the library:

' any const option must be placed above the file include
Const Os_mem_free_size = ###                         ' mandatory
$include "inc\os_malloc_tlsf.inc"

The start adress of the free memory area can not be determined automatically (Bascom-AVR 2.0.8.6). It can be found by examining the compile report, fill in the value from "Space Left".

Compiler report example

Here is a (shortened) example of a compiler report, and where the remaining free memory size could be found. In this example, there are 7477 bytes left.

Report       : tlsf-xmem-sample
Date         : 11-03-2023
Time         : 02:51:23

Compiler     : BASCOM-AVR LIBRARY V 2.0.8.6
Processor    : XM128A1U
SRAM         : 2000 hex
EEPROM       : 800 hex
ROMSIZE      : 22000 hex

ROMIMAGE     : 324A hex  -> Will fit into ROM
ROMIMAGE     :  12874 dec
FLASH USED   :  9  %
UART1        :   0.01 % ERROR
XTAL         : 32000000 Hz

XRAM         : 800000 hex
Stack start  : 3FFF hex
Stack size   : 80 hex
S-Stacksize  : 80 hex
S-Stackstart : 3F80 hex
Framesize    : 80 hex
Framestart   : 3E80 hex
Space left   :  7477  dec          <------

LCD DB7      : PORTB.7
LCD DB6      : PORTB.6
LCD DB5      : PORTB.5
LCD DB4      : PORTB.4
LCD E        : PORTB.3
LCD RS       : PORTB.2
LCD mode     :  4  bit

--------------------------------------------------------------------------------
Variable                         Type            Address(hex)   Address(dec)
--------------------------------------------------------------------------------
DACA0                            Word              0318          792
...

Managing memory

The library works with memory adresses. If a memory request of given size can be served (consecutive block of free memory has been found), the adress of the start of the block is returned. For a size request bigger than what is managed by the algorithm, as fallback a simple search for a block of suitable size through the last list will be performed (O(n)). 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.

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


When the block is no longer needed, it must be returned to the free memory pool by calling:

Free Myblockofmemory

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_size = [Compile Report->Space Left]
Const Os_mem_free_start = [&H200/with XRAM: &H2000]

MANDATORY parameter, as described in Setup. For reasons, it has also default values. Set either one, the other will be calculated. Setting Os_mem_free_size is the recommended method.


Const Os_tlsf_fli_offset = 4

Start of first level block size ranges in powers of 2. Size = 2 ^ (Os_tlsf_fli_offset + 1); with a default value of 4, first level sizes start at 32.


Const Os_tlsf_max_fli = [10/with XRAM: 12]

End of first level block size ranges. Size = 2 ^ Os_tlsf_max_fli; defaults without XRAM to 1024, with XRAM to 4096.


Const Os_tlsf_max_log2_sli = 3

Log2 of Second level range count (max. 3). Since the search process is speed up by using bitmaps, this value should match the platform's word width. For 8-Bit AVR it is 2 ^ 3 = 8.


These 3 parameters result in the following block size ranges (default values, no XRAM):

First level | Second level
(+4+1)      |   0       1       2       3       4       5       6       7
--------------------------------------------------------------------------
0 |    32   |   0       4       8      12      16      20      24      28
1 |    64   |  32      36      40      44      48      52      56      60
2 |   128   |  64      72      80      88      96     104     112     120
3 |   256   | 128     144     160     176     192     208     224     240
4 |   512   | 256     288     320     352     384     416     448     480
5 |  1024   | 512     576     640     704     768     832     896     960

Index 0 is not used, Index 1 is a list containing blocks up to a size of 4 Bytes, Index 2 list has sizes from 4 to 8 Bytes, Index 8 contains 16-32 Byte blocks, ...


Const Debug_level_tlsf = 0

Set debug output verbosity, 0 = no debug, 1 = Print out available memory each Malloc/Free, 2 = Print out memory pool overview, 3 = Print additional info during Malloc/Free. If debug outputs are activated, option Os_task_debug_metrics is also declared.


Const Os_task_dbg_metrics = 0

Declare Const (value doesn't matter) to enable tracking of available memory. If activated, a variable will be created:

#if Os_big_ram = False
   Dim Available_memory As Word
#else
   Dim Available_memory As Dword
#endif


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

Request a block of memory of given size. Returns the starting memory adress or zero if not enough free memory left.


#if Os_big_ram = False
Sub Free(byref Block As Word)
#else
Sub Free(byref Block As Dword)
#endif

Returns a previously allocated block to the free memory pool.


#if Os_big_ram = False
Function Os_malloc_blocksize(byref Block As Word) As Word
#else
Function Os_malloc_blocksize(byref Block As Dword) As Dword
#endif

Returns the size of a block.


Sub Os_malloc_clear()

Empties all lists, resets the allocator


Sub Os_malloc_init()

Initializes the allocator. This is done when the library is included, in standard applications, it is not needed to call. However, to reset the allocator, use Os_malloc_clear before calling Os_malloc_init.

Samples

M32 internal SRAM (runs in Simulator)

$regfile = "m32def.dat"
$crystal = 1000000
$hwstack = 32
$swstack = 32
$framesize = 64
$baud = 9600
Config Submode = New



' Set start address of the free memory pool manually (examine compile report, "show internal variables" - setting enabled)
Const Os_mem_size_free = 1810
Const Os_task_dbg_metrics = 1                               ' constant needs to be defined if we want to see the available memory

' include needed libraries
$include "..\..\src\inc\os_common.inc"                      ' per default os_common.inc and os_memory.inc are included by the memory allocator and are expected
$include "..\..\src\inc\os_memory.inc"                      ' in the subdirectory "inc". To accomodate for the different directory structure of the samples,
                                                             ' we need to override the default includes
$include "..\..\src\inc\os_malloc_tlsf.inc"



Dim Memoryblock1 As Word
Dim Memoryblock2 As Word

Print "Free Memory: " ; Available_memory
Memoryblock1 = Malloc(512)
Memoryblock2 = Malloc(45)
Print "adr1: " ; Memoryblock1 ; "   adr2: " ; Memoryblock2
Print "Free Memory: " ; Available_memory
Free Memoryblock1
Free Memoryblock2
Print "Free Memory: " ; Available_memory

End

Output:

Free Memory: 1804
adr1: 210   adr2: 726
Free Memory: 1236
Free Memory: 1804

ATXMega128A1-Xplained (8Mb ext. SRAM)

$regfile = "xm128a1udef.dat"
$crystal = 32000000
$baud = 115200
$hwstack = 128
$swstack = 128
$framesize = 128
Config Submode = New

Config Osc = Disabled , 32mhzosc = Enabled
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1

Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Open "COM1:" For Binary As #1



' =========== XRAM ============
'the XPLAINED has a 64 MBit SDRAM which is 8 MByte, it is connected in 3 port, 4 bit databus mode
'in the PDF of the SDRAM you can see it is connected as 16 Meg x 4. Refreshcount is 4K and the row address is A0-A11, column addressing is A0-A9
$xramsize = 8388608                                      ' 8 MByte
Config Xram = 3port , Sdbus = 4 , Sdcol = 10 , Sdcas = 3 , Sdrow = 12 , Refresh = 500 , Initdelay = 3200 , Modedelay = 2 , Rowcycledelay = 7 , Rowprechargedelay = 7 , Wrdelay = 1 , Esrdelay = 7 , Rowcoldelay = 7 , Modesel3 = Sdram , Adrsize3 = 8m , Baseadr3 = &H0000
'the config above will set the port registers correct. it will also wait for Ebi_cs3_ctrlb.7



' =========== MEMORY ALLOCATOR ============
' Set size of the free memory pool manually (examine compile report, -> "Space Left")
Const Os_mem_size_free = 7476                               ' activated debug info needs an additional byte

Const Os_task_dbg_metrics = 1                               ' constant needs to be defined if we want to see the available memory (value doesn't matter)
Const Debug_level_tlsf = 0                                  ' print out additional debug info, possible values: 0 (no info), 1, 2 and 3 (full info)

' include needed libraries
$include "..\..\src\inc\os_common.inc"                      ' per default os_common.inc and os_memory.inc are included by the memory allocator and are expected
$include "..\..\src\inc\os_memory.inc"                      ' in the subdirectory "inc". To accomodate for the different directory structure of the samples,
                                                             ' we need to override the default includes
$include "..\..\src\inc\os_malloc_tlsf.inc"



' =========== DEMO ============
Dim Memoryblock1 As Dword
Dim Memoryblock2 As Dword
Dim Memoryblock3 As Dword
Dim Memoryblock4 As Dword
Dim Blocksize As Dword

Print "Start,         free memory: " ; Available_memory

' allocate some smaller blocks (will be served from internal sram first)
Memoryblock1 = Malloc(123)
Blocksize = Os_malloc_blocksize(memoryblock1)
Print "Alloc 123,     free memory: " ; Available_memory
Print "Address 1: " ; Memoryblock1 ; " Size: " ; Blocksize

Memoryblock2 = Malloc(48)
Blocksize = Os_malloc_blocksize(memoryblock2)
Print "Alloc 48,      free memory: " ; Available_memory
Print "Address 2: " ; Memoryblock2 ; " Size: " ; Blocksize

' allocate bigger blocks to force allocation from external memory
Memoryblock3 = Malloc(10000)
Blocksize = Os_malloc_blocksize(memoryblock3)
Print "Alloc 10000,   free memory: " ; Available_memory
Print "Address 3: " ; Memoryblock3 ; " Size: " ; Blocksize

Memoryblock4 = Malloc(12345)
Blocksize = Os_malloc_blocksize(memoryblock4)
Print "Alloc 12345,   free memory: " ; Available_memory
Print "Address 4: " ; Memoryblock4 ; " Size: " ; Blocksize

' free up allocated memory
Free Memoryblock1
Print "Freed block 1, free memory: " ; Available_memory

Free Memoryblock2
Print "Freed block 2, free memory: " ; Available_memory

Free Memoryblock3
Print "Freed block 3, free memory: " ; Available_memory

Free Memoryblock4
Print "Freed block 4, free memory: " ; Available_memory

End

Output:

Start,         free memory: 8396068
Alloc 123,     free memory: 8395932
Address 1: 8531 Size: 128
Alloc 48,      free memory: 8395876
Address 2: 8667 Size: 48
Alloc 10000,   free memory: 8385868
Address 3: 16392 Size: 10000
Alloc 12345,   free memory: 8373512
Address 4: 26400 Size: 12348
Freed block 1, free memory: 8373640
Freed block 2, free memory: 8373704
Freed block 3, free memory: 8383704
Freed block 4, free memory: 8396068

Download