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 .
24 #include <osl/diagnose.h>
25 #include <osl/interlck.h>
27 #include <rtl/byteseq.h>
29 /* static data to be referenced by all empty strings
30 * the refCount is predefined to 1 and must never become 0 !
32 static sal_Sequence aEmpty_rtl_ByteSeq
=
34 1, /* sal_Int32 refCount; */
35 0, /* sal_Int32 length; */
36 { 0 } /* sal_Unicode buffer[1]; */
39 void SAL_CALL
rtl_byte_sequence_reference2One(
40 sal_Sequence
** ppSequence
) SAL_THROW_EXTERN_C()
42 sal_Sequence
* pSequence
, * pNew
;
44 OSL_ENSURE( ppSequence
, "### null ptr!" );
45 pSequence
= *ppSequence
;
47 if (pSequence
->nRefCount
> 1)
49 sal_Int32 nElements
= pSequence
->nElements
;
52 pNew
= static_cast<sal_Sequence
*>(malloc( SAL_SEQUENCE_HEADER_SIZE
+ nElements
));
54 if ( pNew
!= nullptr )
55 memcpy( pNew
->elements
, pSequence
->elements
, nElements
);
57 if (! osl_atomic_decrement( &pSequence
->nRefCount
))
62 pNew
= static_cast<sal_Sequence
*>(malloc( SAL_SEQUENCE_HEADER_SIZE
));
65 if ( pNew
!= nullptr )
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 assert(ppSequence
&& "### null ptr!");
82 pSequence
= *ppSequence
;
83 nElements
= pSequence
->nElements
;
85 if (nElements
== nSize
)
88 if (pSequence
->nRefCount
> 1) // split
90 pNew
= static_cast<sal_Sequence
*>(malloc( SAL_SEQUENCE_HEADER_SIZE
+ nSize
));
92 if ( pNew
!= nullptr )
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
))
111 pSequence
= static_cast<sal_Sequence
*>(realloc(
112 pSequence
, SAL_SEQUENCE_HEADER_SIZE
+ nSize
));
115 if ( pSequence
!= nullptr )
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
!= nullptr )
136 if (! osl_atomic_decrement( &(pSequence
->nRefCount
)) )
143 void SAL_CALL
rtl_byte_sequence_construct( sal_Sequence
**ppSequence
, sal_Int32 nLength
)
146 OSL_ASSERT( ppSequence
);
149 rtl_byte_sequence_release( *ppSequence
);
150 *ppSequence
= nullptr;
155 *ppSequence
= static_cast<sal_Sequence
*>(rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE
+ nLength
));
157 if ( *ppSequence
!= nullptr )
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
);
177 *ppSequence
= nullptr;
180 *ppSequence
= static_cast<sal_Sequence
*>(malloc( SAL_SEQUENCE_HEADER_SIZE
+ nLength
));
182 if ( *ppSequence
!= nullptr )
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
!= nullptr && nLength
!= 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 assert(pSequence1
&& pSequence2
);
219 if (pSequence1
== pSequence2
)
223 if (pSequence1
->nElements
!= pSequence2
->nElements
)
229 pSequence1
->elements
, pSequence2
->elements
, pSequence1
->nElements
)
233 const sal_Int8
*SAL_CALL
rtl_byte_sequence_getConstArray( sal_Sequence
*pSequence
)
236 return reinterpret_cast<sal_Int8
*>(pSequence
->elements
);
239 sal_Int32 SAL_CALL
rtl_byte_sequence_getLength( sal_Sequence
*pSequence
)
242 return pSequence
->nElements
;
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */