| 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 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 private: | 34 private: |
| 35 // Returns the size of |T| aligned up to |kMaxAlign|. | 35 // Returns the size of |T| aligned up to |kMaxAlign|. |
| 36 template <typename T> | 36 template <typename T> |
| 37 static inline uint32_t AlignedSize() { | 37 static inline uint32_t AlignedSize() { |
| 38 return ((sizeof(T) + (kMaxAlign - 1)) / kMaxAlign) * kMaxAlign; | 38 return ((sizeof(T) + (kMaxAlign - 1)) / kMaxAlign) * kMaxAlign; |
| 39 } | 39 } |
| 40 | 40 |
| 41 // Actual storage. | 41 // Actual storage. |
| 42 // Subtle/annoying: the value '8' must be coded explicitly into the alignment | 42 // Subtle/annoying: the value '8' must be coded explicitly into the alignment |
| 43 // declaration. | 43 // declaration for MSVC. |
| 44 QUIC_ALIGNED(8) char storage_[ArenaSize]; | 44 QUIC_ALIGNED(8) char storage_[ArenaSize]; |
| 45 // Current offset into the storage. | 45 // Current offset into the storage. |
| 46 uint32_t offset_; | 46 uint32_t offset_; |
| 47 | 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(QuicOneBlockArena); | 48 DISALLOW_COPY_AND_ASSIGN(QuicOneBlockArena); |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 template <uint32_t ArenaSize> | 51 template <uint32_t ArenaSize> |
| 52 QuicOneBlockArena<ArenaSize>::QuicOneBlockArena() | 52 QuicOneBlockArena<ArenaSize>::QuicOneBlockArena() : offset_(0) {} |
| 53 : offset_(0) {} | |
| 54 | 53 |
| 55 template <uint32_t ArenaSize> | 54 template <uint32_t ArenaSize> |
| 56 template <typename T, typename... Args> | 55 template <typename T, typename... Args> |
| 57 QuicArenaScopedPtr<T> QuicOneBlockArena<ArenaSize>::New(Args&&... args) { | 56 QuicArenaScopedPtr<T> QuicOneBlockArena<ArenaSize>::New(Args&&... args) { |
| 58 DCHECK_LT(AlignedSize<T>(), ArenaSize) | 57 DCHECK_LT(AlignedSize<T>(), ArenaSize) |
| 59 << "Object is too large for the arena."; | 58 << "Object is too large for the arena."; |
| 60 static_assert(QUIC_ALIGN_OF(T) > 1, | 59 static_assert(QUIC_ALIGN_OF(T) > 1, |
| 61 "Objects added to the arena must be at least 2B aligned."); | 60 "Objects added to the arena must be at least 2B aligned."); |
| 62 if (PREDICT_FALSE(offset_ > ArenaSize - AlignedSize<T>())) { | 61 if (PREDICT_FALSE(offset_ > ArenaSize - AlignedSize<T>())) { |
| 63 LOG(DFATAL) << "Ran out of space in QuicOneBlockArena at " << this | 62 LOG(DFATAL) << "Ran out of space in QuicOneBlockArena at " << this |
| 64 << ", max size was " << ArenaSize << ", failing request was " | 63 << ", max size was " << ArenaSize << ", failing request was " |
| 65 << AlignedSize<T>() << ", end of arena was " << offset_; | 64 << AlignedSize<T>() << ", end of arena was " << offset_; |
| 66 return QuicArenaScopedPtr<T>(new T(std::forward<Args>(args)...)); | 65 return QuicArenaScopedPtr<T>(new T(std::forward<Args>(args)...)); |
| 67 } | 66 } |
| 68 | 67 |
| 69 void* buf = &storage_[offset_]; | 68 void* buf = &storage_[offset_]; |
| 70 new (buf) T(std::forward<Args>(args)...); | 69 new (buf) T(std::forward<Args>(args)...); |
| 71 offset_ += AlignedSize<T>(); | 70 offset_ += AlignedSize<T>(); |
| 72 return QuicArenaScopedPtr<T>(buf, | 71 return QuicArenaScopedPtr<T>(buf, |
| 73 QuicArenaScopedPtr<T>::ConstructFrom::kArena); | 72 QuicArenaScopedPtr<T>::ConstructFrom::kArena); |
| 74 } | 73 } |
| 75 | 74 |
| 76 } // namespace net | 75 } // namespace net |
| 77 | 76 |
| 78 #endif // NET_QUIC_QUIC_ONE_BLOCK_ARENA_H_ | 77 #endif // NET_QUIC_QUIC_ONE_BLOCK_ARENA_H_ |
| OLD | NEW |