blob: e464b79ba89c0ac77c8c60984b3ce9cb20dd85d1 [file] [log] [blame]
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +00001//===------------------------ __refstring ---------------------------------===//
2//
Chandler Carruthd2012102019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +00006//
7//===----------------------------------------------------------------------===//
8
Eric Fiselier46123de2017-07-12 01:38:35 +00009#ifndef _LIBCPP_REFSTRING_H
10#define _LIBCPP_REFSTRING_H
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000011
12#include <__config>
Eric Fiselier1e655162016-10-25 19:33:14 +000013#include <stdexcept>
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000014#include <cstddef>
15#include <cstring>
Dan Albert273eb322015-02-05 02:34:59 +000016#ifdef __APPLE__
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000017#include <dlfcn.h>
18#include <mach-o/dyld.h>
19#endif
Weiming Zhaob613db72017-09-19 23:18:03 +000020#include "atomic_support.h"
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000021
22_LIBCPP_BEGIN_NAMESPACE_STD
23
Eric Fiselier1e655162016-10-25 19:33:14 +000024namespace __refstring_imp { namespace {
25typedef int count_t;
26
27struct _Rep_base {
28 std::size_t len;
29 std::size_t cap;
30 count_t count;
31};
32
33inline _Rep_base* rep_from_data(const char *data_) noexcept {
34 char *data = const_cast<char *>(data_);
35 return reinterpret_cast<_Rep_base *>(data - sizeof(_Rep_base));
36}
37
38inline char * data_from_rep(_Rep_base *rep) noexcept {
39 char *data = reinterpret_cast<char *>(rep);
40 return data + sizeof(*rep);
41}
42
43#if defined(__APPLE__)
44inline
45const char* compute_gcc_empty_string_storage() _NOEXCEPT
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000046{
Eric Fiselier1e655162016-10-25 19:33:14 +000047 void* handle = dlopen("/usr/lib/libstdc++.6.dylib", RTLD_NOLOAD);
48 if (handle == nullptr)
49 return nullptr;
50 void* sym = dlsym(handle, "_ZNSs4_Rep20_S_empty_rep_storageE");
51 if (sym == nullptr)
52 return nullptr;
53 return data_from_rep(reinterpret_cast<_Rep_base *>(sym));
54}
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000055
Eric Fiselier1e655162016-10-25 19:33:14 +000056inline
57const char*
58get_gcc_empty_string_storage() _NOEXCEPT
59{
60 static const char* p = compute_gcc_empty_string_storage();
61 return p;
62}
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000063#endif
64
Eric Fiselier1e655162016-10-25 19:33:14 +000065}} // namespace __refstring_imp
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000066
Eric Fiselier1e655162016-10-25 19:33:14 +000067using namespace __refstring_imp;
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000068
Eric Fiselier1e655162016-10-25 19:33:14 +000069inline
70__libcpp_refstring::__libcpp_refstring(const char* msg) {
71 std::size_t len = strlen(msg);
72 _Rep_base* rep = static_cast<_Rep_base *>(::operator new(sizeof(*rep) + len + 1));
73 rep->len = len;
74 rep->cap = len;
75 rep->count = 0;
76 char *data = data_from_rep(rep);
77 std::memcpy(data, msg, len + 1);
78 __imp_ = data;
79}
80
81inline
82__libcpp_refstring::__libcpp_refstring(const __libcpp_refstring &s) _NOEXCEPT
83 : __imp_(s.__imp_)
84{
85 if (__uses_refcount())
Weiming Zhaob613db72017-09-19 23:18:03 +000086 __libcpp_atomic_add(&rep_from_data(__imp_)->count, 1);
Eric Fiselier1e655162016-10-25 19:33:14 +000087}
88
89inline
90__libcpp_refstring& __libcpp_refstring::operator=(__libcpp_refstring const& s) _NOEXCEPT {
91 bool adjust_old_count = __uses_refcount();
92 struct _Rep_base *old_rep = rep_from_data(__imp_);
93 __imp_ = s.__imp_;
94 if (__uses_refcount())
Weiming Zhaob613db72017-09-19 23:18:03 +000095 __libcpp_atomic_add(&rep_from_data(__imp_)->count, 1);
Eric Fiselier1e655162016-10-25 19:33:14 +000096 if (adjust_old_count)
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000097 {
Weiming Zhaob613db72017-09-19 23:18:03 +000098 if (__libcpp_atomic_add(&old_rep->count, count_t(-1)) < 0)
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000099 {
Eric Fiselier1e655162016-10-25 19:33:14 +0000100 ::operator delete(old_rep);
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +0000101 }
102 }
Eric Fiselier1e655162016-10-25 19:33:14 +0000103 return *this;
104}
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +0000105
Eric Fiselier1e655162016-10-25 19:33:14 +0000106inline
107__libcpp_refstring::~__libcpp_refstring() {
108 if (__uses_refcount()) {
109 _Rep_base* rep = rep_from_data(__imp_);
Weiming Zhaob613db72017-09-19 23:18:03 +0000110 if (__libcpp_atomic_add(&rep->count, count_t(-1)) < 0) {
Eric Fiselier1e655162016-10-25 19:33:14 +0000111 ::operator delete(rep);
112 }
113 }
114}
115
116inline
117bool __libcpp_refstring::__uses_refcount() const {
118#ifdef __APPLE__
119 return __imp_ != get_gcc_empty_string_storage();
120#else
121 return true;
122#endif
123}
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +0000124
125_LIBCPP_END_NAMESPACE_STD
126
Eric Fiselier46123de2017-07-12 01:38:35 +0000127#endif //_LIBCPP_REFSTRING_H