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