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 2012 : 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 2012 : pSequence = *ppSequence;
47 :
48 2012 : if (pSequence->nRefCount > 1)
49 : {
50 1 : nElements = pSequence->nElements;
51 1 : if (nElements)
52 : {
53 1 : pNew = static_cast<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 = static_cast<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 2012 : }
75 :
76 18336 : 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 18336 : pSequence = *ppSequence;
84 18336 : nElements = pSequence->nElements;
85 :
86 18336 : if (nElements == nSize)
87 18336 : return;
88 :
89 18336 : if (pSequence->nRefCount > 1) // split
90 : {
91 2298 : pNew = static_cast<sal_Sequence *>(rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nSize ));
92 :
93 2298 : if ( pNew != 0 )
94 : {
95 2298 : if (nSize > nElements)
96 : {
97 2298 : memcpy( pNew->elements, pSequence->elements, nElements );
98 2298 : memset( pNew->elements + nElements, 0, nSize - nElements );
99 : }
100 : else
101 : {
102 0 : memcpy( pNew->elements, pSequence->elements, nSize );
103 : }
104 : }
105 :
106 2298 : if (! osl_atomic_decrement( &pSequence->nRefCount ))
107 0 : rtl_freeMemory( pSequence );
108 2298 : pSequence = pNew;
109 : }
110 : else
111 : {
112 : pSequence = static_cast<sal_Sequence *>(rtl_reallocateMemory(
113 16038 : pSequence, SAL_SEQUENCE_HEADER_SIZE + nSize ));
114 : }
115 :
116 18336 : if ( pSequence != 0 )
117 : {
118 18336 : pSequence->nRefCount = 1;
119 18336 : pSequence->nElements = nSize;
120 : }
121 :
122 18336 : *ppSequence = pSequence;
123 : }
124 :
125 2614167 : void SAL_CALL rtl_byte_sequence_acquire( sal_Sequence *pSequence )
126 : SAL_THROW_EXTERN_C()
127 : {
128 : OSL_ASSERT( pSequence );
129 2614167 : osl_atomic_increment( &(pSequence->nRefCount) );
130 2614167 : }
131 :
132 2614796 : void SAL_CALL rtl_byte_sequence_release( sal_Sequence *pSequence )
133 : SAL_THROW_EXTERN_C()
134 : {
135 2614796 : if ( pSequence != 0 )
136 : {
137 2614875 : if (! osl_atomic_decrement( &(pSequence->nRefCount )) )
138 : {
139 3876 : rtl_freeMemory( pSequence );
140 : }
141 : }
142 2614796 : }
143 :
144 251130 : void SAL_CALL rtl_byte_sequence_construct( sal_Sequence **ppSequence , sal_Int32 nLength )
145 : SAL_THROW_EXTERN_C()
146 : {
147 : OSL_ASSERT( ppSequence );
148 251130 : if( *ppSequence )
149 : {
150 0 : rtl_byte_sequence_release( *ppSequence );
151 0 : *ppSequence = 0;
152 : }
153 :
154 251130 : if( nLength )
155 : {
156 96 : *ppSequence = static_cast<sal_Sequence *>(rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE + nLength ));
157 :
158 96 : if ( *ppSequence != 0 )
159 : {
160 96 : (*ppSequence)->nRefCount = 1;
161 96 : (*ppSequence)->nElements = nLength;
162 : }
163 : }
164 : else
165 : {
166 251034 : *ppSequence = &aEmpty_rtl_ByteSeq;
167 251034 : rtl_byte_sequence_acquire( *ppSequence );
168 : }
169 251132 : }
170 :
171 2168 : void SAL_CALL rtl_byte_sequence_constructNoDefault( sal_Sequence **ppSequence , sal_Int32 nLength )
172 : SAL_THROW_EXTERN_C()
173 : {
174 : OSL_ASSERT( ppSequence );
175 2168 : if( *ppSequence )
176 : {
177 0 : rtl_byte_sequence_release( *ppSequence );
178 0 : *ppSequence = 0;
179 : }
180 :
181 2168 : *ppSequence = static_cast<sal_Sequence *>(rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nLength ));
182 :
183 2168 : if ( *ppSequence != 0 )
184 : {
185 2168 : (*ppSequence)->nRefCount = 1;
186 2168 : (*ppSequence)->nElements = nLength;
187 : }
188 2168 : }
189 :
190 1306 : 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 1306 : rtl_byte_sequence_constructNoDefault( ppSequence , nLength );
195 1306 : if ( *ppSequence != 0 && nLength != 0 )
196 1296 : memcpy( (*ppSequence)->elements, pData, nLength );
197 1306 : }
198 :
199 2589886 : void SAL_CALL rtl_byte_sequence_assign( sal_Sequence **ppSequence , sal_Sequence *pSequence )
200 : SAL_THROW_EXTERN_C()
201 : {
202 2589886 : if ( *ppSequence != pSequence)
203 : {
204 1985354 : if( *ppSequence )
205 : {
206 742187 : rtl_byte_sequence_release( *ppSequence );
207 : }
208 1986348 : *ppSequence = pSequence;
209 1986348 : rtl_byte_sequence_acquire( *ppSequence );
210 : }
211 : // else
212 : // nothing to do
213 :
214 2591004 : }
215 :
216 876724 : 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 876724 : if (pSequence1 == pSequence2)
221 : {
222 724225 : return sal_True;
223 : }
224 152499 : if (pSequence1->nElements != pSequence2->nElements)
225 : {
226 53757 : return sal_False;
227 : }
228 : return
229 : memcmp(
230 98742 : pSequence1->elements, pSequence2->elements, pSequence1->nElements )
231 98742 : == 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 reinterpret_cast<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: */
|