Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright (C) 2017 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 "src/ftrace_reader/test/scattered_stream_delegate_for_testing.h"
     18 
     19 namespace perfetto {
     20 
     21 ScatteredStreamDelegateForTesting::ScatteredStreamDelegateForTesting(
     22     size_t chunk_size)
     23     : chunk_size_(chunk_size) {}
     24 
     25 ScatteredStreamDelegateForTesting::~ScatteredStreamDelegateForTesting() {}
     26 
     27 protozero::ContiguousMemoryRange
     28 ScatteredStreamDelegateForTesting::GetNewBuffer() {
     29   PERFETTO_CHECK(writer_);
     30   if (!chunks_.empty()) {
     31     size_t used = chunk_size_ - writer_->bytes_available();
     32     chunks_used_size_.push_back(used);
     33   }
     34   std::unique_ptr<uint8_t[]> chunk(new uint8_t[chunk_size_]);
     35   uint8_t* begin = chunk.get();
     36   memset(begin, 0xff, chunk_size_);
     37   chunks_.push_back(std::move(chunk));
     38   return {begin, begin + chunk_size_};
     39 }
     40 
     41 std::unique_ptr<uint8_t[]> ScatteredStreamDelegateForTesting::StitchChunks(
     42     size_t size) {
     43   std::unique_ptr<uint8_t[]> buffer =
     44       std::unique_ptr<uint8_t[]>(new uint8_t[size]);
     45   size_t remaining = size;
     46   size_t i = 0;
     47   for (const auto& chunk : chunks_) {
     48     size_t chunk_size = remaining;
     49     if (i < chunks_used_size_.size()) {
     50       chunk_size = chunks_used_size_[i];
     51     }
     52     PERFETTO_CHECK(chunk_size <= chunk_size_);
     53     memcpy(buffer.get() + size - remaining, chunk.get(), chunk_size);
     54     remaining -= chunk_size;
     55     i++;
     56   }
     57 
     58   return buffer;
     59 }
     60 
     61 }  // namespace perfetto
     62