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 void SAL_CALL
rtl_byte_sequence_reference2One(
39 sal_Sequence
** ppSequence
) SAL_THROW_EXTERN_C()
41 sal_Sequence
* pSequence
, * pNew
;
44 OSL_ENSURE( ppSequence
, "### null ptr!" );
45 pSequence
= *ppSequence
;
47 if (pSequence
->nRefCount
> 1)
49 nElements
= pSequence
->nElements
;
52 pNew
= (sal_Sequence
*)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE
+ nElements
);
55 memcpy( pNew
->elements
, pSequence
->elements
, nElements
);
57 if (! osl_atomic_decrement( &pSequence
->nRefCount
))
58 rtl_freeMemory( pSequence
);
62 pNew
= (sal_Sequence
*)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE
);
68 pNew
->nElements
= nElements
;
75 void SAL_CALL
rtl_byte_sequence_realloc(
76 sal_Sequence
** ppSequence
, sal_Int32 nSize
) SAL_THROW_EXTERN_C()
78 sal_Sequence
* pSequence
, * pNew
;
81 OSL_ENSURE( ppSequence
, "### null ptr!" );
82 pSequence
= *ppSequence
;
83 nElements
= pSequence
->nElements
;
85 if (nElements
== nSize
)
88 if (pSequence
->nRefCount
> 1) // split
90 pNew
= (sal_Sequence
*)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE
+ nSize
);
94 if (nSize
> nElements
)
96 memcpy( pNew
->elements
, pSequence
->elements
, nElements
);
97 memset( pNew
->elements
+ nElements
, 0, nSize
- nElements
);
101 memcpy( pNew
->elements
, pSequence
->elements
, nSize
);
105 if (! osl_atomic_decrement( &pSequence
->nRefCount
))
106 rtl_freeMemory( pSequence
);
111 pSequence
= (sal_Sequence
*)rtl_reallocateMemory(
112 pSequence
, SAL_SEQUENCE_HEADER_SIZE
+ nSize
);
115 if ( pSequence
!= 0 )
117 pSequence
->nRefCount
= 1;
118 pSequence
->nElements
= nSize
;
121 *ppSequence
= pSequence
;
124 void SAL_CALL
rtl_byte_sequence_acquire( sal_Sequence
*pSequence
)
127 OSL_ASSERT( pSequence
);
128 osl_atomic_increment( &(pSequence
->nRefCount
) );
131 void SAL_CALL
rtl_byte_sequence_release( sal_Sequence
*pSequence
)
134 if ( pSequence
!= 0 )
136 if (! osl_atomic_decrement( &(pSequence
->nRefCount
)) )
138 rtl_freeMemory( pSequence
);
143 void SAL_CALL
rtl_byte_sequence_construct( sal_Sequence
**ppSequence
, sal_Int32 nLength
)
146 OSL_ASSERT( ppSequence
);
149 rtl_byte_sequence_release( *ppSequence
);
155 *ppSequence
= (sal_Sequence
*) rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE
+ nLength
);
157 if ( *ppSequence
!= 0 )
159 (*ppSequence
)->nRefCount
= 1;
160 (*ppSequence
)->nElements
= nLength
;
165 *ppSequence
= &aEmpty_rtl_ByteSeq
;
166 rtl_byte_sequence_acquire( *ppSequence
);
170 void SAL_CALL
rtl_byte_sequence_constructNoDefault( sal_Sequence
**ppSequence
, sal_Int32 nLength
)
173 OSL_ASSERT( ppSequence
);
176 rtl_byte_sequence_release( *ppSequence
);
180 *ppSequence
= (sal_Sequence
*) rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE
+ nLength
);
182 if ( *ppSequence
!= 0 )
184 (*ppSequence
)->nRefCount
= 1;
185 (*ppSequence
)->nElements
= nLength
;
189 void SAL_CALL
rtl_byte_sequence_constructFromArray(
190 sal_Sequence
**ppSequence
, const sal_Int8
*pData
, sal_Int32 nLength
)
193 rtl_byte_sequence_constructNoDefault( ppSequence
, nLength
);
194 if ( *ppSequence
!= 0 )
195 memcpy( (*ppSequence
)->elements
, pData
, nLength
);
198 void SAL_CALL
rtl_byte_sequence_assign( sal_Sequence
**ppSequence
, sal_Sequence
*pSequence
)
201 if ( *ppSequence
!= pSequence
)
205 rtl_byte_sequence_release( *ppSequence
);
207 *ppSequence
= pSequence
;
208 rtl_byte_sequence_acquire( *ppSequence
);
215 sal_Bool SAL_CALL
rtl_byte_sequence_equals( sal_Sequence
*pSequence1
, sal_Sequence
*pSequence2
)
218 OSL_ASSERT( pSequence1
);
219 OSL_ASSERT( pSequence2
);
220 if (pSequence1
== pSequence2
)
224 if (pSequence1
->nElements
!= pSequence2
->nElements
)
230 pSequence1
->elements
, pSequence2
->elements
, pSequence1
->nElements
)
234 const sal_Int8
*SAL_CALL
rtl_byte_sequence_getConstArray( sal_Sequence
*pSequence
)
237 return ((const sal_Int8
*)(pSequence
->elements
));
240 sal_Int32 SAL_CALL
rtl_byte_sequence_getLength( sal_Sequence
*pSequence
)
243 return pSequence
->nElements
;
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */