MemoryBlock
is a memory area in the working memory (SRAM), actually it is a memory object in Luna's dynamicly managed memory. Creating a block reserves that number of Byte in memory an can be used to manage your data. Access to the object via the object methods only because it is managed dynamicly, e.g. the addresses change dynamicly. Call the „New“-Method to create a Memory Block. The maximus size are 65KB.
Create a new MemoryBlock
dim myvar as MemoryBlock myvar = New MemoryBlock(numBytes)
Release a MemoryBlock
The release of a memory block occurs automatically when assigning a new memory block, and departure from a method, except it is a global variable, a reference („byRef“ parameter) or a static variable („dim x as static …“). Code-Examples for explanation see bottom.
Methods (read only) | ||
---|---|---|
Name | Description | Return Type |
.Addr | Address of the variable in memory | word |
.Ptr | Address of the allocated memory block | word |
.Size | Total number of Bytes of the allocated memory block | word |
Methods (read / write) | ||
Name | Description | Return type |
.ByteValue(offset) | Read/Write Byte | byte |
.WordValue(offset) | Read/Write Word | word |
.IntegerValue(offset) | Read/Write Integer | integer |
.LongValue(offset) | Read/Write Long | long |
.SingleValue(offset) | Read/Write Single | single |
.StringValue(offset,bytes) | Read/Write String of fixed length | string |
.PString(offset) | Read/Write Pascal-String (Start-Byte is the length information) | string |
.CString(offset) | Read/Write C-String (Null terminated) | string |
* offset: Byte-Position within the memory block, basis 0.
- bytes: Number of Byte to read.
Notice
Memory Block boundry violation is permitted and not validated (faster). You may define a exception.
See also: Memory Management, Sram, Dump
Examples
dim a as byte dim s as string dim m as MemoryBlock m.New(100) ' Create Memory Block and release exising block, if defined if m <> nil then ' Check to see if memory block was allocated m.ByteValue(0)=7 ' Write Byte m.CString(1)="I am a String" ' Write String a=m.ByteValue(0) ' Read Byte, output: 7 s=m.CString(1) ' Read C-String, output: "I am a String" m.free ' Release Objekt memory end if
Examples for automatic release.
procedure test(m as MemoryBlock) 'm is byVal and destroyed at return endproc
procedure test() dim m as MemoryBlock 'm is byVal and destroyed at return endproc
procedure test(byRef m as MemoryBlock) 'm is byRef and remains untouched endproc
dim m as MemoryBlock procedure test() 'm is not part of the method and remains untouched 'Reason: 'Visibility of global variables in methods as long as no separate 'Variable with the same name was declared in the method endproc
function test(byRef m as MemoryBlock) as MemoryBlock 'm is byRef and remains untouched return m 'returns a new instance of m endfunc
function test() as MemoryBlock dim m as MemoryBlock return m 'the memoryBlock is dereferenced (detached) from "m" and returned endfunc