<commit_before>#include <stdio.h>
#include <SDL.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "mmu.h"
#include "cpu.h"
#include "state.h"

static struct state_list *list = NULL;

static long maxid = 0;
struct state_section {
  char id[4];
  long size;
  char *data;
};

void state_write_mem_byte(char *ptr, BYTE data)
{
  ptr[0] = data;
}

void state_write_mem_word(char *ptr, WORD data)
{
  ptr[0] = data>>8;
  ptr[1] = data;
}

void state_write_mem_long(char *ptr, LONG data)
{
  ptr[0] = data>>24;
  ptr[1] = data>>16;
  ptr[2] = data>>8;
  ptr[3] = data;
}

BYTE state_read_mem_byte(char *ptr)
{
  return ptr[0];
}

WORD state_read_mem_word(char *ptr)
{
  return (ptr[0]<<8)|ptr[1];
}

LONG state_read_mem_long(char *ptr)
{
  return (ptr[0]<<24)|(ptr[1]<<16)|(ptr[2]<<8)|ptr[3];
}

static void state_write_file_long(FILE *f, long data)
{
  fprintf(f, "%c", (unsigned char)(data>>24));
  fprintf(f, "%c", (unsigned char)(data>>16));
  fprintf(f, "%c", (unsigned char)(data>>8));
  fprintf(f, "%c", (unsigned char)(data));
}

static long state_read_file_long(FILE *f)
{
  unsigned char tmp[4];
  fread(tmp, 4, 1, f);

  return (tmp[0]<<24)|(tmp[1]<<16)|(tmp[2]<<8)|tmp[3];
}

static void state_write_section_header(FILE *f, char *id, long size)
{
  fwrite(id, 4, 1, f);

  /* size is always saved in big endian */
  state_write_file_long(f, size);
}

static void state_write_section(FILE *f, char *id, long size, char *data)
{
  char dummy[4] = {0, 0, 0, 0};

  state_write_section_header(f, id, size);
  fwrite(data, size, 1, f);
  if(size%4) /* Align to 32bit */
    fwrite(dummy, 4-(size%4), 1, f); 
}

static int state_read_section_header(FILE *f, struct state_section *sec)
{
  if(fread(sec->id, 4, 1, f) != 1) {
    return STATE_INVALID;
  }
  sec->size = state_read_file_long(f);
  return STATE_VALID;
}

static int state_read_section(FILE *f, struct state_section *sec)
{
  char dummy[4];

  if(state_read_section_header(f, sec) == STATE_INVALID)
    return STATE_INVALID;

  if(sec->size == 0) {
    sec->data = NULL;
    return STATE_VALID;
  }
  
  sec->data = (char *)malloc(sec->size);
  if(sec->data == NULL) return STATE_INVALID;
  if(fread(sec->data, sec->size, 1, f) != 1) {
    return STATE_INVALID;
  }
  if(sec->size%4)
    fread(dummy, 4-(sec->size%4), 1, f);

  return STATE_VALID;
}

static void state_clear_mmu(struct mmu_state *state)
{
  while(state) {
    free(state->data);
    state = state->next;
  }
}

static void state_clear(struct state *state)
{
  if(state == NULL) return;

  if(state->cpu_state) free(state->cpu_state->data);
  if(state->mmu_state) state_clear_mmu(state->mmu_state);
  free(state);
}

static long state_get_mmu_size(struct mmu_state *state)
{
  long size = 0;

  while(state) {
    size += state->size;
    state = state->next;
  }
  return size;
}

struct state *state_collect()
{
  struct state *new;
  struct state_list *new_list;
  long before;

  before = SDL_GetTicks();

  new = (struct state *)malloc(sizeof(struct state));
  if(new == NULL) return NULL;

  new_list = (struct state_list *)malloc(sizeof(struct state_list));
  if(new_list == NULL) return NULL;
  
  new->cpu_state = cpu_state_collect();
  if(new->cpu_state == NULL) {
    free(new);
    return NULL;
  }
  new->mmu_state = mmu_state_collect();
  if(new->mmu_state == NULL) {
    free(new->cpu_state->data);
    free(new);
    return NULL;
  }

  maxid++;
  new->size = new->cpu_state->size + state_get_mmu_size(new->mmu_state);
  new->id = maxid;
  new->version = STATE_VERSION;
  new->timestamp = 0;
  new->delta = 0;

  new_list->next = list;
  new_list->state = new;
  list = new_list;

  printf("Time to save: %ld ticks\n", SDL_GetTicks()-before);

  return new;
}

static struct state *state_find_state_by_id(long id)
{
  struct state_list *t;

  t = list;

  while(t) {
    if(id == t->state->id) return t->state;
    t = t->next;
  }
}

static long state_compress_rle(char *tmp, long size, char **input)
{
  char last;
  char *rle;
  int i,cnt,j;

  rle = (char *)malloc(size*2+8);
  *input = rle;
  if(rle == NULL) {
    *input = NULL;
    return 0;
  }

  strncpy(rle, "RLE!", 4);
  last = tmp[0];
  cnt = 1;<commit_msg>Make sure that state_find_state_by_id always returns something in order to prevent the "control reaches end of non-void function" error<commit_after>#include <stdio.h>
#include <SDL.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "mmu.h"
#include "cpu.h"
#include "state.h"

static struct state_list *list = NULL;

static long maxid = 0;
struct state_section {
  char id[4];
  long size;
  char *data;
};

void state_write_mem_byte(char *ptr, BYTE data)
{
  ptr[0] = data;
}

void state_write_mem_word(char *ptr, WORD data)
{
  ptr[0] = data>>8;
  ptr[1] = data;
}

void state_write_mem_long(char *ptr, LONG data)
{
  ptr[0] = data>>24;
  ptr[1] = data>>16;
  ptr[2] = data>>8;
  ptr[3] = data;
}

BYTE state_read_mem_byte(char *ptr)
{
  return ptr[0];
}

WORD state_read_mem_word(char *ptr)
{
  return (ptr[0]<<8)|ptr[1];
}

LONG state_read_mem_long(char *ptr)
{
  return (ptr[0]<<24)|(ptr[1]<<16)|(ptr[2]<<8)|ptr[3];
}

static void state_write_file_long(FILE *f, long data)
{
  fprintf(f, "%c", (unsigned char)(data>>24));
  fprintf(f, "%c", (unsigned char)(data>>16));
  fprintf(f, "%c", (unsigned char)(data>>8));
  fprintf(f, "%c", (unsigned char)(data));
}

static long state_read_file_long(FILE *f)
{
  unsigned char tmp[4];
  fread(tmp, 4, 1, f);

  return (tmp[0]<<24)|(tmp[1]<<16)|(tmp[2]<<8)|tmp[3];
}

static void state_write_section_header(FILE *f, char *id, long size)
{
  fwrite(id, 4, 1, f);

  /* size is always saved in big endian */
  state_write_file_long(f, size);
}

static void state_write_section(FILE *f, char *id, long size, char *data)
{
  char dummy[4] = {0, 0, 0, 0};

  state_write_section_header(f, id, size);
  fwrite(data, size, 1, f);
  if(size%4) /* Align to 32bit */
    fwrite(dummy, 4-(size%4), 1, f); 
}

static int state_read_section_header(FILE *f, struct state_section *sec)
{
  if(fread(sec->id, 4, 1, f) != 1) {
    return STATE_INVALID;
  }
  sec->size = state_read_file_long(f);
  return STATE_VALID;
}

static int state_read_section(FILE *f, struct state_section *sec)
{
  char dummy[4];

  if(state_read_section_header(f, sec) == STATE_INVALID)
    return STATE_INVALID;

  if(sec->size == 0) {
    sec->data = NULL;
    return STATE_VALID;
  }
  
  sec->data = (char *)malloc(sec->size);
  if(sec->data == NULL) return STATE_INVALID;
  if(fread(sec->data, sec->size, 1, f) != 1) {
    return STATE_INVALID;
  }
  if(sec->size%4)
    fread(dummy, 4-(sec->size%4), 1, f);

  return STATE_VALID;
}

static void state_clear_mmu(struct mmu_state *state)
{
  while(state) {
    free(state->data);
    state = state->next;
  }
}

static void state_clear(struct state *state)
{
  if(state == NULL) return;

  if(state->cpu_state) free(state->cpu_state->data);
  if(state->mmu_state) state_clear_mmu(state->mmu_state);
  free(state);
}

static long state_get_mmu_size(struct mmu_state *state)
{
  long size = 0;

  while(state) {
    size += state->size;
    state = state->next;
  }
  return size;
}

struct state *state_collect()
{
  struct state *new;
  struct state_list *new_list;
  long before;

  before = SDL_GetTicks();

  new = (struct state *)malloc(sizeof(struct state));
  if(new == NULL) return NULL;

  new_list = (struct state_list *)malloc(sizeof(struct state_list));
  if(new_list == NULL) return NULL;
  
  new->cpu_state = cpu_state_collect();
  if(new->cpu_state == NULL) {
    free(new);
    return NULL;
  }
  new->mmu_state = mmu_state_collect();
  if(new->mmu_state == NULL) {
    free(new->cpu_state->data);
    free(new);
    return NULL;
  }

  maxid++;
  new->size = new->cpu_state->size + state_get_mmu_size(new->mmu_state);
  new->id = maxid;
  new->version = STATE_VERSION;
  new->timestamp = 0;
  new->delta = 0;

  new_list->next = list;
  new_list->state = new;
  list = new_list;

  printf("Time to save: %ld ticks\n", SDL_GetTicks()-before);

  return new;
}

static struct state *state_find_state_by_id(long id)
{
  struct state_list *t;

  t = list;

  while(t) {
    if(id == t->state->id) return t->state;
    t = t->next;
  }

  return NULL;
}

static long state_compress_rle(char *tmp, long size, char **input)
{
  char last;
  char *rle;
  int i,cnt,j;

  rle = (char *)malloc(size*2+8);
  *input = rle;
  if(rle == NULL) {
    *input = NULL;
    return 0;
  }

  strncpy(rle, "RLE!", 4);
  last = tmp[0];
  cnt = 1;
{'fraction_non_alphanumeric': 0.17038777908343125, 'fraction_numerical': 0.018566392479435957, 'mean_word_length': 3.928199189345686, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 119, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2569696', 'n_tokens_mistral': 3561, 'n_tokens_neox': 3426, 'n_words': 781}