1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "perfetto/protozero/message_arena.h" 18 19 #include <atomic> 20 #include <type_traits> 21 22 #include "perfetto/base/logging.h" 23 #include "perfetto/protozero/message_handle.h" 24 25 namespace protozero { 26 MessageArena()27MessageArena::MessageArena() { 28 // The code below assumes that there is always at least one block. 29 blocks_.emplace_front(); 30 static_assert( 31 std::alignment_of<decltype(blocks_.front().storage[0])>::value >= 32 alignof(Message), 33 "MessageArea's storage is not properly aligned"); 34 } 35 36 MessageArena::~MessageArena() = default; 37 NewMessage()38Message* MessageArena::NewMessage() { 39 PERFETTO_DCHECK(!blocks_.empty()); // Should never become empty. 40 41 Block* block = &blocks_.front(); 42 if (PERFETTO_UNLIKELY(block->entries >= Block::kCapacity)) { 43 blocks_.emplace_front(); 44 block = &blocks_.front(); 45 } 46 const auto idx = block->entries++; 47 void* storage = &block->storage[idx]; 48 PERFETTO_ASAN_UNPOISON(storage, sizeof(Message)); 49 return new (storage) Message(); 50 } 51 DeleteLastMessageInternal()52void MessageArena::DeleteLastMessageInternal() { 53 PERFETTO_DCHECK(!blocks_.empty()); // Should never be empty, see below. 54 Block* block = &blocks_.front(); 55 PERFETTO_DCHECK(block->entries > 0); 56 57 // This is the reason why there is no ~Message() call here. 58 // MessageArea::Reset() (see header) also relies on dtor being trivial. 59 static_assert(std::is_trivially_destructible<Message>::value, 60 "Message must be trivially destructible"); 61 62 --block->entries; 63 PERFETTO_ASAN_POISON(&block->storage[block->entries], sizeof(Message)); 64 65 // Don't remove the first block to avoid malloc/free calls when the root 66 // message is reset. Hitting the allocator all the times is a waste of time. 67 if (block->entries == 0 && std::next(blocks_.cbegin()) != blocks_.cend()) { 68 blocks_.pop_front(); 69 } 70 } 71 72 } // namespace protozero 73