2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2015-09-11 18:57:02 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include "port/port.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
inline size_t TruncateToPageBoundary(size_t page_size, size_t s) {
|
|
|
|
s -= (s & (page_size - 1));
|
|
|
|
assert((s % page_size) == 0);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t Roundup(size_t x, size_t y) {
|
|
|
|
return ((x + y - 1) / y) * y;
|
|
|
|
}
|
|
|
|
|
2018-01-26 21:50:48 +01:00
|
|
|
inline size_t Rounddown(size_t x, size_t y) { return (x / y) * y; }
|
|
|
|
|
2015-09-11 18:57:02 +02:00
|
|
|
// This class is to manage an aligned user
|
2016-12-22 21:51:29 +01:00
|
|
|
// allocated buffer for direct I/O purposes
|
2015-09-12 02:36:48 +02:00
|
|
|
// though can be used for any purpose.
|
2015-09-11 18:57:02 +02:00
|
|
|
class AlignedBuffer {
|
|
|
|
size_t alignment_;
|
|
|
|
std::unique_ptr<char[]> buf_;
|
|
|
|
size_t capacity_;
|
|
|
|
size_t cursize_;
|
|
|
|
char* bufstart_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
AlignedBuffer()
|
|
|
|
: alignment_(),
|
|
|
|
capacity_(0),
|
|
|
|
cursize_(0),
|
|
|
|
bufstart_(nullptr) {
|
|
|
|
}
|
|
|
|
|
|
|
|
AlignedBuffer(AlignedBuffer&& o) ROCKSDB_NOEXCEPT {
|
|
|
|
*this = std::move(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
AlignedBuffer& operator=(AlignedBuffer&& o) ROCKSDB_NOEXCEPT {
|
|
|
|
alignment_ = std::move(o.alignment_);
|
|
|
|
buf_ = std::move(o.buf_);
|
|
|
|
capacity_ = std::move(o.capacity_);
|
|
|
|
cursize_ = std::move(o.cursize_);
|
|
|
|
bufstart_ = std::move(o.bufstart_);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
AlignedBuffer(const AlignedBuffer&) = delete;
|
|
|
|
|
|
|
|
AlignedBuffer& operator=(const AlignedBuffer&) = delete;
|
|
|
|
|
2017-01-12 01:42:07 +01:00
|
|
|
static bool isAligned(const void* ptr, size_t alignment) {
|
|
|
|
return reinterpret_cast<uintptr_t>(ptr) % alignment == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isAligned(size_t n, size_t alignment) {
|
|
|
|
return n % alignment == 0;
|
|
|
|
}
|
|
|
|
|
2015-09-12 02:36:48 +02:00
|
|
|
size_t Alignment() const {
|
2015-09-11 18:57:02 +02:00
|
|
|
return alignment_;
|
|
|
|
}
|
|
|
|
|
2015-09-12 02:36:48 +02:00
|
|
|
size_t Capacity() const {
|
2015-09-11 18:57:02 +02:00
|
|
|
return capacity_;
|
|
|
|
}
|
|
|
|
|
2015-09-12 02:36:48 +02:00
|
|
|
size_t CurrentSize() const {
|
2015-09-11 18:57:02 +02:00
|
|
|
return cursize_;
|
|
|
|
}
|
|
|
|
|
2015-09-12 02:36:48 +02:00
|
|
|
const char* BufferStart() const {
|
2015-09-11 18:57:02 +02:00
|
|
|
return bufstart_;
|
|
|
|
}
|
|
|
|
|
2017-01-12 01:42:07 +01:00
|
|
|
char* BufferStart() { return bufstart_; }
|
|
|
|
|
2015-09-11 18:57:02 +02:00
|
|
|
void Clear() {
|
|
|
|
cursize_ = 0;
|
|
|
|
}
|
|
|
|
|
2015-09-12 02:36:48 +02:00
|
|
|
void Alignment(size_t alignment) {
|
2015-09-11 18:57:02 +02:00
|
|
|
assert(alignment > 0);
|
|
|
|
assert((alignment & (alignment - 1)) == 0);
|
|
|
|
alignment_ = alignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocates a new buffer and sets bufstart_ to the aligned first byte
|
2017-06-13 13:34:51 +02:00
|
|
|
void AllocateNewBuffer(size_t requested_capacity, bool copy_data = false) {
|
2015-09-11 18:57:02 +02:00
|
|
|
assert(alignment_ > 0);
|
|
|
|
assert((alignment_ & (alignment_ - 1)) == 0);
|
|
|
|
|
2017-06-13 13:34:51 +02:00
|
|
|
if (copy_data && requested_capacity < cursize_) {
|
|
|
|
// If we are downsizing to a capacity that is smaller than the current
|
|
|
|
// data in the buffer. Ignore the request.
|
|
|
|
return;
|
|
|
|
}
|
2015-09-11 18:57:02 +02:00
|
|
|
|
2017-06-13 13:34:51 +02:00
|
|
|
size_t new_capacity = Roundup(requested_capacity, alignment_);
|
|
|
|
char* new_buf = new char[new_capacity + alignment_];
|
|
|
|
char* new_bufstart = reinterpret_cast<char*>(
|
|
|
|
(reinterpret_cast<uintptr_t>(new_buf) + (alignment_ - 1)) &
|
|
|
|
~static_cast<uintptr_t>(alignment_ - 1));
|
|
|
|
|
|
|
|
if (copy_data) {
|
|
|
|
memcpy(new_bufstart, bufstart_, cursize_);
|
|
|
|
} else {
|
|
|
|
cursize_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufstart_ = new_bufstart;
|
|
|
|
capacity_ = new_capacity;
|
|
|
|
buf_.reset(new_buf);
|
2015-09-11 18:57:02 +02:00
|
|
|
}
|
|
|
|
// Used for write
|
|
|
|
// Returns the number of bytes appended
|
|
|
|
size_t Append(const char* src, size_t append_size) {
|
|
|
|
size_t buffer_remaining = capacity_ - cursize_;
|
|
|
|
size_t to_copy = std::min(append_size, buffer_remaining);
|
|
|
|
|
|
|
|
if (to_copy > 0) {
|
|
|
|
memcpy(bufstart_ + cursize_, src, to_copy);
|
|
|
|
cursize_ += to_copy;
|
|
|
|
}
|
|
|
|
return to_copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Read(char* dest, size_t offset, size_t read_size) const {
|
|
|
|
assert(offset < cursize_);
|
2017-04-27 21:19:55 +02:00
|
|
|
|
|
|
|
size_t to_read = 0;
|
|
|
|
if(offset < cursize_) {
|
|
|
|
to_read = std::min(cursize_ - offset, read_size);
|
|
|
|
}
|
2015-09-11 18:57:02 +02:00
|
|
|
if (to_read > 0) {
|
|
|
|
memcpy(dest, bufstart_ + offset, to_read);
|
|
|
|
}
|
|
|
|
return to_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pad to alignment
|
|
|
|
void PadToAlignmentWith(int padding) {
|
|
|
|
size_t total_size = Roundup(cursize_, alignment_);
|
|
|
|
size_t pad_size = total_size - cursize_;
|
|
|
|
|
|
|
|
if (pad_size > 0) {
|
|
|
|
assert((pad_size + cursize_) <= capacity_);
|
|
|
|
memset(bufstart_ + cursize_, padding, pad_size);
|
|
|
|
cursize_ += pad_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// After a partial flush move the tail to the beginning of the buffer
|
|
|
|
void RefitTail(size_t tail_offset, size_t tail_size) {
|
|
|
|
if (tail_size > 0) {
|
|
|
|
memmove(bufstart_, bufstart_ + tail_offset, tail_size);
|
|
|
|
}
|
|
|
|
cursize_ = tail_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns place to start writing
|
2015-09-12 02:36:48 +02:00
|
|
|
char* Destination() {
|
2015-09-11 18:57:02 +02:00
|
|
|
return bufstart_ + cursize_;
|
|
|
|
}
|
|
|
|
|
2015-09-12 02:36:48 +02:00
|
|
|
void Size(size_t cursize) {
|
2015-09-11 18:57:02 +02:00
|
|
|
cursize_ = cursize;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|