Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sal / rtl / byteseq.cxx
blobc78a1e22f43a3c69d40d909f232c4722c3e6e345
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, * pNew;
44 OSL_ENSURE( ppSequence, "### null ptr!" );
45 pSequence = *ppSequence;
47 if (pSequence->nRefCount > 1)
49 sal_Int32 nElements = pSequence->nElements;
50 if (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 ))
58 free( pSequence );
60 else
62 pNew = static_cast<sal_Sequence *>(malloc( SAL_SEQUENCE_HEADER_SIZE ));
65 if ( pNew != nullptr )
67 pNew->nRefCount = 1;
68 pNew->nElements = nElements;
71 *ppSequence = pNew;
75 void SAL_CALL rtl_byte_sequence_realloc(
76 sal_Sequence ** ppSequence, sal_Int32 nSize ) SAL_THROW_EXTERN_C()
78 sal_Sequence * pSequence, * pNew;
79 sal_Int32 nElements;
81 assert(ppSequence && "### null ptr!");
82 pSequence = *ppSequence;
83 nElements = pSequence->nElements;
85 if (nElements == nSize)
86 return;
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 );
99 else
101 memcpy( pNew->elements, pSequence->elements, nSize );
105 if (! osl_atomic_decrement( &pSequence->nRefCount ))
106 free( pSequence );
107 pSequence = pNew;
109 else
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 )
125 SAL_THROW_EXTERN_C()
127 OSL_ASSERT( pSequence );
128 osl_atomic_increment( &(pSequence->nRefCount) );
131 void SAL_CALL rtl_byte_sequence_release( sal_Sequence *pSequence )
132 SAL_THROW_EXTERN_C()
134 if ( pSequence != nullptr )
136 if (! osl_atomic_decrement( &(pSequence->nRefCount )) )
138 free( pSequence );
143 void SAL_CALL rtl_byte_sequence_construct( sal_Sequence **ppSequence , sal_Int32 nLength )
144 SAL_THROW_EXTERN_C()
146 OSL_ASSERT( ppSequence );
147 if( *ppSequence )
149 rtl_byte_sequence_release( *ppSequence );
150 *ppSequence = nullptr;
153 if( nLength )
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;
163 else
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 )
171 SAL_THROW_EXTERN_C()
173 OSL_ASSERT( ppSequence );
174 if( *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 )
191 SAL_THROW_EXTERN_C()
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 )
199 SAL_THROW_EXTERN_C()
201 if ( *ppSequence != pSequence)
203 if( *ppSequence )
205 rtl_byte_sequence_release( *ppSequence );
207 *ppSequence = pSequence;
208 rtl_byte_sequence_acquire( *ppSequence );
210 // else
211 // nothing to do
215 sal_Bool SAL_CALL rtl_byte_sequence_equals( sal_Sequence *pSequence1 , sal_Sequence *pSequence2 )
216 SAL_THROW_EXTERN_C()
218 assert(pSequence1 && pSequence2);
219 if (pSequence1 == pSequence2)
221 return true;
223 if (pSequence1->nElements != pSequence2->nElements)
225 return false;
227 return
228 memcmp(
229 pSequence1->elements, pSequence2->elements, pSequence1->nElements )
230 == 0;
233 const sal_Int8 *SAL_CALL rtl_byte_sequence_getConstArray( sal_Sequence *pSequence )
234 SAL_THROW_EXTERN_C()
236 return reinterpret_cast<sal_Int8*>(pSequence->elements);
239 sal_Int32 SAL_CALL rtl_byte_sequence_getLength( sal_Sequence *pSequence )
240 SAL_THROW_EXTERN_C()
242 return pSequence->nElements;
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */