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 <assert.h>
21 : #include <string.h>
22 :
23 : #include <osl/diagnose.h>
24 : #include <osl/interlck.h>
25 :
26 : #include <rtl/byteseq.h>
27 : #include <rtl/alloc.h>
28 :
29 : /* static data to be referenced by all empty strings
30 : * the refCount is predefined to 1 and must never become 0 !
31 : */
32 : static sal_Sequence aEmpty_rtl_ByteSeq =
33 : {
34 : 1, /* sal_Int32 refCount; */
35 : 0, /* sal_Int32 length; */
36 : { 0 } /* sal_Unicode buffer[1]; */
37 : };
38 :
39 3604 : 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 3604 : pSequence = *ppSequence;
47 :
48 3604 : if (pSequence->nRefCount > 1)
49 : {
50 2 : nElements = pSequence->nElements;
51 2 : if (nElements)
52 : {
53 2 : pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nElements );
54 :
55 2 : if ( pNew != 0 )
56 2 : memcpy( pNew->elements, pSequence->elements, nElements );
57 :
58 2 : 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 2 : if ( pNew != 0 )
67 : {
68 2 : pNew->nRefCount = 1;
69 2 : pNew->nElements = nElements;
70 : }
71 :
72 2 : *ppSequence = pNew;
73 : }
74 3604 : }
75 :
76 27551 : void SAL_CALL rtl_byte_sequence_realloc(
77 : sal_Sequence ** ppSequence, sal_Int32 nSize ) SAL_THROW_EXTERN_C()
78 : {
79 : sal_Sequence * pSequence, * pNew;
80 : sal_Int32 nElements;
81 :
82 : assert(ppSequence && "### null ptr!");
83 27551 : pSequence = *ppSequence;
84 27551 : nElements = pSequence->nElements;
85 :
86 27551 : if (nElements == nSize)
87 27551 : return;
88 :
89 27551 : if (pSequence->nRefCount > 1) // split
90 : {
91 3095 : pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nSize );
92 :
93 3095 : if ( pNew != 0 )
94 : {
95 3095 : if (nSize > nElements)
96 : {
97 3095 : memcpy( pNew->elements, pSequence->elements, nElements );
98 3095 : memset( pNew->elements + nElements, 0, nSize - nElements );
99 : }
100 : else
101 : {
102 0 : memcpy( pNew->elements, pSequence->elements, nSize );
103 : }
104 : }
105 :
106 3095 : if (! osl_atomic_decrement( &pSequence->nRefCount ))
107 0 : rtl_freeMemory( pSequence );
108 3095 : pSequence = pNew;
109 : }
110 : else
111 : {
112 : pSequence = (sal_Sequence *)rtl_reallocateMemory(
113 24456 : pSequence, SAL_SEQUENCE_HEADER_SIZE + nSize );
114 : }
115 :
116 27551 : if ( pSequence != 0 )
117 : {
118 27551 : pSequence->nRefCount = 1;
119 27551 : pSequence->nElements = nSize;
120 : }
121 :
122 27551 : *ppSequence = pSequence;
123 : }
124 :
125 5134767 : void SAL_CALL rtl_byte_sequence_acquire( sal_Sequence *pSequence )
126 : SAL_THROW_EXTERN_C()
127 : {
128 : OSL_ASSERT( pSequence );
129 5134767 : osl_atomic_increment( &(pSequence->nRefCount) );
130 5134767 : }
131 :
132 5137753 : void SAL_CALL rtl_byte_sequence_release( sal_Sequence *pSequence )
133 : SAL_THROW_EXTERN_C()
134 : {
135 5137753 : if ( pSequence != 0 )
136 : {
137 5137793 : if (! osl_atomic_decrement( &(pSequence->nRefCount )) )
138 : {
139 5920 : rtl_freeMemory( pSequence );
140 : }
141 : }
142 5137753 : }
143 :
144 470256 : void SAL_CALL rtl_byte_sequence_construct( sal_Sequence **ppSequence , sal_Int32 nLength )
145 : SAL_THROW_EXTERN_C()
146 : {
147 : OSL_ASSERT( ppSequence );
148 470256 : if( *ppSequence )
149 : {
150 0 : rtl_byte_sequence_release( *ppSequence );
151 0 : *ppSequence = 0;
152 : }
153 :
154 470256 : if( nLength )
155 : {
156 192 : *ppSequence = (sal_Sequence *) rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
157 :
158 192 : if ( *ppSequence != 0 )
159 : {
160 192 : (*ppSequence)->nRefCount = 1;
161 192 : (*ppSequence)->nElements = nLength;
162 : }
163 : }
164 : else
165 : {
166 470064 : *ppSequence = &aEmpty_rtl_ByteSeq;
167 470064 : rtl_byte_sequence_acquire( *ppSequence );
168 : }
169 470257 : }
170 :
171 3503 : void SAL_CALL rtl_byte_sequence_constructNoDefault( sal_Sequence **ppSequence , sal_Int32 nLength )
172 : SAL_THROW_EXTERN_C()
173 : {
174 : OSL_ASSERT( ppSequence );
175 3503 : if( *ppSequence )
176 : {
177 0 : rtl_byte_sequence_release( *ppSequence );
178 0 : *ppSequence = 0;
179 : }
180 :
181 3503 : *ppSequence = (sal_Sequence *) rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
182 :
183 3503 : if ( *ppSequence != 0 )
184 : {
185 3503 : (*ppSequence)->nRefCount = 1;
186 3503 : (*ppSequence)->nElements = nLength;
187 : }
188 3503 : }
189 :
190 1790 : void SAL_CALL rtl_byte_sequence_constructFromArray(
191 : sal_Sequence **ppSequence, const sal_Int8 *pData , sal_Int32 nLength )
192 : SAL_THROW_EXTERN_C()
193 : {
194 1790 : rtl_byte_sequence_constructNoDefault( ppSequence , nLength );
195 1790 : if ( *ppSequence != 0 )
196 1790 : memcpy( (*ppSequence)->elements, pData, nLength );
197 1790 : }
198 :
199 5051673 : void SAL_CALL rtl_byte_sequence_assign( sal_Sequence **ppSequence , sal_Sequence *pSequence )
200 : SAL_THROW_EXTERN_C()
201 : {
202 5051673 : if ( *ppSequence != pSequence)
203 : {
204 3932129 : if( *ppSequence )
205 : {
206 1248913 : rtl_byte_sequence_release( *ppSequence );
207 : }
208 3932133 : *ppSequence = pSequence;
209 3932133 : rtl_byte_sequence_acquire( *ppSequence );
210 : }
211 : // else
212 : // nothing to do
213 :
214 5051681 : }
215 :
216 1498505 : sal_Bool SAL_CALL rtl_byte_sequence_equals( sal_Sequence *pSequence1 , sal_Sequence *pSequence2 )
217 : SAL_THROW_EXTERN_C()
218 : {
219 : assert(pSequence1 && pSequence2);
220 1498505 : if (pSequence1 == pSequence2)
221 : {
222 1328289 : return sal_True;
223 : }
224 170216 : if (pSequence1->nElements != pSequence2->nElements)
225 : {
226 46838 : return sal_False;
227 : }
228 : return
229 : memcmp(
230 123378 : pSequence1->elements, pSequence2->elements, pSequence1->nElements )
231 123378 : == 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: */
|