RingBuffer util class
This commit is contained in:
parent
862c322cbc
commit
ad8918f326
|
@ -0,0 +1,56 @@
|
||||||
|
/**
|
||||||
|
******************************************************************************
|
||||||
|
* Xenia : Xbox 360 Emulator Research Project *
|
||||||
|
******************************************************************************
|
||||||
|
* Copyright 2015 Ben Vanik. All rights reserved. *
|
||||||
|
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||||
|
******************************************************************************
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "xenia/base/ring_buffer.h"
|
||||||
|
|
||||||
|
namespace xe {
|
||||||
|
|
||||||
|
RingBuffer::RingBuffer(uint8_t *raw_buffer, size_t size, size_t write_offset)
|
||||||
|
: raw_buffer_(raw_buffer),
|
||||||
|
size_(size),
|
||||||
|
write_offset_(write_offset) {}
|
||||||
|
|
||||||
|
int RingBuffer::Write(uint8_t *buffer, size_t num_bytes) {
|
||||||
|
size_t bytes_written = 0;
|
||||||
|
size_t input_offset = 0;
|
||||||
|
size_t bytes_to_write = 0;
|
||||||
|
|
||||||
|
// write offset -> end
|
||||||
|
bytes_to_write =
|
||||||
|
num_bytes < size_ - write_offset_ ? num_bytes : size_ - write_offset_;
|
||||||
|
|
||||||
|
std::memcpy(raw_buffer_ + write_offset_, buffer, bytes_to_write);
|
||||||
|
input_offset = bytes_to_write;
|
||||||
|
write_offset_ += bytes_to_write;
|
||||||
|
|
||||||
|
// Wraparound (begin -> num_bytes)
|
||||||
|
if (input_offset < num_bytes) {
|
||||||
|
bytes_to_write = num_bytes - input_offset;
|
||||||
|
|
||||||
|
std::memcpy(raw_buffer_, buffer + input_offset, bytes_to_write);
|
||||||
|
write_offset_ = bytes_to_write;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t RingBuffer::DistanceToOffset(size_t offset) {
|
||||||
|
if (offset < size_ && offset >= write_offset_) {
|
||||||
|
// Doesn't wraparound.
|
||||||
|
return offset - write_offset_;
|
||||||
|
} else {
|
||||||
|
// Wraparound.
|
||||||
|
size_t dist = size_ - write_offset_;
|
||||||
|
dist += offset;
|
||||||
|
|
||||||
|
return dist;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace xe
|
|
@ -0,0 +1,38 @@
|
||||||
|
/**
|
||||||
|
******************************************************************************
|
||||||
|
* Xenia : Xbox 360 Emulator Research Project *
|
||||||
|
******************************************************************************
|
||||||
|
* Copyright 2015 Ben Vanik. All rights reserved. *
|
||||||
|
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||||
|
******************************************************************************
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef XENIA_BASE_RING_BUFFER_H_
|
||||||
|
#define XENIA_BASE_RING_BUFFER_H_
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace xe {
|
||||||
|
|
||||||
|
class RingBuffer {
|
||||||
|
public:
|
||||||
|
RingBuffer(uint8_t *raw_buffer, size_t size, size_t write_offset = 0);
|
||||||
|
|
||||||
|
int Write(uint8_t *buffer, size_t num_bytes);
|
||||||
|
|
||||||
|
size_t DistanceToOffset(size_t offset);
|
||||||
|
|
||||||
|
void SetWriteOffset(size_t write_offset) { write_offset_ = write_offset; }
|
||||||
|
size_t GetWriteOffset() { return write_offset_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint8_t *raw_buffer_;
|
||||||
|
size_t size_;
|
||||||
|
size_t write_offset_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace xe
|
||||||
|
|
||||||
|
#endif // XENIA_BASE_RING_BUFFER_H_
|
Loading…
Reference in New Issue