blob: d182d3df9e4e87f9b161bc8da09af3d80ede13f2 [file] [log] [blame]
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SRC_STORAGE_LIB_VFS_CPP_TRANSACTION_BUFFERED_OPERATIONS_BUILDER_H_
#define SRC_STORAGE_LIB_VFS_CPP_TRANSACTION_BUFFERED_OPERATIONS_BUILDER_H_
#include <zircon/types.h>
#include <cstddef>
#include <cstdint>
#include <vector>
#include <storage/buffer/block_buffer.h>
#include <storage/operation/operation.h>
#ifdef __Fuchsia__
#include <fuchsia/hardware/block/driver/c/banjo.h>
#include <utility>
#include <storage/buffer/owned_vmoid.h>
#else
#include <zircon/assert.h>
#endif
namespace fs {
namespace internal {
// TODO(https://fxbug.dev/42124749): This interface needs tidying up. For now, add a class here
// which stops the proliferation of BorrowedBuffer classes, which don't fully support the
// BlockBuffer interface.
class BorrowedBuffer : public storage::BlockBuffer {
public:
#ifdef __Fuchsia__
explicit BorrowedBuffer(vmoid_t vmoid) : vmoid_(vmoid) {}
uint16_t vmoid() const final { return vmoid_; }
void* Data(size_t index) final { return nullptr; }
const void* Data(size_t index) const final { return nullptr; }
zx_handle_t Vmo() const final { return ZX_HANDLE_INVALID; }
#else
explicit BorrowedBuffer(void* data) : data_(data) {}
void* Data(size_t index) final {
ZX_ASSERT(index == 0);
return data_;
}
const void* Data(size_t index) const final {
ZX_ASSERT(index == 0);
return data_;
}
#endif
size_t capacity() const final { return 0; }
uint32_t BlockSize() const final { return 0; }
private:
#ifdef __Fuchsia__
vmoid_t vmoid_;
#else
void* data_;
#endif
};
} // namespace internal
// A builder which helps clients collect and coalesce BufferedOperations which target the same
// in-memory / on-disk structures.
class BufferedOperationsBuilder {
public:
BufferedOperationsBuilder() = default;
// Not copyable or movable.
BufferedOperationsBuilder(const BufferedOperationsBuilder&) = delete;
BufferedOperationsBuilder& operator=(const BufferedOperationsBuilder&) = delete;
// Adds a request to the list of operations. Note that there is some coalescing of requests
// performed here, and mixing different types of operations is not supported at this time.
BufferedOperationsBuilder& Add(const storage::Operation& operation, storage::BlockBuffer* buffer);
// Removes the vector of requests, and returns them to the caller.
std::vector<storage::BufferedOperation> TakeOperations();
#ifdef __Fuchsia__
// Adds a vmoid that needs to be detached once the operations have completed.
void AddVmoid(storage::OwnedVmoid vmoid) { vmoids_.push_back(std::move(vmoid)); }
#endif
private:
std::vector<storage::BufferedOperation> operations_;
#ifdef __Fuchsia__
std::vector<storage::OwnedVmoid> vmoids_;
#endif
};
} // namespace fs
#endif // SRC_STORAGE_LIB_VFS_CPP_TRANSACTION_BUFFERED_OPERATIONS_BUILDER_H_