Inline these functions. Should mostly work now...

This commit is contained in:
Alcaro 2014-02-18 08:53:36 +01:00
parent 55fa3d01d4
commit c51bfb7b4b

374
rewind.c
View File

@ -22,93 +22,6 @@
//#define NO_UNALIGNED_MEM
//Uncomment the above if alignment is enforced.
#include "boolean.h"
//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);
struct state_manager {
struct rewindstack * core;
unsigned int state_size;
};
state_manager_t *state_manager_new(size_t state_size, size_t buffer_size, void *init_buffer)
{
state_manager_t *state = (state_manager_t*)calloc(1, sizeof(*state));
if (!state)
return NULL;
state->state_size=state_size;
state->core=rewindstack_create(state_size, buffer_size);
if (!state->core)
{
free(state);
return NULL;
}
void* first_state=state->core->push_begin(state->core);
memcpy(first_state, init_buffer, state_size);
state->core->push_end(state->core);
}
void state_manager_free(state_manager_t *state)
{
state->core->free(state->core);
free(state);
}
bool state_manager_pop(state_manager_t *state, void **data)
{
*data=(void*)state->core->pull(state->core);
return (*data);
}
bool state_manager_push(state_manager_t *state, const void *data)
{
void* next_state=state->core->push_begin(state->core);
memcpy(next_state, data, state->state_size);
state->core->push_end(state->core);
return true;
}
//format per frame:
//size nextstart;
//repeat {
@ -180,9 +93,7 @@ static inline size_t read_size_t(uint16_t* ptr)
#define write_size_t(ptr, val) (*(size_t*)(ptr) = (val))
#endif
struct rewindstack_impl {
struct rewindstack i;
struct state_manager {
char * data;
size_t capacity;
char * head;//read and write here
@ -198,72 +109,134 @@ struct rewindstack_impl {
unsigned int entries;
};
static void reset(struct rewindstack * this_, size_t blocksize, size_t capacity)
state_manager_t *state_manager_new(size_t state_size, size_t buffer_size)
{
struct rewindstack_impl * this=(struct rewindstack_impl*)this_;
state_manager_t * state=malloc(sizeof(*state));
int newblocksize=((blocksize-1)|(sizeof(uint16_t)-1))+1;
if (this->blocksize!=newblocksize)
state->capacity=0;
state->blocksize=0;
int newblocksize=((state_size-1)|(sizeof(uint16_t)-1))+1;
state->blocksize=newblocksize;
const int maxcblkcover=UINT16_MAX*sizeof(uint16_t);
const int maxcblks=(state->blocksize+maxcblkcover-1)/maxcblkcover;
state->maxcompsize=state->blocksize + maxcblks*sizeof(uint16_t)*2 + sizeof(uint16_t)+sizeof(uint32_t) + sizeof(size_t)*2;
state->data=malloc(buffer_size);
state->thisblock=calloc(state->blocksize+sizeof(uint16_t)*8, 1);
state->nextblock=calloc(state->blocksize+sizeof(uint16_t)*8, 1);
if (!state->data || !state->thisblock || !state->nextblock)
{
this->blocksize=newblocksize;
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;
free(this->thisblock);
free(this->nextblock);
this->thisblock=calloc(this->blocksize+sizeof(uint16_t)*8, 1);
this->nextblock=calloc(this->blocksize+sizeof(uint16_t)*8, 1);
//force in a different byte at the end, so we don't need to look for the buffer end in the innermost loop
//there is also a large amount of data that's the same, to stop the other scan
//and finally some padding so we don't read outside the buffer end if we're reading in large blocks
*(uint16_t*)(this->thisblock+this->blocksize+sizeof(uint16_t)*3)=0xFFFF;
*(uint16_t*)(this->nextblock+this->blocksize+sizeof(uint16_t)*3)=0x0000;
free(state->data);
free(state->thisblock);
free(state->nextblock);
free(state);
return NULL;
}
//force in a different byte at the end, so we don't need to look for the buffer end in the innermost loop
//there is also a large amount of data that's the same, to stop the other scan
//and finally some padding so we don't read outside the buffer end if we're reading in large blocks
*(uint16_t*)(state->thisblock+state->blocksize+sizeof(uint16_t)*3)=0xFFFF;
*(uint16_t*)(state->nextblock+state->blocksize+sizeof(uint16_t)*3)=0x0000;
if (capacity!=this->capacity)
{
free(this->data);
this->data=malloc(capacity);
this->capacity=capacity;
}
state->capacity=buffer_size;
this->head=this->data+sizeof(size_t);
this->tail=this->data+sizeof(size_t);
state->head=state->data+sizeof(size_t);
state->tail=state->data+sizeof(size_t);
this->thisblock_valid=false;
state->thisblock_valid=false;
this->entries=0;
state->entries=0;
return state;
}
static void * push_begin(struct rewindstack * this_)
void state_manager_free(state_manager_t *state)
{
struct rewindstack_impl * this=(struct rewindstack_impl*)this_;
return this->nextblock;
free(state->data);
free(state->thisblock);
free(state->nextblock);
free(state);
}
static void push_end(struct rewindstack * this_)
bool state_manager_pop(state_manager_t *state, void **data)
{
struct rewindstack_impl * this=(struct rewindstack_impl*)this_;
if (this->thisblock_valid)
*data=NULL;
if (state->thisblock_valid)
{
if (this->capacity<sizeof(size_t)+this->maxcompsize) return;
state->thisblock_valid=false;
state->entries--;
*data=state->thisblock;
return true;
}
if (state->head==state->tail) return false;
size_t start=read_size_t((uint16_t*)(state->head - sizeof(size_t)));
state->head=state->data+start;
const char * compressed=state->data+start+sizeof(size_t);
char * out=state->thisblock;
//begin decompression code
//out is the previously returned state
const uint16_t * compressed16=(const uint16_t*)compressed;
uint16_t * out16=(uint16_t*)out;
while (true)
{
uint16_t numchanged=*(compressed16++);
if (numchanged)
{
out16+=*(compressed16++);
//we could do memcpy, but it seems that function call overhead is high
// enough that memcpy's higher speed for large blocks won't matter
for (int i=0;i<numchanged;i++) out16[i]=compressed16[i];
compressed16+=numchanged;
out16+=numchanged;
}
else
{
uint32_t numunchanged=compressed16[0] | compressed16[1]<<16;
if (!numunchanged) break;
compressed16+=2;
out16+=numunchanged;
}
}
//end decompression code
state->entries--;
*data=state->thisblock;
return true;
}
void* state_manager_push_where(state_manager_t *state)
{
return state->nextblock;
}
bool state_manager_push_do(state_manager_t *state)
{
if (state->thisblock_valid)
{
if (state->capacity<sizeof(size_t)+state->maxcompsize) return false;
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)
size_t headpos=(state->head-state->data);
size_t tailpos=(state->tail-state->data);
size_t remaining=(tailpos+state->capacity-sizeof(size_t)-headpos-1)%state->capacity + 1;
if (remaining<=state->maxcompsize)
{
this->tail=this->data + read_size_t((uint16_t*)this->tail);
this->entries--;
state->tail=state->data + read_size_t((uint16_t*)state->tail);
state->entries--;
goto recheckcapacity;
}
const char* old=this->thisblock;
const char* new=this->nextblock;
char* compressed=this->head+sizeof(size_t);
const char* old=state->thisblock;
const char* new=state->nextblock;
char* compressed=state->head+sizeof(size_t);
//at the end, 'compressed' must point to the end of the compressed data
//do not include the next/prev pointers
@ -271,7 +244,7 @@ static void push_end(struct rewindstack * this_)
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);
size_t num16s=state->blocksize/sizeof(uint16_t);
while (num16s)
{
const uint16_t * oldprev=old16;
@ -361,124 +334,37 @@ static void push_end(struct rewindstack * this_)
compressed=(char*)(compressed16+3);
//end compression code
if (compressed-this->data+this->maxcompsize > this->capacity)
if (compressed-state->data+state->maxcompsize > state->capacity)
{
compressed=this->data;
if (this->tail==this->data+sizeof(size_t)) this->tail=this->data + *(size_t*)this->tail;
compressed=state->data;
if (state->tail==state->data+sizeof(size_t)) state->tail=state->data + *(size_t*)state->tail;
}
write_size_t((uint16_t*)compressed, this->head-this->data);
write_size_t((uint16_t*)compressed, state->head-state->data);
compressed+=sizeof(size_t);
write_size_t((uint16_t*)this->head, compressed-this->data);
this->head=compressed;
write_size_t((uint16_t*)state->head, compressed-state->data);
state->head=compressed;
}
else
{
this->thisblock_valid=true;
state->thisblock_valid=true;
}
char * swap=this->thisblock;
this->thisblock=this->nextblock;
this->nextblock=swap;
char * swap=state->thisblock;
state->thisblock=state->nextblock;
state->nextblock=swap;
this->entries++;
state->entries++;
return true;
}
static void push_cancel(struct rewindstack * this_)
void state_manager_capacity(state_manager_t *state, unsigned int * entries, size_t * bytes, bool * full)
{
//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->thisblock_valid)
{
this->thisblock_valid=false;
this->entries--;
return this->thisblock;
}
if (this->head==this->tail) 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);
char * out=this->thisblock;
//begin decompression code
//out is the previously returned state
const uint16_t * compressed16=(const uint16_t*)compressed;
uint16_t * out16=(uint16_t*)out;
while (true)
{
uint16_t numchanged=*(compressed16++);
if (numchanged)
{
out16+=*(compressed16++);
//we could do memcpy, but it seems that function call overhead is high
// enough that memcpy's higher speed for large blocks won't matter
for (int i=0;i<numchanged;i++) out16[i]=compressed16[i];
compressed16+=numchanged;
out16+=numchanged;
}
else
{
uint32_t numunchanged=compressed16[0] | compressed16[1]<<16;
if (!numunchanged) break;
compressed16+=2;
out16+=numunchanged;
}
}
//end decompression code
this->entries--;
return this->thisblock;
}
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;
this->blocksize=0;
reset((struct rewindstack*)this, blocksize, capacity);
return (struct rewindstack*)this;
size_t headpos=(state->head-state->data);
size_t tailpos=(state->tail-state->data);
size_t remaining=(tailpos+state->capacity-sizeof(size_t)-headpos-1)%state->capacity + 1;
if (entries) *entries=state->entries;
if (bytes) *bytes=(state->capacity-remaining);
if (full) *full=(remaining<=state->maxcompsize*2);
}