memory_arena
Loading...
Searching...
No Matches
Data Structures | Macros | Functions | Variables
arena_io.h File Reference

Arena snapshot input/output operations (save/load). More...

#include "arena.h"
#include <stdbool.h>
+ Include dependency graph for arena_io.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  s_arena_snapshot_header
 Header structure prepended to saved arena snapshots. More...
 

Macros

#define ARENA_SNAPSHOT_MAGIC   "ARENASNAP"
 Magic string identifying a valid arena snapshot file.
 
#define ARENA_SNAPSHOT_VERSION   1
 Version number of the snapshot file format.
 

Functions

struct s_arena_snapshot_header __attribute__ ((packed)) t_arena_snapshot_header
 Header structure prepended to saved arena snapshots.
 
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.
 

Variables

char magic [9]
 Magic string ("ARENASNAP\0")
 
size_t used
 Number of bytes used in the arena buffer.
 
uint32_t version
 Snapshot file format version.
 

Detailed Description

Arena snapshot input/output operations (save/load).

Author
Toonsa
Date
2025

This header declares functions to serialize and deserialize the memory contents of an arena. These operations allow arenas to be saved to disk and restored later, enabling features such as state persistence, debugging snapshots, or offline replay.

The snapshot includes a compact header (t_arena_snapshot_header) followed by the raw used memory buffer of the arena.

Note
These functions are meant for single-threaded or externally synchronized use. They assume the arena is not being mutated during I/O.

Definition in file arena_io.h.


Data Structure Documentation

◆ s_arena_snapshot_header

struct s_arena_snapshot_header

Header structure prepended to saved arena snapshots.

This packed structure contains metadata about a saved arena buffer, including:

  • A magic string for format verification.
  • A version field for compatibility checks.
  • The number of bytes used in the saved arena.

This header is written/read before the actual memory contents of the arena.

Definition at line 53 of file arena_io.h.

Data Fields
char magic[9] Magic string ("ARENASNAP\0")
size_t used Number of bytes used in the arena buffer.
uint32_t version Snapshot file format version.

Macro Definition Documentation

◆ ARENA_SNAPSHOT_MAGIC

#define ARENA_SNAPSHOT_MAGIC   "ARENASNAP"

Magic string identifying a valid arena snapshot file.

Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_io.c.

Definition at line 34 of file arena_io.h.

◆ ARENA_SNAPSHOT_VERSION

#define ARENA_SNAPSHOT_VERSION   1

Version number of the snapshot file format.

Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_io.c.

Definition at line 37 of file arena_io.h.

Function Documentation

◆ __attribute__()

struct s_arena_snapshot_header __attribute__ ( (packed)  )

Header structure prepended to saved arena snapshots.

This packed structure contains metadata about a saved arena buffer, including:

  • A magic string for format verification.
  • A version field for compatibility checks.
  • The number of bytes used in the saved arena.

This header is written/read before the actual memory contents of the arena.

◆ 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.
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_io.c.

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.
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_io.c.

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.

Variable Documentation

◆ magic

char magic[9]

Magic string ("ARENASNAP\0")

Definition at line 0 of file arena_io.h.

◆ used

size_t used

◆ version

uint32_t version

Snapshot file format version.

Definition at line 1 of file arena_io.h.