You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.1 KiB
C

#pragma once
#include "md.h"
typedef struct
{
1 year ago
b8 failed;
String8 why;
} SerError;
typedef struct
{
1 year ago
u8 *data; // set to 0 to dry run and get maximum size. max doesn't matter in this case
u64 cur;
u64 max;
1 year ago
Arena *arena; // allocate everything new on this, so that if serialization fails allocations can be undone
// Serializing should never allocate. So this can be null when you serialize
1 year ago
int version;
SerError cur_error;
Arena *error_arena; // all error messages are allocated here
1 year ago
b8 serializing;
} SerState;
void ser_bytes(SerState *ser, u8 *bytes, u64 bytes_size)
{
1 year ago
if (!ser->data && !ser->serializing)
{
1 year ago
ser->cur_error = (SerError){.failed = true, .why = S8Lit("Deserializing but the data is null")};
}
1 year ago
if (!ser->cur_error.failed)
{
if (ser->data)
{
// maximum doesn't matter unless writing to data
if (ser->cur + bytes_size > ser->max)
{
ser->cur_error = (SerError){.failed = true, .why = S8Lit("Too big bro")};
}
else
{
if (ser->serializing)
{
memcpy(ser->data + ser->cur, bytes, bytes_size);
}
else
{
memcpy(bytes, ser->data + ser->cur, bytes_size);
}
}
}
1 year ago
ser->cur += bytes_size;
}
}
1 year ago
#define SER_MAKE_FOR_TYPE(type) \
void ser_##type(SerState *ser, type *into) \
{ \
ser_bytes(ser, (u8 *)into, sizeof(*into)); \
}
SER_MAKE_FOR_TYPE(int);
SER_MAKE_FOR_TYPE(u64);
// Embeds the string into the serialized binary. When deserializing copies the
// deserialized data onto a newly allocated buffer
void ser_String8(SerState *ser, String8 *s, Arena *allocate_onto)
{
1 year ago
ser_u64(ser, &s->size);
if (ser->serializing)
{
ser_bytes(ser, s->str, s->size);
}
else
{
s->str = ArenaPush(allocate_onto, s->size);
ser_bytes(ser, s->str, s->size);
}
}