blob: e298c723c1ede0a49fd671cfade6d646f9c911e0 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===------------------------ memory.cpp ----------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
Howard Hinnant46e94932012-07-07 20:56:04 +000010#define _LIBCPP_BUILDING_MEMORY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000011#include "memory"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000012
13_LIBCPP_BEGIN_NAMESPACE_STD
14
15namespace
16{
17
18template <class T>
Howard Hinnantadff4892010-05-24 17:49:41 +000019inline T
Howard Hinnant1694d232011-05-28 14:41:13 +000020increment(T& t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021{
Howard Hinnantadff4892010-05-24 17:49:41 +000022 return __sync_add_and_fetch(&t, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000023}
24
25template <class T>
Howard Hinnantadff4892010-05-24 17:49:41 +000026inline T
Howard Hinnant1694d232011-05-28 14:41:13 +000027decrement(T& t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028{
Howard Hinnantadff4892010-05-24 17:49:41 +000029 return __sync_add_and_fetch(&t, -1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000030}
31
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032} // namespace
33
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000034const allocator_arg_t allocator_arg = allocator_arg_t();
35
Howard Hinnant1694d232011-05-28 14:41:13 +000036bad_weak_ptr::~bad_weak_ptr() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000037
38const char*
Howard Hinnant1694d232011-05-28 14:41:13 +000039bad_weak_ptr::what() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000040{
41 return "bad_weak_ptr";
42}
43
44__shared_count::~__shared_count()
45{
46}
47
48void
Howard Hinnant1694d232011-05-28 14:41:13 +000049__shared_count::__add_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000050{
51 increment(__shared_owners_);
52}
53
Howard Hinnant28dbbe02010-11-16 21:33:17 +000054bool
Howard Hinnant1694d232011-05-28 14:41:13 +000055__shared_count::__release_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056{
57 if (decrement(__shared_owners_) == -1)
Howard Hinnant28dbbe02010-11-16 21:33:17 +000058 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000059 __on_zero_shared();
Howard Hinnant28dbbe02010-11-16 21:33:17 +000060 return true;
61 }
62 return false;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063}
64
65__shared_weak_count::~__shared_weak_count()
66{
67}
68
69void
Howard Hinnant1694d232011-05-28 14:41:13 +000070__shared_weak_count::__add_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000071{
72 __shared_count::__add_shared();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073}
74
75void
Howard Hinnant1694d232011-05-28 14:41:13 +000076__shared_weak_count::__add_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000077{
78 increment(__shared_weak_owners_);
79}
80
81void
Howard Hinnant1694d232011-05-28 14:41:13 +000082__shared_weak_count::__release_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083{
Howard Hinnant28dbbe02010-11-16 21:33:17 +000084 if (__shared_count::__release_shared())
85 __release_weak();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000086}
87
88void
Howard Hinnant1694d232011-05-28 14:41:13 +000089__shared_weak_count::__release_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000090{
91 if (decrement(__shared_weak_owners_) == -1)
92 __on_zero_shared_weak();
93}
94
95__shared_weak_count*
Howard Hinnant1694d232011-05-28 14:41:13 +000096__shared_weak_count::lock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000097{
98 long object_owners = __shared_owners_;
99 while (object_owners != -1)
100 {
Howard Hinnantadff4892010-05-24 17:49:41 +0000101 if (__sync_bool_compare_and_swap(&__shared_owners_,
102 object_owners,
103 object_owners+1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000104 return this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000105 object_owners = __shared_owners_;
106 }
107 return 0;
108}
109
Howard Hinnantd4444702010-08-11 17:04:31 +0000110#ifndef _LIBCPP_NO_RTTI
111
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000112const void*
Howard Hinnant1694d232011-05-28 14:41:13 +0000113__shared_weak_count::__get_deleter(const type_info&) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000114{
115 return 0;
116}
117
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000118#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000119
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000120void
121declare_reachable(void*)
122{
123}
124
125void
126declare_no_pointers(char*, size_t)
127{
128}
129
130void
131undeclare_no_pointers(char*, size_t)
132{
133}
134
135pointer_safety
Howard Hinnant1694d232011-05-28 14:41:13 +0000136get_pointer_safety() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000137{
138 return pointer_safety::relaxed;
139}
140
141void*
142__undeclare_reachable(void* p)
143{
144 return p;
145}
146
147void*
148align(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 Hinnantec3773c2011-12-01 20:21:04 +0000155 size_t d = static_cast<size_t>(p2 - p1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000156 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