libstdc++
|
00001 // <memory> -*- C++ -*- 00002 00003 // Copyright (C) 2001-2015 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /* 00026 * Copyright (c) 1997-1999 00027 * Silicon Graphics Computer Systems, Inc. 00028 * 00029 * Permission to use, copy, modify, distribute and sell this software 00030 * and its documentation for any purpose is hereby granted without fee, 00031 * provided that the above copyright notice appear in all copies and 00032 * that both that copyright notice and this permission notice appear 00033 * in supporting documentation. Silicon Graphics makes no 00034 * representations about the suitability of this software for any 00035 * purpose. It is provided "as is" without express or implied warranty. 00036 * 00037 */ 00038 00039 /** @file include/memory 00040 * This is a Standard C++ Library header. 00041 */ 00042 00043 #ifndef _GLIBCXX_MEMORY 00044 #define _GLIBCXX_MEMORY 1 00045 00046 #pragma GCC system_header 00047 00048 /** 00049 * @defgroup memory Memory 00050 * @ingroup utilities 00051 * 00052 * Components for memory allocation, deallocation, and management. 00053 */ 00054 00055 /** 00056 * @defgroup pointer_abstractions Pointer Abstractions 00057 * @ingroup memory 00058 * 00059 * Smart pointers, etc. 00060 */ 00061 00062 #include <bits/stl_algobase.h> 00063 #include <bits/allocator.h> 00064 #include <bits/stl_construct.h> 00065 #include <bits/stl_uninitialized.h> 00066 #include <bits/stl_tempbuf.h> 00067 #include <bits/stl_raw_storage_iter.h> 00068 00069 #if __cplusplus >= 201103L 00070 # include <exception> // std::exception 00071 # include <typeinfo> // std::type_info in get_deleter 00072 # include <iosfwd> // std::basic_ostream 00073 # include <ext/atomicity.h> 00074 # include <ext/concurrence.h> 00075 # include <bits/functexcept.h> 00076 # include <bits/stl_function.h> // std::less 00077 # include <bits/uses_allocator.h> 00078 # include <type_traits> 00079 # include <functional> 00080 # include <debug/debug.h> 00081 # include <bits/unique_ptr.h> 00082 # include <bits/shared_ptr.h> 00083 # include <bits/shared_ptr_atomic.h> 00084 # if _GLIBCXX_USE_DEPRECATED 00085 # include <backward/auto_ptr.h> 00086 # endif 00087 #else 00088 # include <backward/auto_ptr.h> 00089 #endif 00090 00091 #if __cplusplus >= 201103L 00092 # include <cstdint> 00093 # ifdef _GLIBCXX_USE_C99_STDINT_TR1 00094 namespace std _GLIBCXX_VISIBILITY(default) 00095 { 00096 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00097 00098 /** 00099 * @brief Fit aligned storage in buffer. 00100 * 00101 * [ptr.align] 00102 * 00103 * This function tries to fit @a __size bytes of storage with alignment 00104 * @a __align into the buffer @a __ptr of size @a __space bytes. If such 00105 * a buffer fits then @a __ptr is changed to point to the first byte of the 00106 * aligned storage and @a __space is reduced by the bytes used for alignment. 00107 * 00108 * @param __align A fundamental or extended alignment value. 00109 * @param __size Size of the aligned storage required. 00110 * @param __ptr Pointer to a buffer of @a __space bytes. 00111 * @param __space Size of the buffer pointed to by @a __ptr. 00112 * @return the updated pointer if the aligned storage fits, otherwise nullptr. 00113 */ 00114 inline void* 00115 align(size_t __align, size_t __size, void*& __ptr, size_t& __space) noexcept 00116 { 00117 const auto __intptr = reinterpret_cast<uintptr_t>(__ptr); 00118 const auto __aligned = (__intptr - 1u + __align) & -__align; 00119 const auto __diff = __aligned - __intptr; 00120 if ((__size + __diff) > __space) 00121 return nullptr; 00122 else 00123 { 00124 __space -= __diff; 00125 return __ptr = reinterpret_cast<void*>(__aligned); 00126 } 00127 } 00128 00129 _GLIBCXX_END_NAMESPACE_VERSION 00130 } // namespace 00131 #endif // _GLIBCXX_USE_C99_STDINT_TR1 00132 #endif // C++11 00133 00134 #endif /* _GLIBCXX_MEMORY */