1 // Copyright 2014 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_ZONE_ZONE_ALLOCATOR_H_ 6 #define V8_ZONE_ZONE_ALLOCATOR_H_ 7 #include <limits> 8 9 #include "src/zone/zone.h" 10 11 namespace v8 { 12 namespace internal { 13 14 template <typename T> 15 class zone_allocator { 16 public: 17 typedef T* pointer; 18 typedef const T* const_pointer; 19 typedef T& reference; 20 typedef const T& const_reference; 21 typedef T value_type; 22 typedef size_t size_type; 23 typedef ptrdiff_t difference_type; 24 template <class O> 25 struct rebind { 26 typedef zone_allocator<O> other; 27 }; 28 29 // TODO(bbudge) Remove when V8 updates to MSVS 2015. See crbug.com/603131. zone_allocator()30 zone_allocator() : zone_(nullptr) { UNREACHABLE(); } zone_allocator(Zone * zone)31 explicit zone_allocator(Zone* zone) throw() : zone_(zone) {} zone_allocator(const zone_allocator & other)32 explicit zone_allocator(const zone_allocator& other) throw() 33 : zone_(other.zone_) {} 34 template <typename U> zone_allocator(const zone_allocator<U> & other)35 zone_allocator(const zone_allocator<U>& other) throw() : zone_(other.zone_) {} 36 template <typename U> 37 friend class zone_allocator; 38 address(reference x)39 pointer address(reference x) const { return &x; } address(const_reference x)40 const_pointer address(const_reference x) const { return &x; } 41 42 pointer allocate(size_type n, const void* hint = 0) { 43 return static_cast<pointer>( 44 zone_->NewArray<value_type>(static_cast<int>(n))); 45 } deallocate(pointer p,size_type)46 void deallocate(pointer p, size_type) { /* noop for Zones */ 47 } 48 max_size()49 size_type max_size() const throw() { 50 return std::numeric_limits<int>::max() / sizeof(value_type); 51 } construct(pointer p,const T & val)52 void construct(pointer p, const T& val) { 53 new (static_cast<void*>(p)) T(val); 54 } destroy(pointer p)55 void destroy(pointer p) { p->~T(); } 56 57 bool operator==(zone_allocator const& other) const { 58 return zone_ == other.zone_; 59 } 60 bool operator!=(zone_allocator const& other) const { 61 return zone_ != other.zone_; 62 } 63 zone()64 Zone* zone() { return zone_; } 65 66 private: 67 Zone* zone_; 68 }; 69 70 typedef zone_allocator<bool> ZoneBoolAllocator; 71 typedef zone_allocator<int> ZoneIntAllocator; 72 } // namespace internal 73 } // namespace v8 74 75 #endif // V8_ZONE_ZONE_ALLOCATOR_H_ 76