memory 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // <memory> -*- C++ -*-
  2. // Copyright (C) 2001-2015 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. * Copyright (c) 1997-1999
  22. * Silicon Graphics Computer Systems, Inc.
  23. *
  24. * Permission to use, copy, modify, distribute and sell this software
  25. * and its documentation for any purpose is hereby granted without fee,
  26. * provided that the above copyright notice appear in all copies and
  27. * that both that copyright notice and this permission notice appear
  28. * in supporting documentation. Silicon Graphics makes no
  29. * representations about the suitability of this software for any
  30. * purpose. It is provided "as is" without express or implied warranty.
  31. *
  32. */
  33. /** @file include/memory
  34. * This is a Standard C++ Library header.
  35. */
  36. #ifndef _GLIBCXX_MEMORY
  37. #define _GLIBCXX_MEMORY 1
  38. #pragma GCC system_header
  39. /**
  40. * @defgroup memory Memory
  41. * @ingroup utilities
  42. *
  43. * Components for memory allocation, deallocation, and management.
  44. */
  45. /**
  46. * @defgroup pointer_abstractions Pointer Abstractions
  47. * @ingroup memory
  48. *
  49. * Smart pointers, etc.
  50. */
  51. #include <bits/stl_algobase.h>
  52. #include <bits/allocator.h>
  53. #include <bits/stl_construct.h>
  54. #include <bits/stl_uninitialized.h>
  55. #include <bits/stl_tempbuf.h>
  56. #include <bits/stl_raw_storage_iter.h>
  57. #if __cplusplus >= 201103L
  58. # include <exception> // std::exception
  59. # include <typeinfo> // std::type_info in get_deleter
  60. # include <iosfwd> // std::basic_ostream
  61. # include <ext/atomicity.h>
  62. # include <ext/concurrence.h>
  63. # include <bits/functexcept.h>
  64. # include <bits/stl_function.h> // std::less
  65. # include <bits/uses_allocator.h>
  66. # include <type_traits>
  67. # include <functional>
  68. # include <debug/debug.h>
  69. # include <bits/unique_ptr.h>
  70. # include <bits/shared_ptr.h>
  71. # include <bits/shared_ptr_atomic.h>
  72. # if _GLIBCXX_USE_DEPRECATED
  73. # include <backward/auto_ptr.h>
  74. # endif
  75. #else
  76. # include <backward/auto_ptr.h>
  77. #endif
  78. #if __cplusplus >= 201103L
  79. # include <cstdint>
  80. # ifdef _GLIBCXX_USE_C99_STDINT_TR1
  81. namespace std _GLIBCXX_VISIBILITY(default)
  82. {
  83. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  84. /**
  85. * @brief Fit aligned storage in buffer.
  86. *
  87. * [ptr.align]
  88. *
  89. * This function tries to fit @a __size bytes of storage with alignment
  90. * @a __align into the buffer @a __ptr of size @a __space bytes. If such
  91. * a buffer fits then @a __ptr is changed to point to the first byte of the
  92. * aligned storage and @a __space is reduced by the bytes used for alignment.
  93. *
  94. * @param __align A fundamental or extended alignment value.
  95. * @param __size Size of the aligned storage required.
  96. * @param __ptr Pointer to a buffer of @a __space bytes.
  97. * @param __space Size of the buffer pointed to by @a __ptr.
  98. * @return the updated pointer if the aligned storage fits, otherwise nullptr.
  99. */
  100. inline void*
  101. align(size_t __align, size_t __size, void*& __ptr, size_t& __space) noexcept
  102. {
  103. const auto __intptr = reinterpret_cast<uintptr_t>(__ptr);
  104. const auto __aligned = (__intptr - 1u + __align) & -__align;
  105. const auto __diff = __aligned - __intptr;
  106. if ((__size + __diff) > __space)
  107. return nullptr;
  108. else
  109. {
  110. __space -= __diff;
  111. return __ptr = reinterpret_cast<void*>(__aligned);
  112. }
  113. }
  114. _GLIBCXX_END_NAMESPACE_VERSION
  115. } // namespace
  116. #endif // _GLIBCXX_USE_C99_STDINT_TR1
  117. #endif // C++11
  118. #endif /* _GLIBCXX_MEMORY */