Class wwHeap
about 1 minute to read

A wrapper class around the Windows Heap that allows creating a buffer of character or byte data that you can access quickly even for very large buffers.

This class specifically allows for iterating over strings or byte buffers one byte at a time as required by parsers and encoders. It helps works around the extreme slowness of FoxPro's Substr() function with anything but small buffers (more info)

foxpro
DO wwAPI && load library LOCAL loHeap as wwHeap loHeap = CREATEOBJECT("wwHeap") lnIterations = 10000000 && 10 million characters lcData = REPLICATE("0123456789",lnIterations / 10) loHeap.SetData(lcData) && Sets and Allocates (alternately you can Allocate() separately *** Using a method call lnStart = SECONDS() FOR x = 1 TO lnIterations lcChar = loHeap.Getbytes(x,1) ENDFOR ? SECONDS() - lnStart && 5.0 seconds loHeap.Release()

For Improved Performance in Tight Loops bypass .GetBytes()

GetBytes() is the explicit method to retrieve data from the heap, but it turns off the method wrapper adds significant overhead to retrieval times. If you need the highest performance it's more efficient to call the SYS(2600) function directly to access the memory.

The above code can be changed to:

foxpro
*** Same without a method call lnStart = SECONDS() FOR x = 1 TO lnIterations *** No method call lcChar = Sys(2600,loHeap.nBaseAddress-1 + x,1) ENDFOR ? SECONDS() - lnStart && 1.1 seconds

Class Members

MemberDescription

Allocate

Allocates a block of data on the heap. Specify a size for the block to allocate.

o.wwHeap.Allocate(lnSize, lcData)

GetBytes

Retrieves a number of characters or bytes from the allocated heap at a given position. The position is 1 based.

o.wwHeap.GetBytes(lnOffSet, lnCount)

Release

Releases the active heap memory.

o.wwHeap.Release()

SetData

Sets a block of data into the allocated heap, or if no prior call to .Allocate() was made, also allocates the heap for the size of the block of data.

o.wwHeap.SetData(lcData)

nBaseAddress

Stores the base memory pointer address of where the Heap is allocated which can be used with SYS(2600) to retrieve data from this allocated memory.

Requirements

Assembly: wwApi.prg

See also:

Class wwHeap

© West Wind Technologies, 1996-2025 • Updated: 01/14/25
Comment or report problem with topic