Class MemorySpan

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type MemorySpan = class(TObject)

Description

This is a utility class that provides some helpful routines for quickly filling and copying stretches of memory.

Hierarchy

  • TObject
  • MemorySpan

Overview

Methods

Public class procedure fill(dest: pointer; const len: longword; const value: longword);
Public class procedure fill(dest: pointer; const len: longword; const value: qword);
Public class procedure copy(source, dest: pointer; const len: longword);
Public class function paddingRequired(const current, top: pchar; alignment, increment: longword): longword;

Description

Methods

Public class procedure fill(dest: pointer; const len: longword; const value: longword);

Quickly fill a block of memory with a specific value.

This method fills the memory at dest with the value specified by value, until it has filled len bytes. It speeds up the process by using the native word size on the processor for which it was compiled; thus, for 32-bit systems it fills memory 4 bytes at a time, while on 64-bit systems it fills memory 8 bytes at a time.

len must always specify the number of bytes to fill, independent of the native word size of the processor.

It is the caller's responsibility to ensure that the memory block pointed to by dest has at least len bytes allocated to it; otherwise, memory overruns and segmentation faults may occur.

Public class procedure fill(dest: pointer; const len: longword; const value: qword);

Quickly fill a block of memory with a specific value.

This method fills the memory at dest with the value specified by value, until it has filled len bytes. It speeds up the process by using the native word size on the processor for which it was compiled; thus, for 32-bit systems it fills memory 4 bytes at a time, while on 64-bit systems it fills memory 8 bytes at a time.

len must always specify the number of bytes to fill, independent of the native word size of the processor.

It is the caller's responsibility to ensure that the memory block pointed to by dest has at least len bytes allocated to it; otherwise, memory overruns and segmentation faults may occur.

Public class procedure copy(source, dest: pointer; const len: longword);

Quickly copy data from one memory block to another.

This method quickly copies len bytes of data from source to dest. It speeds up the process by using the native word size on the processor for which it was compiled; thus, for 32-bit systems it copies 4 bytes at a time, while on 64-bit systems it copies 8 bytes at a time.

len must always specify the number of bytes to copy, independent of the native word size of the processor. It is also assumed that the memory regions pointed to by source and dest do NOT overlap; if they do, this method may produce undefined results, up to and including segmentation faults.

It is the caller's responsibility to ensure that the memory blocks pointed to by both source and dest have at least len bytes allocated to them; otherwise, memory overruns and segmentation faults may occur.

Public class function paddingRequired(const current, top: pchar; alignment, increment: longword): longword;

Determine the number of bytes that will need to be added to current so that it is aligned on the specified boundary relative to top.

This method can be used to ensure that data read from a buffer is aligned to a specific address size. current should point to the current position in the buffer, while top points to the "top" of the buffer – that is, the first byte in the buffer.

alignment should specify the alignment required, in bytes. An 8-bit alignment would require a value of 1; a 16-bit alignment a value of 2; a 32-bit alignment a value of 4; and so forth. If the memory position of current does not divide evenly into the specified alignment, then this method will calculate how many bytes need to be added to alignment so that it will divide evenly. This value is then multiplied by increment.

For example, Windows version information is stored in pseudo-structures, the members of which must sometimes be aligned on a 32-bit address boundary using words (16-bit values). To have this method calculate the padding necessary to meet this requirement, you would call this method like so:


      var
        buffer: pchar;
        currentPos: pchar;

      begin
        // Assuming 'buffer' and 'currentPos' are valid addresses, and that
        // 'currentPos' represents a position within 'buffer'...
        inc(currentPos, MemorySpan.paddingRequired(currentPos, buffer,
          sizeof(longword), sizeof(word)));
        // 32 bits = 4 bytes, or sizeof(longword)
        // 16 bits = 2 bytes, or sizeof(word)
      end.
      

If current or top are Nil, or if current does not represent a memory address which is greater than top, then this method does nothing and returns zero (0).


Generated by PasDoc 0.13.0 on 2015-06-23 19:40:11