merge the formfield patch from ooo-build
[ooovba.git] / sal / rtl / source / byteseq.c
blob10399bdb3068a1a833c236251100a0a203458a24
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: byteseq.c,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <osl/diagnose.h>
32 #include <osl/interlck.h>
34 #include <rtl/byteseq.h>
35 #include <rtl/alloc.h>
36 #include <rtl/memory.h>
38 /* static data to be referenced by all empty strings
39 * the refCount is predefined to 1 and must never become 0 !
41 static sal_Sequence aEmpty_rtl_ByteSeq =
43 1, /* sal_Int32 refCount; */
44 0, /* sal_Int32 length; */
45 { 0 } /* sal_Unicode buffer[1]; */
48 //==================================================================================================
49 void SAL_CALL rtl_byte_sequence_reference2One(
50 sal_Sequence ** ppSequence )
52 sal_Sequence * pSequence, * pNew;
53 sal_Int32 nElements;
55 OSL_ENSURE( ppSequence, "### null ptr!" );
56 pSequence = *ppSequence;
58 if (pSequence->nRefCount > 1)
60 nElements = pSequence->nElements;
61 if (nElements)
63 pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nElements );
65 if ( pNew != 0 )
66 rtl_copyMemory( pNew->elements, pSequence->elements, nElements );
68 if (! osl_decrementInterlockedCount( &pSequence->nRefCount ))
69 rtl_freeMemory( pSequence );
71 else
73 pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE );
76 if ( pNew != 0 )
78 pNew->nRefCount = 1;
79 pNew->nElements = nElements;
82 *ppSequence = pNew;
86 //==================================================================================================
87 void SAL_CALL rtl_byte_sequence_realloc(
88 sal_Sequence ** ppSequence, sal_Int32 nSize )
90 sal_Sequence * pSequence, * pNew;
91 sal_Int32 nElements;
93 OSL_ENSURE( ppSequence, "### null ptr!" );
94 pSequence = *ppSequence;
95 nElements = pSequence->nElements;
97 if (nElements == nSize)
98 return;
100 if (pSequence->nRefCount > 1) // split
102 pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nSize );
104 if ( pNew != 0 )
106 if (nSize > nElements)
108 rtl_copyMemory( pNew->elements, pSequence->elements, nElements );
109 rtl_zeroMemory( pNew->elements + nElements, nSize - nElements );
111 else
113 rtl_copyMemory( pNew->elements, pSequence->elements, nSize );
117 if (! osl_decrementInterlockedCount( &pSequence->nRefCount ))
118 rtl_freeMemory( pSequence );
119 pSequence = pNew;
121 else
123 pSequence = (sal_Sequence *)rtl_reallocateMemory(
124 pSequence, SAL_SEQUENCE_HEADER_SIZE + nSize );
127 if ( pSequence != 0 )
129 pSequence->nRefCount = 1;
130 pSequence->nElements = nSize;
133 *ppSequence = pSequence;
136 //==================================================================================================
137 void SAL_CALL rtl_byte_sequence_acquire( sal_Sequence *pSequence )
139 OSL_ASSERT( pSequence );
140 osl_incrementInterlockedCount( &(pSequence->nRefCount) );
143 //==================================================================================================
144 void SAL_CALL rtl_byte_sequence_release( sal_Sequence *pSequence )
146 if ( pSequence != 0 )
148 if (! osl_decrementInterlockedCount( &(pSequence->nRefCount )) )
150 rtl_freeMemory( pSequence );
155 //==================================================================================================
156 void SAL_CALL rtl_byte_sequence_construct( sal_Sequence **ppSequence , sal_Int32 nLength )
158 OSL_ASSERT( ppSequence );
159 if( *ppSequence )
161 rtl_byte_sequence_release( *ppSequence );
162 *ppSequence = 0;
165 if( nLength )
167 *ppSequence = (sal_Sequence *) rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
169 if ( *ppSequence != 0 )
171 (*ppSequence)->nRefCount = 1;
172 (*ppSequence)->nElements = nLength;
175 else
177 *ppSequence = &aEmpty_rtl_ByteSeq;
178 rtl_byte_sequence_acquire( *ppSequence );
182 //==================================================================================================
183 void SAL_CALL rtl_byte_sequence_constructNoDefault( sal_Sequence **ppSequence , sal_Int32 nLength )
185 OSL_ASSERT( ppSequence );
186 if( *ppSequence )
188 rtl_byte_sequence_release( *ppSequence );
189 *ppSequence = 0;
192 *ppSequence = (sal_Sequence *) rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
194 if ( *ppSequence != 0 )
196 (*ppSequence)->nRefCount = 1;
197 (*ppSequence)->nElements = nLength;
201 //==================================================================================================
202 void SAL_CALL rtl_byte_sequence_constructFromArray(
203 sal_Sequence **ppSequence, const sal_Int8 *pData , sal_Int32 nLength )
205 rtl_byte_sequence_constructNoDefault( ppSequence , nLength );
206 if ( *ppSequence != 0 )
207 rtl_copyMemory( (*ppSequence)->elements, pData, nLength );
210 //==================================================================================================
211 void SAL_CALL rtl_byte_sequence_assign( sal_Sequence **ppSequence , sal_Sequence *pSequence )
213 if ( *ppSequence != pSequence)
215 if( *ppSequence )
217 rtl_byte_sequence_release( *ppSequence );
219 *ppSequence = pSequence;
220 rtl_byte_sequence_acquire( *ppSequence );
222 // else
223 // nothing to do
227 //==================================================================================================
228 sal_Bool SAL_CALL rtl_byte_sequence_equals( sal_Sequence *pSequence1 , sal_Sequence *pSequence2 )
230 OSL_ASSERT( pSequence1 );
231 OSL_ASSERT( pSequence2 );
232 if (pSequence1 == pSequence2)
234 return sal_True;
236 if (pSequence1->nElements != pSequence2->nElements)
238 return sal_False;
240 return (sal_Bool)
241 (rtl_compareMemory(
242 pSequence1->elements, pSequence2->elements, pSequence1->nElements )
243 == 0);
247 //==================================================================================================
248 const sal_Int8 *SAL_CALL rtl_byte_sequence_getConstArray( sal_Sequence *pSequence )
250 return ((const sal_Int8*)(pSequence->elements));
253 //==================================================================================================
254 sal_Int32 SAL_CALL rtl_byte_sequence_getLength( sal_Sequence *pSequence )
256 return pSequence->nElements;