diff --git a/src/runtime/c/data.h b/src/runtime/c/data.h index 2b0e2d886..80e0742e9 100644 --- a/src/runtime/c/data.h +++ b/src/runtime/c/data.h @@ -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 diff --git a/src/runtime/c/db.cxx b/src/runtime/c/db.cxx index 35a0a6286..dd9ccd240 100644 --- a/src/runtime/c/db.cxx +++ b/src/runtime/c/db.cxx @@ -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; } diff --git a/src/runtime/c/db.h b/src/runtime/c/db.h index 15e408c6b..457949958 100644 --- a/src/runtime/c/db.h +++ b/src/runtime/c/db.h @@ -86,6 +86,8 @@ public: current_db->set_root_internal(root.offset); } + static void sync(); + private: void init_state(size_t size); diff --git a/src/runtime/c/pgf.cxx b/src/runtime/c/pgf.cxx index 7d5e33a68..9eed30a02 100644 --- a/src/runtime/c/pgf.cxx +++ b/src/runtime/c/pgf.cxx @@ -33,9 +33,11 @@ PgfPGF *pgf_read(const char* fpath, PgfExn* err) } PgfReader rdr(&in); - rdr.read_pgf(pgf); + ref 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 root = DB::malloc(); - 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()->abstract.name)); } diff --git a/src/runtime/c/reader.cxx b/src/runtime/c/reader.cxx index 28248b297..eb591b5df 100644 --- a/src/runtime/c/reader.cxx +++ b/src/runtime/c/reader.cxx @@ -407,7 +407,7 @@ ref PgfReader::read_abscat() return abscat; } -void PgfReader::read_abstract(PgfAbstr* abstract) +void PgfReader::read_abstract(ref abstract) { abstract->name = read_name(); abstract->aflags = read_namespace(&PgfReader::read_flag); @@ -415,12 +415,16 @@ void PgfReader::read_abstract(PgfAbstr* abstract) abstract->cats = read_namespace(&PgfReader::read_abscat); } -void PgfReader::read_pgf(PgfPGFRoot *pgf) +ref PgfReader::read_pgf() { + ref pgf = DB::malloc(); + pgf->major_version = read_u16be(); pgf->minor_version = read_u16be(); pgf->gflags = read_namespace(&PgfReader::read_flag); - read_abstract(&pgf->abstract); + read_abstract(ref::from_ptr(&pgf->abstract)); + + return pgf; } diff --git a/src/runtime/c/reader.h b/src/runtime/c/reader.h index ef9ba2e87..8fccfd05f 100644 --- a/src/runtime/c/reader.h +++ b/src/runtime/c/reader.h @@ -65,9 +65,9 @@ public: ref read_absfun(); ref read_abscat(); - void read_abstract(PgfAbstr* abstract); + void read_abstract(ref abstract); - void read_pgf(PgfPGFRoot* pgf); + ref read_pgf(); private: std::istream *in;