memory_arena
Loading...
Searching...
No Matches
Functions
arena_io.c File Reference

Arena snapshot serialization and deserialization. More...

#include "arena_io.h"
#include "arena_debug.h"
#include <stdio.h>
#include <string.h>
+ Include dependency graph for arena_io.c:

Go to the source code of this file.

Functions

bool arena_load_from_file (t_arena *arena, const char *path)
 Load arena contents from a snapshot file.
 
bool arena_save_to_file (const t_arena *arena, const char *path)
 Save the current contents of an arena to a file.
 

Detailed Description

Arena snapshot serialization and deserialization.

Author
Toonsa
Date
2025

This file provides I/O utilities for saving and loading arena memory snapshots to and from binary files. It enables serialization of the arena's buffer content for persistence or debugging purposes.

Functions included:

Features:

This functionality is particularly useful for:

Note
Compatibility is not guaranteed across platforms or architectures due to possible differences in structure alignment or endianness.

Definition in file arena_io.c.

Function Documentation

◆ arena_load_from_file()

bool arena_load_from_file ( t_arena *  arena,
const char *  path 
)

Load arena contents from a snapshot file.

This function reads a snapshot file created with arena_save_to_file() and writes the contents into the provided arena buffer.

The arena must have been initialized with a buffer large enough to hold the restored data. The arena's internal offset will be updated accordingly.

Parameters
arenaPointer to the arena to restore.
pathPath to the snapshot file.
Returns
true on success, false on failure.

Definition at line 255 of file arena_io.c.

256{
257 if (!arena || !path)
258 return false;
259
260 if (!atomic_load_explicit(&arena->owns_buffer, memory_order_acquire))
261 return false;
262
263 FILE* f = fopen(path, "rb");
264 if (!f)
265 return false;
266
267 t_arena_snapshot_header header;
268 bool ok = fread(&header, sizeof(header), 1, f) == 1 && strncmp(header.magic, ARENA_SNAPSHOT_MAGIC, 9) == 0 &&
269 header.version == ARENA_SNAPSHOT_VERSION && header.used <= arena->size &&
270 fread(arena->buffer, 1, header.used, f) == header.used;
271
272 if (ok)
273 arena->offset = header.used;
274
275 fclose(f);
276 return ok;
277}
#define ARENA_SNAPSHOT_VERSION
Version number of the snapshot file format.
Definition arena_io.h:37
#define ARENA_SNAPSHOT_MAGIC
Magic string identifying a valid arena snapshot file.
Definition arena_io.h:34

References ARENA_SNAPSHOT_MAGIC, and ARENA_SNAPSHOT_VERSION.

◆ arena_save_to_file()

bool arena_save_to_file ( const t_arena *  arena,
const char *  path 
)

Save the current contents of an arena to a file.

This function writes a snapshot of the arena to the specified file path. The snapshot includes a header and all memory up to arena->offset.

Parameters
arenaPointer to the arena to save.
pathPath to the output file.
Returns
true on success, false on failure.

Definition at line 161 of file arena_io.c.

162{
163 if (!arena || !path)
164 return false;
165
166 if (!atomic_load_explicit(&arena->owns_buffer, memory_order_acquire))
167 return false;
168
169 ARENA_LOCK((t_arena*) arena);
170 size_t used = arena->offset;
171 const void* buf = arena->buffer;
172 ARENA_UNLOCK((t_arena*) arena);
173
174 t_arena_snapshot_header header = {.magic = ARENA_SNAPSHOT_MAGIC, .version = ARENA_SNAPSHOT_VERSION, .used = used};
175
176 FILE* f = fopen(path, "wb");
177 if (!f)
178 return false;
179
180 bool ok = fwrite(&header, sizeof(header), 1, f) == 1 && fwrite(buf, 1, used, f) == used;
181
182 fclose(f);
183 return ok;
184}
#define ARENA_UNLOCK(arena)
#define ARENA_LOCK(arena)
Acquire the mutex lock if thread safety is enabled.
size_t used
Number of bytes used in the arena buffer.
Definition arena_io.h:2
The main memory arena structure used for fast allocation.

References ARENA_LOCK, ARENA_SNAPSHOT_MAGIC, ARENA_SNAPSHOT_VERSION, ARENA_UNLOCK, and used.