chore: Remove unused code

This commit is contained in:
John Colvin committed 2023-03-20 13:26:44 -04:00
1 parent d6f172e953
commit 8b26ac4456
1 file changed
-190
@@ -1,190 +0,0 @@
#include <cstddef>
#include <memory>
#include <stdint.h>
#include <utility>
#include "lib/utils/bts_assert.h"
namespace bts {
namespace mirage {
class QuickQueue {
public:
QuickQueue(size_t n_slots, uint32_t slot_sz)
: _buffer(new uint8_t[n_slots * (sizeof(SlotHeader) + slot_sz)]),
_n_slots(n_slots), _slot_sz(slot_sz), _max_msg_sz(n_slots * slot_sz),
_current_slot(0), _current_sn(0) {
bts_assert(slot_sz > 0);
bts_assert(n_slots > 0);
do {
auto p = current_slot();
auto &slot = *p.first;
slot.seq_n = 0;
move_to_next_slot();
} while (_current_slot > 0);
}
enum class Result { MSG_TO_LARGE, OK };
Result put(uint8_t const *msg, size_t msg_sz) {
if (msg_sz > _max_msg_sz)
return Result::MSG_TO_LARGE;
if (msg_sz == 0)
return Result::OK;
auto remaining = msg_sz;
auto msg_cursor = msg;
while (remaining > 0) {
auto p = current_slot();
auto &slot = *p.first;
auto slot_buffer = p.second;
// mark slot as in use
slot.seq_n = 0;
if (remaining > slot_sz()) {
slot.bytes_in_slot = 0; // signal slot is full
memcpy(slot_buffer, msg_cursor, slot_sz());
msg_cursor += slot_sz();
remaining -= slot_sz();
} else {
slot.bytes_in_slot = remaining;
memcpy(slot_buffer, msg_cursor, remaining);
remaining = 0;
}
move_to_next_slot();
// done with slot
slot.seq_n = ++_current_sn;
}
return Result::OK;
}
protected:
friend class QuickQueueReader;
struct SlotHeader {
std::atomic<uint64_t> seq_n;
uint32_t bytes_in_slot;
};
uint32_t slot_sz() const { return _slot_sz; }
size_t n_slots() const { return _n_slots; }
uint8_t const *buffer() const { return _buffer.get(); }
private:
std::pair<SlotHeader *, uint8_t *> current_slot() {
auto offset =
_buffer.get() + _current_slot * (_slot_sz + sizeof(SlotHeader));
SlotHeader *h = (SlotHeader *)offset;
return std::make_pair(h, offset + sizeof(SlotHeader));
}
void move_to_next_slot() {
if (_current_slot == _n_slots)
_current_slot = 0;
else
_current_slot++;
}
const std::unique_ptr<uint8_t[]> _buffer;
const size_t _n_slots;
const uint32_t _slot_sz;
const size_t _max_msg_sz;
size_t _current_slot;
size_t _current_sn;
};
class QuickQueueReader {
public:
QuickQueueReader(QuickQueue const &q)
: _queue(q), _expected_sn(1), _current_slot(0),
_local_buffer_sz(q.slot_sz() * 10),
_local_buffer(new uint8_t[_local_buffer_sz]), _state(ReaderState::OK) {
}
struct Result {
enum { MISSING_MSGS, NO_MSG, OK } result;
uint8_t const *msg;
size_t msg_size;
};
Result get() {
if (_state == ReaderState::LOST_MSGS)
return Result{Result::MISSING_MSGS, nullptr, 0};
auto p = current_slot();
auto &slot = *p.first;
auto slot_buffer = p.second;
// read sn
size_t sn = slot.seq_n;
auto bytes_in_slot = slot.bytes_in_slot;
if (sn == 0)
return Result{Result::NO_MSG, nullptr, 0};
if (sn > _expected_sn) {
_state = ReaderState::LOST_MSGS;
return Result{Result::MISSING_MSGS, nullptr, 0};
}
if (slot.bytes_in_slot != 0) {
// single slot msg
memcpy(_local_buffer.get(), slot_buffer, bytes_in_slot);
size_t sn2 = slot.seq_n;
if (sn == sn2) {
// we read atomically, we are good to go
move_to_next_slot();
return Result{Result::OK, _local_buffer.get(), bytes_in_slot};
} else {
// the message we needed to read was overwritten
_state = ReaderState::LOST_MSGS;
return Result{Result::MISSING_MSGS, nullptr, 0};
}
} else
// multi-slot msg
return get_multi_slot(sn, slot, slot_buffer);
}
private:
enum class ReaderState { OK, LOST_MSGS };
QuickQueue const &_queue;
size_t _expected_sn;
size_t _current_slot;
size_t _local_buffer_sz;
std::unique_ptr<uint8_t[]> _local_buffer;
ReaderState _state;
std::pair<const QuickQueue::SlotHeader *, uint8_t const *> current_slot() {
auto offset =
_queue.buffer() +
_current_slot * (_queue.slot_sz() + sizeof(QuickQueue::SlotHeader));
auto h = (QuickQueue::SlotHeader const *)offset;
return std::make_pair(h, offset + sizeof(QuickQueue::SlotHeader));
}
void move_to_next_slot() {
if (_current_slot == _queue.n_slots())
_current_slot = 0;
else
_current_slot++;
_expected_sn++;
}
Result get_multi_slot(
size_t first_sn,
QuickQueue::SlotHeader const &slot,
uint8_t const *slot_buffer) {
memcpy(_local_buffer.get(), slot_buffer, _queue.slot_sz());
size_t sn2 = slot.seq_n;
if (sn2 != first_sn) {
// the message we needed to read was overwritten
_state = ReaderState::LOST_MSGS;
return Result{Result::MISSING_MSGS, nullptr, 0};
}
// firt slot was read successfully, read the rest
return Result{Result::MISSING_MSGS, nullptr, 0};
}
};
} // namespace mirage
} // namespace bts