From 9dd63d2a0b20ba61889e14a97b1b15f1851cefc0 Mon Sep 17 00:00:00 2001 From: Erik Pilkington Date: Sat, 8 Jul 2017 18:54:07 +0000 Subject: [Demangler] NFC: Move Db struct to beginning of file Differential revision: https://reviews.llvm.org/D35158 git-svn-id: https://llvm.org/svn/llvm-project/libcxxabi/trunk@307481 91177308-0d34-0410-b5e6-96231b3b80d8 --- src/cxa_demangle.cpp | 413 +++++++++++++++++++++++++-------------------------- 1 file changed, 206 insertions(+), 207 deletions(-) diff --git a/src/cxa_demangle.cpp b/src/cxa_demangle.cpp index 6673931..67c8100 100644 --- a/src/cxa_demangle.cpp +++ b/src/cxa_demangle.cpp @@ -58,6 +58,212 @@ template const char* parse_unqualified_name(const char* first, const char* last, C& db); template const char* parse_decltype(const char* first, const char* last, C& db); +template +class arena +{ + static const std::size_t alignment = 16; + alignas(alignment) char buf_[N]; + char* ptr_; + + std::size_t + align_up(std::size_t n) noexcept + {return (n + (alignment-1)) & ~(alignment-1);} + + bool + pointer_in_buffer(char* p) noexcept + {return buf_ <= p && p <= buf_ + N;} + +public: + arena() noexcept : ptr_(buf_) {} + ~arena() {ptr_ = nullptr;} + arena(const arena&) = delete; + arena& operator=(const arena&) = delete; + + char* allocate(std::size_t n); + void deallocate(char* p, std::size_t n) noexcept; + + static constexpr std::size_t size() {return N;} + std::size_t used() const {return static_cast(ptr_ - buf_);} + void reset() {ptr_ = buf_;} +}; + +template +char* +arena::allocate(std::size_t n) +{ + n = align_up(n); + if (static_cast(buf_ + N - ptr_) >= n) + { + char* r = ptr_; + ptr_ += n; + return r; + } + return static_cast(std::malloc(n)); +} + +template +void +arena::deallocate(char* p, std::size_t n) noexcept +{ + if (pointer_in_buffer(p)) + { + n = align_up(n); + if (p + n == ptr_) + ptr_ = p; + } + else + std::free(p); +} + +template +class short_alloc +{ + arena& a_; +public: + typedef T value_type; + +public: + template struct rebind {typedef short_alloc<_Up, N> other;}; + + short_alloc(arena& a) noexcept : a_(a) {} + template + short_alloc(const short_alloc& a) noexcept + : a_(a.a_) {} + short_alloc(const short_alloc&) = default; + short_alloc& operator=(const short_alloc&) = delete; + + T* allocate(std::size_t n) + { + return reinterpret_cast(a_.allocate(n*sizeof(T))); + } + void deallocate(T* p, std::size_t n) noexcept + { + a_.deallocate(reinterpret_cast(p), n*sizeof(T)); + } + + template + friend + bool + operator==(const short_alloc& x, const short_alloc& y) noexcept; + + template friend class short_alloc; +}; + +template +inline +bool +operator==(const short_alloc& x, const short_alloc& y) noexcept +{ + return N == M && &x.a_ == &y.a_; +} + +template +inline +bool +operator!=(const short_alloc& x, const short_alloc& y) noexcept +{ + return !(x == y); +} + +template +class malloc_alloc +{ +public: + typedef T value_type; + typedef T& reference; + typedef const T& const_reference; + typedef T* pointer; + typedef const T* const_pointer; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + + malloc_alloc() = default; + template malloc_alloc(const malloc_alloc&) noexcept {} + + T* allocate(std::size_t n) + { + return static_cast(std::malloc(n*sizeof(T))); + } + void deallocate(T* p, std::size_t) noexcept + { + std::free(p); + } + + template struct rebind { using other = malloc_alloc; }; + template + void construct(U* p, Args&&... args) + { + ::new ((void*)p) U(std::forward(args)...); + } + void destroy(T* p) + { + p->~T(); + } +}; + +template +inline +bool +operator==(const malloc_alloc&, const malloc_alloc&) noexcept +{ + return true; +} + +template +inline +bool +operator!=(const malloc_alloc& x, const malloc_alloc& y) noexcept +{ + return !(x == y); +} + +const size_t bs = 4 * 1024; +template using Alloc = short_alloc; +template using Vector = std::vector>; + +template +struct string_pair +{ + StrT first; + StrT second; + + string_pair() = default; + string_pair(StrT f) : first(std::move(f)) {} + string_pair(StrT f, StrT s) + : first(std::move(f)), second(std::move(s)) {} + template + string_pair(const char (&s)[N]) : first(s, N-1) {} + + size_t size() const {return first.size() + second.size();} + bool empty() const { return first.empty() && second.empty(); } + StrT full() const {return first + second;} + StrT move_full() {return std::move(first) + std::move(second);} +}; + +struct Db +{ + typedef std::basic_string, + malloc_alloc> String; + typedef Vector> sub_type; + typedef Vector template_param_type; + sub_type names; + template_param_type subs; + Vector template_param; + unsigned cv = 0; + unsigned ref = 0; + unsigned encoding_depth = 0; + bool parsed_ctor_dtor_cv = false; + bool tag_templates = true; + bool fix_forward_references = false; + bool try_to_parse_template_args = true; + + template + Db(arena& ar) : + names(ar), + subs(0, names, ar), + template_param(0, subs, ar) + {} +}; template void @@ -4798,213 +5004,6 @@ demangle(const char* first, const char* last, C& db, int& status) status = invalid_mangled_name; } -template -class arena -{ - static const std::size_t alignment = 16; - alignas(alignment) char buf_[N]; - char* ptr_; - - std::size_t - align_up(std::size_t n) noexcept - {return (n + (alignment-1)) & ~(alignment-1);} - - bool - pointer_in_buffer(char* p) noexcept - {return buf_ <= p && p <= buf_ + N;} - -public: - arena() noexcept : ptr_(buf_) {} - ~arena() {ptr_ = nullptr;} - arena(const arena&) = delete; - arena& operator=(const arena&) = delete; - - char* allocate(std::size_t n); - void deallocate(char* p, std::size_t n) noexcept; - - static constexpr std::size_t size() {return N;} - std::size_t used() const {return static_cast(ptr_ - buf_);} - void reset() {ptr_ = buf_;} -}; - -template -char* -arena::allocate(std::size_t n) -{ - n = align_up(n); - if (static_cast(buf_ + N - ptr_) >= n) - { - char* r = ptr_; - ptr_ += n; - return r; - } - return static_cast(std::malloc(n)); -} - -template -void -arena::deallocate(char* p, std::size_t n) noexcept -{ - if (pointer_in_buffer(p)) - { - n = align_up(n); - if (p + n == ptr_) - ptr_ = p; - } - else - std::free(p); -} - -template -class short_alloc -{ - arena& a_; -public: - typedef T value_type; - -public: - template struct rebind {typedef short_alloc<_Up, N> other;}; - - short_alloc(arena& a) noexcept : a_(a) {} - template - short_alloc(const short_alloc& a) noexcept - : a_(a.a_) {} - short_alloc(const short_alloc&) = default; - short_alloc& operator=(const short_alloc&) = delete; - - T* allocate(std::size_t n) - { - return reinterpret_cast(a_.allocate(n*sizeof(T))); - } - void deallocate(T* p, std::size_t n) noexcept - { - a_.deallocate(reinterpret_cast(p), n*sizeof(T)); - } - - template - friend - bool - operator==(const short_alloc& x, const short_alloc& y) noexcept; - - template friend class short_alloc; -}; - -template -inline -bool -operator==(const short_alloc& x, const short_alloc& y) noexcept -{ - return N == M && &x.a_ == &y.a_; -} - -template -inline -bool -operator!=(const short_alloc& x, const short_alloc& y) noexcept -{ - return !(x == y); -} - -template -class malloc_alloc -{ -public: - typedef T value_type; - typedef T& reference; - typedef const T& const_reference; - typedef T* pointer; - typedef const T* const_pointer; - typedef std::size_t size_type; - typedef std::ptrdiff_t difference_type; - - malloc_alloc() = default; - template malloc_alloc(const malloc_alloc&) noexcept {} - - T* allocate(std::size_t n) - { - return static_cast(std::malloc(n*sizeof(T))); - } - void deallocate(T* p, std::size_t) noexcept - { - std::free(p); - } - - template struct rebind { using other = malloc_alloc; }; - template - void construct(U* p, Args&&... args) - { - ::new ((void*)p) U(std::forward(args)...); - } - void destroy(T* p) - { - p->~T(); - } -}; - -template -inline -bool -operator==(const malloc_alloc&, const malloc_alloc&) noexcept -{ - return true; -} - -template -inline -bool -operator!=(const malloc_alloc& x, const malloc_alloc& y) noexcept -{ - return !(x == y); -} - -const size_t bs = 4 * 1024; -template using Alloc = short_alloc; -template using Vector = std::vector>; - -template -struct string_pair -{ - StrT first; - StrT second; - - string_pair() = default; - string_pair(StrT f) : first(std::move(f)) {} - string_pair(StrT f, StrT s) - : first(std::move(f)), second(std::move(s)) {} - template - string_pair(const char (&s)[N]) : first(s, N-1) {} - - size_t size() const {return first.size() + second.size();} - bool empty() const { return first.empty() && second.empty(); } - StrT full() const {return first + second;} - StrT move_full() {return std::move(first) + std::move(second);} -}; - -struct Db -{ - typedef std::basic_string, - malloc_alloc> String; - typedef Vector> sub_type; - typedef Vector template_param_type; - sub_type names; - template_param_type subs; - Vector template_param; - unsigned cv = 0; - unsigned ref = 0; - unsigned encoding_depth = 0; - bool parsed_ctor_dtor_cv = false; - bool tag_templates = true; - bool fix_forward_references = false; - bool try_to_parse_template_args = true; - - template - Db(arena& ar) : - names(ar), - subs(0, names, ar), - template_param(0, subs, ar) - {} -}; - } // unnamed namespace extern "C" _LIBCXXABI_FUNC_VIS char * -- cgit v1.2.3