Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame^] | 1 | //===------------------------ memory.cpp ----------------------------------===// |
| 2 | // |
| 3 | // ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊThe LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "memory" |
| 11 | #include <libkern/OSAtomic.h> |
| 12 | |
| 13 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 14 | |
| 15 | namespace |
| 16 | { |
| 17 | |
| 18 | template <class T> |
| 19 | inline |
| 20 | typename enable_if |
| 21 | < |
| 22 | sizeof(T) * __CHAR_BIT__ == 32, |
| 23 | T |
| 24 | >::type |
| 25 | increment(T& t) |
| 26 | { |
| 27 | return OSAtomicIncrement32Barrier((volatile int32_t*)&t); |
| 28 | } |
| 29 | |
| 30 | template <class T> |
| 31 | inline |
| 32 | typename enable_if |
| 33 | < |
| 34 | sizeof(T) * __CHAR_BIT__ == 32, |
| 35 | T |
| 36 | >::type |
| 37 | decrement(T& t) |
| 38 | { |
| 39 | return OSAtomicDecrement32Barrier((volatile int32_t*)&t); |
| 40 | } |
| 41 | |
| 42 | #ifndef __ppc__ |
| 43 | |
| 44 | template <class T> |
| 45 | inline |
| 46 | typename enable_if |
| 47 | < |
| 48 | sizeof(T) * __CHAR_BIT__ == 64, |
| 49 | T |
| 50 | >::type |
| 51 | increment(T& t) |
| 52 | { |
| 53 | return OSAtomicIncrement64Barrier((volatile int64_t*)&t); |
| 54 | } |
| 55 | |
| 56 | template <class T> |
| 57 | inline |
| 58 | typename enable_if |
| 59 | < |
| 60 | sizeof(T) * __CHAR_BIT__ == 64, |
| 61 | T |
| 62 | >::type |
| 63 | decrement(T& t) |
| 64 | { |
| 65 | return OSAtomicDecrement64Barrier((volatile int64_t*)&t); |
| 66 | } |
| 67 | |
| 68 | #endif |
| 69 | |
| 70 | } // namespace |
| 71 | |
| 72 | |
| 73 | const allocator_arg_t allocator_arg = allocator_arg_t(); |
| 74 | |
| 75 | bad_weak_ptr::~bad_weak_ptr() throw() {} |
| 76 | |
| 77 | const char* |
| 78 | bad_weak_ptr::what() const throw() |
| 79 | { |
| 80 | return "bad_weak_ptr"; |
| 81 | } |
| 82 | |
| 83 | __shared_count::~__shared_count() |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | __shared_count::__add_shared() |
| 89 | { |
| 90 | increment(__shared_owners_); |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | __shared_count::__release_shared() |
| 95 | { |
| 96 | if (decrement(__shared_owners_) == -1) |
| 97 | __on_zero_shared(); |
| 98 | } |
| 99 | |
| 100 | __shared_weak_count::~__shared_weak_count() |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | __shared_weak_count::__add_shared() |
| 106 | { |
| 107 | __shared_count::__add_shared(); |
| 108 | __add_weak(); |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | __shared_weak_count::__add_weak() |
| 113 | { |
| 114 | increment(__shared_weak_owners_); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | __shared_weak_count::__release_shared() |
| 119 | { |
| 120 | __shared_count::__release_shared(); |
| 121 | __release_weak(); |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | __shared_weak_count::__release_weak() |
| 126 | { |
| 127 | if (decrement(__shared_weak_owners_) == -1) |
| 128 | __on_zero_shared_weak(); |
| 129 | } |
| 130 | |
| 131 | __shared_weak_count* |
| 132 | __shared_weak_count::lock() |
| 133 | { |
| 134 | long object_owners = __shared_owners_; |
| 135 | while (object_owners != -1) |
| 136 | { |
| 137 | if (OSAtomicCompareAndSwapLongBarrier(object_owners, |
| 138 | object_owners+1, |
| 139 | &__shared_owners_)) |
| 140 | { |
| 141 | __add_weak(); |
| 142 | return this; |
| 143 | } |
| 144 | object_owners = __shared_owners_; |
| 145 | } |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | const void* |
| 150 | __shared_weak_count::__get_deleter(const type_info&) const |
| 151 | { |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | declare_reachable(void*) |
| 157 | { |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | declare_no_pointers(char*, size_t) |
| 162 | { |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | undeclare_no_pointers(char*, size_t) |
| 167 | { |
| 168 | } |
| 169 | |
| 170 | pointer_safety |
| 171 | get_pointer_safety() |
| 172 | { |
| 173 | return pointer_safety::relaxed; |
| 174 | } |
| 175 | |
| 176 | void* |
| 177 | __undeclare_reachable(void* p) |
| 178 | { |
| 179 | return p; |
| 180 | } |
| 181 | |
| 182 | void* |
| 183 | align(size_t alignment, size_t size, void*& ptr, size_t& space) |
| 184 | { |
| 185 | void* r = nullptr; |
| 186 | if (size <= space) |
| 187 | { |
| 188 | char* p1 = static_cast<char*>(ptr); |
| 189 | char* p2 = (char*)((size_t)(p1 + (alignment - 1)) & -alignment); |
| 190 | ptrdiff_t d = p2 - p1; |
| 191 | if (d <= space - size) |
| 192 | { |
| 193 | r = p2; |
| 194 | ptr = r; |
| 195 | space -= d; |
| 196 | } |
| 197 | } |
| 198 | return r; |
| 199 | } |
| 200 | |
| 201 | _LIBCPP_END_NAMESPACE_STD |