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
;
44 OSL_ENSURE( ppSequence
, "### null ptr!" );
45 pSequence
= *ppSequence
;
47 if (pSequence
->nRefCount
> 1)
50 sal_Int32 nElements
= pSequence
->nElements
;
53 pNew
= static_cast<sal_Sequence
*>(malloc( SAL_SEQUENCE_HEADER_SIZE
+ nElements
));
55 if ( pNew
!= nullptr )
56 memcpy( pNew
->elements
, pSequence
->elements
, nElements
);
58 if (! osl_atomic_decrement( &pSequence
->nRefCount
))
63 pNew
= static_cast<sal_Sequence
*>(malloc( SAL_SEQUENCE_HEADER_SIZE
));
66 if ( pNew
!= nullptr )
69 pNew
->nElements
= nElements
;
76 void SAL_CALL
rtl_byte_sequence_realloc(
77 sal_Sequence
** ppSequence
, sal_Int32 nSize
) SAL_THROW_EXTERN_C()
79 sal_Sequence
* pSequence
;
82 assert(ppSequence
&& "### null ptr!");
83 pSequence
= *ppSequence
;
84 nElements
= pSequence
->nElements
;
86 if (nElements
== nSize
)
89 if (pSequence
->nRefCount
> 1) // split
91 sal_Sequence
*pNew
= static_cast<sal_Sequence
*>(malloc( SAL_SEQUENCE_HEADER_SIZE
+ nSize
));
93 if ( pNew
!= nullptr )
95 if (nSize
> nElements
)
97 memcpy( pNew
->elements
, pSequence
->elements
, nElements
);
98 memset( pNew
->elements
+ nElements
, 0, nSize
- nElements
);
102 memcpy( pNew
->elements
, pSequence
->elements
, nSize
);
106 if (! osl_atomic_decrement( &pSequence
->nRefCount
))
112 pSequence
= static_cast<sal_Sequence
*>(realloc(
113 pSequence
, SAL_SEQUENCE_HEADER_SIZE
+ nSize
));
116 if ( pSequence
!= nullptr )
118 pSequence
->nRefCount
= 1;
119 pSequence
->nElements
= nSize
;
122 *ppSequence
= pSequence
;
125 void SAL_CALL
rtl_byte_sequence_acquire( sal_Sequence
*pSequence
)
128 OSL_ASSERT( pSequence
);
129 osl_atomic_increment( &(pSequence
->nRefCount
) );
132 void SAL_CALL
rtl_byte_sequence_release( sal_Sequence
*pSequence
)
135 if ( pSequence
!= nullptr )
137 if (! osl_atomic_decrement( &(pSequence
->nRefCount
)) )
144 void SAL_CALL
rtl_byte_sequence_construct( sal_Sequence
**ppSequence
, sal_Int32 nLength
)
147 OSL_ASSERT( ppSequence
);
150 rtl_byte_sequence_release( *ppSequence
);
151 *ppSequence
= nullptr;
156 *ppSequence
= static_cast<sal_Sequence
*>(rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE
+ nLength
));
158 if ( *ppSequence
!= nullptr )
160 (*ppSequence
)->nRefCount
= 1;
161 (*ppSequence
)->nElements
= nLength
;
166 *ppSequence
= &aEmpty_rtl_ByteSeq
;
167 rtl_byte_sequence_acquire( *ppSequence
);
171 void SAL_CALL
rtl_byte_sequence_constructNoDefault( sal_Sequence
**ppSequence
, sal_Int32 nLength
)
174 OSL_ASSERT( ppSequence
);
177 rtl_byte_sequence_release( *ppSequence
);
178 *ppSequence
= nullptr;
181 *ppSequence
= static_cast<sal_Sequence
*>(malloc( SAL_SEQUENCE_HEADER_SIZE
+ nLength
));
183 if ( *ppSequence
!= nullptr )
185 (*ppSequence
)->nRefCount
= 1;
186 (*ppSequence
)->nElements
= nLength
;
190 void SAL_CALL
rtl_byte_sequence_constructFromArray(
191 sal_Sequence
**ppSequence
, const sal_Int8
*pData
, sal_Int32 nLength
)
194 rtl_byte_sequence_constructNoDefault( ppSequence
, nLength
);
195 if ( *ppSequence
!= nullptr && nLength
!= 0 )
196 memcpy( (*ppSequence
)->elements
, pData
, nLength
);
199 void SAL_CALL
rtl_byte_sequence_assign( sal_Sequence
**ppSequence
, sal_Sequence
*pSequence
)
202 if ( *ppSequence
!= pSequence
)
206 rtl_byte_sequence_release( *ppSequence
);
208 *ppSequence
= pSequence
;
209 rtl_byte_sequence_acquire( *ppSequence
);
216 sal_Bool SAL_CALL
rtl_byte_sequence_equals( sal_Sequence
*pSequence1
, sal_Sequence
*pSequence2
)
219 assert(pSequence1
&& 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 reinterpret_cast<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: */