OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 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 |
| 7 // if an allocation out of the arena ever fails in debug builds; falls back to |
| 8 // heap allocation in release builds. |
| 9 |
| 10 #ifndef NET_QUIC_QUIC_ONE_BLOCK_ARENA_H_ |
| 11 #define NET_QUIC_QUIC_ONE_BLOCK_ARENA_H_ |
| 12 |
| 13 #include "net/quic/quic_arena_scoped_ptr.h" |
| 14 #include "net/quic/quic_flags.h" |
| 15 #include "net/quic/quic_utils.h" |
| 16 |
| 17 #define PREDICT_FALSE(x) x |
| 18 |
| 19 namespace net { |
| 20 |
| 21 template <uint32_t ArenaSize> |
| 22 class QuicOneBlockArena { |
| 23 static const uint32_t kMaxAlign = 8; |
| 24 |
| 25 public: |
| 26 QuicOneBlockArena(); |
| 27 |
| 28 // Instantiates an object of type |T| with |args|. |args| are perfectly |
| 29 // forwarded to |T|'s constructor. The returned pointer's lifetime is |
| 30 // controlled by QuicArenaScopedPtr. |
| 31 template <typename T, typename... Args> |
| 32 QuicArenaScopedPtr<T> New(Args&&... args); |
| 33 |
| 34 private: |
| 35 // Returns the size of |T| aligned up to |kMaxAlign|. |
| 36 template <typename T> |
| 37 static inline uint32_t AlignedSize() { |
| 38 return ((sizeof(T) + (kMaxAlign - 1)) / kMaxAlign) * kMaxAlign; |
| 39 } |
| 40 |
| 41 // Actual storage. |
| 42 // Subtle/annoying: the value '8' must be coded explicitly into the alignment |
| 43 // declaration. |
| 44 QUIC_ALIGNED(8) char storage_[ArenaSize]; |
| 45 // Current offset into the storage. |
| 46 uint32_t offset_; |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(QuicOneBlockArena); |
| 49 }; |
| 50 |
| 51 template <uint32_t ArenaSize> |
| 52 QuicOneBlockArena<ArenaSize>::QuicOneBlockArena() |
| 53 : offset_(0) {} |
| 54 |
| 55 template <uint32_t ArenaSize> |
| 56 template <typename T, typename... Args> |
| 57 QuicArenaScopedPtr<T> QuicOneBlockArena<ArenaSize>::New(Args&&... args) { |
| 58 DCHECK_LT(AlignedSize<T>(), ArenaSize) |
| 59 << "Object is too large for the arena."; |
| 60 static_assert(QUIC_ALIGN_OF(T) > 1, |
| 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>())) { |
| 66 LOG(DFATAL) << "Ran out of space in QuicOneBlockArena at " << this |
| 67 << ", max size was " << ArenaSize << ", failing request was " |
| 68 << AlignedSize<T>() << ", end of arena was " << offset_; |
| 69 return QuicArenaScopedPtr<T>(new T(std::forward<Args>(args)...)); |
| 70 } |
| 71 |
| 72 void* buf = &storage_[offset_]; |
| 73 new (buf) T(std::forward<Args>(args)...); |
| 74 offset_ += AlignedSize<T>(); |
| 75 return QuicArenaScopedPtr<T>(buf, |
| 76 QuicArenaScopedPtr<T>::ConstructFrom::kArena); |
| 77 } |
| 78 |
| 79 } // namespace net |
| 80 |
| 81 #endif // NET_QUIC_QUIC_ONE_BLOCK_ARENA_H_ |
OLD | NEW |