2000-09-09 04:02:48 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/*
|
|
|
|
* The contents of this file are subject to the Mozilla Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla strings.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 2000 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Scott Collins <scc@mozilla.org> (original author)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef nsBufferHandle_h___
|
|
|
|
#define nsBufferHandle_h___
|
2000-09-09 03:54:23 +04:00
|
|
|
|
2000-09-09 08:42:19 +04:00
|
|
|
#include <stddef.h>
|
|
|
|
// for |ptrdiff_t|
|
|
|
|
|
|
|
|
#include "prtypes.h"
|
|
|
|
// for |PRBool|
|
|
|
|
|
|
|
|
#include "nsDebug.h"
|
|
|
|
// for |NS_ASSERTION|
|
|
|
|
|
|
|
|
#include "nsMemory.h"
|
|
|
|
// for |nsMemory::Free|
|
|
|
|
|
2000-09-09 03:54:23 +04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
template <class CharT>
|
|
|
|
class nsBufferHandle
|
|
|
|
{
|
|
|
|
public:
|
2000-09-09 08:42:19 +04:00
|
|
|
nsBufferHandle( CharT* aDataStart, CharT* aDataEnd ) : mDataStart(aDataStart), mDataEnd(aDataEnd) { }
|
2000-09-09 03:54:23 +04:00
|
|
|
|
2000-09-09 08:42:19 +04:00
|
|
|
void DataStart( CharT* aNewDataStart ) { mDataStart = aNewDataStart; }
|
|
|
|
CharT* DataStart() { return mDataStart; }
|
|
|
|
const CharT* DataStart() const { return mDataStart; }
|
2000-09-09 03:54:23 +04:00
|
|
|
|
2000-09-09 08:42:19 +04:00
|
|
|
void DataEnd( CharT* aNewDataEnd ) { mDataEnd = aNewDataEnd; }
|
|
|
|
CharT* DataEnd() { return mDataEnd; }
|
|
|
|
const CharT* DataEnd() const { return mDataEnd; }
|
2000-09-09 03:54:23 +04:00
|
|
|
|
2000-09-09 08:42:19 +04:00
|
|
|
// void DataLength( ptrdiff_t aNewDataLength ) { mDataEnd = mDataStart+aNewDataLength; }
|
|
|
|
ptrdiff_t DataLength() const { return mDataEnd - mDataStart; }
|
2000-09-09 03:54:23 +04:00
|
|
|
|
|
|
|
protected:
|
2000-09-09 08:42:19 +04:00
|
|
|
CharT* mDataStart;
|
|
|
|
CharT* mDataEnd;
|
2000-09-09 03:54:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
template <class CharT>
|
|
|
|
class nsSharedBufferHandle
|
2000-09-09 08:42:19 +04:00
|
|
|
: public nsBufferHandle<CharT>
|
2000-09-09 03:54:23 +04:00
|
|
|
{
|
2000-09-09 04:25:00 +04:00
|
|
|
protected:
|
|
|
|
enum
|
2000-09-09 03:54:23 +04:00
|
|
|
{
|
2000-09-09 04:25:00 +04:00
|
|
|
kIsShared = 1<<31,
|
|
|
|
kIsSingleAllocationWithBuffer = 1<<30,
|
|
|
|
kIsStorageDefinedSeparately = 1<<29,
|
|
|
|
|
|
|
|
kFlagsMask = kIsShared | kIsSingleAllocationWithBuffer | kIsStorageDefinedSeparately,
|
|
|
|
kRefCountMask = ~kFlagsMask
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2000-09-09 08:42:19 +04:00
|
|
|
nsSharedBufferHandle( CharT* aDataStart, CharT* aDataEnd )
|
2000-09-09 13:44:45 +04:00
|
|
|
: nsBufferHandle<CharT>(aDataStart, aDataEnd)
|
2000-09-09 08:42:19 +04:00
|
|
|
{
|
|
|
|
mFlags |= kIsShared;
|
|
|
|
}
|
|
|
|
|
2000-09-09 04:25:00 +04:00
|
|
|
~nsSharedBufferHandle();
|
2000-09-09 03:54:23 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
AcquireReference() const
|
|
|
|
{
|
2000-09-09 10:32:46 +04:00
|
|
|
nsSharedBufferHandle<CharT>* mutable_this = NS_CONST_CAST(nsSharedBufferHandle<CharT>*, this);
|
|
|
|
mutable_this->set_refcount( get_refcount()+1 );
|
2000-09-09 03:54:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ReleaseReference() const
|
|
|
|
{
|
2000-09-09 10:32:46 +04:00
|
|
|
nsSharedBufferHandle<CharT>* mutable_this = NS_CONST_CAST(nsSharedBufferHandle<CharT>*, this);
|
|
|
|
if ( !mutable_this->set_refcount( get_refcount()-1 ) )
|
|
|
|
delete mutable_this;
|
2000-09-09 03:54:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
IsReferenced() const
|
|
|
|
{
|
|
|
|
return get_refcount() != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2000-09-09 04:25:00 +04:00
|
|
|
PRUint32 mFlags;
|
2000-09-09 03:54:23 +04:00
|
|
|
|
|
|
|
PRUint32
|
|
|
|
get_refcount() const
|
|
|
|
{
|
|
|
|
return mFlags & kRefCountMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32
|
|
|
|
set_refcount( PRUint32 aNewRefCount )
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aNewRefCount <= kRefCountMask, "aNewRefCount <= kRefCountMask");
|
|
|
|
|
|
|
|
mFlags = (mFlags & kFlagsMask) | aNewRefCount;
|
|
|
|
return aNewRefCount;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-09-09 04:25:00 +04:00
|
|
|
// need a name for this
|
2000-09-09 03:54:23 +04:00
|
|
|
template <class CharT>
|
2000-09-09 04:25:00 +04:00
|
|
|
class nsXXXBufferHandle
|
2000-09-09 03:54:23 +04:00
|
|
|
: public nsSharedBufferHandle<CharT>
|
|
|
|
{
|
|
|
|
public:
|
2000-09-09 08:42:19 +04:00
|
|
|
nsXXXBufferHandle( CharT* aDataStart, CharT* aDataEnd, CharT* aStorageStart, CharT* aStorageEnd )
|
2000-09-09 13:44:45 +04:00
|
|
|
: nsSharedBufferHandle<CharT>(aDataStart, aDataEnd),
|
2000-09-09 08:42:19 +04:00
|
|
|
mStorageStart(aStorageStart),
|
|
|
|
mStorageEnd(aStorageEnd)
|
|
|
|
{
|
|
|
|
mFlags |= kIsStorageDefinedSeparately;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StorageStart( CharT* aNewStorageStart ) { mStorageStart = aNewStorageStart; }
|
|
|
|
CharT* StorageStart() { return mStorageStart; }
|
|
|
|
const CharT* StorageStart() const { return mStorageStart; }
|
|
|
|
|
|
|
|
void StorageEnd( CharT* aNewStorageEnd ) { mStorageEnd = aNewStorageEnd; }
|
|
|
|
CharT* StorageEnd() { return mStorageEnd; }
|
|
|
|
const CharT* StorageEnd() const { return mStorageEnd; }
|
|
|
|
|
|
|
|
// void StorageLength( ptrdiff_t aNewStorageLength ) { mStorageEnd = mStorageStart+aNewStorageLength; }
|
|
|
|
ptrdiff_t StorageLength() const { return mStorageEnd - mStorageStart; }
|
2000-09-09 03:54:23 +04:00
|
|
|
|
|
|
|
protected:
|
2000-09-09 08:42:19 +04:00
|
|
|
CharT* mStorageStart;
|
|
|
|
CharT* mStorageEnd;
|
2000-09-09 03:54:23 +04:00
|
|
|
};
|
2000-09-09 04:25:00 +04:00
|
|
|
|
|
|
|
template <class CharT>
|
|
|
|
nsSharedBufferHandle<CharT>::~nsSharedBufferHandle()
|
|
|
|
// really don't want this to be |inline|
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!IsReferenced(), "!IsReferenced()");
|
|
|
|
|
|
|
|
if ( !(mFlags & kIsSingleAllocationWithBuffer) )
|
|
|
|
{
|
|
|
|
CharT* string_storage = mDataStart;
|
|
|
|
if ( mFlags & kIsStorageDefinedSeparately )
|
2000-09-09 14:24:45 +04:00
|
|
|
string_storage = NS_REINTERPRET_CAST(nsXXXBufferHandle<CharT>*, this)->StorageStart();
|
2000-09-09 04:25:00 +04:00
|
|
|
nsMemory::Free(string_storage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-09 04:02:48 +04:00
|
|
|
|
|
|
|
#endif // !defined(nsBufferHandle_h___)
|