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 | // |
| 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" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 11 | |
| 12 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 13 | |
| 14 | namespace |
| 15 | { |
| 16 | |
| 17 | template <class T> |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 18 | inline T |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 19 | increment(T& t) |
| 20 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 21 | return __sync_add_and_fetch(&t, 1); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | template <class T> |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 25 | inline T |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 26 | decrement(T& t) |
| 27 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 28 | return __sync_add_and_fetch(&t, -1); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 31 | } // namespace |
| 32 | |
| 33 | |
| 34 | const allocator_arg_t allocator_arg = allocator_arg_t(); |
| 35 | |
| 36 | bad_weak_ptr::~bad_weak_ptr() throw() {} |
| 37 | |
| 38 | const char* |
| 39 | bad_weak_ptr::what() const throw() |
| 40 | { |
| 41 | return "bad_weak_ptr"; |
| 42 | } |
| 43 | |
| 44 | __shared_count::~__shared_count() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | __shared_count::__add_shared() |
| 50 | { |
| 51 | increment(__shared_owners_); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | __shared_count::__release_shared() |
| 56 | { |
| 57 | if (decrement(__shared_owners_) == -1) |
| 58 | __on_zero_shared(); |
| 59 | } |
| 60 | |
| 61 | __shared_weak_count::~__shared_weak_count() |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | __shared_weak_count::__add_shared() |
| 67 | { |
| 68 | __shared_count::__add_shared(); |
| 69 | __add_weak(); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | __shared_weak_count::__add_weak() |
| 74 | { |
| 75 | increment(__shared_weak_owners_); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | __shared_weak_count::__release_shared() |
| 80 | { |
| 81 | __shared_count::__release_shared(); |
| 82 | __release_weak(); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | __shared_weak_count::__release_weak() |
| 87 | { |
| 88 | if (decrement(__shared_weak_owners_) == -1) |
| 89 | __on_zero_shared_weak(); |
| 90 | } |
| 91 | |
| 92 | __shared_weak_count* |
| 93 | __shared_weak_count::lock() |
| 94 | { |
| 95 | long object_owners = __shared_owners_; |
| 96 | while (object_owners != -1) |
| 97 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 98 | if (__sync_bool_compare_and_swap(&__shared_owners_, |
| 99 | object_owners, |
| 100 | object_owners+1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 101 | { |
| 102 | __add_weak(); |
| 103 | return this; |
| 104 | } |
| 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* |
| 113 | __shared_weak_count::__get_deleter(const type_info&) const |
| 114 | { |
| 115 | return 0; |
| 116 | } |
| 117 | |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame^] | 118 | #endif |
| 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 |
| 136 | get_pointer_safety() |
| 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); |
| 155 | ptrdiff_t d = p2 - p1; |
| 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 |