LCOV - code coverage report
Current view: top level - sal/rtl - byteseq.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 76 88 86.4 %
Date: 2014-04-11 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        2109 : void SAL_CALL rtl_byte_sequence_reference2One(
      39             :     sal_Sequence ** ppSequence ) SAL_THROW_EXTERN_C()
      40             : {
      41             :     sal_Sequence * pSequence, * pNew;
      42             :     sal_Int32 nElements;
      43             : 
      44             :     OSL_ENSURE( ppSequence, "### null ptr!" );
      45        2109 :     pSequence = *ppSequence;
      46             : 
      47        2109 :     if (pSequence->nRefCount > 1)
      48             :     {
      49           1 :         nElements = pSequence->nElements;
      50           1 :         if (nElements)
      51             :         {
      52           1 :             pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nElements );
      53             : 
      54           1 :             if ( pNew != 0 )
      55           1 :                 memcpy( pNew->elements, pSequence->elements, nElements );
      56             : 
      57           1 :             if (! osl_atomic_decrement( &pSequence->nRefCount ))
      58           0 :                 rtl_freeMemory( pSequence );
      59             :         }
      60             :         else
      61             :         {
      62           0 :             pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE );
      63             :         }
      64             : 
      65           1 :         if ( pNew != 0 )
      66             :         {
      67           1 :             pNew->nRefCount = 1;
      68           1 :             pNew->nElements = nElements;
      69             :         }
      70             : 
      71           1 :         *ppSequence = pNew;
      72             :     }
      73        2109 : }
      74             : 
      75       20701 : void SAL_CALL rtl_byte_sequence_realloc(
      76             :     sal_Sequence ** ppSequence, sal_Int32 nSize ) SAL_THROW_EXTERN_C()
      77             : {
      78             :     sal_Sequence * pSequence, * pNew;
      79             :     sal_Int32 nElements;
      80             : 
      81             :     OSL_ENSURE( ppSequence, "### null ptr!" );
      82       20701 :     pSequence = *ppSequence;
      83       20701 :     nElements = pSequence->nElements;
      84             : 
      85       20701 :     if (nElements == nSize)
      86       20701 :         return;
      87             : 
      88       20701 :     if (pSequence->nRefCount > 1) // split
      89             :     {
      90        1650 :         pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nSize );
      91             : 
      92        1650 :         if ( pNew != 0 )
      93             :         {
      94        1650 :             if (nSize > nElements)
      95             :             {
      96        1650 :                 memcpy( pNew->elements, pSequence->elements, nElements );
      97        1650 :                 memset( pNew->elements + nElements, 0, nSize - nElements );
      98             :             }
      99             :             else
     100             :             {
     101           0 :                 memcpy( pNew->elements, pSequence->elements, nSize );
     102             :             }
     103             :         }
     104             : 
     105        1650 :         if (! osl_atomic_decrement( &pSequence->nRefCount ))
     106           0 :             rtl_freeMemory( pSequence );
     107        1650 :         pSequence = pNew;
     108             :     }
     109             :     else
     110             :     {
     111             :         pSequence = (sal_Sequence *)rtl_reallocateMemory(
     112       19051 :             pSequence, SAL_SEQUENCE_HEADER_SIZE + nSize );
     113             :     }
     114             : 
     115       20701 :     if ( pSequence != 0 )
     116             :     {
     117       20701 :         pSequence->nRefCount = 1;
     118       20701 :         pSequence->nElements = nSize;
     119             :     }
     120             : 
     121       20701 :     *ppSequence = pSequence;
     122             : }
     123             : 
     124     2524681 : void SAL_CALL rtl_byte_sequence_acquire( sal_Sequence *pSequence )
     125             :     SAL_THROW_EXTERN_C()
     126             : {
     127             :     OSL_ASSERT( pSequence );
     128     2524681 :     osl_atomic_increment( &(pSequence->nRefCount) );
     129     2524681 : }
     130             : 
     131     2544514 : void SAL_CALL rtl_byte_sequence_release( sal_Sequence *pSequence )
     132             :     SAL_THROW_EXTERN_C()
     133             : {
     134     2544514 :     if ( pSequence != 0 )
     135             :     {
     136     2544568 :         if (! osl_atomic_decrement( &(pSequence->nRefCount )) )
     137             :         {
     138       21384 :             rtl_freeMemory( pSequence );
     139             :         }
     140             :     }
     141     2544514 : }
     142             : 
     143      234939 : void SAL_CALL rtl_byte_sequence_construct( sal_Sequence **ppSequence , sal_Int32 nLength )
     144             :     SAL_THROW_EXTERN_C()
     145             : {
     146             :     OSL_ASSERT( ppSequence );
     147      234939 :     if( *ppSequence )
     148             :     {
     149           0 :         rtl_byte_sequence_release( *ppSequence );
     150           0 :         *ppSequence = 0;
     151             :     }
     152             : 
     153      234939 :     if( nLength )
     154             :     {
     155         250 :         *ppSequence = (sal_Sequence *) rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
     156             : 
     157         250 :         if ( *ppSequence != 0 )
     158             :         {
     159         250 :             (*ppSequence)->nRefCount = 1;
     160         250 :             (*ppSequence)->nElements = nLength;
     161             :         }
     162             :     }
     163             :     else
     164             :     {
     165      234689 :         *ppSequence = &aEmpty_rtl_ByteSeq;
     166      234689 :         rtl_byte_sequence_acquire( *ppSequence );
     167             :     }
     168      234939 : }
     169             : 
     170       19926 : void SAL_CALL rtl_byte_sequence_constructNoDefault( sal_Sequence **ppSequence , sal_Int32 nLength )
     171             :     SAL_THROW_EXTERN_C()
     172             : {
     173             :     OSL_ASSERT( ppSequence );
     174       19926 :     if( *ppSequence )
     175             :     {
     176           0 :         rtl_byte_sequence_release( *ppSequence );
     177           0 :         *ppSequence = 0;
     178             :     }
     179             : 
     180       19926 :     *ppSequence = (sal_Sequence *) rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nLength );
     181             : 
     182       19926 :     if ( *ppSequence != 0 )
     183             :     {
     184       19926 :         (*ppSequence)->nRefCount = 1;
     185       19926 :         (*ppSequence)->nElements = nLength;
     186             :     }
     187       19926 : }
     188             : 
     189       19005 : void SAL_CALL rtl_byte_sequence_constructFromArray(
     190             :     sal_Sequence **ppSequence, const sal_Int8 *pData , sal_Int32 nLength )
     191             :     SAL_THROW_EXTERN_C()
     192             : {
     193       19005 :     rtl_byte_sequence_constructNoDefault( ppSequence , nLength );
     194       19005 :     if ( *ppSequence != 0 )
     195       19005 :         memcpy( (*ppSequence)->elements, pData, nLength );
     196       19005 : }
     197             : 
     198     2490592 : void SAL_CALL rtl_byte_sequence_assign( sal_Sequence **ppSequence , sal_Sequence *pSequence )
     199             :     SAL_THROW_EXTERN_C()
     200             : {
     201     2490592 :     if ( *ppSequence != pSequence)
     202             :     {
     203     1931627 :         if( *ppSequence )
     204             :         {
     205      608633 :             rtl_byte_sequence_release( *ppSequence );
     206             :         }
     207     1931629 :         *ppSequence = pSequence;
     208     1931629 :         rtl_byte_sequence_acquire( *ppSequence );
     209             :     }
     210             : //  else
     211             : //      nothing to do
     212             : 
     213     2490596 : }
     214             : 
     215      730060 : sal_Bool SAL_CALL rtl_byte_sequence_equals( sal_Sequence *pSequence1 , sal_Sequence *pSequence2 )
     216             :     SAL_THROW_EXTERN_C()
     217             : {
     218             :     OSL_ASSERT( pSequence1 );
     219             :     OSL_ASSERT( pSequence2 );
     220      730060 :     if (pSequence1 == pSequence2)
     221             :     {
     222      655106 :         return sal_True;
     223             :     }
     224       74954 :     if (pSequence1->nElements != pSequence2->nElements)
     225             :     {
     226       18142 :         return sal_False;
     227             :     }
     228             :     return
     229             :         memcmp(
     230       56812 :             pSequence1->elements, pSequence2->elements, pSequence1->nElements )
     231       56812 :         == 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: */

Generated by: LCOV version 1.10