Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Protected Member Functions | Protected Attributes
nsSegmentedBuffer Class Reference

#include <nsSegmentedBuffer.h>

Collaboration diagram for nsSegmentedBuffer:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsSegmentedBuffer ()
 ~nsSegmentedBuffer ()
NS_COM nsresult Init (PRUint32 segmentSize, PRUint32 maxSize, nsIMemory *allocator=nsnull)
NS_COM char * AppendNewSegment ()
PRBool DeleteFirstSegment ()
PRBool DeleteLastSegment ()
PRBool ReallocLastSegment (size_t newSize)
NS_COM void Empty ()
PRUint32 GetSegmentCount ()
PRUint32 GetSegmentSize ()
PRUint32 GetMaxSize ()
PRUint32 GetSize ()
char * GetSegment (PRUint32 indx)

Protected Member Functions

PRInt32 ModSegArraySize (PRInt32 n)
PRBool IsFull ()

Protected Attributes

PRUint32 mSegmentSize
PRUint32 mMaxSize
nsIMemorymSegAllocator
char ** mSegmentArray
PRUint32 mSegmentArrayCount
PRInt32 mFirstSegmentIndex
PRInt32 mLastSegmentIndex

Detailed Description

Definition at line 44 of file nsSegmentedBuffer.h.


Constructor & Destructor Documentation

Definition at line 53 of file nsSegmentedBuffer.h.

Here is the call graph for this function:


Member Function Documentation

Definition at line 65 of file nsSegmentedBuffer.cpp.

{
    if (GetSize() >= mMaxSize)
        return nsnull;

    if (mSegmentArray == nsnull) {
        PRUint32 bytes = mSegmentArrayCount * sizeof(char*);
        mSegmentArray = (char**)nsMemory::Alloc(bytes);
        if (mSegmentArray == nsnull)
            return nsnull;
        memset(mSegmentArray, 0, bytes);
    }
    
    if (IsFull()) {
        PRUint32 newArraySize = mSegmentArrayCount * 2;
        PRUint32 bytes = newArraySize * sizeof(char*);
        char** newSegArray = (char**)nsMemory::Realloc(mSegmentArray, bytes);
        if (newSegArray == nsnull)
            return nsnull;
        mSegmentArray = newSegArray;
        // copy wrapped content to new extension
        if (mFirstSegmentIndex > mLastSegmentIndex) {
            // deal with wrap around case
            memcpy(&mSegmentArray[mSegmentArrayCount],
                   mSegmentArray,
                   mLastSegmentIndex * sizeof(char*));
            memset(mSegmentArray, 0, mLastSegmentIndex * sizeof(char*));
            mLastSegmentIndex += mSegmentArrayCount;
            memset(&mSegmentArray[mLastSegmentIndex], 0,
                   (newArraySize - mLastSegmentIndex) * sizeof(char*));
        }
        else {
            memset(&mSegmentArray[mLastSegmentIndex], 0,
                   (newArraySize - mLastSegmentIndex) * sizeof(char*));
        }
        mSegmentArrayCount = newArraySize;
    }

    char* seg = (char*)mSegAllocator->Alloc(mSegmentSize);
    if (seg == nsnull) {
        return nsnull;
    }
    mSegmentArray[mLastSegmentIndex] = seg;
    mLastSegmentIndex = ModSegArraySize(mLastSegmentIndex + 1);
    return seg;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 113 of file nsSegmentedBuffer.cpp.

{
    NS_ASSERTION(mSegmentArray[mFirstSegmentIndex] != nsnull, "deleting bad segment");
    (void)mSegAllocator->Free(mSegmentArray[mFirstSegmentIndex]);
    mSegmentArray[mFirstSegmentIndex] = nsnull;
    PRInt32 last = ModSegArraySize(mLastSegmentIndex - 1);
    if (mFirstSegmentIndex == last) {
        mLastSegmentIndex = last;
        return PR_TRUE;
    }
    else {
        mFirstSegmentIndex = ModSegArraySize(mFirstSegmentIndex + 1);
        return PR_FALSE;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 130 of file nsSegmentedBuffer.cpp.

Here is the call graph for this function:

Definition at line 156 of file nsSegmentedBuffer.cpp.

Here is the caller graph for this function:

Definition at line 84 of file nsSegmentedBuffer.h.

{ return mMaxSize; }

Here is the caller graph for this function:

char* nsSegmentedBuffer::GetSegment ( PRUint32  indx) [inline]

Definition at line 87 of file nsSegmentedBuffer.h.

                                           {
        NS_ASSERTION(indx < GetSegmentCount(), "index out of bounds");
        PRInt32 i = ModSegArraySize(mFirstSegmentIndex + (PRInt32)indx);
        return mSegmentArray[i];
    }

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 76 of file nsSegmentedBuffer.h.

Here is the caller graph for this function:

Definition at line 83 of file nsSegmentedBuffer.h.

{ return mSegmentSize; }

Here is the caller graph for this function:

Definition at line 85 of file nsSegmentedBuffer.h.

{ return GetSegmentCount() * mSegmentSize; }

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsSegmentedBuffer::Init ( PRUint32  segmentSize,
PRUint32  maxSize,
nsIMemory allocator = nsnull 
)

Definition at line 42 of file nsSegmentedBuffer.cpp.

{
    if (mSegmentArrayCount != 0)
        return NS_ERROR_FAILURE;        // initialized more than once
    mSegmentSize = segmentSize;
    mMaxSize = maxSize;
    mSegAllocator = allocator;
    if (mSegAllocator == nsnull) {
        mSegAllocator = nsMemory::GetGlobalMemoryService();
    }
    else {
        NS_ADDREF(mSegAllocator);
    }
#if 0 // testing...
    mSegmentArrayCount = 2;
#else
    mSegmentArrayCount = NS_SEGMENTARRAY_INITIAL_COUNT;
#endif
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool nsSegmentedBuffer::IsFull ( ) [inline, protected]

Definition at line 101 of file nsSegmentedBuffer.h.

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 94 of file nsSegmentedBuffer.h.

                                              {
        PRUint32 result = n & (mSegmentArrayCount - 1);
        NS_ASSERTION(result == n % mSegmentArrayCount,
                     "non-power-of-2 mSegmentArrayCount");
        return result;
    }

Here is the caller graph for this function:

Definition at line 141 of file nsSegmentedBuffer.cpp.

{
    PRInt32 last = ModSegArraySize(mLastSegmentIndex - 1);
    NS_ASSERTION(mSegmentArray[last] != nsnull, "realloc'ing bad segment");
    char *newSegment =
        (char*)mSegAllocator->Realloc(mSegmentArray[last], newSize);
    if (newSegment) {
        mSegmentArray[last] = newSegment;
        return PR_TRUE;
    } else {
        return PR_FALSE;
    }
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 111 of file nsSegmentedBuffer.h.

Definition at line 112 of file nsSegmentedBuffer.h.

Definition at line 107 of file nsSegmentedBuffer.h.

Definition at line 108 of file nsSegmentedBuffer.h.

Definition at line 109 of file nsSegmentedBuffer.h.

Definition at line 110 of file nsSegmentedBuffer.h.

Definition at line 106 of file nsSegmentedBuffer.h.


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