merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / rtl / source / byteseq.c
blob42b905a8d78f57f5bbc15e9c343aad0cd85f83be
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include <osl/diagnose.h>
29 #include <osl/interlck.h>
31 #include <rtl/byteseq.h>
32 #include <rtl/alloc.h>
33 #include <rtl/memory.h>
35 /* static data to be referenced by all empty strings
36 * the refCount is predefined to 1 and must never become 0 !
38 static sal_Sequence aEmpty_rtl_ByteSeq =
40 1, /* sal_Int32 refCount; */
41 0, /* sal_Int32 length; */
42 { 0 } /* sal_Unicode buffer[1]; */
45 //==================================================================================================
46 void SAL_CALL rtl_byte_sequence_reference2One(
47 sal_Sequence ** ppSequence )
49 sal_Sequence * pSequence, * pNew;
50 sal_Int32 nElements;
52 OSL_ENSURE( ppSequence, "### null ptr!" );
53 pSequence = *ppSequence;
55 if (pSequence->nRefCount > 1)
57 nElements = pSequence->nElements;
58 if (nElements)
60 pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nElements );
62 if ( pNew != 0 )
63 rtl_copyMemory( pNew->elements, pSequence->elements, nElements );
65 if (! osl_decrementInterlockedCount( &pSequence->nRefCount ))
66 rtl_freeMemory( pSequence );
68 else
70 pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE );
73 if ( pNew != 0 )
75 pNew->nRefCount = 1;
76 pNew->nElements = nElements;
79 *ppSequence = pNew;
83 //==================================================================================================
84 void SAL_CALL rtl_byte_sequence_realloc(
85 sal_Sequence ** ppSequence, sal_Int32 nSize )
87 sal_Sequence * pSequence, * pNew;
88 sal_Int32 nElements;
90 OSL_ENSURE( ppSequence, "### null ptr!" );
91 pSequence = *ppSequence;
92 nElements = pSequence->nElements;
94 if (nElements == nSize)
95 return;
97 if (pSequence->nRefCount > 1) // split
99 pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nSize );
101 if ( pNew != 0 )
103 if (nSize > nElements)
105 rtl_copyMemory( pNew->elements, pSequence->elements, nElements );
106 rtl_zeroMemory( pNew->elements + nElements, nSize - nElements );
108 else
110 rtl_copyMemory( pNew->elements, pSequence->elements, nSize );
114 if (! osl_decrementInterlockedCount( &pSequence->nRefCount ))
115 rtl_freeMemory( pSequence );
116 pSequence = pNew;
118 else
120 pSequence = (sal_Sequence *)rtl_reallocateMemory(
121 pSequence, SAL_SEQUENCE_HEADER_SIZE + nSize );
124 if ( pSequence != 0 )
126 pSequence->nRefCount = 1;
127 pSequence->nElements = nSize;
130 *ppSequence = pSequence;
133 //==================================================================================================
134 void SAL_CALL rtl_byte_sequence_acquire( sal_Sequence *pSequence )
136 OSL_ASSERT( pSequence );
137 osl_incrementInterlockedCount( &(pSequence->nRefCount) );
140 //==================================================================================================
141 void SAL_CALL rtl_byte_sequence_release( sal_Sequence *pSequence )
143 if ( pSequence != 0 )
145 if (! osl_decrementInterlockedCount( &(pSequence->nRefCount )) )
147 rtl_freeMemory( pSequence );
152 //==================================================================================================
153 void SAL_CALL rtl_byte_sequence_construct( sal_Sequence **ppSequence , sal_Int32 nLength )
155 OSL_ASSERT( ppSequence );
156 if( *ppSequence )
158 rtl_byte_sequence_release( *ppSequence );
159 *ppSequence = 0;
162 if( nLength )
164 *ppSequence = (sal_Sequence *) rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
166 if ( *ppSequence != 0 )
168 (*ppSequence)->nRefCount = 1;
169 (*ppSequence)->nElements = nLength;
172 else
174 *ppSequence = &aEmpty_rtl_ByteSeq;
175 rtl_byte_sequence_acquire( *ppSequence );
179 //==================================================================================================
180 void SAL_CALL rtl_byte_sequence_constructNoDefault( sal_Sequence **ppSequence , sal_Int32 nLength )
182 OSL_ASSERT( ppSequence );
183 if( *ppSequence )
185 rtl_byte_sequence_release( *ppSequence );
186 *ppSequence = 0;
189 *ppSequence = (sal_Sequence *) rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
191 if ( *ppSequence != 0 )
193 (*ppSequence)->nRefCount = 1;
194 (*ppSequence)->nElements = nLength;
198 //==================================================================================================
199 void SAL_CALL rtl_byte_sequence_constructFromArray(
200 sal_Sequence **ppSequence, const sal_Int8 *pData , sal_Int32 nLength )
202 rtl_byte_sequence_constructNoDefault( ppSequence , nLength );
203 if ( *ppSequence != 0 )
204 rtl_copyMemory( (*ppSequence)->elements, pData, nLength );
207 //==================================================================================================
208 void SAL_CALL rtl_byte_sequence_assign( sal_Sequence **ppSequence , sal_Sequence *pSequence )
210 if ( *ppSequence != pSequence)
212 if( *ppSequence )
214 rtl_byte_sequence_release( *ppSequence );
216 *ppSequence = pSequence;
217 rtl_byte_sequence_acquire( *ppSequence );
219 // else
220 // nothing to do
224 //==================================================================================================
225 sal_Bool SAL_CALL rtl_byte_sequence_equals( sal_Sequence *pSequence1 , sal_Sequence *pSequence2 )
227 OSL_ASSERT( pSequence1 );
228 OSL_ASSERT( pSequence2 );
229 if (pSequence1 == pSequence2)
231 return sal_True;
233 if (pSequence1->nElements != pSequence2->nElements)
235 return sal_False;
237 return (sal_Bool)
238 (rtl_compareMemory(
239 pSequence1->elements, pSequence2->elements, pSequence1->nElements )
240 == 0);
244 //==================================================================================================
245 const sal_Int8 *SAL_CALL rtl_byte_sequence_getConstArray( sal_Sequence *pSequence )
247 return ((const sal_Int8*)(pSequence->elements));
250 //==================================================================================================
251 sal_Int32 SAL_CALL rtl_byte_sequence_getLength( sal_Sequence *pSequence )
253 return pSequence->nElements;