LCOV - code coverage report
Current view: top level - libreoffice/sal/rtl/source - byteseq.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 76 88 86.4 %
Date: 2012-12-17 Functions: 9 11 81.8 %
Legend: Lines: hit not hit

          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             : //==================================================================================================
      39          18 : 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          18 :     pSequence = *ppSequence;
      47             : 
      48          18 :     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          18 : }
      75             : 
      76             : //==================================================================================================
      77       22634 : void SAL_CALL rtl_byte_sequence_realloc(
      78             :     sal_Sequence ** ppSequence, sal_Int32 nSize ) SAL_THROW_EXTERN_C()
      79             : {
      80             :     sal_Sequence * pSequence, * pNew;
      81             :     sal_Int32 nElements;
      82             : 
      83             :     OSL_ENSURE( ppSequence, "### null ptr!" );
      84       22634 :     pSequence = *ppSequence;
      85       22634 :     nElements = pSequence->nElements;
      86             : 
      87       22634 :     if (nElements == nSize)
      88       22634 :         return;
      89             : 
      90       22634 :     if (pSequence->nRefCount > 1) // split
      91             :     {
      92        1683 :         pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nSize );
      93             : 
      94        1683 :         if ( pNew != 0 )
      95             :         {
      96        1683 :             if (nSize > nElements)
      97             :             {
      98        1683 :                 memcpy( pNew->elements, pSequence->elements, nElements );
      99        1683 :                 memset( pNew->elements + nElements, 0, nSize - nElements );
     100             :             }
     101             :             else
     102             :             {
     103           0 :                 memcpy( pNew->elements, pSequence->elements, nSize );
     104             :             }
     105             :         }
     106             : 
     107        1683 :         if (! osl_atomic_decrement( &pSequence->nRefCount ))
     108           0 :             rtl_freeMemory( pSequence );
     109        1683 :         pSequence = pNew;
     110             :     }
     111             :     else
     112             :     {
     113             :         pSequence = (sal_Sequence *)rtl_reallocateMemory(
     114       20951 :             pSequence, SAL_SEQUENCE_HEADER_SIZE + nSize );
     115             :     }
     116             : 
     117       22634 :     if ( pSequence != 0 )
     118             :     {
     119       22634 :         pSequence->nRefCount = 1;
     120       22634 :         pSequence->nElements = nSize;
     121             :     }
     122             : 
     123       22634 :     *ppSequence = pSequence;
     124             : }
     125             : 
     126             : //==================================================================================================
     127       26923 : void SAL_CALL rtl_byte_sequence_acquire( sal_Sequence *pSequence )
     128             :     SAL_THROW_EXTERN_C()
     129             : {
     130             :     OSL_ASSERT( pSequence );
     131       26923 :     osl_atomic_increment( &(pSequence->nRefCount) );
     132       26923 : }
     133             : 
     134             : //==================================================================================================
     135       29922 : void SAL_CALL rtl_byte_sequence_release( sal_Sequence *pSequence )
     136             :     SAL_THROW_EXTERN_C()
     137             : {
     138       29922 :     if ( pSequence != 0 )
     139             :     {
     140       29924 :         if (! osl_atomic_decrement( &(pSequence->nRefCount )) )
     141             :         {
     142        4680 :             rtl_freeMemory( pSequence );
     143             :         }
     144             :     }
     145       29922 : }
     146             : 
     147             : //==================================================================================================
     148        6943 : void SAL_CALL rtl_byte_sequence_construct( sal_Sequence **ppSequence , sal_Int32 nLength )
     149             :     SAL_THROW_EXTERN_C()
     150             : {
     151             :     OSL_ASSERT( ppSequence );
     152        6943 :     if( *ppSequence )
     153             :     {
     154           0 :         rtl_byte_sequence_release( *ppSequence );
     155           0 :         *ppSequence = 0;
     156             :     }
     157             : 
     158        6943 :     if( nLength )
     159             :     {
     160           4 :         *ppSequence = (sal_Sequence *) rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
     161             : 
     162           4 :         if ( *ppSequence != 0 )
     163             :         {
     164           4 :             (*ppSequence)->nRefCount = 1;
     165           4 :             (*ppSequence)->nElements = nLength;
     166             :         }
     167             :     }
     168             :     else
     169             :     {
     170        6939 :         *ppSequence = &aEmpty_rtl_ByteSeq;
     171        6939 :         rtl_byte_sequence_acquire( *ppSequence );
     172             :     }
     173        6943 : }
     174             : 
     175             : //==================================================================================================
     176        4090 : void SAL_CALL rtl_byte_sequence_constructNoDefault( sal_Sequence **ppSequence , sal_Int32 nLength )
     177             :     SAL_THROW_EXTERN_C()
     178             : {
     179             :     OSL_ASSERT( ppSequence );
     180        4090 :     if( *ppSequence )
     181             :     {
     182           0 :         rtl_byte_sequence_release( *ppSequence );
     183           0 :         *ppSequence = 0;
     184             :     }
     185             : 
     186        4090 :     *ppSequence = (sal_Sequence *) rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
     187             : 
     188        4090 :     if ( *ppSequence != 0 )
     189             :     {
     190        4090 :         (*ppSequence)->nRefCount = 1;
     191        4090 :         (*ppSequence)->nElements = nLength;
     192             :     }
     193        4090 : }
     194             : 
     195             : //==================================================================================================
     196        4069 : void SAL_CALL rtl_byte_sequence_constructFromArray(
     197             :     sal_Sequence **ppSequence, const sal_Int8 *pData , sal_Int32 nLength )
     198             :     SAL_THROW_EXTERN_C()
     199             : {
     200        4069 :     rtl_byte_sequence_constructNoDefault( ppSequence , nLength );
     201        4069 :     if ( *ppSequence != 0 )
     202        4069 :         memcpy( (*ppSequence)->elements, pData, nLength );
     203        4069 : }
     204             : 
     205             : //==================================================================================================
     206       21916 : void SAL_CALL rtl_byte_sequence_assign( sal_Sequence **ppSequence , sal_Sequence *pSequence )
     207             :     SAL_THROW_EXTERN_C()
     208             : {
     209       21916 :     if ( *ppSequence != pSequence)
     210             :     {
     211       16850 :         if( *ppSequence )
     212             :         {
     213        5521 :             rtl_byte_sequence_release( *ppSequence );
     214             :         }
     215       16850 :         *ppSequence = pSequence;
     216       16850 :         rtl_byte_sequence_acquire( *ppSequence );
     217             :     }
     218             : //  else
     219             : //      nothing to do
     220             : 
     221       21916 : }
     222             : 
     223             : //==================================================================================================
     224        5721 : sal_Bool SAL_CALL rtl_byte_sequence_equals( sal_Sequence *pSequence1 , sal_Sequence *pSequence2 )
     225             :     SAL_THROW_EXTERN_C()
     226             : {
     227             :     OSL_ASSERT( pSequence1 );
     228             :     OSL_ASSERT( pSequence2 );
     229        5721 :     if (pSequence1 == pSequence2)
     230             :     {
     231        5499 :         return sal_True;
     232             :     }
     233         222 :     if (pSequence1->nElements != pSequence2->nElements)
     234             :     {
     235          49 :         return sal_False;
     236             :     }
     237             :     return (sal_Bool)
     238             :         (memcmp(
     239         173 :             pSequence1->elements, pSequence2->elements, pSequence1->nElements )
     240         173 :          == 0);
     241             : }
     242             : 
     243             : 
     244             : //==================================================================================================
     245           0 : const sal_Int8 *SAL_CALL rtl_byte_sequence_getConstArray( sal_Sequence *pSequence )
     246             :     SAL_THROW_EXTERN_C()
     247             : {
     248           0 :     return ((const sal_Int8*)(pSequence->elements));
     249             : }
     250             : 
     251             : //==================================================================================================
     252           0 : sal_Int32 SAL_CALL rtl_byte_sequence_getLength( sal_Sequence *pSequence )
     253             :     SAL_THROW_EXTERN_C()
     254             : {
     255           0 :     return pSequence->nElements;
     256             : }
     257             : 
     258             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10