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 | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 12 | |
13 | _LIBCPP_BEGIN_NAMESPACE_STD | ||||
14 | |||||
15 | namespace | ||||
16 | { | ||||
17 | |||||
18 | template <class T> | ||||
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 19 | inline T |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 20 | increment(T& t) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 21 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 22 | return __sync_add_and_fetch(&t, 1); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 23 | } |
24 | |||||
25 | template <class T> | ||||
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 26 | inline T |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 27 | decrement(T& t) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 28 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 29 | return __sync_add_and_fetch(&t, -1); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 30 | } |
31 | |||||
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 32 | } // namespace |
33 | |||||
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 34 | const allocator_arg_t allocator_arg = allocator_arg_t(); |
35 | |||||
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 36 | bad_weak_ptr::~bad_weak_ptr() _NOEXCEPT {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 37 | |
38 | const char* | ||||
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 39 | bad_weak_ptr::what() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 40 | { |
41 | return "bad_weak_ptr"; | ||||
42 | } | ||||
43 | |||||
44 | __shared_count::~__shared_count() | ||||
45 | { | ||||
46 | } | ||||
47 | |||||
48 | void | ||||
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 49 | __shared_count::__add_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 50 | { |
51 | increment(__shared_owners_); | ||||
52 | } | ||||
53 | |||||
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 54 | bool |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 55 | __shared_count::__release_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 56 | { |
57 | if (decrement(__shared_owners_) == -1) | ||||
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 58 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 59 | __on_zero_shared(); |
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 60 | return true; |
61 | } | ||||
62 | return false; | ||||
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 63 | } |
64 | |||||
65 | __shared_weak_count::~__shared_weak_count() | ||||
66 | { | ||||
67 | } | ||||
68 | |||||
69 | void | ||||
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 70 | __shared_weak_count::__add_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 71 | { |
72 | __shared_count::__add_shared(); | ||||
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 73 | } |
74 | |||||
75 | void | ||||
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 76 | __shared_weak_count::__add_weak() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 77 | { |
78 | increment(__shared_weak_owners_); | ||||
79 | } | ||||
80 | |||||
81 | void | ||||
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 82 | __shared_weak_count::__release_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 83 | { |
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 84 | if (__shared_count::__release_shared()) |
85 | __release_weak(); | ||||
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 86 | } |
87 | |||||
88 | void | ||||
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 89 | __shared_weak_count::__release_weak() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 90 | { |
91 | if (decrement(__shared_weak_owners_) == -1) | ||||
92 | __on_zero_shared_weak(); | ||||
93 | } | ||||
94 | |||||
95 | __shared_weak_count* | ||||
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 96 | __shared_weak_count::lock() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 97 | { |
98 | long object_owners = __shared_owners_; | ||||
99 | while (object_owners != -1) | ||||
100 | { | ||||
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 101 | if (__sync_bool_compare_and_swap(&__shared_owners_, |
102 | object_owners, | ||||
103 | object_owners+1)) | ||||
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 104 | return this; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 105 | object_owners = __shared_owners_; |
106 | } | ||||
107 | return 0; | ||||
108 | } | ||||
109 | |||||
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 110 | #ifndef _LIBCPP_NO_RTTI |
111 | |||||
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 112 | const void* |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 113 | __shared_weak_count::__get_deleter(const type_info&) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 114 | { |
115 | return 0; | ||||
116 | } | ||||
117 | |||||
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 118 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 119 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 120 | void |
121 | declare_reachable(void*) | ||||
122 | { | ||||
123 | } | ||||
124 | |||||
125 | void | ||||
126 | declare_no_pointers(char*, size_t) | ||||
127 | { | ||||
128 | } | ||||
129 | |||||
130 | void | ||||
131 | undeclare_no_pointers(char*, size_t) | ||||
132 | { | ||||
133 | } | ||||
134 | |||||
135 | pointer_safety | ||||
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 136 | get_pointer_safety() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 137 | { |
138 | return pointer_safety::relaxed; | ||||
139 | } | ||||
140 | |||||
141 | void* | ||||
142 | __undeclare_reachable(void* p) | ||||
143 | { | ||||
144 | return p; | ||||
145 | } | ||||
146 | |||||
147 | void* | ||||
148 | align(size_t alignment, size_t size, void*& ptr, size_t& space) | ||||
149 | { | ||||
150 | void* r = nullptr; | ||||
151 | if (size <= space) | ||||
152 | { | ||||
153 | char* p1 = static_cast<char*>(ptr); | ||||
154 | char* p2 = (char*)((size_t)(p1 + (alignment - 1)) & -alignment); | ||||
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 155 | size_t d = static_cast<size_t>(p2 - p1); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 156 | if (d <= space - size) |
157 | { | ||||
158 | r = p2; | ||||
159 | ptr = r; | ||||
160 | space -= d; | ||||
161 | } | ||||
162 | } | ||||
163 | return r; | ||||
164 | } | ||||
165 | |||||
166 | _LIBCPP_END_NAMESPACE_STD |