From 44ee5718e985cc932e2c3829bd5cb10c599b604f Mon Sep 17 00:00:00 2001 From: krangelov Date: Wed, 8 Sep 2021 14:27:52 +0200 Subject: [PATCH] more friendly PgfDB::malloc --- src/runtime/c/pgf/db.h | 9 ++------- src/runtime/c/pgf/expr.cxx | 12 ++++++------ src/runtime/c/pgf/pgf.cxx | 8 ++++---- src/runtime/c/pgf/reader.cxx | 4 ++-- src/runtime/c/pgf/vector.h | 5 +++-- 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/runtime/c/pgf/db.h b/src/runtime/c/pgf/db.h index d8acb856b..2dbca1c75 100644 --- a/src/runtime/c/pgf/db.h +++ b/src/runtime/c/pgf/db.h @@ -69,13 +69,8 @@ public: PGF_INTERNAL_DECL ~PgfDB(); template - static ref malloc() { - return current_db->malloc_internal(sizeof(A)); - } - - template - static ref malloc(size_t bytes) { - return current_db->malloc_internal(bytes); + static ref malloc(size_t extra_bytes=0) { + return current_db->malloc_internal(sizeof(A)+extra_bytes); } template diff --git a/src/runtime/c/pgf/expr.cxx b/src/runtime/c/pgf/expr.cxx index 6692ee50f..79244642b 100644 --- a/src/runtime/c/pgf/expr.cxx +++ b/src/runtime/c/pgf/expr.cxx @@ -111,7 +111,7 @@ PgfType PgfDBMarshaller::match_type(PgfUnmarshaller *u, PgfType ty) PgfExpr PgfDBUnmarshaller::eabs(PgfBindType bind_type, PgfText *name, PgfExpr body) { ref eabs = - PgfDB::malloc(sizeof(PgfExprAbs)+name->size+1); + PgfDB::malloc(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 efun = - PgfDB::malloc(sizeof(PgfExprFun)+name->size+1); + PgfDB::malloc(name->size+1); memcpy(&efun->name, name, sizeof(PgfText)+name->size+1); return ref::tagged(efun); } @@ -173,7 +173,7 @@ PgfExpr PgfDBUnmarshaller::eimplarg(PgfExpr expr) PgfLiteral PgfDBUnmarshaller::lint(size_t size, uintmax_t *val) { ref lit_int = - PgfDB::malloc(sizeof(PgfLiteralInt)+size*sizeof(uintmax_t)); + PgfDB::malloc(size*sizeof(uintmax_t)); lit_int->size = size; memcpy(&lit_int->val, val, size*sizeof(uintmax_t)); return ref::tagged(lit_int); @@ -189,7 +189,7 @@ PgfLiteral PgfDBUnmarshaller::lflt(double val) PgfLiteral PgfDBUnmarshaller::lstr(PgfText *val) { ref lit_str = - PgfDB::malloc(sizeof(PgfLiteralStr)+val->size+1); + PgfDB::malloc(val->size+1); memcpy(&lit_str->val, val, sizeof(PgfText)+val->size+1); return ref::tagged(lit_str); } @@ -199,13 +199,13 @@ PgfType PgfDBUnmarshaller::dtyp(int n_hypos, PgfTypeHypo *hypos, int n_exprs, PgfExpr *exprs) { ref ty = - PgfDB::malloc(sizeof(PgfDTyp)+cat->size+1); + PgfDB::malloc(cat->size+1); memcpy(&ty->name, cat, sizeof(PgfText)+cat->size+1); ty->hypos = vector_new(n_hypos); for (size_t i = 0; i < n_hypos; i++) { ref hypo = vector_elem(ty->hypos,i); hypo->bind_type = hypos[i].bind_type; - hypo->cid = PgfDB::malloc(sizeof(PgfText)+hypos[i].cid->size+1); + hypo->cid = PgfDB::malloc(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); } diff --git a/src/runtime/c/pgf/pgf.cxx b/src/runtime/c/pgf/pgf.cxx index bb9fbc5fd..32ed0d4af 100644 --- a/src/runtime/c/pgf/pgf.cxx +++ b/src/runtime/c/pgf/pgf.cxx @@ -118,7 +118,7 @@ PgfDB *pgf_read_ngf(const char *fpath, if (PgfDB::get_revision(&master) == 0) { is_new = true; - ref pgf = PgfDB::malloc(sizeof(PgfPGF)+master.size+1); + ref pgf = PgfDB::malloc(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 new_pgf = PgfDB::malloc(sizeof(PgfPGF)+name_size+1); + ref new_pgf = PgfDB::malloc(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(sizeof(PgfText)+pgf->abstract.name->size+1); + PgfDB::malloc(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 pgf = PgfDB::revision2pgf(revision); - ref absfun = PgfDB::malloc(sizeof(PgfAbsFun)+name->size+1); + ref absfun = PgfDB::malloc(name->size+1); absfun->type = m->match_type(&u, ty); absfun->arity = 0; absfun->defns = 0; diff --git a/src/runtime/c/pgf/reader.cxx b/src/runtime/c/pgf/reader.cxx index 4fe2579e2..2a378ea64 100644 --- a/src/runtime/c/pgf/reader.cxx +++ b/src/runtime/c/pgf/reader.cxx @@ -205,7 +205,7 @@ PgfLiteral PgfReader::read_literal() } case PgfLiteralInt::tag: { ref lit_int = - PgfDB::malloc(sizeof(PgfLiteralInt)+sizeof(uintmax_t)); + PgfDB::malloc(sizeof(uintmax_t)); lit_int->size = 1; lit_int->val[0] = read_int(); lit = ref::tagged(lit_int); @@ -428,7 +428,7 @@ void PgfReader::read_abstract(ref abstract) ref PgfReader::read_pgf() { - ref pgf = PgfDB::malloc(sizeof(PgfPGF)+master.size+1); + ref pgf = PgfDB::malloc(master.size+1); pgf->major_version = read_u16be(); pgf->minor_version = read_u16be(); diff --git a/src/runtime/c/pgf/vector.h b/src/runtime/c/pgf/vector.h index 261d69020..67621e2ca 100644 --- a/src/runtime/c/pgf/vector.h +++ b/src/runtime/c/pgf/vector.h @@ -10,7 +10,7 @@ struct PgfVector { template inline ref> vector_new(size_t len) { - ref> res = PgfDB::malloc>(sizeof(PgfVector)+len*sizeof(A)); + ref> res = PgfDB::malloc>(len*sizeof(A)); res->len = len; return res; } @@ -18,7 +18,8 @@ ref> vector_new(size_t len) template inline ref vector_new(PgfVector C::* field, size_t len) { - ref res = PgfDB::malloc(((size_t) &(((C*) NULL)->*field))+sizeof(PgfVector)+len*sizeof(A)); + ptrdiff_t offset = (ptrdiff_t) &(((C*) NULL)->*field); + ref res = PgfDB::malloc>(offset+len*sizeof(A)).as_object()-offset; (res->*field).len = len; return res; }