__refstring 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //===------------------------ __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. #ifndef _LIBCPP___REFSTRING
  10. #define _LIBCPP___REFSTRING
  11. #include <__config>
  12. #include <cstddef>
  13. #include <cstring>
  14. #ifdef __APPLE__
  15. #include <dlfcn.h>
  16. #include <mach-o/dyld.h>
  17. #endif
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. class _LIBCPP_HIDDEN __libcpp_refstring
  20. {
  21. private:
  22. const char* str_;
  23. typedef int count_t;
  24. struct _Rep_base
  25. {
  26. std::size_t len;
  27. std::size_t cap;
  28. count_t count;
  29. };
  30. static
  31. _Rep_base*
  32. rep_from_data(const char *data_) _NOEXCEPT
  33. {
  34. char *data = const_cast<char *>(data_);
  35. return reinterpret_cast<_Rep_base *>(data - sizeof(_Rep_base));
  36. }
  37. static
  38. char *
  39. data_from_rep(_Rep_base *rep) _NOEXCEPT
  40. {
  41. char *data = reinterpret_cast<char *>(rep);
  42. return data + sizeof(*rep);
  43. }
  44. #ifdef __APPLE__
  45. static
  46. const char*
  47. compute_gcc_empty_string_storage() _NOEXCEPT
  48. {
  49. void* handle = dlopen("/usr/lib/libstdc++.6.dylib", RTLD_NOLOAD);
  50. if (handle == nullptr)
  51. return nullptr;
  52. void* sym = dlsym(handle, "_ZNSs4_Rep20_S_empty_rep_storageE");
  53. if (sym == nullptr)
  54. return nullptr;
  55. return data_from_rep(reinterpret_cast<_Rep_base *>(sym));
  56. }
  57. static
  58. const char*
  59. get_gcc_empty_string_storage() _NOEXCEPT
  60. {
  61. static const char* p = compute_gcc_empty_string_storage();
  62. return p;
  63. }
  64. bool
  65. uses_refcount() const
  66. {
  67. return str_ != get_gcc_empty_string_storage();
  68. }
  69. #else
  70. bool
  71. uses_refcount() const
  72. {
  73. return true;
  74. }
  75. #endif
  76. public:
  77. explicit __libcpp_refstring(const char* msg) {
  78. std::size_t len = strlen(msg);
  79. _Rep_base* rep = static_cast<_Rep_base *>(::operator new(sizeof(*rep) + len + 1));
  80. rep->len = len;
  81. rep->cap = len;
  82. rep->count = 0;
  83. char *data = data_from_rep(rep);
  84. std::memcpy(data, msg, len + 1);
  85. str_ = data;
  86. }
  87. __libcpp_refstring(const __libcpp_refstring& s) _NOEXCEPT : str_(s.str_)
  88. {
  89. if (uses_refcount())
  90. __sync_add_and_fetch(&rep_from_data(str_)->count, 1);
  91. }
  92. __libcpp_refstring& operator=(const __libcpp_refstring& s) _NOEXCEPT
  93. {
  94. bool adjust_old_count = uses_refcount();
  95. struct _Rep_base *old_rep = rep_from_data(str_);
  96. str_ = s.str_;
  97. if (uses_refcount())
  98. __sync_add_and_fetch(&rep_from_data(str_)->count, 1);
  99. if (adjust_old_count)
  100. {
  101. if (__sync_add_and_fetch(&old_rep->count, count_t(-1)) < 0)
  102. {
  103. ::operator delete(old_rep);
  104. }
  105. }
  106. return *this;
  107. }
  108. ~__libcpp_refstring()
  109. {
  110. if (uses_refcount())
  111. {
  112. _Rep_base* rep = rep_from_data(str_);
  113. if (__sync_add_and_fetch(&rep->count, count_t(-1)) < 0)
  114. {
  115. ::operator delete(rep);
  116. }
  117. }
  118. }
  119. const char* c_str() const _NOEXCEPT {return str_;}
  120. };
  121. _LIBCPP_END_NAMESPACE_STD
  122. #endif //_LIBCPP___REFSTRING