1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <osl/diagnose.h>
23 #include <osl/interlck.h>
25 #include <rtl/byteseq.h>
26 #include <rtl/alloc.h>
28 /* static data to be referenced by all empty strings
29 * the refCount is predefined to 1 and must never become 0 !
31 static sal_Sequence aEmpty_rtl_ByteSeq
=
33 1, /* sal_Int32 refCount; */
34 0, /* sal_Int32 length; */
35 { 0 } /* sal_Unicode buffer[1]; */
38 //==================================================================================================
39 void SAL_CALL
rtl_byte_sequence_reference2One(
40 sal_Sequence
** ppSequence
) SAL_THROW_EXTERN_C()
42 sal_Sequence
* pSequence
, * pNew
;
45 OSL_ENSURE( ppSequence
, "### null ptr!" );
46 pSequence
= *ppSequence
;
48 if (pSequence
->nRefCount
> 1)
50 nElements
= pSequence
->nElements
;
53 pNew
= (sal_Sequence
*)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE
+ nElements
);
56 memcpy( pNew
->elements
, pSequence
->elements
, nElements
);
58 if (! osl_atomic_decrement( &pSequence
->nRefCount
))
59 rtl_freeMemory( pSequence
);
63 pNew
= (sal_Sequence
*)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE
);
69 pNew
->nElements
= nElements
;
76 //==================================================================================================
77 void SAL_CALL
rtl_byte_sequence_realloc(
78 sal_Sequence
** ppSequence
, sal_Int32 nSize
) SAL_THROW_EXTERN_C()
80 sal_Sequence
* pSequence
, * pNew
;
83 OSL_ENSURE( ppSequence
, "### null ptr!" );
84 pSequence
= *ppSequence
;
85 nElements
= pSequence
->nElements
;
87 if (nElements
== nSize
)
90 if (pSequence
->nRefCount
> 1) // split
92 pNew
= (sal_Sequence
*)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE
+ nSize
);
96 if (nSize
> nElements
)
98 memcpy( pNew
->elements
, pSequence
->elements
, nElements
);
99 memset( pNew
->elements
+ nElements
, 0, nSize
- nElements
);
103 memcpy( pNew
->elements
, pSequence
->elements
, nSize
);
107 if (! osl_atomic_decrement( &pSequence
->nRefCount
))
108 rtl_freeMemory( pSequence
);
113 pSequence
= (sal_Sequence
*)rtl_reallocateMemory(
114 pSequence
, SAL_SEQUENCE_HEADER_SIZE
+ nSize
);
117 if ( pSequence
!= 0 )
119 pSequence
->nRefCount
= 1;
120 pSequence
->nElements
= nSize
;
123 *ppSequence
= pSequence
;
126 //==================================================================================================
127 void SAL_CALL
rtl_byte_sequence_acquire( sal_Sequence
*pSequence
)
130 OSL_ASSERT( pSequence
);
131 osl_atomic_increment( &(pSequence
->nRefCount
) );
134 //==================================================================================================
135 void SAL_CALL
rtl_byte_sequence_release( sal_Sequence
*pSequence
)
138 if ( pSequence
!= 0 )
140 if (! osl_atomic_decrement( &(pSequence
->nRefCount
)) )
142 rtl_freeMemory( pSequence
);
147 //==================================================================================================
148 void SAL_CALL
rtl_byte_sequence_construct( sal_Sequence
**ppSequence
, sal_Int32 nLength
)
151 OSL_ASSERT( ppSequence
);
154 rtl_byte_sequence_release( *ppSequence
);
160 *ppSequence
= (sal_Sequence
*) rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE
+ nLength
);
162 if ( *ppSequence
!= 0 )
164 (*ppSequence
)->nRefCount
= 1;
165 (*ppSequence
)->nElements
= nLength
;
170 *ppSequence
= &aEmpty_rtl_ByteSeq
;
171 rtl_byte_sequence_acquire( *ppSequence
);
175 //==================================================================================================
176 void SAL_CALL
rtl_byte_sequence_constructNoDefault( sal_Sequence
**ppSequence
, sal_Int32 nLength
)
179 OSL_ASSERT( ppSequence
);
182 rtl_byte_sequence_release( *ppSequence
);
186 *ppSequence
= (sal_Sequence
*) rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE
+ nLength
);
188 if ( *ppSequence
!= 0 )
190 (*ppSequence
)->nRefCount
= 1;
191 (*ppSequence
)->nElements
= nLength
;
195 //==================================================================================================
196 void SAL_CALL
rtl_byte_sequence_constructFromArray(
197 sal_Sequence
**ppSequence
, const sal_Int8
*pData
, sal_Int32 nLength
)
200 rtl_byte_sequence_constructNoDefault( ppSequence
, nLength
);
201 if ( *ppSequence
!= 0 )
202 memcpy( (*ppSequence
)->elements
, pData
, nLength
);
205 //==================================================================================================
206 void SAL_CALL
rtl_byte_sequence_assign( sal_Sequence
**ppSequence
, sal_Sequence
*pSequence
)
209 if ( *ppSequence
!= pSequence
)
213 rtl_byte_sequence_release( *ppSequence
);
215 *ppSequence
= pSequence
;
216 rtl_byte_sequence_acquire( *ppSequence
);
223 //==================================================================================================
224 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
)
248 return ((const sal_Int8
*)(pSequence
->elements
));
251 //==================================================================================================
252 sal_Int32 SAL_CALL
rtl_byte_sequence_getLength( sal_Sequence
*pSequence
)
255 return pSequence
->nElements
;
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */