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 for MSVC. | |
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() : offset_(0) {} | |
53 | |
54 template <uint32_t ArenaSize> | |
55 template <typename T, typename... Args> | |
56 QuicArenaScopedPtr<T> QuicOneBlockArena<ArenaSize>::New(Args&&... args) { | |
57 DCHECK_LT(AlignedSize<T>(), ArenaSize) | |
58 << "Object is too large for the arena."; | |
59 static_assert(QUIC_ALIGN_OF(T) > 1, | |
60 "Objects added to the arena must be at least 2B aligned."); | |
61 if (PREDICT_FALSE(offset_ > ArenaSize - AlignedSize<T>())) { | |
62 LOG(DFATAL) << "Ran out of space in QuicOneBlockArena at " << this | |
63 << ", max size was " << ArenaSize << ", failing request was " | |
64 << AlignedSize<T>() << ", end of arena was " << offset_; | |
65 return QuicArenaScopedPtr<T>(new T(std::forward<Args>(args)...)); | |
66 } | |
67 | |
68 void* buf = &storage_[offset_]; | |
69 new (buf) T(std::forward<Args>(args)...); | |
70 offset_ += AlignedSize<T>(); | |
71 return QuicArenaScopedPtr<T>(buf, | |
72 QuicArenaScopedPtr<T>::ConstructFrom::kArena); | |
73 } | |
74 | |
75 } // namespace net | |
76 | |
77 #endif // NET_QUIC_QUIC_ONE_BLOCK_ARENA_H_ | |
OLD | NEW |