llvm.org GIT mirror llvm / fec7b48
Avoid using alignas and constexpr. This requires removing the custom allocator, since Demangle cannot depend on Support and so cannot use Compiler.h. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@280750 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 3 years ago
1 changed file(s) with 5 addition(s) and 136 deletion(s). Raw diff Collapse all Expand all
77 //===----------------------------------------------------------------------===//
88
99 #include "llvm/Demangle/Demangle.h"
10 #include "llvm/Support/Compiler.h"
1110
1211 // This file exports a single function: llvm::itanium_demangle.
1312 // It also has no dependencies on the rest of llvm. It is implemented this way
42094208 }
42104209
42114210 namespace {
4212 template class arena {
4213 static const std::size_t alignment = 16;
4214 LLVM_ALIGNAS(16) char buf_[N];
4215 char *ptr_;
4216
4217 std::size_t align_up(std::size_t n) LLVM_NOEXCEPT {
4218 return (n + (alignment - 1)) & ~(alignment - 1);
4219 }
4220
4221 bool pointer_in_buffer(char *p) LLVM_NOEXCEPT {
4222 return buf_ <= p && p <= buf_ + N;
4223 }
4224
4225 public:
4226 arena() LLVM_NOEXCEPT : ptr_(buf_) {}
4227 ~arena() { ptr_ = nullptr; }
4228 arena(const arena &) = delete;
4229 arena &operator=(const arena &) = delete;
4230
4231 char *allocate(std::size_t n);
4232 void deallocate(char *p, std::size_t n) LLVM_NOEXCEPT;
4233
4234 static LLVM_CONSTEXPR std::size_t size() { return N; }
4235 std::size_t used() const { return static_cast(ptr_ - buf_); }
4236 void reset() { ptr_ = buf_; }
4237 };
4238
4239 template char *arena::allocate(std::size_t n) {
4240 n = align_up(n);
4241 if (static_cast(buf_ + N - ptr_) >= n) {
4242 char *r = ptr_;
4243 ptr_ += n;
4244 return r;
4245 }
4246 return static_cast(std::malloc(n));
4247 }
4248
4249 template
4250 void arena::deallocate(char *p, std::size_t n) LLVM_NOEXCEPT {
4251 if (pointer_in_buffer(p)) {
4252 n = align_up(n);
4253 if (p + n == ptr_)
4254 ptr_ = p;
4255 } else
4256 std::free(p);
4257 }
4258
4259 template class short_alloc {
4260 arena &a_;
4261
4262 public:
4263 typedef T value_type;
4264
4265 public:
4266 template struct rebind { typedef short_alloc<_Up, N> other; };
4267
4268 short_alloc(arena &a) LLVM_NOEXCEPT : a_(a) {}
4269 template
4270 short_alloc(const short_alloc &a) LLVM_NOEXCEPT : a_(a.a_) {}
4271 short_alloc(const short_alloc &) = default;
4272 short_alloc &operator=(const short_alloc &) = delete;
4273
4274 T *allocate(std::size_t n) {
4275 return reinterpret_cast(a_.allocate(n * sizeof(T)));
4276 }
4277 void deallocate(T *p, std::size_t n) LLVM_NOEXCEPT {
4278 a_.deallocate(reinterpret_cast(p), n * sizeof(T));
4279 }
4280
4281 template
4282 friend bool operator==(const short_alloc &x,
4283 const short_alloc &y) LLVM_NOEXCEPT;
4284
4285 template friend class short_alloc;
4286 };
4287
4288 template
4289 inline bool operator==(const short_alloc &x,
4290 const short_alloc &y) LLVM_NOEXCEPT {
4291 return N == M && &x.a_ == &y.a_;
4292 }
4293
4294 template
4295 inline bool operator!=(const short_alloc &x,
4296 const short_alloc &y) LLVM_NOEXCEPT {
4297 return !(x == y);
4298 }
4299
4300 template class malloc_alloc {
4301 public:
4302 typedef T value_type;
4303 typedef T &reference;
4304 typedef const T &const_reference;
4305 typedef T *pointer;
4306 typedef const T *const_pointer;
4307 typedef std::size_t size_type;
4308 typedef std::ptrdiff_t difference_type;
4309
4310 malloc_alloc() = default;
4311 template malloc_alloc(const malloc_alloc &) LLVM_NOEXCEPT {}
4312
4313 T *allocate(std::size_t n) {
4314 return static_cast(std::malloc(n * sizeof(T)));
4315 }
4316 void deallocate(T *p, std::size_t) LLVM_NOEXCEPT { std::free(p); }
4317
4318 template struct rebind { using other = malloc_alloc; };
4319 template void construct(U *p, Args &&... args) {
4320 ::new ((void *)p) U(std::forward(args)...);
4321 }
4322 void destroy(T *p) { p->~T(); }
4323 };
4324
4325 template
4326 inline bool operator==(const malloc_alloc &,
4327 const malloc_alloc &) LLVM_NOEXCEPT {
4328 return true;
4329 }
4330
4331 template
4332 inline bool operator!=(const malloc_alloc &x,
4333 const malloc_alloc &y) LLVM_NOEXCEPT {
4334 return !(x == y);
4335 }
4336
4337 const size_t bs = 4 * 1024;
4338 template using Alloc = short_alloc;
4339 template using Vector = std::vector, Alloc>;
4211 template using Vector = std::vector>;
43404212
43414213 template struct string_pair {
43424214 StrT first;
43534225 };
43544226
43554227 struct Db {
4356 typedef std::basic_string, malloc_alloc>
4228 typedef std::basic_string>
43574229 String;
43584230 typedef Vector> sub_type;
43594231 typedef Vector template_param_type;
43684240 bool fix_forward_references;
43694241 bool try_to_parse_template_args;
43704242
4371 template
4372 Db(arena &ar)
4373 : names(ar), subs(0, names, ar), template_param(0, subs, ar) {}
4243 Db() : subs(0, names), template_param(0, subs) {}
43744244 };
43754245 }
43764246
43824252 return nullptr;
43834253 }
43844254 size_t internal_size = buf != nullptr ? *n : 0;
4385 arena a;
4386 Db db(a);
4255 Db db;
43874256 db.cv = 0;
43884257 db.ref = 0;
43894258 db.encoding_depth = 0;
43904259 db.parsed_ctor_dtor_cv = false;
43914260 db.tag_templates = true;
4392 db.template_param.emplace_back(a);
4261 db.template_param.emplace_back();
43934262 db.fix_forward_references = false;
43944263 db.try_to_parse_template_args = true;
43954264 int internal_status = success;