Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===------------------------ memory.cpp ----------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Howard Hinnant | 46e9493 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 10 | #define _LIBCPP_BUILDING_MEMORY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 11 | #include "memory" |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 12 | #include "mutex" |
Howard Hinnant | 65f059b | 2012-07-30 17:13:21 +0000 | [diff] [blame] | 13 | #include "thread" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 14 | |
| 15 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 16 | |
| 17 | namespace |
| 18 | { |
| 19 | |
| 20 | template <class T> |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 21 | inline T |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 22 | increment(T& t) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 23 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 24 | return __sync_add_and_fetch(&t, 1); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | template <class T> |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 28 | inline T |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 29 | decrement(T& t) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 30 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 31 | return __sync_add_and_fetch(&t, -1); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 34 | } // namespace |
| 35 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 36 | const allocator_arg_t allocator_arg = allocator_arg_t(); |
| 37 | |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 38 | bad_weak_ptr::~bad_weak_ptr() _NOEXCEPT {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 39 | |
| 40 | const char* |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 41 | bad_weak_ptr::what() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 42 | { |
| 43 | return "bad_weak_ptr"; |
| 44 | } |
| 45 | |
| 46 | __shared_count::~__shared_count() |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 51 | __shared_count::__add_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 52 | { |
| 53 | increment(__shared_owners_); |
| 54 | } |
| 55 | |
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 56 | bool |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 57 | __shared_count::__release_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 58 | { |
| 59 | if (decrement(__shared_owners_) == -1) |
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 60 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 61 | __on_zero_shared(); |
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 62 | return true; |
| 63 | } |
| 64 | return false; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | __shared_weak_count::~__shared_weak_count() |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 72 | __shared_weak_count::__add_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 73 | { |
| 74 | __shared_count::__add_shared(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 78 | __shared_weak_count::__add_weak() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 79 | { |
| 80 | increment(__shared_weak_owners_); |
| 81 | } |
| 82 | |
| 83 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 84 | __shared_weak_count::__release_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 85 | { |
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 86 | if (__shared_count::__release_shared()) |
| 87 | __release_weak(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 91 | __shared_weak_count::__release_weak() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 92 | { |
| 93 | if (decrement(__shared_weak_owners_) == -1) |
| 94 | __on_zero_shared_weak(); |
| 95 | } |
| 96 | |
| 97 | __shared_weak_count* |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 98 | __shared_weak_count::lock() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 99 | { |
| 100 | long object_owners = __shared_owners_; |
| 101 | while (object_owners != -1) |
| 102 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 103 | if (__sync_bool_compare_and_swap(&__shared_owners_, |
| 104 | object_owners, |
| 105 | object_owners+1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 106 | return this; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 107 | object_owners = __shared_owners_; |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 112 | #ifndef _LIBCPP_NO_RTTI |
| 113 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 114 | const void* |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 115 | __shared_weak_count::__get_deleter(const type_info&) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 116 | { |
| 117 | return 0; |
| 118 | } |
| 119 | |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 120 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 121 | |
Howard Hinnant | 96c60b4 | 2012-08-19 15:13:16 +0000 | [diff] [blame] | 122 | #if __has_feature(cxx_atomic) |
| 123 | |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 124 | static const std::size_t __sp_mut_count = 16; |
Howard Hinnant | e33c2d1 | 2013-03-16 00:17:53 +0000 | [diff] [blame] | 125 | static pthread_mutex_t mut_back_imp[__sp_mut_count] = |
| 126 | { |
| 127 | PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, |
| 128 | PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, |
| 129 | PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, |
| 130 | PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER |
| 131 | }; |
| 132 | |
| 133 | static mutex* mut_back = reinterpret_cast<std::mutex*>(mut_back_imp); |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 134 | |
| 135 | _LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 136 | : __lx(p) |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 137 | { |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | __sp_mut::lock() _NOEXCEPT |
| 142 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 143 | mutex& m = *static_cast<mutex*>(__lx); |
Howard Hinnant | 65f059b | 2012-07-30 17:13:21 +0000 | [diff] [blame] | 144 | unsigned count = 0; |
| 145 | while (!m.try_lock()) |
| 146 | { |
| 147 | if (++count > 16) |
| 148 | { |
| 149 | m.lock(); |
| 150 | break; |
| 151 | } |
| 152 | this_thread::yield(); |
| 153 | } |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void |
| 157 | __sp_mut::unlock() _NOEXCEPT |
| 158 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 159 | static_cast<mutex*>(__lx)->unlock(); |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | __sp_mut& |
| 163 | __get_sp_mut(const void* p) |
| 164 | { |
| 165 | static __sp_mut muts[__sp_mut_count] |
| 166 | { |
| 167 | &mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3], |
| 168 | &mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7], |
| 169 | &mut_back[ 8], &mut_back[ 9], &mut_back[10], &mut_back[11], |
| 170 | &mut_back[12], &mut_back[13], &mut_back[14], &mut_back[15] |
| 171 | }; |
| 172 | return muts[hash<const void*>()(p) & (__sp_mut_count-1)]; |
| 173 | } |
| 174 | |
Howard Hinnant | 96c60b4 | 2012-08-19 15:13:16 +0000 | [diff] [blame] | 175 | #endif // __has_feature(cxx_atomic) |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 176 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 177 | void |
| 178 | declare_reachable(void*) |
| 179 | { |
| 180 | } |
| 181 | |
| 182 | void |
| 183 | declare_no_pointers(char*, size_t) |
| 184 | { |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | undeclare_no_pointers(char*, size_t) |
| 189 | { |
| 190 | } |
| 191 | |
| 192 | pointer_safety |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 193 | get_pointer_safety() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 194 | { |
| 195 | return pointer_safety::relaxed; |
| 196 | } |
| 197 | |
| 198 | void* |
| 199 | __undeclare_reachable(void* p) |
| 200 | { |
| 201 | return p; |
| 202 | } |
| 203 | |
| 204 | void* |
| 205 | align(size_t alignment, size_t size, void*& ptr, size_t& space) |
| 206 | { |
| 207 | void* r = nullptr; |
| 208 | if (size <= space) |
| 209 | { |
| 210 | char* p1 = static_cast<char*>(ptr); |
| 211 | char* p2 = (char*)((size_t)(p1 + (alignment - 1)) & -alignment); |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 212 | size_t d = static_cast<size_t>(p2 - p1); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 213 | if (d <= space - size) |
| 214 | { |
| 215 | r = p2; |
| 216 | ptr = r; |
| 217 | space -= d; |
| 218 | } |
| 219 | } |
| 220 | return r; |
| 221 | } |
| 222 | |
| 223 | _LIBCPP_END_NAMESPACE_STD |