Humble Framework for SkyOS


Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

HMemory Class Reference
[Base]

#include <HMemory.h>

Inheritance diagram for HMemory:

HObjNoCopy HObj

Detailed Description

The HMemory class manages a block of dynamic memory, and provides a more robust and capable version than malloc() or free().

To help detect buffer overflow (or underflow) situations, in debug builds, and additional eight (8) bytes of memory is allocated for each buffer. Six (6) bytes are reserved at the front of the buffer to store the size and an underflow guard (0xDEAD), while the remaining two (2) bytes are reserved past the end of the memory block for an overflow guard (0xDEAD).

When a buffer is finally deallocated, the overflow/underflow guards are checked, and if they are NOT the original guard value (0xDEAD), an ASSERT is fired. Note that if the underflow guard is trashed, then it is assumed that the block size is also invalid, and the overflow guard is ignored.

        +----------+ <= Actual base of allocated block
        |  uint32  |    Size of memory block (in bytes)
        +----------+
        |  uint16  |    Set to 0xDEAD in create(), tested in Destroy()
        +----------+ <= Logical base of buffer (m_pData points here)
        :          :    
        :(m_uBytes):
        :          :
        +----------+ <= Logical end of buffer
        |  uint16  |    Set to 0xDEAD in create(), tested in Destroy()
        +----------+ <= Actual end of allocated block

Definition at line 47 of file HMemory.h.

Public Member Functions

 HMemory (uint32 uSize=0) throw ()
 HMemory (PtrConst pData, uint32 uSize) throw ()
ErrCode Destroy (void)
 Destroys (frees) the buffer.
ErrCode Zero (void)
 Clears the buffer by setting it to zeros.
uint8 GetAt (uint32 udx) const
 Returns the byte at a given offset, or zero if offset is invalid.
Ptr GetPtr (uint32 udx=0) const
 Returns a pointer into the buffer, or NULL if offset is invalid.
uint32 GetSize (void) const
 Returns the size of the buffer, or zero if non-existent.
ErrCode Set (PtrConst pData, uint32 uBytes)
 Copies a series of 1 or more bytes into the buffer.
ErrCode SetAt (uint32 udx, uint8 xVal)
 Sets the byte at a given offset.
ErrCode SetAt (uint32 udx, PtrConst pData, uint32 uBytes)
 Sets a series of bytes at a given offset.
ErrCode SetSize (uint32 uSize)
 Sets the buffer to a given size.
bool Contains (const void *p, uint32 uBytes=1) const
 Determines if a block of data lies completely within the buffer.
bool IsValid (void) const
 Returns true if the buffer is valid, i.e., is at least 1 byte long.
 operator Ptr (void)
 operator PtrData (void)
uint8 operator[] (uint32 udx) const
uint8 & operator[] (uint32 udx) throw (HError)
HMemoryoperator= (HMemory const &rhs) throw ()

Protected Member Functions

ErrCode create (uint32 uBytes, PtrConst pData=NULL)
 Allocates a buffer.
bool validIndex (uint32 udx) const
 Tests an index (offset) value for validity.
bool validPtr (PtrConst pData) const
 Tests a data pointer value for validity.

Protected Attributes

uint32 m_uBytes
 Size of buffer in bytes.
PtrData m_pData
 Ptr to actual memory block.


Member Function Documentation

ErrCode HMemory::Destroy void   ) 
 

Returns:
Always NO_ERROR

Definition at line 86 of file HMemory.h.

ErrCode HMemory::Zero void   ) 
 

Returns:
Always NO_ERROR

Definition at line 114 of file HMemory.h.

ErrCode HMemory::create uint32  uBytes,
PtrConst  pData = NULL
[protected]
 

This method allocates a block of memory and initializes it to zeroes. If the caller provides an optional pointer (pData), the buffer's contents are copied from that pointer. The following errors may be returned:

  • ERR_BAD_PARAMETER
  • ERR_BUY_MORE_RAM

Returns:
NO_ERROR on success, error code otherwise
Parameters:
uBytes Desired size of buffer in bytes
pData [optional] Ptr to data to copy into buffer

Definition at line 140 of file HMemory.h.

uint8 HMemory::GetAt uint32  udx  )  const
 

Returns:
Byte at offset
Parameters:
udx Index of character

Definition at line 193 of file HMemory.h.

Ptr HMemory::GetPtr uint32  udx = 0  )  const
 

Returns:
Pointer into buffer
Parameters:
udx Offset

Definition at line 200 of file HMemory.h.

uint32 HMemory::GetSize void   )  const
 

Returns:
Size of buffer in bytes

Definition at line 206 of file HMemory.h.

ErrCode HMemory::Set PtrConst  pData,
uint32  uBytes
 

This method copies a data block into the buffer, up to the current buffer size or the end of the data block, whichever comes first.

Returns:
NO_ERROR on success, error code otherwise
Parameters:
pData Data to copy
uBytes Length of data

Definition at line 218 of file HMemory.h.

ErrCode HMemory::SetAt uint32  udx,
uint8  xVal
 

Returns:
NO_ERROR on success, error code otherwise
Parameters:
udx Index
xVal Byte to set

Definition at line 239 of file HMemory.h.

ErrCode HMemory::SetAt uint32  udx,
PtrConst  pData,
uint32  uBytes
 

Returns:
NO_ERROR on success, error code otherwise
Parameters:
udx Index
pData Data to copy
uBytes Length of data

Definition at line 258 of file HMemory.h.

ErrCode HMemory::SetSize uint32  uSize  ) 
 

This method grows or shrinks the buffer to a caller-supplied size. Calling SetSize() with a zero size is functionally equivalent to calling Destroy(). Note that resizing an HMemory object is destructive, i.e., the contents of the buffer will be destroyed!

Returns:
NO_ERROR on success, error code otherwise
Parameters:
uSize Desired size (in bytes)

Definition at line 296 of file HMemory.h.

bool HMemory::Contains const void *  p,
uint32  uBytes = 1
const
 

Returns:
True if within buffer, false otherwise
Parameters:
p Ptr to data block
uBytes Size of data block in bytes (defaults to 1)

Definition at line 315 of file HMemory.h.

bool HMemory::IsValid void   )  const
 

Returns:
true if buffer valid, false otherwise

Definition at line 324 of file HMemory.h.

bool HMemory::validIndex uint32  udx  )  const [protected]
 

Returns:
True if the index falls within the buffer
Parameters:
udx Index value to test

Definition at line 332 of file HMemory.h.

bool HMemory::validPtr PtrConst  pData  )  const [protected]
 

A valid pointer is an pointer that is non-null, and is NOT the same as the buffer pointer (to avoid self-replication). Note that this method does not do any type of length checking.

Returns:
True if the pointer is valid
Parameters:
pData Pointer to test

Definition at line 344 of file HMemory.h.


The documentation for this class was generated from the following file:
 

2006.01.09-16:37