LCOV - code coverage report
Current view: top level - libreoffice/sal/cpprt - operators_new_delete.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 58 0.0 %
Date: 2012-12-17 Functions: 0 18 0.0 %
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             : 
      21             : #ifdef WNT /* avoid 'std::bad_alloc' unresolved externals */
      22             : #define _CRTIMP
      23             : #define _NTSDK
      24             : #endif /* WNT */
      25             : 
      26             : #include <algorithm>
      27             : #include <new>
      28             : #include <string.h>
      29             : #include <osl/diagnose.h>
      30             : #include <rtl/alloc.h>
      31             : 
      32             : // =======================================================================
      33             : // AllocatorTraits
      34             : // =======================================================================
      35             : 
      36             : namespace
      37             : {
      38             : 
      39             : struct AllocatorTraits
      40             : {
      41             :     typedef char const signature_type[8];
      42             :     const signature_type & m_signature;
      43             : 
      44           0 :     explicit AllocatorTraits (signature_type const & s) SAL_THROW(())
      45           0 :         : m_signature (s)
      46           0 :     {}
      47             : 
      48           0 :     std::size_t size (std::size_t n) const SAL_THROW(())
      49             :     {
      50           0 :         n = std::max(n, std::size_t(1));
      51             : #if OSL_DEBUG_LEVEL > 0
      52             :         n += sizeof(signature_type);
      53             : #endif  /* OSL_DEBUG_LEVEL  */
      54           0 :         return n;
      55             :     }
      56             : 
      57           0 :     void* init (void * p) const SAL_THROW(())
      58             :     {
      59             : #if OSL_DEBUG_LEVEL > 0
      60             :         memcpy (p, m_signature, sizeof(signature_type));
      61             :         p = static_cast<char*>(p) + sizeof(signature_type);
      62             : #endif  /* OSL_DEBUG_LEVEL */
      63           0 :         return p;
      64             :     }
      65             : 
      66           0 :     void* fini (void * p) const SAL_THROW(())
      67             :     {
      68             : #if OSL_DEBUG_LEVEL > 0
      69             :         p = static_cast<char*>(p) - sizeof(signature_type);
      70             :         if (memcmp (p, m_signature, sizeof(signature_type)) != 0)
      71             :         {
      72             :             OSL_FAIL("operator delete mismatch");
      73             :         }
      74             : #endif  /* OSL_DEBUG_LEVEL */
      75           0 :         return p;
      76             :     }
      77             : };
      78             : 
      79             : // =======================================================================
      80             : 
      81             : struct VectorTraits : public AllocatorTraits
      82             : {
      83             :     static const signature_type g_signature;
      84             : 
      85           0 :     VectorTraits() SAL_THROW(())
      86           0 :         : AllocatorTraits (g_signature)
      87           0 :     {}
      88             : };
      89             : 
      90             : struct ScalarTraits : public AllocatorTraits
      91             : {
      92             :     static const signature_type g_signature;
      93             : 
      94           0 :     ScalarTraits() SAL_THROW(())
      95           0 :         : AllocatorTraits (g_signature)
      96           0 :     {}
      97             : };
      98             : 
      99             : const AllocatorTraits::signature_type VectorTraits::g_signature = "new[]()";
     100             : const AllocatorTraits::signature_type ScalarTraits::g_signature = "new()  ";
     101             : 
     102             : } // anonymous namespace
     103             : 
     104             : // =======================================================================
     105             : // Allocator
     106             : // =======================================================================
     107             : 
     108           0 : static void default_handler (void)
     109             : {
     110             :     // Multithreading race in 'std::set_new_handler()' call sequence below.
     111           0 :     throw std::bad_alloc();
     112             : }
     113             : 
     114             : // =======================================================================
     115             : 
     116           0 : static void* allocate (
     117             :     std::size_t n, AllocatorTraits const & rTraits)
     118             :     SAL_THROW((std::bad_alloc))
     119             : {
     120           0 :     n = rTraits.size (n);
     121           0 :     for (;;)
     122             :     {
     123           0 :         void * p = rtl_allocateMemory (sal_Size(n));
     124           0 :         if (p != 0)
     125           0 :             return rTraits.init (p);
     126             : 
     127           0 :         std::new_handler d = default_handler, f = std::set_new_handler (d);
     128           0 :         if (f != d)
     129           0 :             std::set_new_handler (f);
     130             : 
     131           0 :         if (f == 0)
     132           0 :             throw std::bad_alloc();
     133           0 :         (*f)();
     134             :     }
     135             : }
     136             : 
     137             : // =======================================================================
     138             : 
     139           0 : static void* allocate_nothrow (
     140             :     std::size_t n, AllocatorTraits const & rTraits)
     141             :     SAL_THROW(())
     142             : {
     143             :     try
     144             :     {
     145           0 :         return allocate (n, rTraits);
     146             :     }
     147           0 :     catch (std::bad_alloc const &)
     148             :     {
     149           0 :         return (0);
     150             :     }
     151             : }
     152             : 
     153             : // =======================================================================
     154             : 
     155           0 : static void deallocate (void * p, AllocatorTraits const & rTraits)
     156             :     SAL_THROW(())
     157             : {
     158           0 :     if (p)
     159             :     {
     160           0 :         rtl_freeMemory (rTraits.fini(p));
     161             :     }
     162           0 : }
     163             : 
     164             : // =======================================================================
     165             : // T * p = new T; delete p;
     166             : // =======================================================================
     167             : 
     168           0 : void* SAL_CALL operator new (std::size_t n) throw (std::bad_alloc)
     169             : {
     170           0 :     return allocate (n, ScalarTraits());
     171             : }
     172             : 
     173             : // =======================================================================
     174             : 
     175           0 : void SAL_CALL operator delete (void * p) throw ()
     176             : {
     177           0 :     deallocate (p, ScalarTraits());
     178           0 : }
     179             : 
     180             : // =======================================================================
     181             : // T * p = new(nothrow) T; delete(nothrow) p;
     182             : // =======================================================================
     183             : 
     184           0 : void* SAL_CALL operator new (std::size_t n, std::nothrow_t const &) throw ()
     185             : {
     186           0 :     return allocate_nothrow (n, ScalarTraits());
     187             : }
     188             : 
     189             : // =======================================================================
     190             : 
     191           0 : void SAL_CALL operator delete (void * p, std::nothrow_t const &) throw ()
     192             : {
     193           0 :     deallocate (p, ScalarTraits());
     194           0 : }
     195             : 
     196             : // =======================================================================
     197             : // T * p = new T[n]; delete[] p;
     198             : // =======================================================================
     199             : 
     200           0 : void* SAL_CALL operator new[] (std::size_t n) throw (std::bad_alloc)
     201             : {
     202           0 :     return allocate (n, VectorTraits());
     203             : }
     204             : 
     205             : // =======================================================================
     206             : 
     207           0 : void SAL_CALL operator delete[] (void * p) throw ()
     208             : {
     209           0 :     deallocate (p, VectorTraits());
     210           0 : }
     211             : 
     212             : // =======================================================================
     213             : // T * p = new(nothrow) T[n]; delete(nothrow)[] p;
     214             : // =======================================================================
     215             : 
     216           0 : void* SAL_CALL operator new[] (std::size_t n, std::nothrow_t const &) throw ()
     217             : {
     218           0 :     return allocate_nothrow (n, VectorTraits());
     219             : }
     220             : 
     221             : // =======================================================================
     222             : 
     223           0 : void SAL_CALL operator delete[] (void * p, std::nothrow_t const &) throw ()
     224             : {
     225           0 :     deallocate (p, VectorTraits());
     226           0 : }
     227             : 
     228             : // =======================================================================
     229             : 
     230             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10