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 1 : void SAL_CALL rtl_byte_sequence_reference2One(
39 : sal_Sequence ** ppSequence ) SAL_THROW_EXTERN_C()
40 : {
41 : sal_Sequence * pSequence, * pNew;
42 : sal_Int32 nElements;
43 :
44 : OSL_ENSURE( ppSequence, "### null ptr!" );
45 1 : pSequence = *ppSequence;
46 :
47 1 : if (pSequence->nRefCount > 1)
48 : {
49 0 : nElements = pSequence->nElements;
50 0 : if (nElements)
51 : {
52 0 : pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nElements );
53 :
54 0 : if ( pNew != 0 )
55 0 : memcpy( pNew->elements, pSequence->elements, nElements );
56 :
57 0 : if (! osl_atomic_decrement( &pSequence->nRefCount ))
58 0 : rtl_freeMemory( pSequence );
59 : }
60 : else
61 : {
62 0 : pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE );
63 : }
64 :
65 0 : if ( pNew != 0 )
66 : {
67 0 : pNew->nRefCount = 1;
68 0 : pNew->nElements = nElements;
69 : }
70 :
71 0 : *ppSequence = pNew;
72 : }
73 1 : }
74 :
75 3485515 : void SAL_CALL rtl_byte_sequence_realloc(
76 : sal_Sequence ** ppSequence, sal_Int32 nSize ) SAL_THROW_EXTERN_C()
77 : {
78 : sal_Sequence * pSequence, * pNew;
79 : sal_Int32 nElements;
80 :
81 : OSL_ENSURE( ppSequence, "### null ptr!" );
82 3485515 : pSequence = *ppSequence;
83 3485515 : nElements = pSequence->nElements;
84 :
85 3485515 : if (nElements == nSize)
86 3485515 : return;
87 :
88 3485515 : if (pSequence->nRefCount > 1) // split
89 : {
90 255038 : pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nSize );
91 :
92 255038 : if ( pNew != 0 )
93 : {
94 255038 : if (nSize > nElements)
95 : {
96 255038 : memcpy( pNew->elements, pSequence->elements, nElements );
97 255038 : memset( pNew->elements + nElements, 0, nSize - nElements );
98 : }
99 : else
100 : {
101 0 : memcpy( pNew->elements, pSequence->elements, nSize );
102 : }
103 : }
104 :
105 255038 : if (! osl_atomic_decrement( &pSequence->nRefCount ))
106 0 : rtl_freeMemory( pSequence );
107 255038 : pSequence = pNew;
108 : }
109 : else
110 : {
111 : pSequence = (sal_Sequence *)rtl_reallocateMemory(
112 3230477 : pSequence, SAL_SEQUENCE_HEADER_SIZE + nSize );
113 : }
114 :
115 3485515 : if ( pSequence != 0 )
116 : {
117 3485515 : pSequence->nRefCount = 1;
118 3485515 : pSequence->nElements = nSize;
119 : }
120 :
121 3485515 : *ppSequence = pSequence;
122 : }
123 :
124 892610 : void SAL_CALL rtl_byte_sequence_acquire( sal_Sequence *pSequence )
125 : SAL_THROW_EXTERN_C()
126 : {
127 : OSL_ASSERT( pSequence );
128 892610 : osl_atomic_increment( &(pSequence->nRefCount) );
129 892610 : }
130 :
131 1062628 : void SAL_CALL rtl_byte_sequence_release( sal_Sequence *pSequence )
132 : SAL_THROW_EXTERN_C()
133 : {
134 1062628 : if ( pSequence != 0 )
135 : {
136 1062628 : if (! osl_atomic_decrement( &(pSequence->nRefCount )) )
137 : {
138 425056 : rtl_freeMemory( pSequence );
139 : }
140 : }
141 1062628 : }
142 :
143 425057 : void SAL_CALL rtl_byte_sequence_construct( sal_Sequence **ppSequence , sal_Int32 nLength )
144 : SAL_THROW_EXTERN_C()
145 : {
146 : OSL_ASSERT( ppSequence );
147 425057 : if( *ppSequence )
148 : {
149 0 : rtl_byte_sequence_release( *ppSequence );
150 0 : *ppSequence = 0;
151 : }
152 :
153 425057 : if( nLength )
154 : {
155 0 : *ppSequence = (sal_Sequence *) rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
156 :
157 0 : if ( *ppSequence != 0 )
158 : {
159 0 : (*ppSequence)->nRefCount = 1;
160 0 : (*ppSequence)->nElements = nLength;
161 : }
162 : }
163 : else
164 : {
165 425057 : *ppSequence = &aEmpty_rtl_ByteSeq;
166 425057 : rtl_byte_sequence_acquire( *ppSequence );
167 : }
168 425057 : }
169 :
170 170018 : void SAL_CALL rtl_byte_sequence_constructNoDefault( sal_Sequence **ppSequence , sal_Int32 nLength )
171 : SAL_THROW_EXTERN_C()
172 : {
173 : OSL_ASSERT( ppSequence );
174 170018 : if( *ppSequence )
175 : {
176 0 : rtl_byte_sequence_release( *ppSequence );
177 0 : *ppSequence = 0;
178 : }
179 :
180 170018 : *ppSequence = (sal_Sequence *) rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
181 :
182 170018 : if ( *ppSequence != 0 )
183 : {
184 170018 : (*ppSequence)->nRefCount = 1;
185 170018 : (*ppSequence)->nElements = nLength;
186 : }
187 170018 : }
188 :
189 170018 : void SAL_CALL rtl_byte_sequence_constructFromArray(
190 : sal_Sequence **ppSequence, const sal_Int8 *pData , sal_Int32 nLength )
191 : SAL_THROW_EXTERN_C()
192 : {
193 170018 : rtl_byte_sequence_constructNoDefault( ppSequence , nLength );
194 170018 : if ( *ppSequence != 0 )
195 170018 : memcpy( (*ppSequence)->elements, pData, nLength );
196 170018 : }
197 :
198 340035 : void SAL_CALL rtl_byte_sequence_assign( sal_Sequence **ppSequence , sal_Sequence *pSequence )
199 : SAL_THROW_EXTERN_C()
200 : {
201 340035 : if ( *ppSequence != pSequence)
202 : {
203 340035 : if( *ppSequence )
204 : {
205 170017 : rtl_byte_sequence_release( *ppSequence );
206 : }
207 340035 : *ppSequence = pSequence;
208 340035 : rtl_byte_sequence_acquire( *ppSequence );
209 : }
210 : // else
211 : // nothing to do
212 :
213 340035 : }
214 :
215 0 : sal_Bool SAL_CALL rtl_byte_sequence_equals( sal_Sequence *pSequence1 , sal_Sequence *pSequence2 )
216 : SAL_THROW_EXTERN_C()
217 : {
218 : OSL_ASSERT( pSequence1 );
219 : OSL_ASSERT( pSequence2 );
220 0 : if (pSequence1 == pSequence2)
221 : {
222 0 : return sal_True;
223 : }
224 0 : if (pSequence1->nElements != pSequence2->nElements)
225 : {
226 0 : return sal_False;
227 : }
228 : return
229 : memcmp(
230 0 : pSequence1->elements, pSequence2->elements, pSequence1->nElements )
231 0 : == 0;
232 : }
233 :
234 0 : const sal_Int8 *SAL_CALL rtl_byte_sequence_getConstArray( sal_Sequence *pSequence )
235 : SAL_THROW_EXTERN_C()
236 : {
237 0 : return ((const sal_Int8*)(pSequence->elements));
238 : }
239 :
240 0 : sal_Int32 SAL_CALL rtl_byte_sequence_getLength( sal_Sequence *pSequence )
241 : SAL_THROW_EXTERN_C()
242 : {
243 0 : return pSequence->nElements;
244 : }
245 :
246 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|