Tim Yao | e8c0d4a | 2019-11-27 14:47:35 -0800 | [diff] [blame^] | 1 | #include <algorithm> // for std::min |
| 2 | #include <iostream> |
| 3 | #include <cstring> |
| 4 | |
| 5 | #include <boost/interprocess/managed_shared_memory.hpp> |
| 6 | |
| 7 | #include "CircularBuffer.h" |
| 8 | |
| 9 | using namespace boost::interprocess; |
| 10 | |
| 11 | CircularBuffer::CircularBuffer(managed_shared_memory &segment, size_t capacity) |
| 12 | : begin_index_(0) |
| 13 | , end_index_(0) |
| 14 | , size_(0) |
| 15 | , capacity_(capacity) |
| 16 | , segment_(segment) |
| 17 | { |
| 18 | void *shptr = segment_.allocate(capacity); |
| 19 | handle_ = segment_.get_handle_from_address(shptr); |
| 20 | } |
| 21 | |
| 22 | CircularBuffer::~CircularBuffer() |
| 23 | { |
| 24 | void *shptr = segment_.get_address_from_handle(handle_); |
| 25 | segment_.deallocate(shptr); |
| 26 | } |
| 27 | |
| 28 | size_t CircularBuffer::write(managed_shared_memory &segment, const uint8_t *data, size_t bytes) |
| 29 | { |
| 30 | if (bytes == 0) return 0; |
| 31 | |
| 32 | uint8_t *ptr = static_cast<uint8_t *>(segment.get_address_from_handle(handle_)); |
| 33 | size_t capacity = capacity_; |
| 34 | size_t bytes_to_write = std::min(bytes, capacity - size_); |
| 35 | |
| 36 | if (bytes_to_write <= capacity - end_index_) { |
| 37 | memcpy(ptr + end_index_, data, bytes_to_write); |
| 38 | end_index_ += bytes_to_write; |
| 39 | if (end_index_ == capacity) end_index_ = 0; |
| 40 | } else { |
| 41 | size_t size_1 = capacity - end_index_; |
| 42 | memcpy(ptr + end_index_, data, size_1); |
| 43 | size_t size_2 = bytes_to_write - size_1; |
| 44 | memcpy(ptr, data + size_1, size_2); |
| 45 | end_index_ = size_2; |
| 46 | } |
| 47 | |
| 48 | size_ += bytes_to_write; |
| 49 | return bytes_to_write; |
| 50 | } |
| 51 | |
| 52 | size_t CircularBuffer::read(managed_shared_memory &segment, uint8_t *data, size_t bytes) |
| 53 | { |
| 54 | if (bytes == 0) return 0; |
| 55 | |
| 56 | uint8_t *ptr = static_cast<uint8_t *>(segment.get_address_from_handle(handle_)); |
| 57 | size_t capacity = capacity_; |
| 58 | size_t bytes_to_read = std::min(bytes, size_); |
| 59 | |
| 60 | if (bytes_to_read <= capacity - begin_index_) { |
| 61 | memcpy(data, ptr + begin_index_, bytes_to_read); |
| 62 | begin_index_ += bytes_to_read; |
| 63 | if (begin_index_ == capacity) begin_index_ = 0; |
| 64 | } else { |
| 65 | size_t size_1 = capacity - begin_index_; |
| 66 | memcpy(data, ptr + begin_index_, size_1); |
| 67 | size_t size_2 = bytes_to_read - size_1; |
| 68 | memcpy(data + size_1, ptr, size_2); |
| 69 | begin_index_ = size_2; |
| 70 | } |
| 71 | |
| 72 | size_ -= bytes_to_read; |
| 73 | return bytes_to_read; |
| 74 | } |
| 75 | |
| 76 | uint8_t* CircularBuffer::start_ptr(managed_shared_memory &segment) |
| 77 | { |
| 78 | return static_cast<uint8_t *>(segment.get_address_from_handle(handle_)); |
| 79 | } |
| 80 | |
| 81 | void CircularBuffer::reset() |
| 82 | { |
| 83 | begin_index_ = end_index_ = size_ = 0; |
| 84 | } |