OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium 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 // An arena that consists of a single inlined block of |ArenaSize|. Useful to | 5 // An arena that consists of a single inlined block of |ArenaSize|. Useful to |
6 // avoid repeated calls to malloc/new and to improve memory locality. DCHECK's | 6 // avoid repeated calls to malloc/new and to improve memory locality. DCHECK's |
7 // if an allocation out of the arena ever fails in debug builds; falls back to | 7 // if an allocation out of the arena ever fails in debug builds; falls back to |
8 // heap allocation in release builds. | 8 // heap allocation in release builds. |
9 | 9 |
10 #ifndef NET_QUIC_QUIC_ONE_BLOCK_ARENA_H_ | 10 #ifndef NET_QUIC_QUIC_ONE_BLOCK_ARENA_H_ |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 QuicOneBlockArena<ArenaSize>::QuicOneBlockArena() | 52 QuicOneBlockArena<ArenaSize>::QuicOneBlockArena() |
53 : offset_(0) {} | 53 : offset_(0) {} |
54 | 54 |
55 template <uint32_t ArenaSize> | 55 template <uint32_t ArenaSize> |
56 template <typename T, typename... Args> | 56 template <typename T, typename... Args> |
57 QuicArenaScopedPtr<T> QuicOneBlockArena<ArenaSize>::New(Args&&... args) { | 57 QuicArenaScopedPtr<T> QuicOneBlockArena<ArenaSize>::New(Args&&... args) { |
58 DCHECK_LT(AlignedSize<T>(), ArenaSize) | 58 DCHECK_LT(AlignedSize<T>(), ArenaSize) |
59 << "Object is too large for the arena."; | 59 << "Object is too large for the arena."; |
60 static_assert(QUIC_ALIGN_OF(T) > 1, | 60 static_assert(QUIC_ALIGN_OF(T) > 1, |
61 "Objects added to the arena must be at least 2B aligned."); | 61 "Objects added to the arena must be at least 2B aligned."); |
62 if (!FLAGS_quic_enable_arena_allocation) { | |
63 return QuicArenaScopedPtr<T>(new T(std::forward<Args>(args)...)); | |
64 } | |
65 if (PREDICT_FALSE(offset_ > ArenaSize - AlignedSize<T>())) { | 62 if (PREDICT_FALSE(offset_ > ArenaSize - AlignedSize<T>())) { |
66 LOG(DFATAL) << "Ran out of space in QuicOneBlockArena at " << this | 63 LOG(DFATAL) << "Ran out of space in QuicOneBlockArena at " << this |
67 << ", max size was " << ArenaSize << ", failing request was " | 64 << ", max size was " << ArenaSize << ", failing request was " |
68 << AlignedSize<T>() << ", end of arena was " << offset_; | 65 << AlignedSize<T>() << ", end of arena was " << offset_; |
69 return QuicArenaScopedPtr<T>(new T(std::forward<Args>(args)...)); | 66 return QuicArenaScopedPtr<T>(new T(std::forward<Args>(args)...)); |
70 } | 67 } |
71 | 68 |
72 void* buf = &storage_[offset_]; | 69 void* buf = &storage_[offset_]; |
73 new (buf) T(std::forward<Args>(args)...); | 70 new (buf) T(std::forward<Args>(args)...); |
74 offset_ += AlignedSize<T>(); | 71 offset_ += AlignedSize<T>(); |
75 return QuicArenaScopedPtr<T>(buf, | 72 return QuicArenaScopedPtr<T>(buf, |
76 QuicArenaScopedPtr<T>::ConstructFrom::kArena); | 73 QuicArenaScopedPtr<T>::ConstructFrom::kArena); |
77 } | 74 } |
78 | 75 |
79 } // namespace net | 76 } // namespace net |
80 | 77 |
81 #endif // NET_QUIC_QUIC_ONE_BLOCK_ARENA_H_ | 78 #endif // NET_QUIC_QUIC_ONE_BLOCK_ARENA_H_ |
OLD | NEW |