Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

AxPipe::CSeg Class Reference

Reference counted memory buffer objects. More...

#include <CSeg.h>

Inheritance diagram for AxPipe::CSeg:

Inheritance graph
[legend]
Collaboration diagram for AxPipe::CSeg:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 CSeg (size_t cbBuf=0, void *pvBuf=NULL, bool fReadOnly=false)
 Default, and full-function, ctor for non-owned data (not deleted on destruction).

 CSeg (size_t cbBuf, const void *pvBuf)
 For constant data, and thus set ReadOnly true; Non-owned data (not deleted).

 CSeg (const void *pvBuf, size_t cbLen, size_t cbGrowBuf=0)
 Construct an owned buffer with a copy of provided data, possibly also in a larger buffer.

virtual ~CSeg ()
 Delete buffer if owned, Release() parent, if child and delete critical section.

unsigned char * Ptr ()
 Get the raw buffer pointer.

const unsigned char * PtrRd ()
 Get a read-only pointer to the valid data in the buffer, using m_cbOff.

unsigned char * PtrWr ()
 Get a writeable pointer to the valid data.

void * PtrRelease ()
 Get an independent free buffer with valid data and also Release().

size_t Size (void)
 Get the number of useable bytes in the buffer.

size_t Len (void)
 Get the length of valid data in bytes in the segment.

CSegLen (size_t cbLen)
 Set the length of valid data in bytes in the segment.

CSegWriteable ()
 Get a definitely writeable CSeg *, possibly a copy.

CSegDrop (size_t cbOff)
 Drop cbOff bytes at the start of the buffer.

CSegAddRef ()
 Increment the reference count of this object.

int Release ()
 Decrement the reference count of this object.

int Type ()
 Get the type, an opaque user-defined non-zero integer.

CSegSetType (int iType)
 Set the type, an opaque user-defined non-zero integer.

CSegClone ()
 Make a clone of ourself.

virtual void * RTClassId ()
 Run-Time version of our type identification.


Static Public Member Functions

bool IsSeg (CSeg *pSeg)
 Check if a segment pointer is valid reference to standard data.

void * ClassId ()
 Run-time type identifcation.


Private Member Functions

void Init (size_t cbBuf, void *pvBuf, bool fReadOnly, int iType)
 Constructor helper, init's a CSeg appropriately.

CSegoperator= (CSeg &rhs)
 Make a dependent copy of the original.


Private Attributes

volatile LONG m_iRefCnt
 Reference count. volatile due to InterlockedXXX req's.

CRITICAL_SECTION m_CritSect
 This object may be shared among threads, thus needs this.

void * m_pvBuf
 The raw data buffer pointer. Never changed once set.

size_t m_cbBuf
 The raw data buffer size - not the same as how much valid data.

CSegm_pMom
 Owning object, if this is a child.

bool m_fOwnPtr
 True if this CSeg owns and manages the data buffer.

bool m_fReadOnly
 True if this is a read-only object.

int m_iType
 Opaque type indicator, free to use. Do note usage in AxPipe::eSegType.

size_t m_cbOff
 Current offset from start of raw buffer to valid data.

size_t m_cbLen
 Current number of valid bytes of raw data, starting at CSeg::m_cbOff.


Detailed Description

Reference counted memory buffer objects.

Objects of this kind *must* not be instantiated by any other means than 'new'. No arrays, no static, and no automatic. When the reference count falls to zero, they are deleted automagically.

You must *never* reference a pointer after a call to Release(). You must *never* 'delete' a pointer manually. Let Release() do it.

The default is that these objects are read-write. You can determine this by attempting to get a PtrWr(). If you get NULL, you must copy. You set them read-only by specifying this in the constructor.

A CSeg consists of a buffer, referenced by a pointer. The buffer may be owned by the CSeg object, or not. If it is, it'll be deleted when the last reference to the object is Released() 'd.

Not all of the buffer may consist valid data, the start in the buffer may be adjusted by an internally maintained offset, and the length of valid data by an internally maintained length of valid data starting from the offset.

General rule of usage: Always allocate with new - never create static or automatic CSeg objects, only through CAutoSeg When you pass a copy elsewhere, especially if it is potentially a different thread, call AddRef(). When you're done with your instance, call Release(). Never perform a 'delete' on a CSeg *. Never reference a CSeg after passing it anywhere without a prior AddRef() call!

CSeg's are thread safe in two ways. The reference count, m_iRefCnt, is only read and written using the InterlockedIncrement()/InterlockedDecrement() API which are inherently thread safe. In other functions, references to values that may be written at other times than object construction and initialization, a critical section is used as necessary to ensure thread safeness.

Definition at line 72 of file CSeg.h.


Constructor & Destructor Documentation

AxPipe::CSeg::CSeg size_t  cbBuf = 0,
void *  pvBuf = NULL,
bool  fReadOnly = false
 

Default, and full-function, ctor for non-owned data (not deleted on destruction).

Parameters:
cbBuf The size of the buffer provided. If provided, please provide a non-NULL buffer.
pvBuf Pointer to a buffer with cbBuf bytes. This buffer will be referred to by the CSeg.
fReadOnly Set to true if the provided buffer is read-only.

Definition at line 84 of file CSeg.cpp.

Referenced by Writeable().

AxPipe::CSeg::CSeg size_t  cbBuf,
const void *  pvBuf
 

For constant data, and thus set ReadOnly true; Non-owned data (not deleted).

Parameters:
cbBuf The size of the buffer provided. Please provide a non-NULL buffer.
pvBuf Pointer to a buffer with cbBuf bytes of read-only data.

Definition at line 90 of file CSeg.cpp.

AxPipe::CSeg::CSeg const void *  pvBuf,
size_t  cbLen,
size_t  cbGrowBuf = 0
 

Construct an owned buffer with a copy of provided data, possibly also in a larger buffer.

Parameters:
pvBuf Pointer to a buffer with cbLen bytes of valid data to copy
cbLen Number of valid bytes data in the buffer to copy
cbGrowBuf Number of bytes to increase the new buffer to

Definition at line 97 of file CSeg.cpp.

AxPipe::CSeg::~CSeg  )  [virtual]
 

Delete buffer if owned, Release() parent, if child and delete critical section.

Never allocate a CSeg as auto or static.

Note that the destructor does nothing - Release() does all the work, including delete this; Here we don't need a critical section, as the destructor is only called from within the class, and by definition only by one thread. With the exception of catastrophic internal error where the destructor is called prematurely, this is by definition thread-safe in the sense that only one thread should be active and attempt to destruct it.

Definition at line 115 of file CSeg.cpp.


Member Function Documentation

CSeg * AxPipe::CSeg::AddRef  ) 
 

Increment the reference count of this object.

Returns:
A pointer to this.

Definition at line 247 of file CSeg.cpp.

References m_iRefCnt.

Referenced by AxPipe::CSplit::PumpSplit(), AxPipe::CPipe::Work(), and Writeable().

void * AxPipe::CSeg::ClassId  )  [static]
 

Run-time type identifcation.

We're not using the built in RTTI because we sometimes want to be able to forego most of the run time library, as well as exceptions and RTTI.

The point here is to create a guaranteed unique value that is the same for all instances of a class, while not requiring any inits outside of the class declaration, and also to 'fool' optimizing compilers, so that they cannot perform global optimization and figure out that it can fold identical functions into one. It happened in a previous version... That's why we include the static int, it can't be optimized away, at least not easily. You need to override ClassId() and RTClassId() in all derived clases you want to distinguish, this is most easily done by simply copying and pasting exactly these definitions. There is also the Run-Time version, accessible through a pointer to a polymorphic base-class for example, RTClassId().

Reimplemented in AxPipe::CSegMap.

Definition at line 329 of file CSeg.cpp.

Referenced by RTClassId().

CSeg * AxPipe::CSeg::Clone  ) 
 

Make a clone of ourself.

The result is a child copy of the original - they share the buffer, but have individual offsets and lengths.

Returns:
A pointer to this.

Definition at line 306 of file CSeg.cpp.

Referenced by AxPipe::Stock::CPipeHMAC_SHA1< iBits >::Out(), AxPipe::CPipeBlock::OutPump(), and AxPipe::CFilterBlock::ReadBlock().

CSeg * AxPipe::CSeg::Drop size_t  cbOff  ) 
 

Drop cbOff bytes at the start of the buffer.

Don't Drop() more than Len() bytes.

Parameters:
cbOff The number of bytes to drop at the start.
Returns:
A pointer to this.

Definition at line 237 of file CSeg.cpp.

References m_cbOff, and m_CritSect.

Referenced by AxPipe::Stock::CPipeHMAC_SHA1< iBits >::Out(), AxPipe::CPipeBlock::OutPump(), AxPipe::CFilterBlock::ReadBlock(), AxPipe::CFilterByte::ReadByte(), and AxPipe::CFilterByte::Skip().

bool AxPipe::CSeg::IsSeg CSeg pSeg  )  [static]
 

Check if a segment pointer is valid reference to standard data.

Checks for NULL pointer and non-default Type().

Returns:
true if the pointer is a valid standard segment pointer.

Definition at line 298 of file CSeg.cpp.

References Type().

CSeg * AxPipe::CSeg::Len size_t  cbLen  ) 
 

Set the length of valid data in bytes in the segment.

Returns:
A pointer to this.

Definition at line 206 of file CSeg.cpp.

References m_cbLen, m_cbOff, and m_CritSect.

size_t AxPipe::CSeg::Len void   ) 
 

Get the length of valid data in bytes in the segment.

Returns:
The number of bytes of valid data.

Definition at line 195 of file CSeg.cpp.

References m_cbLen, m_cbOff, and m_CritSect.

Referenced by AxPipe::CFilter::CoStartFilter(), AxPipe::CSink::DoSegWork(), AxPipe::CSource::Drain(), AxPipe::CFilterByte::GetNextSeg(), CJoinInterleave::In(), AxPipe::Stock::CPipeSHA1::Out(), AxPipe::Stock::CPipeInflate::Out(), AxPipe::Stock::CPipeHMAC_SHA1< iBits >::Out(), AxPipe::CSinkMemFile::Out(), AxPipe::CPipeBlock::OutPump(), PtrRelease(), AxPipe::CFilterBlock::ReadBlock(), AxPipe::CFilterByte::ReadByte(), AxPipe::CFilterByte::Skip(), and Writeable().

CSeg & AxPipe::CSeg::operator= CSeg rhs  )  [private]
 

Make a dependent copy of the original.

Copy by assignment makes a dependent copy of the original section. It inherits the buffer, but is considered a child of the original.

Definition at line 61 of file CSeg.cpp.

References m_cbBuf, m_cbLen, m_cbOff, m_CritSect, m_fOwnPtr, m_fReadOnly, m_pMom, and m_pvBuf.

unsigned char * AxPipe::CSeg::Ptr  ) 
 

Get the raw buffer pointer.

This is the raw buffer pointer, unaffected by offsets or anything else, and it's also not const or anything, regardless of read-only status.

Returns:
The really raw buffer pointer. Use with caution.

Definition at line 133 of file CSeg.cpp.

References m_pvBuf.

const unsigned char * AxPipe::CSeg::PtrRd  ) 
 

Get a read-only pointer to the valid data in the buffer, using m_cbOff.

Returns:
const pointer to valid data, including offset.

Definition at line 141 of file CSeg.cpp.

References m_cbOff, m_CritSect, and m_pvBuf.

Referenced by AxPipe::Stock::CPipeSHA1::Out(), AxPipe::Stock::CPipeInflate::Out(), AxPipe::CSinkMemFile::Out(), AxPipe::CPipeBlock::OutPump(), PtrRelease(), PtrWr(), AxPipe::CFilterBlock::ReadBlock(), AxPipe::CFilterByte::ReadByte(), and Writeable().

void * AxPipe::CSeg::PtrRelease  ) 
 

Get an independent free buffer with valid data and also Release().

You may not refer to this CSeg* again, as it will have been Release() 'd. It only returns the data known to be valid in the buffer, not the raw buffer. Do check the length with Len() first, to find how much data there is.

Returns:
A buffer free to use, no longer associated with this CSeg.

Definition at line 162 of file CSeg.cpp.

References Len(), m_cbOff, m_CritSect, m_fOwnPtr, m_iRefCnt, m_pMom, m_pvBuf, PtrRd(), and Release().

unsigned char * AxPipe::CSeg::PtrWr  ) 
 

Get a writeable pointer to the valid data.

If you really need to get write-access to the buffer, use CSeg::Writeable().

Returns:
Pointer to valid writeable data, unless it's read-only. Then return NULL.

Definition at line 152 of file CSeg.cpp.

References m_fReadOnly, and PtrRd().

Referenced by AxPipe::Stock::CPipeInflate::Out(), AxPipe::CSinkMemFile::Out(), AxPipe::CPipeBlock::OutPump(), AxPipe::CFilterBlock::ReadBlock(), and Writeable().

int AxPipe::CSeg::Release  ) 
 

Decrement the reference count of this object.

Decrement the reference counter, and self destruct if it reaches zero.

If we're a child of another base section, decrement that reference count if our reference reaches zero.

Never reference a CSeg * after calling Release().

Returns:
Zero if this was the last reference.

Definition at line 262 of file CSeg.cpp.

References m_iRefCnt.

Referenced by AxPipe::CSink::DoSegWork(), AxPipe::CSource::Drain(), AxPipe::CFilterByte::GetNextSeg(), AxPipe::CSource::Out(), AxPipe::Stock::CPipeInflate::Out(), AxPipe::CJoin::CTSinkJoin::Out(), AxPipe::CSinkMemFile::Out(), AxPipe::Stock::CPipeInflate::OutClose(), AxPipe::CPipeBlock::OutPump(), AxPipe::CSink::OutSpecial(), PtrRelease(), AxPipe::CPipe::Pump(), AxPipe::CFilterBlock::ReadBlock(), and AxPipe::CPipe::Work().

void * AxPipe::CSeg::RTClassId  )  [virtual]
 

Run-Time version of our type identification.

See also:
ClassId()

Reimplemented in AxPipe::CSegMap.

Definition at line 337 of file CSeg.cpp.

References ClassId().

Referenced by AxPipe::CSinkMemFile::Out().

CSeg * AxPipe::CSeg::SetType int  iType  ) 
 

Set the type, an opaque user-defined non-zero integer.

See also:
Type() This is not thread-safe strictly speaking - so must only be called when there is a single reference.
Returns:
A pointer to 'this' CSeg

Definition at line 290 of file CSeg.cpp.

References m_iType.

size_t AxPipe::CSeg::Size void   ) 
 

Get the number of useable bytes in the buffer.

This is not necessarily the same as the Len() of the buffer, nor is it necessarily the same as the size of the raw buffer.

Returns:
Bytes in buffer from offset to end of raw buffer.

Definition at line 184 of file CSeg.cpp.

References m_cbBuf, m_cbOff, and m_CritSect.

Referenced by AxPipe::Stock::CPipeInflate::Out().

int AxPipe::CSeg::Type  ) 
 

Get the type, an opaque user-defined non-zero integer.

A CSeg may have an arbitrary int associated with it. The default is zero, but it may be set to any value by other classes, and it may be used for any purpose. AxPipe defines some reserved values in AxPipe::eSegType.

See also:
eSegType
Returns:
The type as an int.

Definition at line 280 of file CSeg.cpp.

References m_iType.

Referenced by AxPipe::CSink::DoSegWork(), and IsSeg().

CSeg * AxPipe::CSeg::Writeable  ) 
 

Get a definitely writeable CSeg *, possibly a copy.

Make a writeable CSeg of ourselves. If we already are writeable, just return 'this' and increment the ref count.

If we're readonly, make a new section and copy the valid data we have there.

Returns:
Pointer to a writeable CSeg.

Definition at line 220 of file CSeg.cpp.

References AddRef(), CSeg(), Len(), m_CritSect, m_fReadOnly, PtrRd(), and PtrWr().


The documentation for this class was generated from the following files:
Generated on Mon Feb 2 13:19:27 2004 for AxPipe by doxygen 1.3.5