Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3f1381fb authored by Tom Cherry's avatar Tom Cherry Committed by Automerger Merge Worker
Browse files

Merge changes I9638e90b,Ib2636dfc am: c58d1e4a

Original change: https://android-review.googlesource.com/c/platform/system/core/+/1349344

Change-Id: I54b5873749ec46f6489d6e6f8224759239b0b25e
parents b3a10e52 c58d1e4a
Loading
Loading
Loading
Loading
+22 −22
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ CompressionEngine& CompressionEngine::GetInstance() {
    return *engine;
}

bool ZlibCompressionEngine::Compress(std::span<uint8_t> in, std::vector<uint8_t>& out) {
bool ZlibCompressionEngine::Compress(SerializedData& in, size_t data_length, SerializedData& out) {
    z_stream strm;
    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
@@ -37,34 +37,34 @@ bool ZlibCompressionEngine::Compress(std::span<uint8_t> in, std::vector<uint8_t>
        LOG(FATAL) << "deflateInit() failed";
    }

    CHECK_LE(in.size(), static_cast<int64_t>(std::numeric_limits<uint32_t>::max()));
    uint32_t out_size = deflateBound(&strm, in.size());
    CHECK_LE(data_length, in.size());
    CHECK_LE(in.size(), std::numeric_limits<uint32_t>::max());
    uint32_t deflate_bound = deflateBound(&strm, in.size());

    out.resize(out_size);
    strm.avail_in = in.size();
    strm.next_in = const_cast<uint8_t*>(in.data());
    strm.avail_out = out_size;
    out.Resize(deflate_bound);

    strm.avail_in = data_length;
    strm.next_in = in.data();
    strm.avail_out = out.size();
    strm.next_out = out.data();
    ret = deflate(&strm, Z_FINISH);
    CHECK_EQ(ret, Z_STREAM_END);

    uint32_t compressed_data_size = strm.total_out;
    uint32_t compressed_size = strm.total_out;
    deflateEnd(&strm);
    out.resize(compressed_data_size);

    out.Resize(compressed_size);

    return true;
}

bool ZlibCompressionEngine::Decompress(const std::vector<uint8_t>& in, std::vector<uint8_t>& out,
                                       size_t out_size) {
    out.resize(out_size);

bool ZlibCompressionEngine::Decompress(SerializedData& in, SerializedData& out) {
    z_stream strm;
    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;
    strm.avail_in = in.size();
    strm.next_in = const_cast<uint8_t*>(in.data());
    strm.next_in = in.data();
    strm.avail_out = out.size();
    strm.next_out = out.data();

@@ -79,22 +79,22 @@ bool ZlibCompressionEngine::Decompress(const std::vector<uint8_t>& in, std::vect
    return true;
}

bool ZstdCompressionEngine::Compress(std::span<uint8_t> in, std::vector<uint8_t>& out) {
    size_t out_size = ZSTD_compressBound(in.size());
    out.resize(out_size);
bool ZstdCompressionEngine::Compress(SerializedData& in, size_t data_length, SerializedData& out) {
    CHECK_LE(data_length, in.size());

    size_t compress_bound = ZSTD_compressBound(data_length);
    out.Resize(compress_bound);

    out_size = ZSTD_compress(out.data(), out_size, in.data(), in.size(), 1);
    size_t out_size = ZSTD_compress(out.data(), out.size(), in.data(), data_length, 1);
    if (ZSTD_isError(out_size)) {
        LOG(FATAL) << "ZSTD_compress failed: " << ZSTD_getErrorName(out_size);
    }
    out.resize(out_size);
    out.Resize(out_size);

    return true;
}

bool ZstdCompressionEngine::Decompress(const std::vector<uint8_t>& in, std::vector<uint8_t>& out,
                                       size_t out_size) {
    out.resize(out_size);
bool ZstdCompressionEngine::Decompress(SerializedData& in, SerializedData& out) {
    size_t result = ZSTD_decompress(out.data(), out.size(), in.data(), in.size());
    if (ZSTD_isError(result)) {
        LOG(FATAL) << "ZSTD_decompress failed: " << ZSTD_getErrorName(result);
+11 −13
Original line number Diff line number Diff line
@@ -16,8 +16,9 @@

#pragma once

#include <span>
#include <vector>
#include <memory>

#include "SerializedData.h"

class CompressionEngine {
  public:
@@ -25,23 +26,20 @@ class CompressionEngine {

    virtual ~CompressionEngine(){};

    virtual bool Compress(std::span<uint8_t> in, std::vector<uint8_t>& out) = 0;
    // Decompress the contents of `in` into `out`.  `out_size` must be set to the decompressed size
    // of the contents.
    virtual bool Decompress(const std::vector<uint8_t>& in, std::vector<uint8_t>& out,
                            size_t out_size) = 0;
    virtual bool Compress(SerializedData& in, size_t data_length, SerializedData& out) = 0;
    // Decompress the contents of `in` into `out`.  `out.size()` must be set to the decompressed
    // size of the contents.
    virtual bool Decompress(SerializedData& in, SerializedData& out) = 0;
};

class ZlibCompressionEngine : public CompressionEngine {
  public:
    bool Compress(std::span<uint8_t> in, std::vector<uint8_t>& out) override;
    bool Decompress(const std::vector<uint8_t>& in, std::vector<uint8_t>& out,
                    size_t out_size) override;
    bool Compress(SerializedData& in, size_t data_length, SerializedData& out) override;
    bool Decompress(SerializedData& in, SerializedData& out) override;
};

class ZstdCompressionEngine : public CompressionEngine {
  public:
    bool Compress(std::span<uint8_t> in, std::vector<uint8_t>& out) override;
    bool Decompress(const std::vector<uint8_t>& in, std::vector<uint8_t>& out,
                    size_t out_size) override;
    bool Compress(SerializedData& in, size_t data_length, SerializedData& out) override;
    bool Decompress(SerializedData& in, SerializedData& out) override;
};
 No newline at end of file

logd/SerializedData.h

0 → 100644
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <sys/types.h>

#include <algorithm>
#include <memory>

// This class is used instead of std::string or std::vector because their clear(), erase(), etc
// functions don't actually deallocate.  shrink_to_fit() does deallocate but is not guaranteed to
// work and swapping with an empty string/vector is clunky.
class SerializedData {
  public:
    SerializedData() {}
    SerializedData(size_t size) : data_(new uint8_t[size]), size_(size) {}

    void Resize(size_t new_size) {
        if (size_ == 0) {
            data_.reset(new uint8_t[new_size]);
            size_ = new_size;
        } else if (new_size == 0) {
            data_.reset();
            size_ = 0;
        } else if (new_size != size_) {
            std::unique_ptr<uint8_t[]> new_data(new uint8_t[new_size]);
            size_t copy_size = std::min(size_, new_size);
            memcpy(new_data.get(), data_.get(), copy_size);
            data_.swap(new_data);
            size_ = new_size;
        }
    }

    uint8_t* data() { return data_.get(); }
    const uint8_t* data() const { return data_.get(); }
    size_t size() const { return size_; }

  private:
    std::unique_ptr<uint8_t[]> data_;
    size_t size_ = 0;
};
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -121,7 +121,7 @@ MinHeapElement SerializedFlushToState::PopNextUnreadLog() {

    log_positions_[log_id]->read_offset += entry->total_len();

    AddMinHeapEntry(log_id);
    logs_needed_from_next_position_[log_id] = true;

    return top;
}
+10 −8
Original line number Diff line number Diff line
@@ -61,15 +61,13 @@ class SerializedFlushToState : public FlushToState {
        if (logs_ == nullptr) logs_ = logs;
    }

    // Checks to see if any log buffers set in logs_needed_from_next_position_ have new logs and
    // calls AddMinHeapEntry() if so.
    void CheckForNewLogs();

    bool HasUnreadLogs() { return !min_heap_.empty(); }
    bool HasUnreadLogs() {
        CheckForNewLogs();
        return !min_heap_.empty();
    }

    // Pops the next unread log from the min heap.  Add the next log for that log_id to the min heap
    // if one is available otherwise set logs_needed_from_next_position_ to indicate that we're
    // waiting for more logs.
    // Pops the next unread log from the min heap and sets logs_needed_from_next_position_ to
    // indicate that we're waiting for more logs from the associated log buffer.
    MinHeapElement PopNextUnreadLog();

    // If the parent log buffer prunes logs, the reference that this class contains may become
@@ -86,6 +84,10 @@ class SerializedFlushToState : public FlushToState {
    // first chunk and then first log entry within that chunk that is greater or equal to start().
    void CreateLogPosition(log_id_t log_id);

    // Checks to see if any log buffers set in logs_needed_from_next_position_ have new logs and
    // calls AddMinHeapEntry() if so.
    void CheckForNewLogs();

    std::list<SerializedLogChunk>* logs_ = nullptr;
    // An optional structure that contains an iterator to the serialized log buffer and offset into
    // it that this logger should handle next.
Loading