aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Oliver <contact@pauloliver.dev>2025-10-14 04:35:12 +0200
committerPaul Oliver <contact@pauloliver.dev>2025-10-19 22:35:42 +0200
commit70db2bcd5b31d3ee76932d240787848e41f763a0 (patch)
tree7b892ff645478cfb75ccfcd33f9a61c6a280431a /src
parent5b765abf4748b8b26f8cca5939839dcd1c14134a (diff)
Adds optional compression using zlib
Diffstat (limited to 'src')
-rw-r--r--src/salis.c93
-rw-r--r--src/ui/curses.c2
2 files changed, 94 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
diff --git a/src/ui/curses.c b/src/ui/curses.c
index 1d70ca7..d1f821d 100644
--- a/src/ui/curses.c
+++ b/src/ui/curses.c
@@ -6,6 +6,8 @@
* Implements a TUI for the Salis simulator using the ncurses library.
*/
+// GCC_EXTRA_FLAGS -lncursesw
+
#include <curses.h>
#include <locale.h>
#include <time.h>