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
|
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) |
|
HMemory & | operator= (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.
|