SimpleFIFO - get rid of vector usage

This commit is contained in:
twinaphex 2020-10-04 17:29:53 +02:00
parent 1d4739e62b
commit 98d3f33481

View File

@ -1,7 +1,6 @@
#ifndef __MDFN_SIMPLEFIFO_H #ifndef __MDFN_SIMPLEFIFO_H
#define __MDFN_SIMPLEFIFO_H #define __MDFN_SIMPLEFIFO_H
#include <vector>
#include <stdint.h> #include <stdint.h>
#include <assert.h> #include <assert.h>
@ -10,89 +9,92 @@ class SimpleFIFO
public: public:
// Constructor // Constructor
SimpleFIFO(uint32 the_size) // Size should be a power of 2! SimpleFIFO(uint32 the_size)
{ {
data.resize(round_up_pow2(the_size)); /* Size should be a power of 2! */
size = the_size; assert(the_size && !(the_size & (the_size - 1)));
read_pos = 0;
write_pos = 0; data = (uint8_t*)malloc(the_size * sizeof(uint8_t));
in_count = 0; size = the_size;
read_pos = 0;
write_pos = 0;
in_count = 0;
} }
// Destructor // Destructor
INLINE ~SimpleFIFO() INLINE ~SimpleFIFO()
{ {
if (data)
} free(data);
INLINE void SaveStatePostLoad(void)
{
read_pos %= data.size();
write_pos %= data.size();
in_count %= (data.size() + 1);
} }
INLINE uint32 CanWrite(void) INLINE uint32 CanWrite(void)
{ {
return(size - in_count); return(size - in_count);
} }
INLINE uint8_t ReadUnit(bool peek = false) INLINE uint8_t ReadUnit(bool peek = false)
{ {
uint8_t ret = data[read_pos]; uint8_t ret = data[read_pos];
if(!peek) if(!peek)
{ {
read_pos = (read_pos + 1) & (data.size() - 1); read_pos = (read_pos + 1) & (size - 1);
in_count--; in_count--;
} }
return(ret); return(ret);
} }
INLINE uint8_t ReadByte(bool peek = false) INLINE uint8_t ReadByte(bool peek = false)
{ {
assert(sizeof(uint8_t) == 1); assert(sizeof(uint8_t) == 1);
return(ReadUnit(peek)); return(ReadUnit(peek));
} }
INLINE void Write(const uint8_t *happy_data, uint32 happy_count) INLINE void Write(const uint8_t *happy_data, uint32 happy_count)
{ {
assert(CanWrite() >= happy_count); assert(CanWrite() >= happy_count);
while(happy_count) while(happy_count)
{ {
data[write_pos] = *happy_data; data[write_pos] = *happy_data;
write_pos = (write_pos + 1) & (data.size() - 1); write_pos = (write_pos + 1) & (size - 1);
in_count++; in_count++;
happy_data++; happy_data++;
happy_count--; happy_count--;
} }
} }
INLINE void WriteUnit(const uint8_t& wr_data) INLINE void WriteUnit(const uint8_t& wr_data)
{ {
Write(&wr_data, 1); Write(&wr_data, 1);
} }
INLINE void WriteByte(const uint8_t& wr_data) INLINE void WriteByte(const uint8_t& wr_data)
{ {
assert(sizeof(uint8_t) == 1); assert(sizeof(uint8_t) == 1);
Write(&wr_data, 1); Write(&wr_data, 1);
} }
INLINE void Flush(void) INLINE void Flush(void)
{ {
read_pos = 0; read_pos = 0;
write_pos = 0; write_pos = 0;
in_count = 0; in_count = 0;
} }
//private: INLINE void SaveStatePostLoad(void)
std::vector<uint8_t> data; {
read_pos %= size;
write_pos %= size;
in_count %= (size + 1);
}
uint8_t *data;
uint32 size; uint32 size;
uint32 read_pos; // Read position uint32 read_pos; // Read position
uint32 write_pos; // Write position uint32 write_pos; // Write position