00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef _QUEUE_H
00013
00014
00015
00016
00017
00018
00019
00020 #define _QUEUE_H
00021
00022 #include <exception>
00023 #include <sst_stdint.h>
00024
00025 class queue_overflow : public std::exception {
00026 private:
00027 public:
00028 virtual const char* what() { return "Queue overflow."; }
00029 };
00030
00031 class queue_underflow : public std::exception {
00032 private:
00033 public:
00034 virtual const char* what() { return "Attempt to read from empty Queue."; }
00035 };
00036
00037
00038 enum Queue_opc {
00039
00040 QOPC_CALLBACKREQUEST = 0,
00041 QOPC_SETIPC,
00042
00043
00044 QOPC_CALLBACK = 128,
00045 QOPC_MEMOP,
00046 QOPC_INSTRUCTION,
00047 QOPC_MAGICINST
00048 };
00049
00050 class Queue {
00051 private:
00052 uint8_t *data, *limit;
00053 uint8_t *read_pos, *write_pos;
00054 size_t bytes, size;
00055
00056 public:
00057 Queue(size_t len) : size(len), bytes(0) {
00058 data = new uint8_t[size];
00059 limit = data + size;
00060 read_pos = write_pos = data;
00061 }
00062
00063 ~Queue() { delete[] data; }
00064
00065 template <class T> void put(T x) {
00066 size_t max_write = size - bytes;
00067 if (sizeof(x) > max_write) throw queue_overflow();
00068
00069 *(T *)(write_pos) = x;
00070 write_pos += sizeof(x);
00071 bytes += sizeof(x);
00072 if (write_pos >= limit) write_pos -= (limit - data);
00073 }
00074
00075 template <class T> void get(T &x) {
00076 if (sizeof(x) > bytes) throw queue_underflow();
00077
00078 x = *(T *)(read_pos);
00079 read_pos += sizeof(x);
00080 bytes -= sizeof(x);
00081 if (read_pos >= limit) read_pos -= (limit - data);
00082 }
00083
00084 template <class T> void peek(T &x) {
00085 if (sizeof(x) > bytes) throw queue_underflow();
00086
00087 x = *(T *)(read_pos);
00088 }
00089
00090 void discard(size_t n) {
00091 if (n > bytes) n = bytes;
00092 bytes -= n;
00093 write_pos -= n;
00094 if (write_pos < data) write_pos += (limit - data);
00095 }
00096
00097 int full() { return bytes == size; }
00098 int empty() { return bytes == 0; }
00099 size_t space() { return size - bytes; }
00100 };
00101 #endif