more friendly PgfDB::malloc

This commit is contained in:
krangelov
2021-09-08 14:27:52 +02:00
parent 9d63c8a903
commit 44ee5718e9
5 changed files with 17 additions and 21 deletions

View File

@@ -69,13 +69,8 @@ public:
PGF_INTERNAL_DECL ~PgfDB();
template<class A>
static ref<A> malloc() {
return current_db->malloc_internal(sizeof(A));
}
template<class A>
static ref<A> malloc(size_t bytes) {
return current_db->malloc_internal(bytes);
static ref<A> malloc(size_t extra_bytes=0) {
return current_db->malloc_internal(sizeof(A)+extra_bytes);
}
template<class A>

View File

@@ -111,7 +111,7 @@ PgfType PgfDBMarshaller::match_type(PgfUnmarshaller *u, PgfType ty)
PgfExpr PgfDBUnmarshaller::eabs(PgfBindType bind_type, PgfText *name, PgfExpr body)
{
ref<PgfExprAbs> eabs =
PgfDB::malloc<PgfExprAbs>(sizeof(PgfExprAbs)+name->size+1);
PgfDB::malloc<PgfExprAbs>(name->size+1);
eabs->bind_type = bind_type;
eabs->body = m->match_expr(this, body);
memcpy(&eabs->name, name, sizeof(PgfText)+name->size+1);
@@ -143,7 +143,7 @@ PgfExpr PgfDBUnmarshaller::emeta(PgfMetaId meta_id)
PgfExpr PgfDBUnmarshaller::efun(PgfText *name)
{
ref<PgfExprFun> efun =
PgfDB::malloc<PgfExprFun>(sizeof(PgfExprFun)+name->size+1);
PgfDB::malloc<PgfExprFun>(name->size+1);
memcpy(&efun->name, name, sizeof(PgfText)+name->size+1);
return ref<PgfExprFun>::tagged(efun);
}
@@ -173,7 +173,7 @@ PgfExpr PgfDBUnmarshaller::eimplarg(PgfExpr expr)
PgfLiteral PgfDBUnmarshaller::lint(size_t size, uintmax_t *val)
{
ref<PgfLiteralInt> lit_int =
PgfDB::malloc<PgfLiteralInt>(sizeof(PgfLiteralInt)+size*sizeof(uintmax_t));
PgfDB::malloc<PgfLiteralInt>(size*sizeof(uintmax_t));
lit_int->size = size;
memcpy(&lit_int->val, val, size*sizeof(uintmax_t));
return ref<PgfLiteralInt>::tagged(lit_int);
@@ -189,7 +189,7 @@ PgfLiteral PgfDBUnmarshaller::lflt(double val)
PgfLiteral PgfDBUnmarshaller::lstr(PgfText *val)
{
ref<PgfLiteralStr> lit_str =
PgfDB::malloc<PgfLiteralStr>(sizeof(PgfLiteralStr)+val->size+1);
PgfDB::malloc<PgfLiteralStr>(val->size+1);
memcpy(&lit_str->val, val, sizeof(PgfText)+val->size+1);
return ref<PgfLiteralStr>::tagged(lit_str);
}
@@ -199,13 +199,13 @@ PgfType PgfDBUnmarshaller::dtyp(int n_hypos, PgfTypeHypo *hypos,
int n_exprs, PgfExpr *exprs)
{
ref<PgfDTyp> ty =
PgfDB::malloc<PgfDTyp>(sizeof(PgfDTyp)+cat->size+1);
PgfDB::malloc<PgfDTyp>(cat->size+1);
memcpy(&ty->name, cat, sizeof(PgfText)+cat->size+1);
ty->hypos = vector_new<PgfHypo>(n_hypos);
for (size_t i = 0; i < n_hypos; i++) {
ref<PgfHypo> hypo = vector_elem(ty->hypos,i);
hypo->bind_type = hypos[i].bind_type;
hypo->cid = PgfDB::malloc<PgfText>(sizeof(PgfText)+hypos[i].cid->size+1);
hypo->cid = PgfDB::malloc<PgfText>(hypos[i].cid->size+1);
memcpy(hypo->cid, hypos[i].cid, sizeof(PgfText)+hypos[i].cid->size+1);
hypo->type = m->match_type(this, hypos[i].type);
}

View File

@@ -118,7 +118,7 @@ PgfDB *pgf_read_ngf(const char *fpath,
if (PgfDB::get_revision(&master) == 0) {
is_new = true;
ref<PgfPGF> pgf = PgfDB::malloc<PgfPGF>(sizeof(PgfPGF)+master.size+1);
ref<PgfPGF> pgf = PgfDB::malloc<PgfPGF>(master.size+1);
pgf->major_version = 2;
pgf->minor_version = 0;
pgf->gflags = 0;
@@ -445,7 +445,7 @@ PgfRevision pgf_clone_revision(PgfDB *db, PgfRevision revision,
size_t name_size =
(name == NULL) ? pgf->name.size : name->size;
ref<PgfPGF> new_pgf = PgfDB::malloc<PgfPGF>(sizeof(PgfPGF)+name_size+1);
ref<PgfPGF> new_pgf = PgfDB::malloc<PgfPGF>(name_size+1);
new_pgf->major_version = pgf->major_version;
new_pgf->minor_version = pgf->minor_version;
@@ -454,7 +454,7 @@ PgfRevision pgf_clone_revision(PgfDB *db, PgfRevision revision,
pgf->gflags->ref_count++;
new_pgf->abstract.name =
PgfDB::malloc<PgfText>(sizeof(PgfText)+pgf->abstract.name->size+1);
PgfDB::malloc<PgfText>(pgf->abstract.name->size+1);
memcpy(new_pgf->abstract.name, pgf->abstract.name, sizeof(PgfText)+pgf->abstract.name->size+1);
new_pgf->abstract.aflags = pgf->abstract.aflags;
@@ -524,7 +524,7 @@ void pgf_create_function(PgfDB *db, PgfRevision revision,
PgfDBUnmarshaller u(m);
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
ref<PgfAbsFun> absfun = PgfDB::malloc<PgfAbsFun>(sizeof(PgfAbsFun)+name->size+1);
ref<PgfAbsFun> absfun = PgfDB::malloc<PgfAbsFun>(name->size+1);
absfun->type = m->match_type(&u, ty);
absfun->arity = 0;
absfun->defns = 0;

View File

@@ -205,7 +205,7 @@ PgfLiteral PgfReader::read_literal()
}
case PgfLiteralInt::tag: {
ref<PgfLiteralInt> lit_int =
PgfDB::malloc<PgfLiteralInt>(sizeof(PgfLiteralInt)+sizeof(uintmax_t));
PgfDB::malloc<PgfLiteralInt>(sizeof(uintmax_t));
lit_int->size = 1;
lit_int->val[0] = read_int();
lit = ref<PgfLiteralInt>::tagged(lit_int);
@@ -428,7 +428,7 @@ void PgfReader::read_abstract(ref<PgfAbstr> abstract)
ref<PgfPGF> PgfReader::read_pgf()
{
ref<PgfPGF> pgf = PgfDB::malloc<PgfPGF>(sizeof(PgfPGF)+master.size+1);
ref<PgfPGF> pgf = PgfDB::malloc<PgfPGF>(master.size+1);
pgf->major_version = read_u16be();
pgf->minor_version = read_u16be();

View File

@@ -10,7 +10,7 @@ struct PgfVector {
template <class A> inline
ref<PgfVector<A>> vector_new(size_t len)
{
ref<PgfVector<A>> res = PgfDB::malloc<PgfVector<A>>(sizeof(PgfVector<A>)+len*sizeof(A));
ref<PgfVector<A>> res = PgfDB::malloc<PgfVector<A>>(len*sizeof(A));
res->len = len;
return res;
}
@@ -18,7 +18,8 @@ ref<PgfVector<A>> vector_new(size_t len)
template <class C, class A> inline
ref<C> vector_new(PgfVector<A> C::* field, size_t len)
{
ref<C> res = PgfDB::malloc<C>(((size_t) &(((C*) NULL)->*field))+sizeof(PgfVector<A>)+len*sizeof(A));
ptrdiff_t offset = (ptrdiff_t) &(((C*) NULL)->*field);
ref<C> res = PgfDB::malloc<PgfVector<A>>(offset+len*sizeof(A)).as_object()-offset;
(res->*field).len = len;
return res;
}