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 } 31 32 MessageArena::~MessageArena() = default; 33 NewMessage()34Message* MessageArena::NewMessage() { 35 PERFETTO_DCHECK(!blocks_.empty()); // Should never become empty. 36 37 Block* block = &blocks_.front(); 38 if (PERFETTO_UNLIKELY(block->entries >= Block::kCapacity)) { 39 blocks_.emplace_front(); 40 block = &blocks_.front(); 41 } 42 const auto idx = block->entries++; 43 void* storage = block->storage[idx]; 44 PERFETTO_ASAN_UNPOISON(storage, sizeof(Message)); 45 return new (storage) Message(); 46 } 47 DeleteLastMessageInternal()48void MessageArena::DeleteLastMessageInternal() { 49 PERFETTO_DCHECK(!blocks_.empty()); // Should never be empty, see below. 50 Block* block = &blocks_.front(); 51 PERFETTO_DCHECK(block->entries > 0); 52 53 // This is the reason why there is no ~Message() call here. 54 // MessageArea::Reset() (see header) also relies on dtor being trivial. 55 static_assert(std::is_trivially_destructible<Message>::value, 56 "Message must be trivially destructible"); 57 58 --block->entries; 59 PERFETTO_ASAN_POISON(&block->storage[block->entries], sizeof(Message)); 60 61 // Don't remove the first block to avoid malloc/free calls when the root 62 // message is reset. Hitting the allocator all the times is a waste of time. 63 if (block->entries == 0 && std::next(blocks_.cbegin()) != blocks_.cend()) { 64 blocks_.pop_front(); 65 } 66 } 67 68 } // namespace protozero 69