1
0
forked from GitHub/gf-core

make sure that changes in the database are always flushed

This commit is contained in:
krangelov
2021-08-05 20:05:29 +02:00
parent 2d3aac5aa1
commit 36e87668e0
6 changed files with 28 additions and 21 deletions

View File

@@ -120,13 +120,9 @@ struct PGF_INTERNAL_DECL PgfPGFRoot {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
struct PgfPGF : public PgfPGFRoot {
DB db;
PGF_INTERNAL_DECL PgfPGF(const char* fpath) : db(fpath) {};
struct PgfPGF : DB {
PGF_INTERNAL_DECL PgfPGF(const char* fpath) : DB(fpath) {};
PGF_INTERNAL_DECL ~PgfPGF() {};
PGF_INTERNAL_DECL void set_root();
};
#pragma GCC diagnostic pop

View File

@@ -309,6 +309,16 @@ DB::~DB() {
close(fd);
}
void DB::sync()
{
size_t size =
current_db->ms->top + size + sizeof(size_t);
int res = msync((void *) current_db->ms, size, MS_SYNC | MS_INVALIDATE);
if (res != 0)
throw std::system_error(errno, std::generic_category());
}
moffset DB::get_root_internal() {
return ms->root_offset;
}

View File

@@ -86,6 +86,8 @@ public:
current_db->set_root_internal(root.offset);
}
static void sync();
private:
void init_state(size_t size);

View File

@@ -33,9 +33,11 @@ PgfPGF *pgf_read(const char* fpath, PgfExn* err)
}
PgfReader rdr(&in);
rdr.read_pgf(pgf);
ref<PgfPGFRoot> pgf_root = rdr.read_pgf();
pgf->set_root();
pgf->set_root(pgf_root);
DB::sync();
}
return pgf;
@@ -53,13 +55,6 @@ PgfPGF *pgf_read(const char* fpath, PgfExn* err)
return NULL;
}
void PgfPGF::set_root() {
ref<PgfPGFRoot> root = DB::malloc<PgfPGFRoot>();
root->major_version = major_version;
root->minor_version = minor_version;
DB::set_root(root);
}
PGF_API
void pgf_free(PgfPGF *pgf)
{
@@ -69,5 +64,5 @@ void pgf_free(PgfPGF *pgf)
PGF_API
PgfText *pgf_abstract_name(PgfPGF* pgf)
{
return textdup(&(*pgf->abstract.name));
return textdup(&(*pgf->get_root<PgfPGFRoot>()->abstract.name));
}

View File

@@ -407,7 +407,7 @@ ref<PgfAbsCat> PgfReader::read_abscat()
return abscat;
}
void PgfReader::read_abstract(PgfAbstr* abstract)
void PgfReader::read_abstract(ref<PgfAbstr> abstract)
{
abstract->name = read_name();
abstract->aflags = read_namespace<PgfFlag>(&PgfReader::read_flag);
@@ -415,12 +415,16 @@ void PgfReader::read_abstract(PgfAbstr* abstract)
abstract->cats = read_namespace<PgfAbsCat>(&PgfReader::read_abscat);
}
void PgfReader::read_pgf(PgfPGFRoot *pgf)
ref<PgfPGFRoot> PgfReader::read_pgf()
{
ref<PgfPGFRoot> pgf = DB::malloc<PgfPGFRoot>();
pgf->major_version = read_u16be();
pgf->minor_version = read_u16be();
pgf->gflags = read_namespace<PgfFlag>(&PgfReader::read_flag);
read_abstract(&pgf->abstract);
read_abstract(ref<PgfAbstr>::from_ptr(&pgf->abstract));
return pgf;
}

View File

@@ -65,9 +65,9 @@ public:
ref<PgfAbsFun> read_absfun();
ref<PgfAbsCat> read_abscat();
void read_abstract(PgfAbstr* abstract);
void read_abstract(ref<PgfAbstr> abstract);
void read_pgf(PgfPGFRoot* pgf);
ref<PgfPGFRoot> read_pgf();
private:
std::istream *in;