blob: de89b79a4b9d0c10b42c36eae6a69f2150abe0be [file] [log] [blame]
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +00001//===------------------------ __refstring ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___REFSTRING
11#define _LIBCPP___REFSTRING
12
13#include <__config>
Eric Fiselier1e655162016-10-25 19:33:14 +000014#include <stdexcept>
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000015#include <cstddef>
16#include <cstring>
Weiming Zhaocb9c8682017-07-10 21:37:35 +000017#include <__atomic_support>
Dan Albert273eb322015-02-05 02:34:59 +000018#ifdef __APPLE__
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000019#include <dlfcn.h>
20#include <mach-o/dyld.h>
21#endif
22
23_LIBCPP_BEGIN_NAMESPACE_STD
24
Eric Fiselier1e655162016-10-25 19:33:14 +000025namespace __refstring_imp { namespace {
26typedef int count_t;
27
28struct _Rep_base {
29 std::size_t len;
30 std::size_t cap;
31 count_t count;
32};
33
34inline _Rep_base* rep_from_data(const char *data_) noexcept {
35 char *data = const_cast<char *>(data_);
36 return reinterpret_cast<_Rep_base *>(data - sizeof(_Rep_base));
37}
38
39inline char * data_from_rep(_Rep_base *rep) noexcept {
40 char *data = reinterpret_cast<char *>(rep);
41 return data + sizeof(*rep);
42}
43
44#if defined(__APPLE__)
45inline
46const char* compute_gcc_empty_string_storage() _NOEXCEPT
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000047{
Eric Fiselier1e655162016-10-25 19:33:14 +000048 void* handle = dlopen("/usr/lib/libstdc++.6.dylib", RTLD_NOLOAD);
49 if (handle == nullptr)
50 return nullptr;
51 void* sym = dlsym(handle, "_ZNSs4_Rep20_S_empty_rep_storageE");
52 if (sym == nullptr)
53 return nullptr;
54 return data_from_rep(reinterpret_cast<_Rep_base *>(sym));
55}
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000056
Eric Fiselier1e655162016-10-25 19:33:14 +000057inline
58const char*
59get_gcc_empty_string_storage() _NOEXCEPT
60{
61 static const char* p = compute_gcc_empty_string_storage();
62 return p;
63}
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000064#endif
65
Eric Fiselier1e655162016-10-25 19:33:14 +000066}} // namespace __refstring_imp
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000067
Eric Fiselier1e655162016-10-25 19:33:14 +000068using namespace __refstring_imp;
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000069
Eric Fiselier1e655162016-10-25 19:33:14 +000070inline
71__libcpp_refstring::__libcpp_refstring(const char* msg) {
72 std::size_t len = strlen(msg);
73 _Rep_base* rep = static_cast<_Rep_base *>(::operator new(sizeof(*rep) + len + 1));
74 rep->len = len;
75 rep->cap = len;
76 rep->count = 0;
77 char *data = data_from_rep(rep);
78 std::memcpy(data, msg, len + 1);
79 __imp_ = data;
80}
81
82inline
83__libcpp_refstring::__libcpp_refstring(const __libcpp_refstring &s) _NOEXCEPT
84 : __imp_(s.__imp_)
85{
86 if (__uses_refcount())
Weiming Zhaocb9c8682017-07-10 21:37:35 +000087 __libcpp_sync_add_and_fetch(&rep_from_data(__imp_)->count, 1);
Eric Fiselier1e655162016-10-25 19:33:14 +000088}
89
90inline
91__libcpp_refstring& __libcpp_refstring::operator=(__libcpp_refstring const& s) _NOEXCEPT {
92 bool adjust_old_count = __uses_refcount();
93 struct _Rep_base *old_rep = rep_from_data(__imp_);
94 __imp_ = s.__imp_;
95 if (__uses_refcount())
Weiming Zhaocb9c8682017-07-10 21:37:35 +000096 __libcpp_sync_add_and_fetch(&rep_from_data(__imp_)->count, 1);
Eric Fiselier1e655162016-10-25 19:33:14 +000097 if (adjust_old_count)
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000098 {
Weiming Zhaocb9c8682017-07-10 21:37:35 +000099 if (__libcpp_sync_add_and_fetch(&old_rep->count, count_t(-1)) < 0)
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +0000100 {
Eric Fiselier1e655162016-10-25 19:33:14 +0000101 ::operator delete(old_rep);
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +0000102 }
103 }
Eric Fiselier1e655162016-10-25 19:33:14 +0000104 return *this;
105}
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +0000106
Eric Fiselier1e655162016-10-25 19:33:14 +0000107inline
108__libcpp_refstring::~__libcpp_refstring() {
109 if (__uses_refcount()) {
110 _Rep_base* rep = rep_from_data(__imp_);
Weiming Zhaocb9c8682017-07-10 21:37:35 +0000111 if (__libcpp_sync_add_and_fetch(&rep->count, count_t(-1)) < 0) {
Eric Fiselier1e655162016-10-25 19:33:14 +0000112 ::operator delete(rep);
113 }
114 }
115}
116
117inline
118bool __libcpp_refstring::__uses_refcount() const {
119#ifdef __APPLE__
120 return __imp_ != get_gcc_empty_string_storage();
121#else
122 return true;
123#endif
124}
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +0000125
126_LIBCPP_END_NAMESPACE_STD
127
128#endif //_LIBCPP___REFSTRING