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 3937 : 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 3937 : pSequence = *ppSequence;
47 :
48 3937 : 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 3937 : }
75 :
76 : //==================================================================================================
77 49025 : 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 49025 : pSequence = *ppSequence;
85 49025 : nElements = pSequence->nElements;
86 :
87 49025 : if (nElements == nSize)
88 49025 : return;
89 :
90 49025 : if (pSequence->nRefCount > 1) // split
91 : {
92 5540 : pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nSize );
93 :
94 5540 : if ( pNew != 0 )
95 : {
96 5540 : if (nSize > nElements)
97 : {
98 5540 : memcpy( pNew->elements, pSequence->elements, nElements );
99 5540 : memset( pNew->elements + nElements, 0, nSize - nElements );
100 : }
101 : else
102 : {
103 0 : memcpy( pNew->elements, pSequence->elements, nSize );
104 : }
105 : }
106 :
107 5540 : if (! osl_atomic_decrement( &pSequence->nRefCount ))
108 0 : rtl_freeMemory( pSequence );
109 5540 : pSequence = pNew;
110 : }
111 : else
112 : {
113 : pSequence = (sal_Sequence *)rtl_reallocateMemory(
114 43485 : pSequence, SAL_SEQUENCE_HEADER_SIZE + nSize );
115 : }
116 :
117 49025 : if ( pSequence != 0 )
118 : {
119 49025 : pSequence->nRefCount = 1;
120 49025 : pSequence->nElements = nSize;
121 : }
122 :
123 49025 : *ppSequence = pSequence;
124 : }
125 :
126 : //==================================================================================================
127 2401983 : void SAL_CALL rtl_byte_sequence_acquire( sal_Sequence *pSequence )
128 : SAL_THROW_EXTERN_C()
129 : {
130 : OSL_ASSERT( pSequence );
131 2401983 : osl_atomic_increment( &(pSequence->nRefCount) );
132 2401983 : }
133 :
134 : //==================================================================================================
135 2407377 : void SAL_CALL rtl_byte_sequence_release( sal_Sequence *pSequence )
136 : SAL_THROW_EXTERN_C()
137 : {
138 2407377 : if ( pSequence != 0 )
139 : {
140 2407409 : if (! osl_atomic_decrement( &(pSequence->nRefCount )) )
141 : {
142 10385 : rtl_freeMemory( pSequence );
143 : }
144 : }
145 2407377 : }
146 :
147 : //==================================================================================================
148 237942 : void SAL_CALL rtl_byte_sequence_construct( sal_Sequence **ppSequence , sal_Int32 nLength )
149 : SAL_THROW_EXTERN_C()
150 : {
151 : OSL_ASSERT( ppSequence );
152 237942 : if( *ppSequence )
153 : {
154 0 : rtl_byte_sequence_release( *ppSequence );
155 0 : *ppSequence = 0;
156 : }
157 :
158 237942 : if( nLength )
159 : {
160 96 : *ppSequence = (sal_Sequence *) rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
161 :
162 96 : if ( *ppSequence != 0 )
163 : {
164 96 : (*ppSequence)->nRefCount = 1;
165 96 : (*ppSequence)->nElements = nLength;
166 : }
167 : }
168 : else
169 : {
170 237846 : *ppSequence = &aEmpty_rtl_ByteSeq;
171 237846 : rtl_byte_sequence_acquire( *ppSequence );
172 : }
173 237942 : }
174 :
175 : //==================================================================================================
176 11516 : void SAL_CALL rtl_byte_sequence_constructNoDefault( sal_Sequence **ppSequence , sal_Int32 nLength )
177 : SAL_THROW_EXTERN_C()
178 : {
179 : OSL_ASSERT( ppSequence );
180 11516 : if( *ppSequence )
181 : {
182 0 : rtl_byte_sequence_release( *ppSequence );
183 0 : *ppSequence = 0;
184 : }
185 :
186 11516 : *ppSequence = (sal_Sequence *) rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
187 :
188 11516 : if ( *ppSequence != 0 )
189 : {
190 11516 : (*ppSequence)->nRefCount = 1;
191 11516 : (*ppSequence)->nElements = nLength;
192 : }
193 11516 : }
194 :
195 : //==================================================================================================
196 10526 : 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 10526 : rtl_byte_sequence_constructNoDefault( ppSequence , nLength );
201 10526 : if ( *ppSequence != 0 )
202 10526 : memcpy( (*ppSequence)->elements, pData, nLength );
203 10526 : }
204 :
205 : //==================================================================================================
206 2347887 : void SAL_CALL rtl_byte_sequence_assign( sal_Sequence **ppSequence , sal_Sequence *pSequence )
207 : SAL_THROW_EXTERN_C()
208 : {
209 2347887 : if ( *ppSequence != pSequence)
210 : {
211 1814196 : if( *ppSequence )
212 : {
213 552696 : rtl_byte_sequence_release( *ppSequence );
214 : }
215 1814196 : *ppSequence = pSequence;
216 1814196 : rtl_byte_sequence_acquire( *ppSequence );
217 : }
218 : // else
219 : // nothing to do
220 :
221 2347890 : }
222 :
223 : //==================================================================================================
224 694777 : 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 694777 : if (pSequence1 == pSequence2)
230 : {
231 622960 : return sal_True;
232 : }
233 71817 : if (pSequence1->nElements != pSequence2->nElements)
234 : {
235 20701 : return sal_False;
236 : }
237 : return (sal_Bool)
238 : (memcmp(
239 51116 : pSequence1->elements, pSequence2->elements, pSequence1->nElements )
240 51116 : == 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: */
|