Changes to parser to allow less copying. Use of the new nsSliding[Sub]String classes so that tokens can hold substrings that keep references into the scanner buffer. Cleaned up token interface and general string usage. r=harishd,heikki sr=jst

git-svn-id: svn://10.0.0.236/trunk@83553 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
vidur%netscape.com
2000-12-12 21:58:14 +00:00
parent 33d6574ffe
commit 3e62b3f9f3
129 changed files with 5425 additions and 3603 deletions

View File

@@ -24,6 +24,18 @@
#include "nsSlidingString.h"
#ifndef nsBufferHandleUtils_h___
#include "nsBufferHandleUtils.h"
#endif
#include <new.h>
/**
* |nsSlidingSharedBufferList|
*/
void
nsSlidingSharedBufferList::DiscardUnreferencedPrefix( Buffer* aRecentlyReleasedBuffer )
{
@@ -35,62 +47,159 @@ nsSlidingSharedBufferList::DiscardUnreferencedPrefix( Buffer* aRecentlyReleasedB
}
ptrdiff_t Distance( const nsSharedBufferList::Position&, const nsSharedBufferList::Position& );
ptrdiff_t
Distance( const nsSharedBufferList::Position& aStart, const nsSharedBufferList::Position& aEnd )
{
ptrdiff_t result = 0;
if ( aStart.mBuffer == aEnd.mBuffer )
result = aEnd.mPosInBuffer - aStart.mPosInBuffer;
else
{
result = aStart.mBuffer->DataEnd() - aStart.mPosInBuffer;
for ( nsSharedBufferList::Buffer* b = aStart.mBuffer->mNext; b != aEnd.mBuffer; b = b->mNext )
result += b->DataLength();
result += aEnd.mPosInBuffer - aEnd.mBuffer->DataStart();
}
return result;
}
/**
* |nsSlidingSubstring|
*/
// copy constructor
nsSlidingSubstring::nsSlidingSubstring( const nsSlidingSubstring& aString )
: mStart(aString.mStart),
mEnd(aString.mEnd),
mBufferList(aString.mBufferList),
mLength(aString.mLength)
{
mBufferList.AcquireReference();
mStart.mBuffer->AcquireReference();
acquire_ownership_of_buffer_list();
}
nsSlidingSubstring::nsSlidingSubstring( const nsSlidingSubstring& aString, const nsReadingIterator<PRUnichar>& aStart, const nsReadingIterator<PRUnichar>& aEnd )
: mStart(aStart),
mEnd(aEnd),
mBufferList(aString.mBufferList),
mLength(PRUint32(Distance(mStart, mEnd)))
mLength(PRUint32(Position::Distance(mStart, mEnd)))
{
mBufferList.AcquireReference();
mStart.mBuffer->AcquireReference();
acquire_ownership_of_buffer_list();
}
nsSlidingSubstring::nsSlidingSubstring( nsSlidingSharedBufferList& aBufferList )
nsSlidingSubstring::nsSlidingSubstring( const nsSlidingString& aString )
: mStart(aString.mStart),
mEnd(aString.mEnd),
mBufferList(aString.mBufferList),
mLength(aString.mLength)
{
acquire_ownership_of_buffer_list();
}
nsSlidingSubstring::nsSlidingSubstring( const nsSlidingString& aString, const nsReadingIterator<PRUnichar>& aStart, const nsReadingIterator<PRUnichar>& aEnd )
: mStart(aStart),
mEnd(aEnd),
mBufferList(aString.mBufferList),
mLength(PRUint32(Position::Distance(mStart, mEnd)))
{
acquire_ownership_of_buffer_list();
}
nsSlidingSubstring::nsSlidingSubstring( nsSlidingSharedBufferList* aBufferList )
: mBufferList(aBufferList)
{
mBufferList.AcquireReference();
init_range_from_buffer_list();
acquire_ownership_of_buffer_list();
}
mStart.PointBefore(mBufferList.GetFirstBuffer());
mStart.mBuffer->AcquireReference();
typedef const nsSharedBufferList::Buffer* Buffer_ptr;
mEnd.PointAfter(mBufferList.GetLastBuffer());
mLength = PRUint32(Distance(mStart, mEnd));
static nsSharedBufferList::Buffer*
AllocateContiguousHandleWithData( Buffer_ptr aDummyHandlePtr, const nsAReadableString& aDataSource )
{
typedef const PRUnichar* PRUnichar_ptr;
// figure out the number of bytes needed the |HandleT| part, including padding to correctly align the data part
size_t handle_size = NS_AlignedHandleSize(aDummyHandlePtr, PRUnichar_ptr(0));
// figure out how many |CharT|s wee need to fit in the data part
size_t string_length = aDataSource.Length();
// how many bytes is that (including a zero-terminator so we can claim to be flat)?
size_t string_size = (string_length+1) * sizeof(PRUnichar);
nsSharedBufferList::Buffer* result = 0;
void* handle_ptr = ::operator new(handle_size + string_size);
if ( handle_ptr )
{
typedef PRUnichar* PRUnichar_ptr;
PRUnichar* string_start_ptr = PRUnichar_ptr(NS_STATIC_CAST(unsigned char*, handle_ptr) + handle_size);
PRUnichar* string_end_ptr = string_start_ptr + string_length;
nsReadingIterator<PRUnichar> fromBegin, fromEnd;
PRUnichar* toBegin = string_start_ptr;
copy_string(aDataSource.BeginReading(fromBegin), aDataSource.EndReading(fromEnd), toBegin);
result = new (handle_ptr) nsSharedBufferList::Buffer(string_start_ptr, string_end_ptr, string_start_ptr, string_end_ptr+1, PR_TRUE);
}
return result;
}
nsSlidingSubstring::nsSlidingSubstring( const nsAReadableString& aSourceString )
: mBufferList(new nsSlidingSharedBufferList(AllocateContiguousHandleWithData(Buffer_ptr(0), aSourceString)))
{
init_range_from_buffer_list();
acquire_ownership_of_buffer_list();
}
void
nsSlidingSubstring::Rebind( const nsSlidingSubstring& aString )
{
aString.acquire_ownership_of_buffer_list();
release_ownership_of_buffer_list();
mStart = aString.mStart;
mEnd = aString.mEnd;
mBufferList = aString.mBufferList;
mLength = aString.mLength;
}
void
nsSlidingSubstring::Rebind( const nsSlidingSubstring& aString, const nsReadingIterator<PRUnichar>& aStart, const nsReadingIterator<PRUnichar>& aEnd )
{
release_ownership_of_buffer_list();
mStart = aStart;
mEnd = aEnd;
mBufferList = aString.mBufferList;
mLength = PRUint32(Position::Distance(mStart, mEnd));
acquire_ownership_of_buffer_list();
}
void
nsSlidingSubstring::Rebind( const nsSlidingString& aString )
{
aString.acquire_ownership_of_buffer_list();
release_ownership_of_buffer_list();
mStart = aString.mStart;
mEnd = aString.mEnd;
mBufferList = aString.mBufferList;
mLength = aString.mLength;
}
void
nsSlidingSubstring::Rebind( const nsSlidingString& aString, const nsReadingIterator<PRUnichar>& aStart, const nsReadingIterator<PRUnichar>& aEnd )
{
release_ownership_of_buffer_list();
mStart = aStart;
mEnd = aEnd;
mBufferList = aString.mBufferList;
mLength = PRUint32(Position::Distance(mStart, mEnd));
acquire_ownership_of_buffer_list();
}
void
nsSlidingSubstring::Rebind( const nsAReadableString& aSourceString )
{
release_ownership_of_buffer_list();
mBufferList = new nsSlidingSharedBufferList(AllocateContiguousHandleWithData(Buffer_ptr(0), aSourceString));
init_range_from_buffer_list();
acquire_ownership_of_buffer_list();
}
nsSlidingSubstring::~nsSlidingSubstring()
{
mStart.mBuffer->ReleaseReference();
mBufferList.DiscardUnreferencedPrefix(mStart.mBuffer);
mBufferList.ReleaseReference();
release_ownership_of_buffer_list();
}
const PRUnichar*
@@ -149,8 +258,12 @@ nsSlidingSubstring::GetReadableFragment( nsReadableFragment<PRUnichar>& aFragmen
/**
* |nsSlidingString|
*/
nsSlidingString::nsSlidingString( PRUnichar* aStorageStart, PRUnichar* aDataEnd, PRUnichar* aStorageEnd )
: nsSlidingSubstring(*(new nsSlidingSharedBufferList(nsSlidingSharedBufferList::NewWrappingBuffer(aStorageStart, aDataEnd, aStorageEnd))))
: nsSlidingSubstring(new nsSlidingSharedBufferList(nsSlidingSharedBufferList::NewWrappingBuffer(aStorageStart, aDataEnd, aStorageEnd)))
{
// nothing else to do here
}
@@ -159,8 +272,8 @@ void
nsSlidingString::AppendBuffer( PRUnichar* aStorageStart, PRUnichar* aDataEnd, PRUnichar* aStorageEnd )
{
Buffer* new_buffer = new Buffer(aStorageStart, aDataEnd, aStorageStart, aStorageEnd);
Buffer* old_last_buffer = mBufferList.GetLastBuffer();
mBufferList.LinkBuffer(old_last_buffer, new_buffer, 0);
Buffer* old_last_buffer = mBufferList->GetLastBuffer();
mBufferList->LinkBuffer(old_last_buffer, new_buffer, 0);
mLength += new_buffer->DataLength();
mEnd.PointAfter(new_buffer);
@@ -171,10 +284,16 @@ nsSlidingString::DiscardPrefix( const nsReadingIterator<PRUnichar>& aIter )
{
Position old_start(mStart);
mStart = aIter;
mLength -= Distance(old_start, mStart);
mLength -= Position::Distance(old_start, mStart);
mStart.mBuffer->AcquireReference();
old_start.mBuffer->ReleaseReference();
mStart.mBuffer->AcquireNonOwningReference();
old_start.mBuffer->ReleaseNonOwningReference();
mBufferList.DiscardUnreferencedPrefix(old_start.mBuffer);
mBufferList->DiscardUnreferencedPrefix(old_start.mBuffer);
}
const PRUnichar*
nsSlidingString::GetReadableFragment( nsReadableFragment<PRUnichar>& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const
{
return nsSlidingSubstring::GetReadableFragment(aFragment, aRequest, aOffset);
}