2000-12-12 20:13:33 +00: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 nsBufferHandleUtils_h___
|
|
|
|
#define nsBufferHandleUtils_h___
|
|
|
|
|
2001-04-02 19:40:52 +00:00
|
|
|
#ifndef nsAString_h___
|
|
|
|
#include "nsAString.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef nsStringTraits_h___
|
|
|
|
#include "nsStringTraits.h"
|
2000-12-12 20:13:33 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <new.h>
|
|
|
|
// for placement |new|
|
|
|
|
|
|
|
|
|
2001-04-02 19:40:52 +00:00
|
|
|
template <class CharT>
|
|
|
|
class nsAutoBufferHandle
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsAutoBufferHandle() : mHandle(0) { }
|
|
|
|
|
|
|
|
nsAutoBufferHandle( const nsAutoBufferHandle<CharT>& aOther )
|
|
|
|
: mHandle(aOther.get())
|
|
|
|
{
|
|
|
|
if ( mHandle)
|
|
|
|
mHandle->AcquireReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit
|
|
|
|
nsAutoBufferHandle( const nsSharedBufferHandle<CharT>* aHandle )
|
2001-04-02 22:42:49 +00:00
|
|
|
: mHandle( NS_CONST_CAST(nsSharedBufferHandle<CharT>*, aHandle) )
|
2001-04-02 19:40:52 +00:00
|
|
|
{
|
|
|
|
if ( mHandle)
|
|
|
|
mHandle->AcquireReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
~nsAutoBufferHandle()
|
|
|
|
{
|
|
|
|
if ( mHandle )
|
|
|
|
mHandle->ReleaseReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoBufferHandle<CharT>&
|
|
|
|
operator=( const nsSharedBufferHandle<CharT>* rhs )
|
|
|
|
{
|
|
|
|
nsSharedBufferHandle<CharT>* old_handle = mHandle;
|
|
|
|
if ( (mHandle = NS_CONST_CAST(nsSharedBufferHandle<CharT>*, rhs)) )
|
|
|
|
mHandle->AcquireReference();
|
|
|
|
if ( old_handle )
|
|
|
|
old_handle->ReleaseReference();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoBufferHandle<CharT>&
|
|
|
|
operator=( const nsAutoBufferHandle<CharT>& rhs )
|
|
|
|
{
|
|
|
|
return operator=(rhs.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSharedBufferHandle<CharT>*
|
|
|
|
get() const
|
|
|
|
{
|
|
|
|
return mHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator nsSharedBufferHandle<CharT>*() const
|
|
|
|
{
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSharedBufferHandle<CharT>*
|
|
|
|
operator->() const
|
|
|
|
{
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsSharedBufferHandle<CharT>* mHandle;
|
|
|
|
};
|
|
|
|
|
2000-12-12 20:13:33 +00:00
|
|
|
|
|
|
|
template <class HandleT, class CharT>
|
|
|
|
inline
|
|
|
|
size_t
|
|
|
|
NS_AlignedHandleSize( const HandleT*, const CharT* )
|
|
|
|
{
|
|
|
|
// figure out the number of bytes needed the |HandleT| part, including padding to correctly align the data part
|
|
|
|
return ((sizeof(HandleT) + sizeof(CharT) - 1) / sizeof(CharT)) * sizeof(CharT);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class HandleT, class CharT>
|
|
|
|
inline
|
|
|
|
const CharT*
|
|
|
|
NS_DataAfterHandle( const HandleT* aHandlePtr, const CharT* aDummyCharTPtr )
|
|
|
|
{
|
|
|
|
typedef const CharT* CharT_ptr;
|
|
|
|
return CharT_ptr(NS_STATIC_CAST(const unsigned char*, aHandlePtr) + NS_AlignedHandleSize(aHandlePtr, aDummyCharTPtr));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class HandleT, class CharT>
|
|
|
|
inline
|
|
|
|
CharT*
|
|
|
|
NS_DataAfterHandle( HandleT* aHandlePtr, const CharT* aDummyCharTPtr )
|
|
|
|
{
|
|
|
|
typedef CharT* CharT_ptr;
|
|
|
|
return CharT_ptr(NS_STATIC_CAST(unsigned char*, aHandlePtr) + NS_AlignedHandleSize(aHandlePtr, aDummyCharTPtr));
|
|
|
|
}
|
|
|
|
|
2001-04-02 19:40:52 +00:00
|
|
|
template <class HandleT, class StringT>
|
2000-12-12 20:13:33 +00:00
|
|
|
HandleT*
|
2001-04-02 19:40:52 +00:00
|
|
|
NS_AllocateContiguousHandleWithData( const HandleT* aDummyHandlePtr, const StringT& aDataSource, PRUint32 aAdditionalCapacity )
|
2000-12-12 20:13:33 +00:00
|
|
|
{
|
2001-04-02 19:40:52 +00:00
|
|
|
typedef typename StringT::char_type char_type;
|
|
|
|
typedef char_type* char_ptr;
|
2000-12-12 20:13:33 +00:00
|
|
|
|
|
|
|
// figure out the number of bytes needed the |HandleT| part, including padding to correctly align the data part
|
2001-04-02 19:40:52 +00:00
|
|
|
size_t handle_size = NS_AlignedHandleSize(aDummyHandlePtr, char_ptr(0));
|
2000-12-12 20:13:33 +00:00
|
|
|
|
2001-04-02 19:40:52 +00:00
|
|
|
// figure out how many |char_type|s wee need to fit in the data part
|
2000-12-12 20:13:33 +00:00
|
|
|
size_t data_length = aDataSource.Length();
|
|
|
|
size_t buffer_length = data_length + aAdditionalCapacity;
|
|
|
|
|
|
|
|
// how many bytes is that (including a zero-terminator so we can claim to be flat)?
|
2001-04-02 19:40:52 +00:00
|
|
|
size_t buffer_size = buffer_length * sizeof(char_type);
|
2000-12-12 20:13:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
HandleT* result = 0;
|
|
|
|
void* handle_ptr = ::operator new(handle_size + buffer_size);
|
|
|
|
|
|
|
|
if ( handle_ptr )
|
|
|
|
{
|
2001-04-02 19:40:52 +00:00
|
|
|
char_ptr data_start_ptr = char_ptr(NS_STATIC_CAST(unsigned char*, handle_ptr) + handle_size);
|
|
|
|
char_ptr data_end_ptr = data_start_ptr + data_length;
|
|
|
|
char_ptr buffer_end_ptr = data_start_ptr + buffer_length;
|
2000-12-12 20:13:33 +00:00
|
|
|
|
2001-04-02 19:40:52 +00:00
|
|
|
typename StringT::const_iterator fromBegin, fromEnd;
|
|
|
|
char_ptr toBegin = data_start_ptr;
|
2000-12-12 20:13:33 +00:00
|
|
|
copy_string(aDataSource.BeginReading(fromBegin), aDataSource.EndReading(fromEnd), toBegin);
|
|
|
|
|
|
|
|
// and if the caller bothered asking for a buffer bigger than their string, we'll zero-terminate
|
|
|
|
if ( aAdditionalCapacity > 0 )
|
2001-04-02 19:40:52 +00:00
|
|
|
*toBegin = char_type(0);
|
2000-12-12 20:13:33 +00:00
|
|
|
|
|
|
|
result = new (handle_ptr) HandleT(data_start_ptr, data_end_ptr, data_start_ptr, buffer_end_ptr, PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !defined(nsBufferHandleUtils_h___)
|