Debian Codegraph @

sal/rtl/byteseq.cxx

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