Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <string.h>
21 :
22 : #include <osl/diagnose.h>
23 : #include <osl/interlck.h>
24 :
25 : #include <rtl/byteseq.h>
26 : #include <rtl/alloc.h>
27 :
28 : /* static data to be referenced by all empty strings
29 : * the refCount is predefined to 1 and must never become 0 !
30 : */
31 : static sal_Sequence aEmpty_rtl_ByteSeq =
32 : {
33 : 1, /* sal_Int32 refCount; */
34 : 0, /* sal_Int32 length; */
35 : { 0 } /* sal_Unicode buffer[1]; */
36 : };
37 :
38 : //==================================================================================================
39 1 : void SAL_CALL rtl_byte_sequence_reference2One(
40 : sal_Sequence ** ppSequence ) SAL_THROW_EXTERN_C()
41 : {
42 : sal_Sequence * pSequence, * pNew;
43 : sal_Int32 nElements;
44 :
45 : OSL_ENSURE( ppSequence, "### null ptr!" );
46 1 : pSequence = *ppSequence;
47 :
48 1 : if (pSequence->nRefCount > 1)
49 : {
50 1 : nElements = pSequence->nElements;
51 1 : if (nElements)
52 : {
53 1 : pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nElements );
54 :
55 1 : if ( pNew != 0 )
56 1 : memcpy( pNew->elements, pSequence->elements, nElements );
57 :
58 1 : if (! osl_atomic_decrement( &pSequence->nRefCount ))
59 0 : rtl_freeMemory( pSequence );
60 : }
61 : else
62 : {
63 0 : pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE );
64 : }
65 :
66 1 : if ( pNew != 0 )
67 : {
68 1 : pNew->nRefCount = 1;
69 1 : pNew->nElements = nElements;
70 : }
71 :
72 1 : *ppSequence = pNew;
73 : }
74 1 : }
75 :
76 : //==================================================================================================
77 5140 : void SAL_CALL rtl_byte_sequence_realloc(
78 : sal_Sequence ** ppSequence, sal_Int32 nSize ) SAL_THROW_EXTERN_C()
79 : {
80 : sal_Sequence * pSequence, * pNew;
81 : sal_Int32 nElements;
82 :
83 : OSL_ENSURE( ppSequence, "### null ptr!" );
84 5140 : pSequence = *ppSequence;
85 5140 : nElements = pSequence->nElements;
86 :
87 5140 : if (nElements == nSize)
88 5140 : return;
89 :
90 5140 : if (pSequence->nRefCount > 1) // split
91 : {
92 257 : pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nSize );
93 :
94 257 : if ( pNew != 0 )
95 : {
96 257 : if (nSize > nElements)
97 : {
98 257 : memcpy( pNew->elements, pSequence->elements, nElements );
99 257 : memset( pNew->elements + nElements, 0, nSize - nElements );
100 : }
101 : else
102 : {
103 0 : memcpy( pNew->elements, pSequence->elements, nSize );
104 : }
105 : }
106 :
107 257 : if (! osl_atomic_decrement( &pSequence->nRefCount ))
108 0 : rtl_freeMemory( pSequence );
109 257 : pSequence = pNew;
110 : }
111 : else
112 : {
113 : pSequence = (sal_Sequence *)rtl_reallocateMemory(
114 4883 : pSequence, SAL_SEQUENCE_HEADER_SIZE + nSize );
115 : }
116 :
117 5140 : if ( pSequence != 0 )
118 : {
119 5140 : pSequence->nRefCount = 1;
120 5140 : pSequence->nElements = nSize;
121 : }
122 :
123 5140 : *ppSequence = pSequence;
124 : }
125 :
126 : //==================================================================================================
127 776 : void SAL_CALL rtl_byte_sequence_acquire( sal_Sequence *pSequence )
128 : SAL_THROW_EXTERN_C()
129 : {
130 : OSL_ASSERT( pSequence );
131 776 : osl_atomic_increment( &(pSequence->nRefCount) );
132 776 : }
133 :
134 : //==================================================================================================
135 2147 : void SAL_CALL rtl_byte_sequence_release( sal_Sequence *pSequence )
136 : SAL_THROW_EXTERN_C()
137 : {
138 2147 : if ( pSequence != 0 )
139 : {
140 2147 : if (! osl_atomic_decrement( &(pSequence->nRefCount )) )
141 : {
142 1628 : rtl_freeMemory( pSequence );
143 : }
144 : }
145 2147 : }
146 :
147 : //==================================================================================================
148 775 : void SAL_CALL rtl_byte_sequence_construct( sal_Sequence **ppSequence , sal_Int32 nLength )
149 : SAL_THROW_EXTERN_C()
150 : {
151 : OSL_ASSERT( ppSequence );
152 775 : if( *ppSequence )
153 : {
154 0 : rtl_byte_sequence_release( *ppSequence );
155 0 : *ppSequence = 0;
156 : }
157 :
158 775 : if( nLength )
159 : {
160 2 : *ppSequence = (sal_Sequence *) rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
161 :
162 2 : if ( *ppSequence != 0 )
163 : {
164 2 : (*ppSequence)->nRefCount = 1;
165 2 : (*ppSequence)->nElements = nLength;
166 : }
167 : }
168 : else
169 : {
170 773 : *ppSequence = &aEmpty_rtl_ByteSeq;
171 773 : rtl_byte_sequence_acquire( *ppSequence );
172 : }
173 775 : }
174 :
175 : //==================================================================================================
176 1732 : void SAL_CALL rtl_byte_sequence_constructNoDefault( sal_Sequence **ppSequence , sal_Int32 nLength )
177 : SAL_THROW_EXTERN_C()
178 : {
179 : OSL_ASSERT( ppSequence );
180 1732 : if( *ppSequence )
181 : {
182 0 : rtl_byte_sequence_release( *ppSequence );
183 0 : *ppSequence = 0;
184 : }
185 :
186 1732 : *ppSequence = (sal_Sequence *) rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
187 :
188 1732 : if ( *ppSequence != 0 )
189 : {
190 1732 : (*ppSequence)->nRefCount = 1;
191 1732 : (*ppSequence)->nElements = nLength;
192 : }
193 1732 : }
194 :
195 : //==================================================================================================
196 1730 : void SAL_CALL rtl_byte_sequence_constructFromArray(
197 : sal_Sequence **ppSequence, const sal_Int8 *pData , sal_Int32 nLength )
198 : SAL_THROW_EXTERN_C()
199 : {
200 1730 : rtl_byte_sequence_constructNoDefault( ppSequence , nLength );
201 1730 : if ( *ppSequence != 0 )
202 1730 : memcpy( (*ppSequence)->elements, pData, nLength );
203 1730 : }
204 :
205 : //==================================================================================================
206 2 : void SAL_CALL rtl_byte_sequence_assign( sal_Sequence **ppSequence , sal_Sequence *pSequence )
207 : SAL_THROW_EXTERN_C()
208 : {
209 2 : if ( *ppSequence != pSequence)
210 : {
211 2 : if( *ppSequence )
212 : {
213 0 : rtl_byte_sequence_release( *ppSequence );
214 : }
215 2 : *ppSequence = pSequence;
216 2 : rtl_byte_sequence_acquire( *ppSequence );
217 : }
218 : // else
219 : // nothing to do
220 :
221 2 : }
222 :
223 : //==================================================================================================
224 12 : sal_Bool SAL_CALL rtl_byte_sequence_equals( sal_Sequence *pSequence1 , sal_Sequence *pSequence2 )
225 : SAL_THROW_EXTERN_C()
226 : {
227 : OSL_ASSERT( pSequence1 );
228 : OSL_ASSERT( pSequence2 );
229 12 : if (pSequence1 == pSequence2)
230 : {
231 4 : return sal_True;
232 : }
233 8 : if (pSequence1->nElements != pSequence2->nElements)
234 : {
235 4 : return sal_False;
236 : }
237 : return (sal_Bool)
238 : (memcmp(
239 4 : pSequence1->elements, pSequence2->elements, pSequence1->nElements )
240 4 : == 0);
241 : }
242 :
243 :
244 : //==================================================================================================
245 0 : const sal_Int8 *SAL_CALL rtl_byte_sequence_getConstArray( sal_Sequence *pSequence )
246 : SAL_THROW_EXTERN_C()
247 : {
248 0 : return ((const sal_Int8*)(pSequence->elements));
249 : }
250 :
251 : //==================================================================================================
252 0 : sal_Int32 SAL_CALL rtl_byte_sequence_getLength( sal_Sequence *pSequence )
253 : SAL_THROW_EXTERN_C()
254 : {
255 0 : return pSequence->nElements;
256 : }
257 :
258 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|