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