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)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2001-03-25 22:06:27 +04:00
|
|
|
/* nsBufferHandle.h --- the collection of classes that describe the atomic hunks of strings */
|
|
|
|
|
2000-09-09 04:02:48 +04:00
|
|
|
#ifndef nsBufferHandle_h___
|
|
|
|
#define nsBufferHandle_h___
|
2000-09-09 03:54:23 +04:00
|
|
|
|
2001-06-29 16:47:43 +04:00
|
|
|
#ifndef nsStringDefines_h___
|
|
|
|
#include "nsStringDefines.h"
|
|
|
|
#endif
|
|
|
|
|
2000-09-09 08:42:19 +04:00
|
|
|
#include "prtypes.h"
|
|
|
|
// for |PRBool|
|
|
|
|
|
|
|
|
#include "nsDebug.h"
|
|
|
|
// for |NS_ASSERTION|
|
|
|
|
|
2001-03-25 05:14:26 +04:00
|
|
|
#include "nscore.h"
|
2001-03-25 22:06:27 +04:00
|
|
|
// for |PRUnichar|, |NS_REINTERPRET_CAST|
|
|
|
|
|
2001-05-11 12:41:21 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
// VC++ erroneously warns about incompatible linkage in these templates
|
|
|
|
// It's a lie, and it's bothersome. This |#pragma| quiets the warning.
|
|
|
|
#pragma warning( disable: 4251 )
|
|
|
|
#endif
|
2001-03-25 22:06:27 +04:00
|
|
|
|
2001-11-28 08:22:57 +03:00
|
|
|
/*
|
|
|
|
* The classes in this file are collectively called `buffer handles'.
|
|
|
|
* All buffer handles begin with a pointer-tuple that delimits the
|
|
|
|
* useful content of a hunk of string. A buffer handle that points to
|
|
|
|
* a sharable hunk of string data additionally has a field which
|
|
|
|
* multiplexes some flags and a reference count.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* ns[Const]BufferHandle nsSharedBufferHandle
|
|
|
|
* +-----+-----+-----+-----+ +-----+-----+-----+-----+
|
|
|
|
* | mDataStart | | mDataStart |
|
|
|
|
* +-----+-----+-----+-----+ +-----+-----+-----+-----+
|
|
|
|
* | mDataEnd | | mDataEnd |
|
|
|
|
* +-----+-----+-----+-----+ +-----+-----+-----+-----+
|
|
|
|
* | mFlags |
|
|
|
|
* +-----+-----+-----+-----+
|
|
|
|
* | mStorageLength |
|
|
|
|
* +-----+-----+-----+-----+
|
|
|
|
* . mAllocator .
|
|
|
|
* .........................
|
|
|
|
*
|
|
|
|
* Given only a |ns[Const]BufferHandle|, there is no legal way to tell
|
|
|
|
* if it is sharable. In all cases, the data might immediately follow
|
|
|
|
* the handle in the same allocated block. From the |mFlags| field,
|
|
|
|
* you can tell exactly what configuration of a handle you actually
|
|
|
|
* have.
|
|
|
|
*
|
|
|
|
* An |nsSharedBufferHandle| has the limitation that its |mDataStart|
|
|
|
|
* must also be the beginning of the allocated storage. However,
|
|
|
|
* allowing otherwise would introduce significant additional
|
|
|
|
* complexity for a feature that would better be handled by allowing
|
|
|
|
* an owninng substring string class that owned a reference to the
|
|
|
|
* buffer handle.
|
2001-03-25 22:06:27 +04:00
|
|
|
*/
|
|
|
|
|
2000-09-09 08:42:19 +04:00
|
|
|
|
2000-09-09 03:54:23 +04:00
|
|
|
/**
|
|
|
|
*
|
2002-08-01 21:52:56 +04:00
|
|
|
* @status FROZEN
|
2000-09-09 03:54:23 +04:00
|
|
|
*/
|
|
|
|
template <class CharT>
|
|
|
|
class nsBufferHandle
|
|
|
|
{
|
|
|
|
public:
|
2001-11-28 08:22:57 +03:00
|
|
|
typedef PRUint32 size_type;
|
|
|
|
|
2001-05-22 03:45:03 +04:00
|
|
|
nsBufferHandle() { }
|
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
|
|
|
|
2001-11-28 08:22:57 +03:00
|
|
|
size_type 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
|
|
|
};
|
|
|
|
|
2002-08-01 21:52:56 +04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @status FROZEN
|
|
|
|
*/
|
2001-03-25 22:06:27 +04:00
|
|
|
template <class CharT>
|
|
|
|
class nsConstBufferHandle
|
|
|
|
{
|
|
|
|
public:
|
2001-11-28 08:22:57 +03:00
|
|
|
typedef PRUint32 size_type;
|
|
|
|
|
2001-05-22 03:45:03 +04:00
|
|
|
nsConstBufferHandle() { }
|
2001-03-25 22:06:27 +04:00
|
|
|
nsConstBufferHandle( const CharT* aDataStart, const CharT* aDataEnd ) : mDataStart(aDataStart), mDataEnd(aDataEnd) { }
|
|
|
|
|
2001-05-22 03:45:03 +04:00
|
|
|
void DataStart( const CharT* aNewDataStart ) { mDataStart = aNewDataStart; }
|
2001-03-25 22:06:27 +04:00
|
|
|
const CharT* DataStart() const { return mDataStart; }
|
2001-05-22 03:45:03 +04:00
|
|
|
|
|
|
|
void DataEnd( const CharT* aNewDataEnd ) { mDataEnd = aNewDataEnd; }
|
2001-03-25 22:06:27 +04:00
|
|
|
const CharT* DataEnd() const { return mDataEnd; }
|
2001-05-22 03:45:03 +04:00
|
|
|
|
2001-11-28 08:22:57 +03:00
|
|
|
size_type DataLength() const { return mDataEnd - mDataStart; }
|
2001-03-25 22:06:27 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
const CharT* mDataStart;
|
|
|
|
const CharT* mDataEnd;
|
|
|
|
};
|
|
|
|
|
2000-09-09 03:54:23 +04:00
|
|
|
|
2001-03-25 05:14:26 +04:00
|
|
|
/**
|
|
|
|
* string allocator stuff needs to move to its own file
|
|
|
|
* also see http://bugzilla.mozilla.org/show_bug.cgi?id=70087
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <class CharT>
|
|
|
|
class nsStringAllocator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// more later
|
|
|
|
virtual void Deallocate( CharT* ) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the following two routines must be provided by the client embedding strings
|
|
|
|
*/
|
2001-03-25 06:26:33 +04:00
|
|
|
NS_COM nsStringAllocator<char>& StringAllocator_char();
|
|
|
|
NS_COM nsStringAllocator<PRUnichar>& StringAllocator_wchar_t();
|
2001-03-25 05:14:26 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this traits class lets templated clients pick the appropriate non-template global allocator
|
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
struct nsStringAllocatorTraits
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_SPECIALIZE_TEMPLATE
|
|
|
|
struct nsStringAllocatorTraits<char>
|
|
|
|
{
|
|
|
|
static nsStringAllocator<char>& global_string_allocator() { return StringAllocator_char(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_SPECIALIZE_TEMPLATE
|
|
|
|
struct nsStringAllocatorTraits<PRUnichar>
|
|
|
|
{
|
|
|
|
static nsStringAllocator<PRUnichar>& global_string_allocator() { return StringAllocator_wchar_t(); }
|
|
|
|
};
|
|
|
|
|
2001-03-25 22:06:27 +04:00
|
|
|
// end of string allocator stuff that needs to move
|
|
|
|
|
2001-03-25 05:14:26 +04:00
|
|
|
|
2000-09-09 03:54:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2002-08-01 21:52:56 +04:00
|
|
|
* @status FROZEN
|
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
|
|
|
{
|
2001-11-28 08:22:57 +03:00
|
|
|
public:
|
|
|
|
typedef PRUint32 size_type;
|
|
|
|
|
2000-09-09 04:25:00 +04:00
|
|
|
protected:
|
|
|
|
enum
|
2000-09-09 03:54:23 +04:00
|
|
|
{
|
2001-12-11 06:33:09 +03:00
|
|
|
kIsImmutable = 0x01000000, // if this is set, the buffer cannot be modified even if its refcount is 1
|
2001-11-28 08:22:57 +03:00
|
|
|
kIsSingleAllocationWithBuffer = 0x02000000, // handle and buffer are one piece, no separate deallocation is possible for the buffer
|
|
|
|
kIsUserAllocator = 0x04000000, // can't |delete|, call a hook instead
|
2001-05-11 12:41:21 +04:00
|
|
|
|
|
|
|
// the following flags are opaque to the string library itself
|
|
|
|
kIsNULL = 0x80000000, // the most common request of external clients is a scheme by which they can express `NULL'-ness
|
|
|
|
kImplementationFlagsMask = 0xF0000000, // 4 bits for use by site-implementations, e.g., for `NULL'-ness
|
|
|
|
|
|
|
|
kFlagsMask = 0xFF000000,
|
|
|
|
kRefCountMask = 0x00FFFFFF
|
2000-09-09 04:25:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2001-11-28 08:22:57 +03:00
|
|
|
nsSharedBufferHandle( CharT* aDataStart, CharT* aDataEnd, size_type aStorageLength, PRBool isSingleAllocation )
|
|
|
|
: nsBufferHandle<CharT>(aDataStart, aDataEnd),
|
2001-12-11 06:33:09 +03:00
|
|
|
mFlags(0),
|
2001-11-28 08:22:57 +03:00
|
|
|
mStorageLength(aStorageLength)
|
2001-04-02 23:40:52 +04:00
|
|
|
{
|
|
|
|
if ( isSingleAllocation )
|
|
|
|
mFlags |= kIsSingleAllocationWithBuffer;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-01-16 06:08:22 +03:00
|
|
|
void ReleaseReference() const;
|
2000-09-09 03:54:23 +04:00
|
|
|
|
|
|
|
PRBool
|
|
|
|
IsReferenced() const
|
|
|
|
{
|
|
|
|
return get_refcount() != 0;
|
|
|
|
}
|
|
|
|
|
2001-11-28 08:22:57 +03:00
|
|
|
PRBool
|
2001-12-11 06:33:09 +03:00
|
|
|
IsMutable() const
|
2001-11-28 08:22:57 +03:00
|
|
|
{
|
2001-12-11 06:33:09 +03:00
|
|
|
// (get_refcount() == 1) && !(GetImplementationFlags() & kIsImmutable)
|
|
|
|
return (mFlags & (kRefCountMask | kIsImmutable) == 1);
|
2001-11-28 08:22:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void StorageLength( size_type aNewStorageLength )
|
|
|
|
{
|
|
|
|
mStorageLength = aNewStorageLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_type
|
|
|
|
StorageLength() const
|
|
|
|
{
|
|
|
|
return mStorageLength;
|
|
|
|
}
|
|
|
|
|
2001-04-02 23:40:52 +04:00
|
|
|
PRUint32
|
|
|
|
GetImplementationFlags() const
|
|
|
|
{
|
|
|
|
return mFlags & kImplementationFlagsMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetImplementationFlags( PRUint32 aNewFlags )
|
|
|
|
{
|
|
|
|
mFlags = (mFlags & ~kImplementationFlagsMask) | (aNewFlags & kImplementationFlagsMask);
|
|
|
|
}
|
|
|
|
|
2000-09-09 03:54:23 +04:00
|
|
|
protected:
|
2000-09-09 04:25:00 +04:00
|
|
|
PRUint32 mFlags;
|
2001-11-28 08:22:57 +03:00
|
|
|
size_type mStorageLength;
|
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;
|
|
|
|
}
|
2001-03-25 05:14:26 +04:00
|
|
|
|
|
|
|
nsStringAllocator<CharT>& get_allocator() const;
|
2000-09-09 03:54:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-03-25 05:14:26 +04:00
|
|
|
template <class CharT>
|
|
|
|
class nsSharedBufferHandleWithAllocator
|
|
|
|
: public nsSharedBufferHandle<CharT>
|
|
|
|
{
|
|
|
|
public:
|
2001-11-28 08:22:57 +03:00
|
|
|
// why is this needed again?
|
|
|
|
typedef PRUint32 size_type;
|
2001-03-25 05:14:26 +04:00
|
|
|
|
2001-11-28 08:22:57 +03:00
|
|
|
nsSharedBufferHandleWithAllocator( CharT* aDataStart, CharT* aDataEnd, size_type aStorageLength, nsStringAllocator<CharT>& aAllocator )
|
|
|
|
: nsSharedBufferHandle<CharT>(aDataStart, aDataEnd,
|
|
|
|
aStorageLength, PR_FALSE),
|
2001-03-25 05:14:26 +04:00
|
|
|
mAllocator(aAllocator)
|
|
|
|
{
|
2001-03-25 22:06:27 +04:00
|
|
|
this->mFlags |= this->kIsUserAllocator;
|
2001-03-25 05:14:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsStringAllocator<CharT>& get_allocator() const { return mAllocator; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsStringAllocator<CharT>& mAllocator;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-01-16 06:08:22 +03:00
|
|
|
// Derive from this class to implement a |Destroy| method.
|
|
|
|
template <class CharT>
|
|
|
|
class nsSharedBufferHandleWithDestroy
|
|
|
|
: public nsSharedBufferHandle<CharT>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// why is this needed again?
|
|
|
|
typedef PRUint32 size_type;
|
|
|
|
|
|
|
|
nsSharedBufferHandleWithDestroy( CharT* aDataStart, CharT* aDataEnd, size_type aStorageLength)
|
|
|
|
: nsSharedBufferHandle<CharT>(aDataStart, aDataEnd,
|
|
|
|
aStorageLength, PR_FALSE)
|
|
|
|
{
|
|
|
|
this->mFlags |=
|
|
|
|
this->kIsUserAllocator | this->kIsSingleAllocationWithBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Destroy() = 0;
|
|
|
|
|
|
|
|
// This doesn't really need to be |virtual|, but it saves us from
|
|
|
|
// having to turn off gcc warnings that might be useful to
|
|
|
|
// someone.
|
|
|
|
virtual ~nsSharedBufferHandleWithDestroy() { }
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class CharT>
|
|
|
|
class nsNonDestructingSharedBufferHandle
|
|
|
|
: public nsSharedBufferHandleWithDestroy<CharT>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// why is this needed again?
|
|
|
|
typedef PRUint32 size_type;
|
|
|
|
|
|
|
|
nsNonDestructingSharedBufferHandle( CharT* aDataStart, CharT* aDataEnd, size_type aStorageLength)
|
|
|
|
: nsSharedBufferHandleWithDestroy<CharT>(aDataStart, aDataEnd,
|
|
|
|
aStorageLength)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Destroy()
|
|
|
|
{
|
|
|
|
// Oops, threads raced to set the refcount. Set the refcount
|
|
|
|
// back to 1.
|
2002-01-16 07:48:24 +03:00
|
|
|
this->set_refcount(1);
|
2002-01-16 06:08:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-03-25 05:14:26 +04:00
|
|
|
template <class CharT>
|
|
|
|
nsStringAllocator<CharT>&
|
|
|
|
nsSharedBufferHandle<CharT>::get_allocator() const
|
2001-03-25 22:06:27 +04:00
|
|
|
// really don't want this to be |inline|
|
2001-03-25 05:14:26 +04:00
|
|
|
{
|
|
|
|
if ( mFlags & kIsUserAllocator )
|
|
|
|
{
|
2001-11-28 08:22:57 +03:00
|
|
|
return NS_REINTERPRET_CAST(const nsSharedBufferHandleWithAllocator<CharT>*, this)->get_allocator();
|
2001-03-25 05:14:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsStringAllocatorTraits<CharT>::global_string_allocator();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) )
|
|
|
|
{
|
2000-09-09 16:53:38 +04:00
|
|
|
CharT* string_storage = this->mDataStart;
|
2001-03-25 05:14:26 +04:00
|
|
|
get_allocator().Deallocate(string_storage);
|
2000-09-09 04:25:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-16 06:08:22 +03:00
|
|
|
template <class CharT>
|
|
|
|
void
|
|
|
|
nsSharedBufferHandle<CharT>::ReleaseReference() const
|
|
|
|
{
|
|
|
|
nsSharedBufferHandle<CharT>* mutable_this = NS_CONST_CAST(nsSharedBufferHandle<CharT>*, this);
|
|
|
|
if ( !mutable_this->set_refcount( get_refcount()-1 ) )
|
|
|
|
{
|
|
|
|
if ( ~mFlags & (kIsUserAllocator|kIsSingleAllocationWithBuffer) )
|
|
|
|
delete mutable_this;
|
|
|
|
else
|
|
|
|
// If |kIsUserAllocator| and |kIsSingleAllocationWithBuffer|,
|
|
|
|
// the handle is an |nsSharedBufferHandleWithDestroy<CharT>|,
|
|
|
|
// so call its |Destroy| method.
|
|
|
|
NS_STATIC_CAST(nsSharedBufferHandleWithDestroy<CharT>*,
|
|
|
|
mutable_this)->Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-09 04:02:48 +04:00
|
|
|
|
|
|
|
#endif // !defined(nsBufferHandle_h___)
|