xref: /aoo4110/main/sal/rtl/source/byteseq.c (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <osl/diagnose.h>
25 #include <osl/interlck.h>
26 
27 #include <rtl/byteseq.h>
28 #include <rtl/alloc.h>
29 #include <rtl/memory.h>
30 
31 /* static data to be referenced by all empty strings
32  * the refCount is predefined to 1 and must never become 0 !
33  */
34 static sal_Sequence aEmpty_rtl_ByteSeq =
35 {
36     1,      /* sal_Int32    refCount;   */
37     0,      /* sal_Int32    length;     */
38     { 0 }   /* sal_Unicode  buffer[1];  */
39 };
40 
41 //==================================================================================================
rtl_byte_sequence_reference2One(sal_Sequence ** ppSequence)42 void SAL_CALL rtl_byte_sequence_reference2One(
43 	sal_Sequence ** ppSequence )
44 {
45 	sal_Sequence * pSequence, * pNew;
46 	sal_Int32 nElements;
47 
48 	OSL_ENSURE( ppSequence, "### null ptr!" );
49 	pSequence = *ppSequence;
50 
51 	if (pSequence->nRefCount > 1)
52 	{
53 		nElements = pSequence->nElements;
54 		if (nElements)
55 		{
56 			pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nElements );
57 
58             if ( pNew != 0 )
59                 rtl_copyMemory( pNew->elements, pSequence->elements, nElements );
60 
61             if (! osl_decrementInterlockedCount( &pSequence->nRefCount ))
62                 rtl_freeMemory( pSequence );
63 		}
64 		else
65 		{
66 			pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE );
67 		}
68 
69         if ( pNew != 0 )
70         {
71             pNew->nRefCount = 1;
72             pNew->nElements = nElements;
73         }
74 
75         *ppSequence = pNew;
76 	}
77 }
78 
79 //==================================================================================================
rtl_byte_sequence_realloc(sal_Sequence ** ppSequence,sal_Int32 nSize)80 void SAL_CALL rtl_byte_sequence_realloc(
81 	sal_Sequence ** ppSequence, sal_Int32 nSize )
82 {
83 	sal_Sequence * pSequence, * pNew;
84 	sal_Int32 nElements;
85 
86 	OSL_ENSURE( ppSequence, "### null ptr!" );
87 	pSequence = *ppSequence;
88 	nElements = pSequence->nElements;
89 
90 	if (nElements == nSize)
91 		return;
92 
93 	if (pSequence->nRefCount > 1) // split
94 	{
95 		pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nSize );
96 
97         if ( pNew != 0 )
98         {
99             if (nSize > nElements)
100             {
101                 rtl_copyMemory( pNew->elements, pSequence->elements, nElements );
102                 rtl_zeroMemory( pNew->elements + nElements, nSize - nElements );
103             }
104             else
105             {
106                 rtl_copyMemory( pNew->elements, pSequence->elements, nSize );
107             }
108         }
109 
110         if (! osl_decrementInterlockedCount( &pSequence->nRefCount ))
111             rtl_freeMemory( pSequence );
112 		pSequence = pNew;
113 	}
114 	else
115 	{
116 		pSequence = (sal_Sequence *)rtl_reallocateMemory(
117 			pSequence, SAL_SEQUENCE_HEADER_SIZE + nSize );
118 	}
119 
120     if ( pSequence != 0 )
121     {
122         pSequence->nRefCount = 1;
123         pSequence->nElements = nSize;
124     }
125 
126 	*ppSequence = pSequence;
127 }
128 
129 //==================================================================================================
rtl_byte_sequence_acquire(sal_Sequence * pSequence)130 void SAL_CALL rtl_byte_sequence_acquire( sal_Sequence *pSequence )
131 {
132 	OSL_ASSERT( pSequence );
133 	osl_incrementInterlockedCount( &(pSequence->nRefCount) );
134 }
135 
136 //==================================================================================================
rtl_byte_sequence_release(sal_Sequence * pSequence)137 void SAL_CALL rtl_byte_sequence_release( sal_Sequence *pSequence )
138 {
139     if ( pSequence != 0 )
140     {
141         if (! osl_decrementInterlockedCount( &(pSequence->nRefCount )) )
142         {
143             rtl_freeMemory( pSequence );
144         }
145     }
146 }
147 
148 //==================================================================================================
rtl_byte_sequence_construct(sal_Sequence ** ppSequence,sal_Int32 nLength)149 void SAL_CALL rtl_byte_sequence_construct( sal_Sequence **ppSequence , sal_Int32 nLength )
150 {
151 	OSL_ASSERT( ppSequence );
152 	if( *ppSequence )
153 	{
154 		rtl_byte_sequence_release( *ppSequence );
155 		*ppSequence = 0;
156 	}
157 
158 	if( nLength )
159 	{
160 		*ppSequence = (sal_Sequence *) rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
161 
162         if ( *ppSequence != 0 )
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 //==================================================================================================
rtl_byte_sequence_constructNoDefault(sal_Sequence ** ppSequence,sal_Int32 nLength)176 void SAL_CALL rtl_byte_sequence_constructNoDefault(	sal_Sequence **ppSequence , sal_Int32 nLength )
177 {
178 	OSL_ASSERT( ppSequence );
179 	if( *ppSequence )
180 	{
181 		rtl_byte_sequence_release( *ppSequence );
182 		*ppSequence = 0;
183 	}
184 
185 	*ppSequence = (sal_Sequence *) rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
186 
187 	if ( *ppSequence != 0 )
188 	{
189 		(*ppSequence)->nRefCount = 1;
190 		(*ppSequence)->nElements = nLength;
191 	}
192 }
193 
194 //==================================================================================================
rtl_byte_sequence_constructFromArray(sal_Sequence ** ppSequence,const sal_Int8 * pData,sal_Int32 nLength)195 void SAL_CALL rtl_byte_sequence_constructFromArray(
196 	sal_Sequence **ppSequence, const sal_Int8 *pData , sal_Int32 nLength )
197 {
198 	rtl_byte_sequence_constructNoDefault( ppSequence , nLength );
199     if ( *ppSequence != 0 )
200         rtl_copyMemory( (*ppSequence)->elements, pData, nLength );
201 }
202 
203 //==================================================================================================
rtl_byte_sequence_assign(sal_Sequence ** ppSequence,sal_Sequence * pSequence)204 void SAL_CALL rtl_byte_sequence_assign( sal_Sequence **ppSequence , sal_Sequence *pSequence )
205 {
206 	if ( *ppSequence != pSequence)
207 	{
208 		if( *ppSequence )
209 		{
210 			rtl_byte_sequence_release( *ppSequence );
211 		}
212 		*ppSequence = pSequence;
213 		rtl_byte_sequence_acquire( *ppSequence );
214 	}
215 //	else
216 //      nothing to do
217 
218 }
219 
220 //==================================================================================================
rtl_byte_sequence_equals(sal_Sequence * pSequence1,sal_Sequence * pSequence2)221 sal_Bool SAL_CALL rtl_byte_sequence_equals( sal_Sequence *pSequence1 , sal_Sequence *pSequence2 )
222 {
223 	OSL_ASSERT( pSequence1 );
224 	OSL_ASSERT( pSequence2 );
225 	if (pSequence1 == pSequence2)
226 	{
227 		return sal_True;
228 	}
229 	if (pSequence1->nElements != pSequence2->nElements)
230 	{
231 		return sal_False;
232 	}
233 	return (sal_Bool)
234         (rtl_compareMemory(
235             pSequence1->elements, pSequence2->elements, pSequence1->nElements )
236          == 0);
237 }
238 
239 
240 //==================================================================================================
rtl_byte_sequence_getConstArray(sal_Sequence * pSequence)241 const sal_Int8 *SAL_CALL rtl_byte_sequence_getConstArray( sal_Sequence *pSequence )
242 {
243 	return ((const sal_Int8*)(pSequence->elements));
244 }
245 
246 //==================================================================================================
rtl_byte_sequence_getLength(sal_Sequence * pSequence)247 sal_Int32 SAL_CALL rtl_byte_sequence_getLength( sal_Sequence *pSequence )
248 {
249 	return pSequence->nElements;
250 }
251