File Coverage

/usr/include/c++/5/bits/allocated_ptr.h
Criterion Covered Total %
statement 14 14 100.0
branch 33 70 47.1
condition n/a
subroutine n/a
pod n/a
total 47 84 55.9


line stmt bran cond sub pod time code
1             // Guarded Allocation -*- C++ -*-
2              
3             // Copyright (C) 2014-2015 Free Software Foundation, Inc.
4             //
5             // This file is part of the GNU ISO C++ Library. This library is free
6             // software; you can redistribute it and/or modify it under the
7             // terms of the GNU General Public License as published by the
8             // Free Software Foundation; either version 3, or (at your option)
9             // any later version.
10              
11             // This library is distributed in the hope that it will be useful,
12             // but WITHOUT ANY WARRANTY; without even the implied warranty of
13             // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14             // GNU General Public License for more details.
15              
16             // Under Section 7 of GPL version 3, you are granted additional
17             // permissions described in the GCC Runtime Library Exception, version
18             // 3.1, as published by the Free Software Foundation.
19              
20             // You should have received a copy of the GNU General Public License and
21             // a copy of the GCC Runtime Library Exception along with this program;
22             // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23             // .
24              
25             /** @file bits/allocated_ptr.h
26             * This is an internal header file, included by other library headers.
27             * Do not attempt to use it directly. @headername{memory}
28             */
29              
30             #ifndef _ALLOCATED_PTR_H
31             #define _ALLOCATED_PTR_H 1
32              
33             #if __cplusplus < 201103L
34             # include
35             #else
36             # include
37             # include
38             # include
39              
40             namespace std _GLIBCXX_VISIBILITY(default)
41             {
42             _GLIBCXX_BEGIN_NAMESPACE_VERSION
43              
44             /// Non-standard RAII type for managing pointers obtained from allocators.
45             template
46             struct __allocated_ptr
47             {
48             using pointer = typename allocator_traits<_Alloc>::pointer;
49             using value_type = typename allocator_traits<_Alloc>::value_type;
50              
51             /// Take ownership of __ptr
52 146           __allocated_ptr(_Alloc& __a, pointer __ptr) noexcept
53 146           : _M_alloc(&__a), _M_ptr(__ptr)
54 146           { }
55              
56             /// Convert __ptr to allocator's pointer type and take ownership of it
57             template
58             typename _Req = _Require>>
59             __allocated_ptr(_Alloc& __a, _Ptr __ptr)
60             : _M_alloc(&__a), _M_ptr(pointer_traits::pointer_to(*__ptr))
61             { }
62              
63             /// Transfer ownership of the owned pointer
64             __allocated_ptr(__allocated_ptr&& __gd) noexcept
65             : _M_alloc(__gd._M_alloc), _M_ptr(__gd._M_ptr)
66             { __gd._M_ptr = nullptr; }
67              
68             /// Deallocate the owned pointer
69 146           ~__allocated_ptr()
70             {
71 146 0         if (_M_ptr != nullptr)
    0          
    0          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    0          
    0          
    50          
    50          
    50          
    50          
    50          
    100          
    0          
    100          
    100          
    100          
    0          
    50          
    50          
    100          
72 53           std::allocator_traits<_Alloc>::deallocate(*_M_alloc, _M_ptr, 1);
73 146           }
74              
75             /// Release ownership of the owned pointer
76             __allocated_ptr&
77 93           operator=(std::nullptr_t) noexcept
78             {
79 93           _M_ptr = nullptr;
80 93           return *this;
81             }
82              
83             /// Get the address that the owned pointer refers to.
84 186           value_type* get() { return _S_raw_ptr(_M_ptr); }
85              
86             private:
87 186           value_type* _S_raw_ptr(value_type* __ptr) { return __ptr; }
88              
89             template
90             auto _S_raw_ptr(_Ptr __ptr) -> decltype(_S_raw_ptr(__ptr.operator->()))
91             { return _S_raw_ptr(__ptr.operator->()); }
92              
93             _Alloc* _M_alloc;
94             pointer _M_ptr;
95             };
96              
97             /// Allocate space for a single object using __a
98             template
99             __allocated_ptr<_Alloc>
100 93           __allocate_guarded(_Alloc& __a)
101             {
102 93           return { __a, std::allocator_traits<_Alloc>::allocate(__a, 1) };
103             }
104              
105             _GLIBCXX_END_NAMESPACE_VERSION
106             } // namespace std
107              
108             #endif
109             #endif