New rewind implementation. Not in RetroArch's coding style; I feel I've done my part. I don't know how RetroArch wants things done, anyways.

This commit is contained in:
Alcaro 2014-02-11 20:54:46 +01:00
parent b370d3f048
commit 159dfbd965
2 changed files with 433 additions and 0 deletions

392
rewind-alcaro.c Normal file
View File

@ -0,0 +1,392 @@
#include "rewind-alcaro.h"
#include <stdlib.h>
#include <string.h>
//format per frame:
//size nextstart;
//repeat {
// uint16 numchanged; // everything is counted in units of uint16
// if (numchanged) {
// uint16 numunchanged; // skip these before handling numchanged
// uint16[numchanged] changeddata;
// }
// else
// {
// uint32 numunchanged;
// if (!numunchanged) break;
// }
//}
//size thisstart;
//
//the start offsets point to 'nextstart' of any given compressed frame
//multibyte values are stored native endian if alignment is not enforced; if it is, little endian
//the start of the buffer contains a size pointing to the end of the buffer; the end points to its start
//wrapping is handled by returning to the start of the buffer if the compressed data could potentially hit the edge
//if the compressed data could potentially overwrite the tail pointer, the tail retreats until it can no longer collide
//so on average, ~2*maxcompsize is unused at any given moment
//
//if unaligned memory access is illegal, define NO_UNALIGNED_MEM
#if SIZE_MAX == 0xFFFFFFFF
extern char test[(sizeof(size_t)==4)?1:-1];
#elif SIZE_MAX == 0xFFFFFFFFFFFFFFFF
extern char test[(sizeof(size_t)==8)?1:-1];
#define USE_64BIT
#else
#error your compiler is insane.
#endif
#ifdef NO_UNALIGNED_MEM
//These functions assume 16bit alignment.
//They do not make any attempt at matching system native endian; values written by these can only be read by the matching partner.
#ifdef USE_64BIT
static inline void write_size_t(uint16_t* ptr, size_t val)
{
ptr[0]=val>>0;
ptr[1]=val>>16;
ptr[2]=val>>32;
ptr[3]=val>>48;
}
static inline size_t read_size_t(uint16_t* ptr)
{
return ((size_t)ptr[0]<<0 |
(size_t)ptr[1]<<16 |
(size_t)ptr[2]<<32 |
(size_t)ptr[3]<<48);
}
#else
static inline void write_size_t(uint16_t* ptr, size_t val)
{
ptr[0]=val;
ptr[1]=val>>16;
}
static inline size_t read_size_t(uint16_t* ptr)
{
return (ptr[0] | (size_t)ptr[1]<<16);
}
#endif
#else
#define read_size_t(ptr) (*(size_t*)(ptr))
#define write_size_t(ptr, val) (*(size_t*)(ptr) = (val))
#endif
struct rewindstack_impl {
struct rewindstack i;
char * data;
size_t capacity;
char * head;//read and write here
char * tail;//delete here if head is close
char * thisblock;
char * nextblock;
size_t blocksize;//rounded up from reset::blocksize
size_t maxcompsize;//size_t+(blocksize+131071)/131072*(blocksize+u16+u16)+u16+u32+size_t
unsigned int entries;
};
static void reset(struct rewindstack * this_, size_t blocksize, size_t capacity)
{
struct rewindstack_impl * this=(struct rewindstack_impl*)this_;
this->blocksize=((blocksize-1)|(sizeof(uint16_t)-1))+1;
const int maxcblkcover=UINT16_MAX*sizeof(uint16_t);
const int maxcblks=(this->blocksize+maxcblkcover-1)/maxcblkcover;
this->maxcompsize=this->blocksize + maxcblks*sizeof(uint16_t)*2 + sizeof(uint16_t)+sizeof(uint32_t) + sizeof(size_t)*2;
if (capacity!=this->capacity)
{
free(this->data);
this->data=malloc(capacity);
this->capacity=capacity;
}
this->head=this->data+sizeof(size_t);
this->tail=this->data+sizeof(size_t);
free(this->thisblock);
this->thisblock=NULL;
free(this->nextblock);
this->nextblock=calloc(this->blocksize+sizeof(uint16_t)*8, 1);
*(uint16_t*)(this->nextblock+this->blocksize+sizeof(uint16_t)*3)=0x0000;
this->entries=0;
}
static void * push_begin(struct rewindstack * this_)
{
struct rewindstack_impl * this=(struct rewindstack_impl*)this_;
return this->nextblock;
}
static void push_end(struct rewindstack * this_)
{
struct rewindstack_impl * this=(struct rewindstack_impl*)this_;
if (this->thisblock)
{
recheckcapacity:;
size_t headpos=(this->head-this->data);
size_t tailpos=(this->tail-this->data);
size_t remaining=(tailpos+this->capacity-sizeof(size_t)-headpos-1)%this->capacity + 1;
if (remaining<=this->maxcompsize)
{
this->tail=this->data + read_size_t((uint16_t*)this->tail);
this->entries--;
goto recheckcapacity;
}
const char* old=this->thisblock;
const char* new=this->nextblock;
char* compressed=this->head+sizeof(size_t);
//at the end, 'compressed' must point to the end of the compressed data
//do not include the next/prev pointers
//begin compression code
const uint16_t * old16=(const uint16_t*)old;
const uint16_t * new16=(const uint16_t*)new;
uint16_t * compressed16=(uint16_t*)compressed;
size_t num16s=this->blocksize/sizeof(uint16_t);
while (num16s)
{
const uint16_t * oldprev=old16;
#ifdef NO_UNALIGNED_MEM
while ((uintptr_t)old16 & (sizeof(size_t)-1) && *old16==*new16)
{
old16++;
new16++;
}
if (*old16==*new16)
#endif
{
const size_t* olds=(const size_t*)old16;
const size_t* news=(const size_t*)new16;
//size_t diff=(news-olds);
//while (olds[0]==olds[diff]) olds++;
//news+=(olds-(size_t*)oldprev);
while (*olds==*news)
{
olds++;
news++;
}
old16=(const uint16_t*)olds;
new16=(const uint16_t*)news;
while (*old16==*new16)
{
old16++;
new16++;
}
}
size_t skip=(old16-oldprev);
if (skip>=num16s) break;
//old16+=skip;
//new16+=skip;
num16s-=skip;
if (skip>UINT16_MAX)
{
if (skip>UINT32_MAX)
{
old16-=skip;
new16-=skip;
skip=UINT32_MAX;
old16+=skip;
new16+=skip;
}
*(compressed16++)=0;
*(compressed16++)=skip;
*(compressed16++)=skip>>16;
compressed16+=2;
skip=0;
continue;
}
size_t changed;
const uint16_t * old16prev=old16;
//comparing two or three words makes no real difference
//with three, we get larger blocks which should be a minuscle bit faster
//with two, the smaller blocks are less likely to be chopped up elsewhere due to 64KB
while (old16[0]!=new16[0] || old16[1]!=new16[1])
{
old16++;
new16++;
while (*old16!=*new16)
{
old16++;
new16++;
}
}
changed=(old16-old16prev);
if (!changed) continue;
if (changed>UINT16_MAX)
{
old16-=changed;
new16-=changed;
changed=UINT16_MAX;
old16+=changed;
new16+=changed;
}
num16s-=changed;
*(compressed16++)=changed;
*(compressed16++)=skip;
memcpy(compressed16, old16prev, changed*sizeof(uint16_t));
compressed16+=changed;
}
compressed16[0]=0;
compressed16[1]=0;
compressed16[2]=0;
compressed=(char*)(compressed16+3);
//end compression code
if (compressed-this->data+this->maxcompsize > this->capacity)
{
compressed=this->data;
if (this->tail==this->data+sizeof(size_t)) this->tail=this->data + *(size_t*)this->tail;
}
write_size_t((uint16_t*)compressed, this->head-this->data);
compressed+=sizeof(size_t);
write_size_t((uint16_t*)this->head, compressed-this->data);
this->head=compressed;
}
else
{
if (this->capacity<sizeof(size_t)+this->maxcompsize) return;//refuse to cooperate if we can't fit anything
this->thisblock=calloc(this->blocksize+sizeof(uint16_t)*8, 1);
*(uint16_t*)(this->thisblock+this->blocksize+sizeof(uint16_t)*3)=0xFFFF;
}
char * swap=this->thisblock;
this->thisblock=this->nextblock;
this->nextblock=swap;
this->entries++;
}
static void push_cancel(struct rewindstack * this_)
{
//struct rewindstack_impl * this=(struct rewindstack_impl*)this_;
//do nothing - push_begin just returns a pointer anyways
}
static const void * pull(struct rewindstack * this_)
{
struct rewindstack_impl * this=(struct rewindstack_impl*)this_;
if (this->head==this->tail)
{
if (this->thisblock)
{
char * swap=this->thisblock;
this->thisblock=this->nextblock;
this->nextblock=swap;
free(this->thisblock);
this->thisblock=NULL;
this->entries--;
return this->nextblock;
}
return NULL;
}
size_t start=read_size_t((uint16_t*)(this->head - sizeof(size_t)));
this->head=this->data+start;
const char * compressed=this->data+start+sizeof(size_t);
const char * in=this->thisblock;
char * out=this->nextblock;
//begin decompression code
//out is completely uninitialized
char * outend=out+this->blocksize;
const uint16_t * compressed16=(const uint16_t*)compressed;
const uint16_t * in16=(const uint16_t*)in;
uint16_t * out16=(uint16_t*)out;
while (true)
{
uint16_t numchanged=*(compressed16++);
if (numchanged)
{
uint16_t numunchanged=*(compressed16++);
memcpy(out16, in16, numunchanged*sizeof(uint16_t));
in16+=numunchanged;
out16+=numunchanged;
memcpy(out16, compressed16, numchanged*sizeof(uint16_t));
compressed16+=numchanged;
in16+=numchanged;
out16+=numchanged;
}
else
{
uint32_t numunchanged=compressed16[0] | compressed16[1]<<16;
if (!numunchanged)
{
memcpy(out16, in16, outend-(char*)out16);
break;
}
compressed16+=2;
memcpy(out16, in16, numunchanged*sizeof(uint16_t));
in16+=numunchanged;
out16+=numunchanged;
}
}
//end decompression code
char * swap=this->thisblock;
this->thisblock=this->nextblock;
this->nextblock=swap;
this->entries--;
return this->nextblock;
}
static void capacity_f(struct rewindstack * this_, unsigned int * entries, size_t * bytes, bool * full)
{
struct rewindstack_impl * this=(struct rewindstack_impl*)this_;
size_t headpos=(this->head-this->data);
size_t tailpos=(this->tail-this->data);
size_t remaining=(tailpos+this->capacity-sizeof(size_t)-headpos-1)%this->capacity + 1;
if (entries) *entries=this->entries;
if (bytes) *bytes=(this->capacity-remaining);
if (full) *full=(remaining<=this->maxcompsize*2);
}
static void free_(struct rewindstack * this_)
{
struct rewindstack_impl * this=(struct rewindstack_impl*)this_;
free(this->data);
free(this->thisblock);
free(this->nextblock);
free(this);
}
struct rewindstack * rewindstack_create(size_t blocksize, size_t capacity)
{
struct rewindstack_impl * this=malloc(sizeof(struct rewindstack_impl));
this->i.reset=reset;
this->i.push_begin=push_begin;
this->i.push_end=push_end;
this->i.push_cancel=push_cancel;
this->i.pull=pull;
this->i.capacity=capacity_f;
this->i.free=free_;
this->data=NULL;
this->thisblock=NULL;
this->nextblock=NULL;
this->capacity=0;
reset((struct rewindstack*)this, blocksize, capacity);
return (struct rewindstack*)this;
}

41
rewind-alcaro.h Normal file
View File

@ -0,0 +1,41 @@
//#define NO_UNALIGNED_MEM
//Uncomment the above if alignment is enforced.
//A compressing, lossy stack. Optimized for large, mostly similar, blocks of data; optimized for
// writing, less so for reading. Will discard old data if its capacity is exhausted.
struct rewindstack {
//This is equivalent to deleting and recreating the structure, with the exception that
// it won't reallocate the big block if the capacity is unchanged. It is safe to set the capacity
// to 0, though this will make the structure rather useless.
//The structure may hand out bigger blocks of data than requested. This is not detectable; just
// ignore the extra bytes.
//The structure may allocate a reasonable multiple of blocksize, in addition to capacity.
//It is not possible to accurately predict how many blocks will fit in the structure; it varies
// depending on how much the data changes. Emulator savestates are usually compressed to about
// 0.5-2% of their original size. You can stick in some data and use capacity().
void (*reset)(struct rewindstack * this, size_t blocksize, size_t capacity);
//Asks where to put a new block. Size is same as blocksize. Don't read from it; contents are undefined.
//push_end or push_cancel must be the first function called on the structure after this; not even free() is allowed.
//This function cannot fail, though a pull() directly afterwards may fail.
void * (*push_begin)(struct rewindstack * this);
//Tells that the savestate has been written. Don't use the pointer from push_begin after this point.
void (*push_end)(struct rewindstack * this);
//Tells that things were not written to the pointer from push_begin. Equivalent
// to push_end+pull, but faster, and may avoid discarding something. It is allowed to have written to the pointer.
void (*push_cancel)(struct rewindstack * this);
//Pulls off a block. Don't change it; it'll be used to generate the next one. The returned pointer is only
// guaranteed valid until the first call to any function in this structure, with the exception that capacity()
// will not invalidate anything. If the requested block has been discarded, or was never pushed, it returns NULL.
const void * (*pull)(struct rewindstack * this);
//Tells how many entries are in the structure, how many bytes are used, and whether the structure
// is likely to discard something if a new item is appended. The full flag is guaranteed true if
// it has discarded anything since the last pull() or reset(); however, it may be set even before
// discarding, if the implementation feels like doing that.
void (*capacity)(struct rewindstack * this, unsigned int * entries, size_t * bytes, bool * full);
void (*free)(struct rewindstack * this);
};
struct rewindstack * rewindstack_create(size_t blocksize, size_t capacity);