MemoryBlock

From REALbasicWiki

Jump to: navigation, search
Overall article skill Skill ranges from beginner (green) to expert (red)
A MemoryBlock is a wrapper for a block of memory allocated by the application.
MemoryBlock class
Available on Mac OS X Linux Windows
General
Superclass Object
Interfaces none
Constants
  • SizeUnknown = –1
Methods
  • BooleanValue...
  • Byte...
  • ColorValue...
  • CString...
  • CurrencyValue...
  • DoubleValue...
  • Int16Value...
  • Int32Value...
  • Int64Value...
  • Int8Value...
  • LeftB...
  • Long...
  • MidB...
  • PString...
  • Ptr...
  • RightB...
  • Short...
  • SingleValue...
  • StringValue...
  • UInt16Value...
  • UInt32Value...
  • UInt64Value...
  • UInt8Value...
  • UShort...
  • WString...
Properties
Events

none

NOTE: You can get descriptions of the items above from the REALbasic documentation.


When a MemoryBlock is created by some means other than the New operator or NewMemoryBlock, the object has no way of knowing its size. In this case, the Size property of the MemoryBlock object returns SizeUnknown.

Contents

[edit] Sub MemoryBlock(size as Integer)

This is the constructor. If you subclass MemoryBlock and want to override the constructor, it is this method that you must override. If a block of the requested size cannot be allocated, an OutOfMemoryException will be raised.

[edit] Function MidB(offset as Integer, [bytes as Integer]) as MemoryBlock

MidB returns a new MemoryBlock containing the data starting at offset offset. The second, optional parameter specfiies the size of the block to copy. if omitted, MidB returns the piece of the block starting at offset.

[edit] Size as Integer

Size returns the size in bytes of the allocated memory, if known. Otherwise, it returns SizeUnknown - 1. The usual way of getting a MemoryBlock of unknown size is by assignment from a Ptr. Here is an example.

dim m as new MemoryBlock(1024)
dim p as Ptr = m
dim m2 as MemoryBlock = p

You can execute this code and inspect the Size property of m2 to see that it is -1.


Assigning to the Size property resizes the MemoryBlock, while preserving existing data. More precisely, the assignment allocates a new block of memory, copies data from the old block to the new block, and releases the old block if it was originally allocated by MemoryBlock. The MemoryBlock object now wraps the new block. The two blocks of code that follow should do the same thing.

dim m as new MemoryBlock(X)
m.Size = Y
dim m as new MemoryBlock(X)
dim mCopy as new MemoryBlock(Y)
mCopy.StringValue(0, Min(X, Y)) = m.StringValue(0, Min(X, Y)
m = mCopy
mCopy = nil

[edit] Getting the Address of a MemoryBlock

You can get the address of the memory to which a MemoryBlock points by converting the MemoryBlock to a Ptr.

dim m as new MemoryBlock(1024)
dim p as Ptr = m
Personal tools
related