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

Generated by: LCOV version 1.10