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
;
52 OSL_ENSURE( ppSequence
, "### null ptr!" );
53 pSequence
= *ppSequence
;
55 if (pSequence
->nRefCount
> 1)
57 nElements
= pSequence
->nElements
;
60 pNew
= (sal_Sequence
*)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE
+ nElements
);
63 rtl_copyMemory( pNew
->elements
, pSequence
->elements
, nElements
);
65 if (! osl_decrementInterlockedCount( &pSequence
->nRefCount
))
66 rtl_freeMemory( pSequence
);
70 pNew
= (sal_Sequence
*)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE
);
76 pNew
->nElements
= nElements
;
83 //==================================================================================================
84 void SAL_CALL
rtl_byte_sequence_realloc(
85 sal_Sequence
** ppSequence
, sal_Int32 nSize
)
87 sal_Sequence
* pSequence
, * pNew
;
90 OSL_ENSURE( ppSequence
, "### null ptr!" );
91 pSequence
= *ppSequence
;
92 nElements
= pSequence
->nElements
;
94 if (nElements
== nSize
)
97 if (pSequence
->nRefCount
> 1) // split
99 pNew
= (sal_Sequence
*)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE
+ nSize
);
103 if (nSize
> nElements
)
105 rtl_copyMemory( pNew
->elements
, pSequence
->elements
, nElements
);
106 rtl_zeroMemory( pNew
->elements
+ nElements
, nSize
- nElements
);
110 rtl_copyMemory( pNew
->elements
, pSequence
->elements
, nSize
);
114 if (! osl_decrementInterlockedCount( &pSequence
->nRefCount
))
115 rtl_freeMemory( pSequence
);
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
);
158 rtl_byte_sequence_release( *ppSequence
);
164 *ppSequence
= (sal_Sequence
*) rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE
+ nLength
);
166 if ( *ppSequence
!= 0 )
168 (*ppSequence
)->nRefCount
= 1;
169 (*ppSequence
)->nElements
= nLength
;
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
);
185 rtl_byte_sequence_release( *ppSequence
);
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
)
214 rtl_byte_sequence_release( *ppSequence
);
216 *ppSequence
= pSequence
;
217 rtl_byte_sequence_acquire( *ppSequence
);
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
)
233 if (pSequence1
->nElements
!= pSequence2
->nElements
)
239 pSequence1
->elements
, pSequence2
->elements
, pSequence1
->nElements
)
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
;