Static Memory Allocation: Unterschied zwischen den Versionen

Aus ProjectWiki
Wechseln zu:Navigation, Suche
K (Interface description)
K (Index)
 
(5 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
 +
== Index ==
 +
* [[TLSF Dynamic Memory Allocation|''[OOP 1a/3] Dynamic Memory Allocation using TLSF'']]
 +
* [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 ==
 
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.
 
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.
Zeile 12: Zeile 21:
  
 
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_static.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 (refer to [[TLSF_Dynamic_Memory_Allocation#Compiler_report_example|TLSF Dynamic Memory Allocation]]).
 
  
 +
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". (refer to [[TLSF_Dynamic_Memory_Allocation#Compiler_report_example|TLSF Dynamic Memory Allocation]]).
  
 
=== Managing memory ===
 
=== Managing memory ===
Zeile 45: Zeile 52:
  
 
<pre>Dim Available_memory As Word/Dword</pre>
 
<pre>Dim Available_memory As Word/Dword</pre>
If the constant ''Os_task_dbg_metrics'' is defined, the remaining memory is available to the application by this variable.
+
If the constant ''Os_task_dbg_metrics'' is defined, the remaining memory is available to the application via this variable.
  
  
Zeile 56: Zeile 63:
  
 
== Samples ==
 
== Samples ==
WIP
+
=== M32 (runs in Simulator) ===
 +
<pre>$regfile = "m32def.dat"
 +
$crystal = 1000000
 +
$hwstack = 32
 +
$swstack = 32
 +
$framesize = 64
 +
$baud = 9600
 +
Config Submode = New
 +
 
 +
 
 +
 
 +
' =========== MEMORY ALLOCATOR ============
 +
' Set size of the free memory pool manually (examine compile report, -> "Space Left")
 +
Const Os_mem_size_free = 1911
 +
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_static.inc"
 +
 
 +
 
 +
 
 +
' =========== DEMO ============
 +
Dim Memoryblock1 As Word
 +
Dim Memoryblock2 As Word
 +
 
 +
Print "Free Memory: " ; Available_memory
 +
Memoryblock1 = Malloc(512)
 +
Print "Free Memory: " ; Available_memory
 +
Memoryblock2 = Malloc(45)
 +
Print "adr1: " ; Memoryblock1 ; "  adr2: " ; Memoryblock2
 +
Print "Free Memory: " ; Available_memory
 +
 
 +
End</pre>
 +
 
 +
Output:
 +
<pre>Free Memory: 1911
 +
Free Memory: 1399
 +
adr1: 104  adr2: 616
 +
Free Memory: 1354
 +
</pre>
 +
 
 +
=== ATXMega128A1-Xplained (8Mb ext. SRAM) ===
 +
<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 = 7742
 +
 
 +
Const Os_task_dbg_metrics = 1                              ' constant needs to be defined if we want to see the available memory (value doesn't matter)
 +
 
 +
' 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_static.inc"
 +
 
 +
 
  
 +
' =========== DEMO ============
 +
Dim Memoryblock1 As Dword
 +
Dim Memoryblock2 As Dword
 +
Dim Memoryblock3 As Dword
 +
Dim Memoryblock4 As Dword
 +
 +
Print "Start,        free memory: " ; Available_memory
 +
 +
' allocate some smaller blocks (will be served from internal sram first)
 +
Memoryblock1 = Malloc(123)
 +
Print "Alloc 123,    free memory: " ; Available_memory
 +
Print "Address 1: " ; Memoryblock1
 +
 +
Memoryblock2 = Malloc(48)
 +
Print "Alloc 48,      free memory: " ; Available_memory
 +
Print "Address 2: " ; Memoryblock2
 +
 +
' allocate bigger blocks to force allocation from external memory
 +
Memoryblock3 = Malloc(10000)
 +
Print "Alloc 10000,  free memory: " ; Available_memory
 +
Print "Address 3: " ; Memoryblock3
 +
 +
Memoryblock4 = Malloc(12345)
 +
Print "Alloc 12345,  free memory: " ; Available_memory
 +
Print "Address 4: " ; Memoryblock4
 +
 +
End</pre>
 +
 +
Output:
 +
<pre>Start,        free memory: 8396350
 +
Alloc 123,    free memory: 8396227
 +
Address 1: 8257
 +
Alloc 48,      free memory: 8396179
 +
Address 2: 8380
 +
Alloc 10000,  free memory: 8378608
 +
Address 3: 16384
 +
Alloc 12345,  free memory: 8366263
 +
Address 4: 26384</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

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_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". (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

M32 (runs in Simulator)

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



' =========== MEMORY ALLOCATOR ============
' Set size of the free memory pool manually (examine compile report, -> "Space Left")
Const Os_mem_size_free = 1911
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_static.inc"



' =========== DEMO ============
Dim Memoryblock1 As Word
Dim Memoryblock2 As Word

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

End

Output:

Free Memory: 1911
Free Memory: 1399
adr1: 104   adr2: 616
Free Memory: 1354

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

Const Os_task_dbg_metrics = 1                               ' constant needs to be defined if we want to see the available memory (value doesn't matter)

' 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_static.inc"



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

Print "Start,         free memory: " ; Available_memory

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

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

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

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

End

Output:

Start,         free memory: 8396350
Alloc 123,     free memory: 8396227
Address 1: 8257
Alloc 48,      free memory: 8396179
Address 2: 8380
Alloc 10000,   free memory: 8378608
Address 3: 16384
Alloc 12345,   free memory: 8366263
Address 4: 26384

Download