Update git submodules
[LibreOffice.git] / sal / rtl / byteseq.cxx
blob3c4967d5456de9b5480f9535af14e5a3c8a96eb4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <assert.h>
21 #include <string.h>
22 #include <stdlib.h>
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)
49 sal_Sequence *pNew;
50 sal_Int32 nElements = pSequence->nElements;
51 if (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 ))
59 free( pSequence );
61 else
63 pNew = static_cast<sal_Sequence *>(malloc( SAL_SEQUENCE_HEADER_SIZE ));
66 if ( pNew != nullptr )
68 pNew->nRefCount = 1;
69 pNew->nElements = nElements;
72 *ppSequence = pNew;
76 void SAL_CALL rtl_byte_sequence_realloc(
77 sal_Sequence ** ppSequence, sal_Int32 nSize ) SAL_THROW_EXTERN_C()
79 sal_Sequence * pSequence;
80 sal_Int32 nElements;
82 assert(ppSequence && "### null ptr!");
83 pSequence = *ppSequence;
84 nElements = pSequence->nElements;
86 if (nElements == nSize)
87 return;
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 );
100 else
102 memcpy( pNew->elements, pSequence->elements, nSize );
106 if (! osl_atomic_decrement( &pSequence->nRefCount ))
107 free( pSequence );
108 pSequence = pNew;
110 else
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 )
126 SAL_THROW_EXTERN_C()
128 OSL_ASSERT( pSequence );
129 osl_atomic_increment( &(pSequence->nRefCount) );
132 void SAL_CALL rtl_byte_sequence_release( sal_Sequence *pSequence )
133 SAL_THROW_EXTERN_C()
135 if ( pSequence != nullptr )
137 if (! osl_atomic_decrement( &(pSequence->nRefCount )) )
139 free( pSequence );
144 void SAL_CALL rtl_byte_sequence_construct( sal_Sequence **ppSequence , sal_Int32 nLength )
145 SAL_THROW_EXTERN_C()
147 OSL_ASSERT( ppSequence );
148 if( *ppSequence )
150 rtl_byte_sequence_release( *ppSequence );
151 *ppSequence = nullptr;
154 if( nLength )
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;
164 else
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 )
172 SAL_THROW_EXTERN_C()
174 OSL_ASSERT( ppSequence );
175 if( *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 )
192 SAL_THROW_EXTERN_C()
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 )
200 SAL_THROW_EXTERN_C()
202 if ( *ppSequence != pSequence)
204 if( *ppSequence )
206 rtl_byte_sequence_release( *ppSequence );
208 *ppSequence = pSequence;
209 rtl_byte_sequence_acquire( *ppSequence );
211 // else
212 // nothing to do
216 sal_Bool SAL_CALL rtl_byte_sequence_equals( sal_Sequence *pSequence1 , sal_Sequence *pSequence2 )
217 SAL_THROW_EXTERN_C()
219 assert(pSequence1 && pSequence2);
220 if (pSequence1 == pSequence2)
222 return true;
224 if (pSequence1->nElements != pSequence2->nElements)
226 return false;
228 return
229 memcmp(
230 pSequence1->elements, pSequence2->elements, pSequence1->nElements )
231 == 0;
234 const sal_Int8 *SAL_CALL rtl_byte_sequence_getConstArray( sal_Sequence *pSequence )
235 SAL_THROW_EXTERN_C()
237 return reinterpret_cast<sal_Int8*>(pSequence->elements);
240 sal_Int32 SAL_CALL rtl_byte_sequence_getLength( sal_Sequence *pSequence )
241 SAL_THROW_EXTERN_C()
243 return pSequence->nElements;
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */