aboutsummaryrefslogtreecommitdiff
path: root/src/salis.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/salis.c')
-rw-r--r--src/salis.c93
1 files changed, 92 insertions, 1 deletions
diff --git a/src/salis.c b/src/salis.c
index 6714038..74054f6 100644
--- a/src/salis.c
+++ b/src/salis.c
@@ -16,6 +16,10 @@
#include <string.h>
#include <threads.h>
+#ifdef COMPRESS
+#include <zlib.h>
+#endif
+
#define ACT_BENCH (1)
#define ACT_LOAD (2)
#define ACT_NEW (3)
@@ -518,7 +522,13 @@ void core_step(Core *core) {
#if ACTION == ACT_LOAD || ACTION == ACT_NEW
void salis_save(const char *path) {
- FILE *f = fopen(path, "wb");
+#ifdef COMPRESS
+ size_t size = 0;
+ char *in = NULL;
+ FILE *f = open_memstream(&in, &size);
+#else
+ FILE *f = fopen(path, "wb");
+#endif
assert(f);
@@ -529,6 +539,39 @@ void salis_save(const char *path) {
fwrite(&g_steps, sizeof(u64), 1, f);
fwrite(&g_syncs, sizeof(u64), 1, f);
fclose(f);
+
+#ifdef COMPRESS
+ assert(size);
+
+ char *out = malloc(size);
+ assert(out);
+
+ z_stream strm = { 0 };
+ strm.zalloc = NULL,
+ strm.zfree = NULL,
+ strm.opaque = NULL,
+
+ deflateInit(&strm, Z_DEFAULT_COMPRESSION);
+
+ strm.avail_in = size;
+ strm.avail_out = size;
+ strm.next_in = (Bytef *)in;
+ strm.next_out = (Bytef *)out;
+
+ deflate(&strm, Z_FINISH);
+
+ FILE *fx = fopen(path, "wb");
+ assert(fx);
+
+ fwrite(&size, sizeof(size_t), 1, fx);
+ fwrite(out, sizeof(char), strm.total_out, fx);
+ fclose(fx);
+
+ deflateEnd(&strm);
+
+ free(in);
+ free(out);
+#endif
}
void salis_auto_save() {
@@ -578,7 +621,50 @@ void salis_init() {
#if ACTION == ACT_LOAD
void salis_load() {
+#ifdef COMPRESS
+ FILE *fx = fopen(SIM_PATH, "rb");
+ assert(fx);
+
+ fseek(fx, 0, SEEK_END);
+ size_t x_size = ftell(fx) - sizeof(size_t);
+ char *in = malloc(x_size);
+ rewind(fx);
+ assert(x_size);
+ assert(in);
+
+ size_t size = 0;
+ fread(&size, sizeof(size_t), 1, fx);
+ fread(in, 1, x_size, fx);
+ fclose(fx);
+ assert(size);
+
+ char *out = malloc(size);
+ assert(out);
+
+ z_stream strm = { 0 };
+ strm.next_in = (Bytef *)in;
+ strm.avail_in = x_size;
+ strm.zalloc = NULL;
+ strm.zfree = NULL;
+ strm.opaque = NULL;
+
+ inflateInit(&strm);
+
+ strm.avail_out = size;
+ strm.next_out = (Bytef *)out;
+
+#ifdef NDEBUG
+ inflate(&strm, Z_FINISH);
+#else
+ assert(inflate(&strm, Z_FINISH));
+#endif
+
+ inflateEnd(&strm);
+
+ FILE *f = fmemopen(out, size, "rb");
+#else
FILE *f = fopen(SIM_PATH, "rb");
+#endif
assert(f);
@@ -593,6 +679,11 @@ void salis_load() {
#pragma GCC diagnostic pop
fclose(f);
+
+#ifdef COMPRESS
+ free(in);
+ free(out);
+#endif
}
#endif