update credits
[LibreOffice.git] / sal / rtl / byteseq.cxx
blobdfc0d96158be7d9ff7b8dfdb8d83d969fb7101f9
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 <string.h>
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 //==================================================================================================
39 void SAL_CALL rtl_byte_sequence_reference2One(
40 sal_Sequence ** ppSequence ) SAL_THROW_EXTERN_C()
42 sal_Sequence * pSequence, * pNew;
43 sal_Int32 nElements;
45 OSL_ENSURE( ppSequence, "### null ptr!" );
46 pSequence = *ppSequence;
48 if (pSequence->nRefCount > 1)
50 nElements = pSequence->nElements;
51 if (nElements)
53 pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nElements );
55 if ( pNew != 0 )
56 memcpy( pNew->elements, pSequence->elements, nElements );
58 if (! osl_atomic_decrement( &pSequence->nRefCount ))
59 rtl_freeMemory( pSequence );
61 else
63 pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE );
66 if ( pNew != 0 )
68 pNew->nRefCount = 1;
69 pNew->nElements = nElements;
72 *ppSequence = pNew;
76 //==================================================================================================
77 void SAL_CALL rtl_byte_sequence_realloc(
78 sal_Sequence ** ppSequence, sal_Int32 nSize ) SAL_THROW_EXTERN_C()
80 sal_Sequence * pSequence, * pNew;
81 sal_Int32 nElements;
83 OSL_ENSURE( ppSequence, "### null ptr!" );
84 pSequence = *ppSequence;
85 nElements = pSequence->nElements;
87 if (nElements == nSize)
88 return;
90 if (pSequence->nRefCount > 1) // split
92 pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nSize );
94 if ( pNew != 0 )
96 if (nSize > nElements)
98 memcpy( pNew->elements, pSequence->elements, nElements );
99 memset( pNew->elements + nElements, 0, nSize - nElements );
101 else
103 memcpy( pNew->elements, pSequence->elements, nSize );
107 if (! osl_atomic_decrement( &pSequence->nRefCount ))
108 rtl_freeMemory( pSequence );
109 pSequence = pNew;
111 else
113 pSequence = (sal_Sequence *)rtl_reallocateMemory(
114 pSequence, SAL_SEQUENCE_HEADER_SIZE + nSize );
117 if ( pSequence != 0 )
119 pSequence->nRefCount = 1;
120 pSequence->nElements = nSize;
123 *ppSequence = pSequence;
126 //==================================================================================================
127 void SAL_CALL rtl_byte_sequence_acquire( sal_Sequence *pSequence )
128 SAL_THROW_EXTERN_C()
130 OSL_ASSERT( pSequence );
131 osl_atomic_increment( &(pSequence->nRefCount) );
134 //==================================================================================================
135 void SAL_CALL rtl_byte_sequence_release( sal_Sequence *pSequence )
136 SAL_THROW_EXTERN_C()
138 if ( pSequence != 0 )
140 if (! osl_atomic_decrement( &(pSequence->nRefCount )) )
142 rtl_freeMemory( pSequence );
147 //==================================================================================================
148 void SAL_CALL rtl_byte_sequence_construct( sal_Sequence **ppSequence , sal_Int32 nLength )
149 SAL_THROW_EXTERN_C()
151 OSL_ASSERT( ppSequence );
152 if( *ppSequence )
154 rtl_byte_sequence_release( *ppSequence );
155 *ppSequence = 0;
158 if( nLength )
160 *ppSequence = (sal_Sequence *) rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
162 if ( *ppSequence != 0 )
164 (*ppSequence)->nRefCount = 1;
165 (*ppSequence)->nElements = nLength;
168 else
170 *ppSequence = &aEmpty_rtl_ByteSeq;
171 rtl_byte_sequence_acquire( *ppSequence );
175 //==================================================================================================
176 void SAL_CALL rtl_byte_sequence_constructNoDefault( sal_Sequence **ppSequence , sal_Int32 nLength )
177 SAL_THROW_EXTERN_C()
179 OSL_ASSERT( ppSequence );
180 if( *ppSequence )
182 rtl_byte_sequence_release( *ppSequence );
183 *ppSequence = 0;
186 *ppSequence = (sal_Sequence *) rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
188 if ( *ppSequence != 0 )
190 (*ppSequence)->nRefCount = 1;
191 (*ppSequence)->nElements = nLength;
195 //==================================================================================================
196 void SAL_CALL rtl_byte_sequence_constructFromArray(
197 sal_Sequence **ppSequence, const sal_Int8 *pData , sal_Int32 nLength )
198 SAL_THROW_EXTERN_C()
200 rtl_byte_sequence_constructNoDefault( ppSequence , nLength );
201 if ( *ppSequence != 0 )
202 memcpy( (*ppSequence)->elements, pData, nLength );
205 //==================================================================================================
206 void SAL_CALL rtl_byte_sequence_assign( sal_Sequence **ppSequence , sal_Sequence *pSequence )
207 SAL_THROW_EXTERN_C()
209 if ( *ppSequence != pSequence)
211 if( *ppSequence )
213 rtl_byte_sequence_release( *ppSequence );
215 *ppSequence = pSequence;
216 rtl_byte_sequence_acquire( *ppSequence );
218 // else
219 // nothing to do
223 //==================================================================================================
224 sal_Bool SAL_CALL rtl_byte_sequence_equals( sal_Sequence *pSequence1 , sal_Sequence *pSequence2 )
225 SAL_THROW_EXTERN_C()
227 OSL_ASSERT( pSequence1 );
228 OSL_ASSERT( pSequence2 );
229 if (pSequence1 == pSequence2)
231 return sal_True;
233 if (pSequence1->nElements != pSequence2->nElements)
235 return sal_False;
237 return (sal_Bool)
238 (memcmp(
239 pSequence1->elements, pSequence2->elements, pSequence1->nElements )
240 == 0);
244 //==================================================================================================
245 const sal_Int8 *SAL_CALL rtl_byte_sequence_getConstArray( sal_Sequence *pSequence )
246 SAL_THROW_EXTERN_C()
248 return ((const sal_Int8*)(pSequence->elements));
251 //==================================================================================================
252 sal_Int32 SAL_CALL rtl_byte_sequence_getLength( sal_Sequence *pSequence )
253 SAL_THROW_EXTERN_C()
255 return pSequence->nElements;
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */