From 7cd6dffb7e3cd5e9bb93d5398c5902fc36537b7e Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Mon, 25 Apr 2022 19:11:34 +0900 Subject: [PATCH 1/6] Pass pcc_context_t instead of pcc_auxil_t in many places This is preparation for the next change reducing malloc. Signed-off-by: Masatake YAMATO --- src/packcc.c | 188 ++++++++++++++++++++++++++------------------------- 1 file changed, 97 insertions(+), 91 deletions(-) diff --git a/src/packcc.c b/src/packcc.c index 601688c..4ee05a2 100644 --- a/src/packcc.c +++ b/src/packcc.c @@ -3873,26 +3873,27 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" ); - stream__puts( + stream__printf( &sstream, "MARK_USED_FUNC\n" - "static pcc_thunk_chunk_t *pcc_thunk_chunk__create(pcc_auxil_t auxil) {\n" - " pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_chunk_t));\n" - " pcc_value_table__init(auxil, &chunk->values);\n" - " pcc_capture_table__init(auxil, &chunk->capts);\n" - " pcc_thunk_array__init(auxil, &chunk->thunks);\n" + "static pcc_thunk_chunk_t *pcc_thunk_chunk__create(%s_context_t *ctx) {\n" + " pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)PCC_MALLOC(ctx->auxil, sizeof(pcc_thunk_chunk_t));\n" + " pcc_value_table__init(ctx->auxil, &chunk->values);\n" + " pcc_capture_table__init(ctx->auxil, &chunk->capts);\n" + " pcc_thunk_array__init(ctx->auxil, &chunk->thunks);\n" " chunk->pos = 0;\n" " return chunk;\n" "}\n" "\n" - "static void pcc_thunk_chunk__destroy(pcc_auxil_t auxil, pcc_thunk_chunk_t *chunk) {\n" + "static void pcc_thunk_chunk__destroy(%s_context_t *ctx, pcc_thunk_chunk_t *chunk) {\n" " if (chunk == NULL) return;\n" - " pcc_thunk_array__term(auxil, &chunk->thunks);\n" - " pcc_capture_table__term(auxil, &chunk->capts);\n" - " pcc_value_table__term(auxil, &chunk->values);\n" - " PCC_FREE(auxil, chunk);\n" + " pcc_thunk_array__term(ctx->auxil, &chunk->thunks);\n" + " pcc_capture_table__term(ctx->auxil, &chunk->capts);\n" + " pcc_value_table__term(ctx->auxil, &chunk->values);\n" + " PCC_FREE(ctx->auxil, chunk);\n" "}\n" - "\n" + "\n", + get_prefix(ctx), get_prefix(ctx) ); stream__puts( &sstream, @@ -3950,32 +3951,33 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" ); - stream__puts( + stream__printf( &sstream, - "static pcc_lr_head_t *pcc_lr_head__create(pcc_auxil_t auxil, pcc_rule_t rule) {\n" - " pcc_lr_head_t *const head = (pcc_lr_head_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_head_t));\n" + "static pcc_lr_head_t *pcc_lr_head__create(%s_context_t *ctx, pcc_rule_t rule) {\n" + " pcc_lr_head_t *const head = (pcc_lr_head_t *)PCC_MALLOC(ctx->auxil, sizeof(pcc_lr_head_t));\n" " head->rule = rule;\n" - " pcc_rule_set__init(auxil, &head->invol);\n" - " pcc_rule_set__init(auxil, &head->eval);\n" + " pcc_rule_set__init(ctx->auxil, &head->invol);\n" + " pcc_rule_set__init(ctx->auxil, &head->eval);\n" " head->hold = NULL;\n" " return head;\n" "}\n" "\n" - "static void pcc_lr_head__destroy(pcc_auxil_t auxil, pcc_lr_head_t *head) {\n" + "static void pcc_lr_head__destroy(%s_context_t *ctx, pcc_lr_head_t *head) {\n" " if (head == NULL) return;\n" - " pcc_lr_head__destroy(auxil, head->hold);\n" - " pcc_rule_set__term(auxil, &head->eval);\n" - " pcc_rule_set__term(auxil, &head->invol);\n" - " PCC_FREE(auxil, head);\n" + " pcc_lr_head__destroy(ctx, head->hold);\n" + " pcc_rule_set__term(ctx->auxil, &head->eval);\n" + " pcc_rule_set__term(ctx->auxil, &head->invol);\n" + " PCC_FREE(ctx->auxil, head);\n" "}\n" - "\n" + "\n", + get_prefix(ctx), get_prefix(ctx) ); - stream__puts( + stream__printf( &sstream, "static void pcc_lr_entry__destroy(pcc_auxil_t auxil, pcc_lr_entry_t *lr);\n" "\n" - "static pcc_lr_answer_t *pcc_lr_answer__create(pcc_auxil_t auxil, pcc_lr_answer_type_t type, size_t pos) {\n" - " pcc_lr_answer_t *answer = (pcc_lr_answer_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_answer_t));\n" + "static pcc_lr_answer_t *pcc_lr_answer__create(%s_context_t *ctx, pcc_lr_answer_type_t type, size_t pos) {\n" + " pcc_lr_answer_t *answer = (pcc_lr_answer_t *)PCC_MALLOC(ctx->auxil, sizeof(pcc_lr_answer_t));\n" " answer->type = type;\n" " answer->pos = pos;\n" " answer->hold = NULL;\n" @@ -3987,14 +3989,14 @@ static bool_t generate(context_t *ctx) { " answer->data.chunk = NULL;\n" " break;\n" " default: /* unknown */\n" - " PCC_FREE(auxil, answer);\n" + " PCC_FREE(ctx->auxil, answer);\n" " answer = NULL;\n" " }\n" " return answer;\n" "}\n" "\n" - "static void pcc_lr_answer__set_chunk(pcc_auxil_t auxil, pcc_lr_answer_t *answer, pcc_thunk_chunk_t *chunk) {\n" - " pcc_lr_answer_t *const a = pcc_lr_answer__create(auxil, answer->type, answer->pos);\n" + "static void pcc_lr_answer__set_chunk(%s_context_t *ctx, pcc_lr_answer_t *answer, pcc_thunk_chunk_t *chunk) {\n" + " pcc_lr_answer_t *const a = pcc_lr_answer__create(ctx, answer->type, answer->pos);\n" " switch (answer->type) {\n" " case PCC_LR_ANSWER_LR:\n" " a->data.lr = answer->data.lr;\n" @@ -4011,26 +4013,27 @@ static bool_t generate(context_t *ctx) { " answer->data.chunk = chunk;\n" "}\n" "\n" - "static void pcc_lr_answer__destroy(pcc_auxil_t auxil, pcc_lr_answer_t *answer) {\n" + "static void pcc_lr_answer__destroy(%s_context_t *ctx, pcc_lr_answer_t *answer) {\n" " while (answer != NULL) {\n" " pcc_lr_answer_t *const a = answer->hold;\n" " switch (answer->type) {\n" " case PCC_LR_ANSWER_LR:\n" - " pcc_lr_entry__destroy(auxil, answer->data.lr);\n" + " pcc_lr_entry__destroy(ctx->auxil, answer->data.lr);\n" " break;\n" " case PCC_LR_ANSWER_CHUNK:\n" - " pcc_thunk_chunk__destroy(auxil, answer->data.chunk);\n" + " pcc_thunk_chunk__destroy(ctx, answer->data.chunk);\n" " break;\n" " default: /* unknown */\n" " break;\n" " }\n" - " PCC_FREE(auxil, answer);\n" + " PCC_FREE(ctx->auxil, answer);\n" " answer = a;\n" " }\n" "}\n" - "\n" + "\n", + get_prefix(ctx), get_prefix(ctx), get_prefix(ctx) ); - stream__puts( + stream__printf( &sstream, "static void pcc_lr_memo_map__init(pcc_auxil_t auxil, pcc_lr_memo_map_t *map) {\n" " map->len = 0;\n" @@ -4046,10 +4049,10 @@ static bool_t generate(context_t *ctx) { " return PCC_VOID_VALUE;\n" "}\n" "\n" - "static void pcc_lr_memo_map__put(pcc_auxil_t auxil, pcc_lr_memo_map_t *map, pcc_rule_t rule, pcc_lr_answer_t *answer) {\n" - " const size_t i = pcc_lr_memo_map__index(auxil, map, rule);\n" + "static void pcc_lr_memo_map__put(%s_context_t *ctx, pcc_lr_memo_map_t *map, pcc_rule_t rule, pcc_lr_answer_t *answer) {\n" + " const size_t i = pcc_lr_memo_map__index(ctx->auxil, map, rule);\n" " if (i != PCC_VOID_VALUE) {\n" - " pcc_lr_answer__destroy(auxil, map->buf[i].answer);\n" + " pcc_lr_answer__destroy(ctx, map->buf[i].answer);\n" " map->buf[i].answer = answer;\n" " }\n" " else {\n" @@ -4059,7 +4062,7 @@ static bool_t generate(context_t *ctx) { " if (m == 0) m = PCC_ARRAYSIZE;\n" " while (m < n && m != 0) m <<= 1;\n" " if (m == 0) m = n;\n" - " map->buf = (pcc_lr_memo_t *)PCC_REALLOC(auxil, map->buf, sizeof(pcc_lr_memo_t) * m);\n" + " map->buf = (pcc_lr_memo_t *)PCC_REALLOC(ctx->auxil, map->buf, sizeof(pcc_lr_memo_t) * m);\n" " map->max = m;\n" " }\n" " map->buf[map->len].rule = rule;\n" @@ -4073,16 +4076,17 @@ static bool_t generate(context_t *ctx) { " return (i != PCC_VOID_VALUE) ? map->buf[i].answer : NULL;\n" "}\n" "\n" - "static void pcc_lr_memo_map__term(pcc_auxil_t auxil, pcc_lr_memo_map_t *map) {\n" + "static void pcc_lr_memo_map__term(%s_context_t *ctx, pcc_lr_memo_map_t *map) {\n" " while (map->len > 0) {\n" " map->len--;\n" - " pcc_lr_answer__destroy(auxil, map->buf[map->len].answer);\n" + " pcc_lr_answer__destroy(ctx, map->buf[map->len].answer);\n" " }\n" - " PCC_FREE(auxil, map->buf);\n" + " PCC_FREE(ctx->auxil, map->buf);\n" "}\n" - "\n" + "\n", + get_prefix(ctx), get_prefix(ctx) ); - stream__puts( + stream__printf( &sstream, "static pcc_lr_table_entry_t *pcc_lr_table_entry__create(pcc_auxil_t auxil) {\n" " pcc_lr_table_entry_t *const entry = (pcc_lr_table_entry_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_table_entry_t));\n" @@ -4093,16 +4097,17 @@ static bool_t generate(context_t *ctx) { " return entry;\n" "}\n" "\n" - "static void pcc_lr_table_entry__destroy(pcc_auxil_t auxil, pcc_lr_table_entry_t *entry) {\n" + "static void pcc_lr_table_entry__destroy(%s_context_t *ctx, pcc_lr_table_entry_t *entry) {\n" " if (entry == NULL) return;\n" - " pcc_lr_head__destroy(auxil, entry->hold_h);\n" - " pcc_lr_answer__destroy(auxil, entry->hold_a);\n" - " pcc_lr_memo_map__term(auxil, &entry->memos);\n" - " PCC_FREE(auxil, entry);\n" + " pcc_lr_head__destroy(ctx, entry->hold_h);\n" + " pcc_lr_answer__destroy(ctx, entry->hold_a);\n" + " pcc_lr_memo_map__term(ctx, &entry->memos);\n" + " PCC_FREE(ctx->auxil, entry);\n" "}\n" - "\n" + "\n", + get_prefix(ctx) ); - stream__puts( + stream__printf( &sstream, "static void pcc_lr_table__init(pcc_auxil_t auxil, pcc_lr_table_t *table) {\n" " table->ofs = 0;\n" @@ -4111,47 +4116,47 @@ static bool_t generate(context_t *ctx) { " table->buf = NULL;\n" "}\n" "\n" - "static void pcc_lr_table__resize(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t len) {\n" + "static void pcc_lr_table__resize(%s_context_t *ctx, pcc_lr_table_t *table, size_t len) {\n" " size_t i;\n" - " for (i = len; i < table->len; i++) pcc_lr_table_entry__destroy(auxil, table->buf[i]);\n" + " for (i = len; i < table->len; i++) pcc_lr_table_entry__destroy(ctx, table->buf[i]);\n" " if (table->max < len) {\n" " size_t m = table->max;\n" " if (m == 0) m = PCC_ARRAYSIZE;\n" " while (m < len && m != 0) m <<= 1;\n" " if (m == 0) m = len;\n" - " table->buf = (pcc_lr_table_entry_t **)PCC_REALLOC(auxil, table->buf, sizeof(pcc_lr_table_entry_t *) * m);\n" + " table->buf = (pcc_lr_table_entry_t **)PCC_REALLOC(ctx->auxil, table->buf, sizeof(pcc_lr_table_entry_t *) * m);\n" " table->max = m;\n" " }\n" " for (i = table->len; i < len; i++) table->buf[i] = NULL;\n" " table->len = len;\n" "}\n" "\n" - "static void pcc_lr_table__set_head(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {\n" + "static void pcc_lr_table__set_head(%s_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {\n" " index += table->ofs;\n" - " if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);\n" - " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);\n" + " if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);\n" + " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx->auxil);\n" " table->buf[index]->head = head;\n" "}\n" "\n" - "static void pcc_lr_table__hold_head(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {\n" + "static void pcc_lr_table__hold_head(%s_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {\n" " index += table->ofs;\n" - " if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);\n" - " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);\n" + " if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);\n" + " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx->auxil);\n" " head->hold = table->buf[index]->hold_h;\n" " table->buf[index]->hold_h = head;\n" "}\n" "\n" - "static void pcc_lr_table__set_answer(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_rule_t rule, pcc_lr_answer_t *answer) {\n" + "static void pcc_lr_table__set_answer(%s_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_rule_t rule, pcc_lr_answer_t *answer) {\n" " index += table->ofs;\n" - " if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);\n" - " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);\n" - " pcc_lr_memo_map__put(auxil, &table->buf[index]->memos, rule, answer);\n" + " if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);\n" + " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx->auxil);\n" + " pcc_lr_memo_map__put(ctx, &table->buf[index]->memos, rule, answer);\n" "}\n" "\n" - "static void pcc_lr_table__hold_answer(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_lr_answer_t *answer) {\n" + "static void pcc_lr_table__hold_answer(%s_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_lr_answer_t *answer) {\n" " index += table->ofs;\n" - " if (index >= table->len) pcc_lr_table__resize(auxil, table, index + 1);\n" - " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(auxil);\n" + " if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);\n" + " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx->auxil);\n" " answer->hold = table->buf[index]->hold_a;\n" " table->buf[index]->hold_a = answer;\n" "}\n" @@ -4168,10 +4173,10 @@ static bool_t generate(context_t *ctx) { " return pcc_lr_memo_map__get(auxil, &table->buf[index]->memos, rule);\n" "}\n" "\n" - "static void pcc_lr_table__shift(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t count) {\n" + "static void pcc_lr_table__shift(%s_context_t *ctx, pcc_lr_table_t *table, size_t count) {\n" " size_t i;\n" " if (count > table->len - table->ofs) count = table->len - table->ofs;\n" - " for (i = 0; i < count; i++) pcc_lr_table_entry__destroy(auxil, table->buf[table->ofs++]);\n" + " for (i = 0; i < count; i++) pcc_lr_table_entry__destroy(ctx, table->buf[table->ofs++]);\n" " if (table->ofs > (table->max >> 1)) {\n" " memmove(table->buf, table->buf + table->ofs, sizeof(pcc_lr_table_entry_t *) * (table->len - table->ofs));\n" " table->len -= table->ofs;\n" @@ -4179,14 +4184,15 @@ static bool_t generate(context_t *ctx) { " }\n" "}\n" "\n" - "static void pcc_lr_table__term(pcc_auxil_t auxil, pcc_lr_table_t *table) {\n" + "static void pcc_lr_table__term(%s_context_t *ctx, pcc_lr_table_t *table) {\n" " while (table->len > table->ofs) {\n" " table->len--;\n" - " pcc_lr_table_entry__destroy(auxil, table->buf[table->len]);\n" + " pcc_lr_table_entry__destroy(ctx, table->buf[table->len]);\n" " }\n" - " PCC_FREE(auxil, table->buf);\n" + " PCC_FREE(ctx->auxil, table->buf);\n" "}\n" - "\n" + "\n", + get_prefix(ctx), get_prefix(ctx), get_prefix(ctx), get_prefix(ctx), get_prefix(ctx), get_prefix(ctx), get_prefix(ctx) ); stream__puts( &sstream, @@ -4263,7 +4269,7 @@ static bool_t generate(context_t *ctx) { " if (ctx == NULL) return;\n" " pcc_thunk_array__term(ctx->auxil, &ctx->thunks);\n" " pcc_lr_stack__term(ctx->auxil, &ctx->lrstack);\n" - " pcc_lr_table__term(ctx->auxil, &ctx->lrtable);\n" + " pcc_lr_table__term(ctx, &ctx->lrtable);\n" " pcc_char_array__term(ctx->auxil, &ctx->buffer);\n" " PCC_FREE(ctx->auxil, ctx);\n" "}\n" @@ -4297,7 +4303,7 @@ static bool_t generate(context_t *ctx) { " memmove(ctx->buffer.buf, ctx->buffer.buf + ctx->cur, ctx->buffer.len - ctx->cur);\n" " ctx->buffer.len -= ctx->cur;\n" " ctx->pos += ctx->cur;\n" - " pcc_lr_table__shift(ctx->auxil, &ctx->lrtable, ctx->cur);\n" + " pcc_lr_table__shift(ctx, &ctx->lrtable, ctx->cur);\n" " ctx->cur = 0;\n" "}\n" "\n" @@ -4400,9 +4406,9 @@ static bool_t generate(context_t *ctx) { " else if (pcc_rule_set__remove(ctx->auxil, &h->eval, rule)) {\n" " b = PCC_FALSE;\n" " c = rule(ctx);\n" - " a = pcc_lr_answer__create(ctx->auxil, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur);\n" + " a = pcc_lr_answer__create(ctx, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur);\n" " a->data.chunk = c;\n" - " pcc_lr_table__hold_answer(ctx->auxil, &ctx->lrtable, p, a);\n" + " pcc_lr_table__hold_answer(ctx, &ctx->lrtable, p, a);\n" " }\n" " }\n" " if (b) {\n" @@ -4411,8 +4417,8 @@ static bool_t generate(context_t *ctx) { " switch (a->type) {\n" " case PCC_LR_ANSWER_LR:\n" " if (a->data.lr->head == NULL) {\n" - " a->data.lr->head = pcc_lr_head__create(ctx->auxil, rule);\n" - " pcc_lr_table__hold_head(ctx->auxil, &ctx->lrtable, p, a->data.lr->head);\n" + " a->data.lr->head = pcc_lr_head__create(ctx, rule);\n" + " pcc_lr_table__hold_head(ctx, &ctx->lrtable, p, a->data.lr->head);\n" " }\n" " {\n" " size_t i = ctx->lrstack.len;\n" @@ -4435,41 +4441,41 @@ static bool_t generate(context_t *ctx) { " else {\n" " pcc_lr_entry_t *const e = pcc_lr_entry__create(ctx->auxil, rule);\n" " pcc_lr_stack__push(ctx->auxil, &ctx->lrstack, e);\n" - " a = pcc_lr_answer__create(ctx->auxil, PCC_LR_ANSWER_LR, p);\n" + " a = pcc_lr_answer__create(ctx, PCC_LR_ANSWER_LR, p);\n" " a->data.lr = e;\n" - " pcc_lr_table__set_answer(ctx->auxil, &ctx->lrtable, p, rule, a);\n" + " pcc_lr_table__set_answer(ctx, &ctx->lrtable, p, rule, a);\n" " c = rule(ctx);\n" " pcc_lr_stack__pop(ctx->auxil, &ctx->lrstack);\n" " a->pos = ctx->pos + ctx->cur;\n" " if (e->head == NULL) {\n" - " pcc_lr_answer__set_chunk(ctx->auxil, a, c);\n" + " pcc_lr_answer__set_chunk(ctx, a, c);\n" " }\n" " else {\n" " e->seed = c;\n" " h = a->data.lr->head;\n" " if (h->rule != rule) {\n" " c = a->data.lr->seed;\n" - " a = pcc_lr_answer__create(ctx->auxil, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur);\n" + " a = pcc_lr_answer__create(ctx, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur);\n" " a->data.chunk = c;\n" - " pcc_lr_table__hold_answer(ctx->auxil, &ctx->lrtable, p, a);\n" + " pcc_lr_table__hold_answer(ctx, &ctx->lrtable, p, a);\n" " }\n" " else {\n" - " pcc_lr_answer__set_chunk(ctx->auxil, a, a->data.lr->seed);\n" + " pcc_lr_answer__set_chunk(ctx, a, a->data.lr->seed);\n" " if (a->data.chunk == NULL) {\n" " c = NULL;\n" " }\n" " else {\n" - " pcc_lr_table__set_head(ctx->auxil, &ctx->lrtable, p, h);\n" + " pcc_lr_table__set_head(ctx, &ctx->lrtable, p, h);\n" " for (;;) {\n" " ctx->cur = p - ctx->pos;\n" " pcc_rule_set__copy(ctx->auxil, &h->eval, &h->invol);\n" " c = rule(ctx);\n" " if (c == NULL || ctx->pos + ctx->cur <= a->pos) break;\n" - " pcc_lr_answer__set_chunk(ctx->auxil, a, c);\n" + " pcc_lr_answer__set_chunk(ctx, a, c);\n" " a->pos = ctx->pos + ctx->cur;\n" " }\n" - " pcc_thunk_chunk__destroy(ctx->auxil, c);\n" - " pcc_lr_table__set_head(ctx->auxil, &ctx->lrtable, p, NULL);\n" + " pcc_thunk_chunk__destroy(ctx, c);\n" + " pcc_lr_table__set_head(ctx, &ctx->lrtable, p, NULL);\n" " ctx->cur = a->pos - ctx->pos;\n" " c = a->data.chunk;\n" " }\n" @@ -4658,7 +4664,7 @@ static bool_t generate(context_t *ctx) { ); stream__printf( &sstream, - " pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx->auxil);\n" + " pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx);\n" " chunk->pos = ctx->cur;\n" " PCC_DEBUG(ctx->auxil, PCC_DBG_EVALUATE, \"%s\", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));\n" " ctx->level++;\n", @@ -4694,7 +4700,7 @@ static bool_t generate(context_t *ctx) { "L0000:;\n" " ctx->level--;\n" " PCC_DEBUG(ctx->auxil, PCC_DBG_NOMATCH, \"%s\", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));\n" - " pcc_thunk_chunk__destroy(ctx->auxil, chunk);\n" + " pcc_thunk_chunk__destroy(ctx, chunk);\n" " return NULL;\n", ctx->rules.buf[i]->data.rule.name ); From f3a5c7e772da28b3e0fd88e9cb7bc3eb3487c504 Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Mon, 25 Apr 2022 21:08:08 +0900 Subject: [PATCH 2/6] Preallocate memory objects for pcc_thunk_chunk_t, pcc_lr_head_t, and pcc_lr_answer_t A generated parser allocates memory objects for the types frequently. The objects are not resized so we can preallocate them. bencharmk.sh reports the code with this change is about 10~ faster than the original code in run times. benchmark.sh output for the original code: Generation times: ================= master calc 513 us (100%) json 511 us (100%) kotlin 2320 us (100%) Build times: ============ master calc 69 ms (100%) json 64 ms (100%) kotlin 731 ms (100%) Run times: ========== master calc 126 ms (100%) json 34 ms (100%) kotlin 279 ms (100%) benchmark.sh output for the code with this change: Generation times: ================= recycle-list calc 538 us (100%) json 512 us (100%) kotlin 2225 us (100%) Build times: ============ recycle-list calc 72 ms (100%) json 68 ms (100%) kotlin 732 ms (100%) Run times: ========== recycle-list calc 110 ms (100%) json 28 ms (100%) kotlin 222 ms (100%) Signed-off-by: Masatake YAMATO --- src/packcc.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 6 deletions(-) diff --git a/src/packcc.c b/src/packcc.c index 4ee05a2..439b505 100644 --- a/src/packcc.c +++ b/src/packcc.c @@ -3575,6 +3575,20 @@ static bool_t generate(context_t *ctx) { "} pcc_lr_stack_t;\n" "\n" ); + stream__puts( + &sstream, + "typedef struct pcc_recycle_list_tag {\n" + " struct pcc_recycle_list_tag *next;\n" + "} pcc_recycle_list_t;\n" + "typedef struct pcc_recycle_manager_tag {\n" + " pcc_recycle_list_t *list;\n" + " size_t element_size;\n" + " size_t allocated;\n" + " size_t inuse;\n" + " void *base;\n" + "} pcc_recycle_manager_t;\n" + "\n" + ); stream__printf( &sstream, "struct %s_context_tag {\n" @@ -3586,6 +3600,9 @@ static bool_t generate(context_t *ctx) { " pcc_lr_stack_t lrstack;\n" " pcc_thunk_array_t thunks;\n" " pcc_auxil_t auxil;\n" + " pcc_recycle_manager_t thunk_chunk_recycle_manager;\n" + " pcc_recycle_manager_t lr_head_recycle_manager;\n" + " pcc_recycle_manager_t lr_answer_recycle_manager;\n" "};\n" "\n", get_prefix(ctx) @@ -3873,11 +3890,54 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" ); + + stream__puts( + &sstream, + "static void *pcc_recycle_alloc(pcc_auxil_t auxil, pcc_recycle_manager_t *recycle_manager) {\n" + " if (recycle_manager->list) {\n" + " pcc_recycle_list_t *tmp = recycle_manager->list;\n" + " recycle_manager->list = tmp->next;\n" + " return tmp;\n" + " }\n" + " if (recycle_manager->inuse < recycle_manager->allocated) {\n" + " char *base = recycle_manager->base;\n" + " char *tmp = base + (recycle_manager->inuse * recycle_manager->element_size);\n" + " recycle_manager->inuse++;\n" + " return tmp;\n" + " }\n" + " return PCC_MALLOC(auxil, recycle_manager->element_size);\n" + "}\n" + "static void pcc_recycle_return(pcc_recycle_manager_t *recycle_manager, void *obj) {\n" + " pcc_recycle_list_t *tmp = obj;\n" + " tmp->next = recycle_manager->list;\n" + " recycle_manager->list = tmp;\n" + "}\n" + "static void pcc_recycle_manager_alloc(pcc_auxil_t auxil, pcc_recycle_manager_t *manager, size_t element_size) {\n" + " manager->list = NULL;\n" + " manager->element_size = element_size;\n" + " manager->allocated = 1024 * 1024;\n" + " manager->inuse = 0;\n" + " manager->base = PCC_MALLOC(auxil, manager->element_size * manager->allocated);\n" + "}\n" + "static void pcc_recycle_manager_destroy(pcc_auxil_t auxil, pcc_recycle_manager_t *recycle_manager) {\n" + " char *start = recycle_manager->base;\n" + " char *end = recycle_manager->base + (recycle_manager->allocated * recycle_manager->element_size);\n" + " while (recycle_manager->list) {\n" + " pcc_recycle_list_t *tmp = recycle_manager->list;\n" + " recycle_manager->list = tmp->next;\n" + " if (start <= (char *)tmp && (char *)tmp < end)\n" + " continue;\n" + " PCC_FREE(auxil, tmp);\n" + " }\n" + " PCC_FREE(auxil, recycle_manager->base);\n" + "}\n" + "\n" + ); stream__printf( &sstream, "MARK_USED_FUNC\n" "static pcc_thunk_chunk_t *pcc_thunk_chunk__create(%s_context_t *ctx) {\n" - " pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)PCC_MALLOC(ctx->auxil, sizeof(pcc_thunk_chunk_t));\n" + " pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)pcc_recycle_alloc(ctx->auxil, &ctx->thunk_chunk_recycle_manager);\n" " pcc_value_table__init(ctx->auxil, &chunk->values);\n" " pcc_capture_table__init(ctx->auxil, &chunk->capts);\n" " pcc_thunk_array__init(ctx->auxil, &chunk->thunks);\n" @@ -3890,7 +3950,7 @@ static bool_t generate(context_t *ctx) { " pcc_thunk_array__term(ctx->auxil, &chunk->thunks);\n" " pcc_capture_table__term(ctx->auxil, &chunk->capts);\n" " pcc_value_table__term(ctx->auxil, &chunk->values);\n" - " PCC_FREE(ctx->auxil, chunk);\n" + " pcc_recycle_return(&ctx->thunk_chunk_recycle_manager, chunk);\n" "}\n" "\n", get_prefix(ctx), get_prefix(ctx) @@ -3954,7 +4014,7 @@ static bool_t generate(context_t *ctx) { stream__printf( &sstream, "static pcc_lr_head_t *pcc_lr_head__create(%s_context_t *ctx, pcc_rule_t rule) {\n" - " pcc_lr_head_t *const head = (pcc_lr_head_t *)PCC_MALLOC(ctx->auxil, sizeof(pcc_lr_head_t));\n" + " pcc_lr_head_t *const head = (pcc_lr_head_t *)pcc_recycle_alloc(ctx->auxil, &ctx->lr_head_recycle_manager);\n" " head->rule = rule;\n" " pcc_rule_set__init(ctx->auxil, &head->invol);\n" " pcc_rule_set__init(ctx->auxil, &head->eval);\n" @@ -3967,7 +4027,7 @@ static bool_t generate(context_t *ctx) { " pcc_lr_head__destroy(ctx, head->hold);\n" " pcc_rule_set__term(ctx->auxil, &head->eval);\n" " pcc_rule_set__term(ctx->auxil, &head->invol);\n" - " PCC_FREE(ctx->auxil, head);\n" + " pcc_recycle_return(&ctx->lr_head_recycle_manager, head);\n" "}\n" "\n", get_prefix(ctx), get_prefix(ctx) @@ -3977,7 +4037,7 @@ static bool_t generate(context_t *ctx) { "static void pcc_lr_entry__destroy(pcc_auxil_t auxil, pcc_lr_entry_t *lr);\n" "\n" "static pcc_lr_answer_t *pcc_lr_answer__create(%s_context_t *ctx, pcc_lr_answer_type_t type, size_t pos) {\n" - " pcc_lr_answer_t *answer = (pcc_lr_answer_t *)PCC_MALLOC(ctx->auxil, sizeof(pcc_lr_answer_t));\n" + " pcc_lr_answer_t *answer = (pcc_lr_answer_t *)pcc_recycle_alloc(ctx->auxil, &ctx->lr_answer_recycle_manager);\n" " answer->type = type;\n" " answer->pos = pos;\n" " answer->hold = NULL;\n" @@ -4026,7 +4086,7 @@ static bool_t generate(context_t *ctx) { " default: /* unknown */\n" " break;\n" " }\n" - " PCC_FREE(ctx->auxil, answer);\n" + " pcc_recycle_return(&ctx->lr_answer_recycle_manager, answer);\n" " answer = a;\n" " }\n" "}\n" @@ -4254,6 +4314,9 @@ static bool_t generate(context_t *ctx) { " pcc_lr_table__init(auxil, &ctx->lrtable);\n" " pcc_lr_stack__init(auxil, &ctx->lrstack);\n" " pcc_thunk_array__init(auxil, &ctx->thunks);\n" + " pcc_recycle_manager_alloc(auxil, &ctx->thunk_chunk_recycle_manager, sizeof(pcc_thunk_chunk_t));\n" + " pcc_recycle_manager_alloc(auxil, &ctx->lr_head_recycle_manager, sizeof(pcc_lr_head_t));\n" + " pcc_recycle_manager_alloc(auxil, &ctx->lr_answer_recycle_manager, sizeof(pcc_lr_answer_t));\n" " ctx->auxil = auxil;\n" " return ctx;\n" "}\n" @@ -4266,11 +4329,15 @@ static bool_t generate(context_t *ctx) { ); stream__puts( &sstream, + " pcc_recycle_list_t *tmp;\n" " if (ctx == NULL) return;\n" " pcc_thunk_array__term(ctx->auxil, &ctx->thunks);\n" " pcc_lr_stack__term(ctx->auxil, &ctx->lrstack);\n" " pcc_lr_table__term(ctx, &ctx->lrtable);\n" " pcc_char_array__term(ctx->auxil, &ctx->buffer);\n" + " pcc_recycle_manager_destroy(ctx->auxil, &ctx->thunk_chunk_recycle_manager);\n" + " pcc_recycle_manager_destroy(ctx->auxil, &ctx->lr_head_recycle_manager);\n" + " pcc_recycle_manager_destroy(ctx->auxil, &ctx->lr_answer_recycle_manager);\n" " PCC_FREE(ctx->auxil, ctx);\n" "}\n" "\n" From 4dbcaae480926d3f3289a718c1e0578b43ea2fbc Mon Sep 17 00:00:00 2001 From: Arihiro Yoshida Date: Fri, 29 Apr 2022 16:30:02 +0900 Subject: [PATCH 3/6] Rename identifiers related to memory recycling --- src/packcc.c | 103 ++++++++++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 50 deletions(-) diff --git a/src/packcc.c b/src/packcc.c index 6191a38..1d141f6 100644 --- a/src/packcc.c +++ b/src/packcc.c @@ -3577,16 +3577,17 @@ static bool_t generate(context_t *ctx) { ); stream__puts( &sstream, - "typedef struct pcc_recycle_list_tag {\n" - " struct pcc_recycle_list_tag *next;\n" - "} pcc_recycle_list_t;\n" - "typedef struct pcc_recycle_manager_tag {\n" - " pcc_recycle_list_t *list;\n" + "typedef struct pcc_recycling_list_tag {\n" + " struct pcc_recycling_list_tag *next;\n" + "} pcc_recycling_list_t;\n" + "\n" + "typedef struct pcc_memory_recycler_tag {\n" + " pcc_recycling_list_t *list;\n" " size_t element_size;\n" " size_t allocated;\n" " size_t inuse;\n" " void *base;\n" - "} pcc_recycle_manager_t;\n" + "} pcc_memory_recycler_t;\n" "\n" ); stream__printf( @@ -3600,9 +3601,9 @@ static bool_t generate(context_t *ctx) { " pcc_lr_stack_t lrstack;\n" " pcc_thunk_array_t thunks;\n" " pcc_auxil_t auxil;\n" - " pcc_recycle_manager_t thunk_chunk_recycle_manager;\n" - " pcc_recycle_manager_t lr_head_recycle_manager;\n" - " pcc_recycle_manager_t lr_answer_recycle_manager;\n" + " pcc_memory_recycler_t thunk_chunk_recycler;\n" + " pcc_memory_recycler_t lr_head_recycler;\n" + " pcc_memory_recycler_t lr_answer_recycler;\n" "};\n" "\n", get_prefix(ctx) @@ -3890,46 +3891,48 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" ); - stream__puts( &sstream, - "static void *pcc_recycle_alloc(pcc_auxil_t auxil, pcc_recycle_manager_t *recycle_manager) {\n" - " if (recycle_manager->list) {\n" - " pcc_recycle_list_t *tmp = recycle_manager->list;\n" - " recycle_manager->list = tmp->next;\n" + "static void pcc_memory_recycler__init(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler, size_t element_size) {\n" + " recycler->list = NULL;\n" + " recycler->element_size = element_size;\n" + " recycler->allocated = 1024 * 1024;\n" + " recycler->inuse = 0;\n" + " recycler->base = PCC_MALLOC(auxil, recycler->element_size * recycler->allocated);\n" + "}\n" + "\n" + "static void *pcc_memory_recycler__supply(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler) {\n" + " if (recycler->list) {\n" + " pcc_recycling_list_t *tmp = recycler->list;\n" + " recycler->list = tmp->next;\n" " return tmp;\n" " }\n" - " if (recycle_manager->inuse < recycle_manager->allocated) {\n" - " char *base = recycle_manager->base;\n" - " char *tmp = base + (recycle_manager->inuse * recycle_manager->element_size);\n" - " recycle_manager->inuse++;\n" + " if (recycler->inuse < recycler->allocated) {\n" + " char *base = recycler->base;\n" + " char *tmp = base + (recycler->inuse * recycler->element_size);\n" + " recycler->inuse++;\n" " return tmp;\n" " }\n" - " return PCC_MALLOC(auxil, recycle_manager->element_size);\n" + " return PCC_MALLOC(auxil, recycler->element_size);\n" "}\n" - "static void pcc_recycle_return(pcc_recycle_manager_t *recycle_manager, void *obj) {\n" - " pcc_recycle_list_t *tmp = obj;\n" - " tmp->next = recycle_manager->list;\n" - " recycle_manager->list = tmp;\n" - "}\n" - "static void pcc_recycle_manager_alloc(pcc_auxil_t auxil, pcc_recycle_manager_t *manager, size_t element_size) {\n" - " manager->list = NULL;\n" - " manager->element_size = element_size;\n" - " manager->allocated = 1024 * 1024;\n" - " manager->inuse = 0;\n" - " manager->base = PCC_MALLOC(auxil, manager->element_size * manager->allocated);\n" + "\n" + "static void pcc_memory_recycler__recycle(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler, void *obj) {\n" + " pcc_recycling_list_t *tmp = obj;\n" + " tmp->next = recycler->list;\n" + " recycler->list = tmp;\n" "}\n" - "static void pcc_recycle_manager_destroy(pcc_auxil_t auxil, pcc_recycle_manager_t *recycle_manager) {\n" - " char *start = recycle_manager->base;\n" - " char *end = recycle_manager->base + (recycle_manager->allocated * recycle_manager->element_size);\n" - " while (recycle_manager->list) {\n" - " pcc_recycle_list_t *tmp = recycle_manager->list;\n" - " recycle_manager->list = tmp->next;\n" + "\n" + "static void pcc_memory_recycler__term(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler) {\n" + " char *start = recycler->base;\n" + " char *end = recycler->base + (recycler->allocated * recycler->element_size);\n" + " while (recycler->list) {\n" + " pcc_recycling_list_t *tmp = recycler->list;\n" + " recycler->list = tmp->next;\n" " if (start <= (char *)tmp && (char *)tmp < end)\n" " continue;\n" " PCC_FREE(auxil, tmp);\n" " }\n" - " PCC_FREE(auxil, recycle_manager->base);\n" + " PCC_FREE(auxil, recycler->base);\n" "}\n" "\n" ); @@ -3937,7 +3940,7 @@ static bool_t generate(context_t *ctx) { &sstream, "MARK_FUNC_AS_USED\n" "static pcc_thunk_chunk_t *pcc_thunk_chunk__create(%s_context_t *ctx) {\n" - " pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)pcc_recycle_alloc(ctx->auxil, &ctx->thunk_chunk_recycle_manager);\n" + " pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)pcc_memory_recycler__supply(ctx->auxil, &ctx->thunk_chunk_recycler);\n" " pcc_value_table__init(ctx->auxil, &chunk->values);\n" " pcc_capture_table__init(ctx->auxil, &chunk->capts);\n" " pcc_thunk_array__init(ctx->auxil, &chunk->thunks);\n" @@ -3950,7 +3953,7 @@ static bool_t generate(context_t *ctx) { " pcc_thunk_array__term(ctx->auxil, &chunk->thunks);\n" " pcc_capture_table__term(ctx->auxil, &chunk->capts);\n" " pcc_value_table__term(ctx->auxil, &chunk->values);\n" - " pcc_recycle_return(&ctx->thunk_chunk_recycle_manager, chunk);\n" + " pcc_memory_recycler__recycle(ctx->auxil, &ctx->thunk_chunk_recycler, chunk);\n" "}\n" "\n", get_prefix(ctx), get_prefix(ctx) @@ -4014,7 +4017,7 @@ static bool_t generate(context_t *ctx) { stream__printf( &sstream, "static pcc_lr_head_t *pcc_lr_head__create(%s_context_t *ctx, pcc_rule_t rule) {\n" - " pcc_lr_head_t *const head = (pcc_lr_head_t *)pcc_recycle_alloc(ctx->auxil, &ctx->lr_head_recycle_manager);\n" + " pcc_lr_head_t *const head = (pcc_lr_head_t *)pcc_memory_recycler__supply(ctx->auxil, &ctx->lr_head_recycler);\n" " head->rule = rule;\n" " pcc_rule_set__init(ctx->auxil, &head->invol);\n" " pcc_rule_set__init(ctx->auxil, &head->eval);\n" @@ -4027,7 +4030,7 @@ static bool_t generate(context_t *ctx) { " pcc_lr_head__destroy(ctx, head->hold);\n" " pcc_rule_set__term(ctx->auxil, &head->eval);\n" " pcc_rule_set__term(ctx->auxil, &head->invol);\n" - " pcc_recycle_return(&ctx->lr_head_recycle_manager, head);\n" + " pcc_memory_recycler__recycle(ctx->auxil, &ctx->lr_head_recycler, head);\n" "}\n" "\n", get_prefix(ctx), get_prefix(ctx) @@ -4037,7 +4040,7 @@ static bool_t generate(context_t *ctx) { "static void pcc_lr_entry__destroy(pcc_auxil_t auxil, pcc_lr_entry_t *lr);\n" "\n" "static pcc_lr_answer_t *pcc_lr_answer__create(%s_context_t *ctx, pcc_lr_answer_type_t type, size_t pos) {\n" - " pcc_lr_answer_t *answer = (pcc_lr_answer_t *)pcc_recycle_alloc(ctx->auxil, &ctx->lr_answer_recycle_manager);\n" + " pcc_lr_answer_t *answer = (pcc_lr_answer_t *)pcc_memory_recycler__supply(ctx->auxil, &ctx->lr_answer_recycler);\n" " answer->type = type;\n" " answer->pos = pos;\n" " answer->hold = NULL;\n" @@ -4086,7 +4089,7 @@ static bool_t generate(context_t *ctx) { " default: /* unknown */\n" " break;\n" " }\n" - " pcc_recycle_return(&ctx->lr_answer_recycle_manager, answer);\n" + " pcc_memory_recycler__recycle(ctx->auxil, &ctx->lr_answer_recycler, answer);\n" " answer = a;\n" " }\n" "}\n" @@ -4314,9 +4317,9 @@ static bool_t generate(context_t *ctx) { " pcc_lr_table__init(auxil, &ctx->lrtable);\n" " pcc_lr_stack__init(auxil, &ctx->lrstack);\n" " pcc_thunk_array__init(auxil, &ctx->thunks);\n" - " pcc_recycle_manager_alloc(auxil, &ctx->thunk_chunk_recycle_manager, sizeof(pcc_thunk_chunk_t));\n" - " pcc_recycle_manager_alloc(auxil, &ctx->lr_head_recycle_manager, sizeof(pcc_lr_head_t));\n" - " pcc_recycle_manager_alloc(auxil, &ctx->lr_answer_recycle_manager, sizeof(pcc_lr_answer_t));\n" + " pcc_memory_recycler__init(auxil, &ctx->thunk_chunk_recycler, sizeof(pcc_thunk_chunk_t));\n" + " pcc_memory_recycler__init(auxil, &ctx->lr_head_recycler, sizeof(pcc_lr_head_t));\n" + " pcc_memory_recycler__init(auxil, &ctx->lr_answer_recycler, sizeof(pcc_lr_answer_t));\n" " ctx->auxil = auxil;\n" " return ctx;\n" "}\n" @@ -4329,15 +4332,15 @@ static bool_t generate(context_t *ctx) { ); stream__puts( &sstream, - " pcc_recycle_list_t *tmp;\n" + " pcc_recycling_list_t *tmp;\n" " if (ctx == NULL) return;\n" " pcc_thunk_array__term(ctx->auxil, &ctx->thunks);\n" " pcc_lr_stack__term(ctx->auxil, &ctx->lrstack);\n" " pcc_lr_table__term(ctx, &ctx->lrtable);\n" " pcc_char_array__term(ctx->auxil, &ctx->buffer);\n" - " pcc_recycle_manager_destroy(ctx->auxil, &ctx->thunk_chunk_recycle_manager);\n" - " pcc_recycle_manager_destroy(ctx->auxil, &ctx->lr_head_recycle_manager);\n" - " pcc_recycle_manager_destroy(ctx->auxil, &ctx->lr_answer_recycle_manager);\n" + " pcc_memory_recycler__term(ctx->auxil, &ctx->thunk_chunk_recycler);\n" + " pcc_memory_recycler__term(ctx->auxil, &ctx->lr_head_recycler);\n" + " pcc_memory_recycler__term(ctx->auxil, &ctx->lr_answer_recycler);\n" " PCC_FREE(ctx->auxil, ctx);\n" "}\n" "\n" From 176f5c0f8d12ba3474cc3e31a00a84fa6998db92 Mon Sep 17 00:00:00 2001 From: Arihiro Yoshida Date: Fri, 29 Apr 2022 17:21:40 +0900 Subject: [PATCH 4/6] Simplify the code using pcc_context_t typedef --- src/packcc.c | 183 ++++++++++++++++++++------------------------------- 1 file changed, 72 insertions(+), 111 deletions(-) diff --git a/src/packcc.c b/src/packcc.c index 1d141f6..f0194ec 100644 --- a/src/packcc.c +++ b/src/packcc.c @@ -3409,6 +3409,14 @@ static bool_t generate(context_t *ctx) { "\n", at, ap ? "" : " " ); + if (strcmp(get_prefix(ctx), "pcc") != 0) { + stream__printf( + &sstream, + "typedef %s_context_t pcc_context_t;\n" + "\n", + get_prefix(ctx) + ); + } stream__puts( &sstream, "typedef struct pcc_value_table_tag {\n" @@ -3443,12 +3451,8 @@ static bool_t generate(context_t *ctx) { "typedef struct pcc_thunk_tag pcc_thunk_t;\n" "typedef struct pcc_thunk_array_tag pcc_thunk_array_t;\n" "\n" - ); - stream__printf( - &sstream, - "typedef void (*pcc_action_t)(%s_context_t *, pcc_thunk_t *, pcc_value_t *);\n" - "\n", - get_prefix(ctx) + "typedef void (*pcc_action_t)(pcc_context_t *, pcc_thunk_t *, pcc_value_t *);\n" + "\n" ); stream__puts( &sstream, @@ -3514,14 +3518,10 @@ static bool_t generate(context_t *ctx) { "};\n" "\n" ); - stream__printf( - &sstream, - "typedef pcc_thunk_chunk_t *(*pcc_rule_t)(%s_context_t *);\n" - "\n", - get_prefix(ctx) - ); stream__puts( &sstream, + "typedef pcc_thunk_chunk_t *(*pcc_rule_t)(pcc_context_t *);\n" + "\n" "typedef struct pcc_rule_set_tag {\n" " pcc_rule_t *buf;\n" " size_t max;\n" @@ -3936,10 +3936,10 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" ); - stream__printf( + stream__puts( &sstream, "MARK_FUNC_AS_USED\n" - "static pcc_thunk_chunk_t *pcc_thunk_chunk__create(%s_context_t *ctx) {\n" + "static pcc_thunk_chunk_t *pcc_thunk_chunk__create(pcc_context_t *ctx) {\n" " pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)pcc_memory_recycler__supply(ctx->auxil, &ctx->thunk_chunk_recycler);\n" " pcc_value_table__init(ctx->auxil, &chunk->values);\n" " pcc_capture_table__init(ctx->auxil, &chunk->capts);\n" @@ -3948,15 +3948,14 @@ static bool_t generate(context_t *ctx) { " return chunk;\n" "}\n" "\n" - "static void pcc_thunk_chunk__destroy(%s_context_t *ctx, pcc_thunk_chunk_t *chunk) {\n" + "static void pcc_thunk_chunk__destroy(pcc_context_t *ctx, pcc_thunk_chunk_t *chunk) {\n" " if (chunk == NULL) return;\n" " pcc_thunk_array__term(ctx->auxil, &chunk->thunks);\n" " pcc_capture_table__term(ctx->auxil, &chunk->capts);\n" " pcc_value_table__term(ctx->auxil, &chunk->values);\n" " pcc_memory_recycler__recycle(ctx->auxil, &ctx->thunk_chunk_recycler, chunk);\n" "}\n" - "\n", - get_prefix(ctx), get_prefix(ctx) + "\n" ); stream__puts( &sstream, @@ -4014,9 +4013,9 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" ); - stream__printf( + stream__puts( &sstream, - "static pcc_lr_head_t *pcc_lr_head__create(%s_context_t *ctx, pcc_rule_t rule) {\n" + "static pcc_lr_head_t *pcc_lr_head__create(pcc_context_t *ctx, pcc_rule_t rule) {\n" " pcc_lr_head_t *const head = (pcc_lr_head_t *)pcc_memory_recycler__supply(ctx->auxil, &ctx->lr_head_recycler);\n" " head->rule = rule;\n" " pcc_rule_set__init(ctx->auxil, &head->invol);\n" @@ -4025,21 +4024,20 @@ static bool_t generate(context_t *ctx) { " return head;\n" "}\n" "\n" - "static void pcc_lr_head__destroy(%s_context_t *ctx, pcc_lr_head_t *head) {\n" + "static void pcc_lr_head__destroy(pcc_context_t *ctx, pcc_lr_head_t *head) {\n" " if (head == NULL) return;\n" " pcc_lr_head__destroy(ctx, head->hold);\n" " pcc_rule_set__term(ctx->auxil, &head->eval);\n" " pcc_rule_set__term(ctx->auxil, &head->invol);\n" " pcc_memory_recycler__recycle(ctx->auxil, &ctx->lr_head_recycler, head);\n" "}\n" - "\n", - get_prefix(ctx), get_prefix(ctx) + "\n" ); - stream__printf( + stream__puts( &sstream, "static void pcc_lr_entry__destroy(pcc_auxil_t auxil, pcc_lr_entry_t *lr);\n" "\n" - "static pcc_lr_answer_t *pcc_lr_answer__create(%s_context_t *ctx, pcc_lr_answer_type_t type, size_t pos) {\n" + "static pcc_lr_answer_t *pcc_lr_answer__create(pcc_context_t *ctx, pcc_lr_answer_type_t type, size_t pos) {\n" " pcc_lr_answer_t *answer = (pcc_lr_answer_t *)pcc_memory_recycler__supply(ctx->auxil, &ctx->lr_answer_recycler);\n" " answer->type = type;\n" " answer->pos = pos;\n" @@ -4058,7 +4056,7 @@ static bool_t generate(context_t *ctx) { " return answer;\n" "}\n" "\n" - "static void pcc_lr_answer__set_chunk(%s_context_t *ctx, pcc_lr_answer_t *answer, pcc_thunk_chunk_t *chunk) {\n" + "static void pcc_lr_answer__set_chunk(pcc_context_t *ctx, pcc_lr_answer_t *answer, pcc_thunk_chunk_t *chunk) {\n" " pcc_lr_answer_t *const a = pcc_lr_answer__create(ctx, answer->type, answer->pos);\n" " switch (answer->type) {\n" " case PCC_LR_ANSWER_LR:\n" @@ -4076,7 +4074,7 @@ static bool_t generate(context_t *ctx) { " answer->data.chunk = chunk;\n" "}\n" "\n" - "static void pcc_lr_answer__destroy(%s_context_t *ctx, pcc_lr_answer_t *answer) {\n" + "static void pcc_lr_answer__destroy(pcc_context_t *ctx, pcc_lr_answer_t *answer) {\n" " while (answer != NULL) {\n" " pcc_lr_answer_t *const a = answer->hold;\n" " switch (answer->type) {\n" @@ -4093,10 +4091,9 @@ static bool_t generate(context_t *ctx) { " answer = a;\n" " }\n" "}\n" - "\n", - get_prefix(ctx), get_prefix(ctx), get_prefix(ctx) + "\n" ); - stream__printf( + stream__puts( &sstream, "static void pcc_lr_memo_map__init(pcc_auxil_t auxil, pcc_lr_memo_map_t *map) {\n" " map->len = 0;\n" @@ -4104,7 +4101,7 @@ static bool_t generate(context_t *ctx) { " map->buf = NULL;\n" "}\n" "\n" - "static size_t pcc_lr_memo_map__index(pcc_auxil_t auxil, pcc_lr_memo_map_t *map, pcc_rule_t rule) {\n" + "static size_t pcc_lr_memo_map__index(pcc_context_t *ctx, pcc_lr_memo_map_t *map, pcc_rule_t rule) {\n" " size_t i;\n" " for (i = 0; i < map->len; i++) {\n" " if (map->buf[i].rule == rule) return i;\n" @@ -4112,7 +4109,7 @@ static bool_t generate(context_t *ctx) { " return PCC_VOID_VALUE;\n" "}\n" "\n" - "static void pcc_lr_memo_map__put(%s_context_t *ctx, pcc_lr_memo_map_t *map, pcc_rule_t rule, pcc_lr_answer_t *answer) {\n" + "static void pcc_lr_memo_map__put(pcc_context_t *ctx, pcc_lr_memo_map_t *map, pcc_rule_t rule, pcc_lr_answer_t *answer) {\n" " const size_t i = pcc_lr_memo_map__index(ctx->auxil, map, rule);\n" " if (i != PCC_VOID_VALUE) {\n" " pcc_lr_answer__destroy(ctx, map->buf[i].answer);\n" @@ -4134,43 +4131,41 @@ static bool_t generate(context_t *ctx) { " }\n" "}\n" "\n" - "static pcc_lr_answer_t *pcc_lr_memo_map__get(pcc_auxil_t auxil, pcc_lr_memo_map_t *map, pcc_rule_t rule) {\n" - " const size_t i = pcc_lr_memo_map__index(auxil, map, rule);\n" + "static pcc_lr_answer_t *pcc_lr_memo_map__get(pcc_context_t *ctx, pcc_lr_memo_map_t *map, pcc_rule_t rule) {\n" + " const size_t i = pcc_lr_memo_map__index(ctx->auxil, map, rule);\n" " return (i != PCC_VOID_VALUE) ? map->buf[i].answer : NULL;\n" "}\n" "\n" - "static void pcc_lr_memo_map__term(%s_context_t *ctx, pcc_lr_memo_map_t *map) {\n" + "static void pcc_lr_memo_map__term(pcc_context_t *ctx, pcc_lr_memo_map_t *map) {\n" " while (map->len > 0) {\n" " map->len--;\n" " pcc_lr_answer__destroy(ctx, map->buf[map->len].answer);\n" " }\n" " PCC_FREE(ctx->auxil, map->buf);\n" "}\n" - "\n", - get_prefix(ctx), get_prefix(ctx) + "\n" ); - stream__printf( + stream__puts( &sstream, - "static pcc_lr_table_entry_t *pcc_lr_table_entry__create(pcc_auxil_t auxil) {\n" - " pcc_lr_table_entry_t *const entry = (pcc_lr_table_entry_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_table_entry_t));\n" + "static pcc_lr_table_entry_t *pcc_lr_table_entry__create(pcc_context_t *ctx) {\n" + " pcc_lr_table_entry_t *const entry = (pcc_lr_table_entry_t *)PCC_MALLOC(ctx->auxil, sizeof(pcc_lr_table_entry_t));\n" " entry->head = NULL;\n" - " pcc_lr_memo_map__init(auxil, &entry->memos);\n" + " pcc_lr_memo_map__init(ctx->auxil, &entry->memos);\n" " entry->hold_a = NULL;\n" " entry->hold_h = NULL;\n" " return entry;\n" "}\n" "\n" - "static void pcc_lr_table_entry__destroy(%s_context_t *ctx, pcc_lr_table_entry_t *entry) {\n" + "static void pcc_lr_table_entry__destroy(pcc_context_t *ctx, pcc_lr_table_entry_t *entry) {\n" " if (entry == NULL) return;\n" " pcc_lr_head__destroy(ctx, entry->hold_h);\n" " pcc_lr_answer__destroy(ctx, entry->hold_a);\n" " pcc_lr_memo_map__term(ctx, &entry->memos);\n" " PCC_FREE(ctx->auxil, entry);\n" "}\n" - "\n", - get_prefix(ctx) + "\n" ); - stream__printf( + stream__puts( &sstream, "static void pcc_lr_table__init(pcc_auxil_t auxil, pcc_lr_table_t *table) {\n" " table->ofs = 0;\n" @@ -4179,7 +4174,7 @@ static bool_t generate(context_t *ctx) { " table->buf = NULL;\n" "}\n" "\n" - "static void pcc_lr_table__resize(%s_context_t *ctx, pcc_lr_table_t *table, size_t len) {\n" + "static void pcc_lr_table__resize(pcc_context_t *ctx, pcc_lr_table_t *table, size_t len) {\n" " size_t i;\n" " for (i = len; i < table->len; i++) pcc_lr_table_entry__destroy(ctx, table->buf[i]);\n" " if (table->max < len) {\n" @@ -4194,49 +4189,49 @@ static bool_t generate(context_t *ctx) { " table->len = len;\n" "}\n" "\n" - "static void pcc_lr_table__set_head(%s_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {\n" + "static void pcc_lr_table__set_head(pcc_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {\n" " index += table->ofs;\n" " if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);\n" - " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx->auxil);\n" + " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx);\n" " table->buf[index]->head = head;\n" "}\n" "\n" - "static void pcc_lr_table__hold_head(%s_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {\n" + "static void pcc_lr_table__hold_head(pcc_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {\n" " index += table->ofs;\n" " if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);\n" - " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx->auxil);\n" + " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx);\n" " head->hold = table->buf[index]->hold_h;\n" " table->buf[index]->hold_h = head;\n" "}\n" "\n" - "static void pcc_lr_table__set_answer(%s_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_rule_t rule, pcc_lr_answer_t *answer) {\n" + "static void pcc_lr_table__set_answer(pcc_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_rule_t rule, pcc_lr_answer_t *answer) {\n" " index += table->ofs;\n" " if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);\n" - " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx->auxil);\n" + " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx);\n" " pcc_lr_memo_map__put(ctx, &table->buf[index]->memos, rule, answer);\n" "}\n" "\n" - "static void pcc_lr_table__hold_answer(%s_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_lr_answer_t *answer) {\n" + "static void pcc_lr_table__hold_answer(pcc_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_lr_answer_t *answer) {\n" " index += table->ofs;\n" " if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);\n" - " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx->auxil);\n" + " if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx);\n" " answer->hold = table->buf[index]->hold_a;\n" " table->buf[index]->hold_a = answer;\n" "}\n" "\n" - "static pcc_lr_head_t *pcc_lr_table__get_head(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index) {\n" + "static pcc_lr_head_t *pcc_lr_table__get_head(pcc_context_t *ctx, pcc_lr_table_t *table, size_t index) {\n" " index += table->ofs;\n" " if (index >= table->len || table->buf[index] == NULL) return NULL;\n" " return table->buf[index]->head;\n" "}\n" "\n" - "static pcc_lr_answer_t *pcc_lr_table__get_answer(pcc_auxil_t auxil, pcc_lr_table_t *table, size_t index, pcc_rule_t rule) {\n" + "static pcc_lr_answer_t *pcc_lr_table__get_answer(pcc_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_rule_t rule) {\n" " index += table->ofs;\n" " if (index >= table->len || table->buf[index] == NULL) return NULL;\n" - " return pcc_lr_memo_map__get(auxil, &table->buf[index]->memos, rule);\n" + " return pcc_lr_memo_map__get(ctx, &table->buf[index]->memos, rule);\n" "}\n" "\n" - "static void pcc_lr_table__shift(%s_context_t *ctx, pcc_lr_table_t *table, size_t count) {\n" + "static void pcc_lr_table__shift(pcc_context_t *ctx, pcc_lr_table_t *table, size_t count) {\n" " size_t i;\n" " if (count > table->len - table->ofs) count = table->len - table->ofs;\n" " for (i = 0; i < count; i++) pcc_lr_table_entry__destroy(ctx, table->buf[table->ofs++]);\n" @@ -4247,15 +4242,14 @@ static bool_t generate(context_t *ctx) { " }\n" "}\n" "\n" - "static void pcc_lr_table__term(%s_context_t *ctx, pcc_lr_table_t *table) {\n" + "static void pcc_lr_table__term(pcc_context_t *ctx, pcc_lr_table_t *table) {\n" " while (table->len > table->ofs) {\n" " table->len--;\n" " pcc_lr_table_entry__destroy(ctx, table->buf[table->len]);\n" " }\n" " PCC_FREE(ctx->auxil, table->buf);\n" "}\n" - "\n", - get_prefix(ctx), get_prefix(ctx), get_prefix(ctx), get_prefix(ctx), get_prefix(ctx), get_prefix(ctx), get_prefix(ctx) + "\n" ); stream__puts( &sstream, @@ -4302,14 +4296,10 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" ); - stream__printf( - &sstream, - "static %s_context_t *pcc_context__create(pcc_auxil_t auxil) {\n" - " %s_context_t *const ctx = (%s_context_t *)PCC_MALLOC(auxil, sizeof(%s_context_t));\n", - get_prefix(ctx), get_prefix(ctx), get_prefix(ctx), get_prefix(ctx) - ); stream__puts( &sstream, + "static pcc_context_t *pcc_context__create(pcc_auxil_t auxil) {\n" + " pcc_context_t *const ctx = (pcc_context_t *)PCC_MALLOC(auxil, sizeof(pcc_context_t));\n" " ctx->pos = 0;\n" " ctx->cur = 0;\n" " ctx->level = 0;\n" @@ -4325,14 +4315,9 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" ); - stream__printf( - &sstream, - "static void pcc_context__destroy(%s_context_t *ctx) {\n", - get_prefix(ctx) - ); stream__puts( &sstream, - " pcc_recycling_list_t *tmp;\n" + "static void pcc_context__destroy(pcc_context_t *ctx) {\n" " if (ctx == NULL) return;\n" " pcc_thunk_array__term(ctx->auxil, &ctx->thunks);\n" " pcc_lr_stack__term(ctx->auxil, &ctx->lrstack);\n" @@ -4345,13 +4330,9 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" ); - stream__printf( - &sstream, - "static size_t pcc_refill_buffer(%s_context_t *ctx, size_t num) {\n", - get_prefix(ctx) - ); stream__puts( &sstream, + "static size_t pcc_refill_buffer(pcc_context_t *ctx, size_t num) {\n" " if (ctx->buffer.len >= ctx->cur + num) return ctx->buffer.len - ctx->cur;\n" " while (ctx->buffer.len < ctx->cur + num) {\n" " const int c = PCC_GETCHAR(ctx->auxil);\n" @@ -4362,14 +4343,10 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" ); - stream__printf( - &sstream, - "MARK_FUNC_AS_USED\n" - "static void pcc_commit_buffer(%s_context_t *ctx) {\n", - get_prefix(ctx) - ); stream__puts( &sstream, + "MARK_FUNC_AS_USED\n" + "static void pcc_commit_buffer(pcc_context_t *ctx) {\n" " memmove(ctx->buffer.buf, ctx->buffer.buf + ctx->cur, ctx->buffer.len - ctx->cur);\n" " ctx->buffer.len -= ctx->cur;\n" " ctx->pos += ctx->cur;\n" @@ -4378,14 +4355,10 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" ); - stream__printf( - &sstream, - "MARK_FUNC_AS_USED\n" - "static const char *pcc_get_capture_string(%s_context_t *ctx, const pcc_capture_t *capt) {\n", - get_prefix(ctx) - ); stream__puts( &sstream, + "MARK_FUNC_AS_USED\n" + "static const char *pcc_get_capture_string(pcc_context_t *ctx, const pcc_capture_t *capt) {\n" " if (capt->string == NULL)\n" " ((pcc_capture_t *)capt)->string =\n" " pcc_strndup_e(ctx->auxil, ctx->buffer.buf + capt->range.start, capt->range.end - capt->range.start);\n" @@ -4394,13 +4367,9 @@ static bool_t generate(context_t *ctx) { "\n" ); if (ctx->flags & CODE_FLAG__UTF8_CHARCLASS_USED) { - stream__printf( - &sstream, - "static size_t pcc_get_char_as_utf32(%s_context_t *ctx, int *out) { /* with checking UTF-8 validity */\n", - get_prefix(ctx) - ); stream__puts( &sstream, + "static size_t pcc_get_char_as_utf32(pcc_context_t *ctx, int *out) { /* with checking UTF-8 validity */\n" " int c, u;\n" " size_t n;\n" " if (pcc_refill_buffer(ctx, 1) < 1) return 0;\n" @@ -4454,19 +4423,15 @@ static bool_t generate(context_t *ctx) { "\n" ); } - stream__printf( - &sstream, - "MARK_FUNC_AS_USED\n" - "static pcc_bool_t pcc_apply_rule(%s_context_t *ctx, pcc_rule_t rule, pcc_thunk_array_t *thunks, pcc_value_t *value) {\n", - get_prefix(ctx) - ); stream__puts( &sstream, + "MARK_FUNC_AS_USED\n" + "static pcc_bool_t pcc_apply_rule(pcc_context_t *ctx, pcc_rule_t rule, pcc_thunk_array_t *thunks, pcc_value_t *value) {\n" " static pcc_value_t null;\n" " pcc_thunk_chunk_t *c = NULL;\n" " const size_t p = ctx->pos + ctx->cur;\n" " pcc_bool_t b = PCC_TRUE;\n" - " pcc_lr_answer_t *a = pcc_lr_table__get_answer(ctx->auxil, &ctx->lrtable, p, rule);\n" + " pcc_lr_answer_t *a = pcc_lr_table__get_answer(ctx, &ctx->lrtable, p, rule);\n" " pcc_lr_head_t *h = pcc_lr_table__get_head(ctx->auxil, &ctx->lrtable, p);\n" " if (h != NULL) {\n" " if (a == NULL && rule != h->rule && pcc_rule_set__index(ctx->auxil, &h->invol, rule) == PCC_VOID_VALUE) {\n" @@ -4561,14 +4526,10 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" ); - stream__printf( - &sstream, - "MARK_FUNC_AS_USED\n" - "static void pcc_do_action(%s_context_t *ctx, const pcc_thunk_array_t *thunks, pcc_value_t *value) {\n", - get_prefix(ctx) - ); stream__puts( &sstream, + "MARK_FUNC_AS_USED\n" + "static void pcc_do_action(pcc_context_t *ctx, const pcc_thunk_array_t *thunks, pcc_value_t *value) {\n" " size_t i;\n" " for (i = 0; i < thunks->len; i++) {\n" " pcc_thunk_t *const thunk = thunks->buf[i];\n" @@ -4712,8 +4673,8 @@ static bool_t generate(context_t *ctx) { for (i = 0; i < ctx->rules.len; i++) { stream__printf( &sstream, - "static pcc_thunk_chunk_t *pcc_evaluate_rule_%s(%s_context_t *ctx);\n", - ctx->rules.buf[i]->data.rule.name, get_prefix(ctx) + "static pcc_thunk_chunk_t *pcc_evaluate_rule_%s(pcc_context_t *ctx);\n", + ctx->rules.buf[i]->data.rule.name ); } stream__puts( @@ -4729,8 +4690,8 @@ static bool_t generate(context_t *ctx) { g.ascii = ctx->opts.ascii; stream__printf( &sstream, - "static pcc_thunk_chunk_t *pcc_evaluate_rule_%s(%s_context_t *ctx) {\n", - ctx->rules.buf[i]->data.rule.name, get_prefix(ctx) + "static pcc_thunk_chunk_t *pcc_evaluate_rule_%s(pcc_context_t *ctx) {\n", + ctx->rules.buf[i]->data.rule.name ); stream__printf( &sstream, From b3f74549602bfd76f80e75bd89d95f51cb64cbba Mon Sep 17 00:00:00 2001 From: Arihiro Yoshida Date: Fri, 29 Apr 2022 17:30:08 +0900 Subject: [PATCH 5/6] Add a typecast and const modifiers --- src/packcc.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/packcc.c b/src/packcc.c index f0194ec..75eac1d 100644 --- a/src/packcc.c +++ b/src/packcc.c @@ -3903,13 +3903,13 @@ static bool_t generate(context_t *ctx) { "\n" "static void *pcc_memory_recycler__supply(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler) {\n" " if (recycler->list) {\n" - " pcc_recycling_list_t *tmp = recycler->list;\n" + " pcc_recycling_list_t *const tmp = recycler->list;\n" " recycler->list = tmp->next;\n" " return tmp;\n" " }\n" " if (recycler->inuse < recycler->allocated) {\n" - " char *base = recycler->base;\n" - " char *tmp = base + (recycler->inuse * recycler->element_size);\n" + " char *const base = recycler->base;\n" + " char *const tmp = base + (recycler->inuse * recycler->element_size);\n" " recycler->inuse++;\n" " return tmp;\n" " }\n" @@ -3917,16 +3917,16 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" "static void pcc_memory_recycler__recycle(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler, void *obj) {\n" - " pcc_recycling_list_t *tmp = obj;\n" + " pcc_recycling_list_t *const tmp = obj;\n" " tmp->next = recycler->list;\n" " recycler->list = tmp;\n" "}\n" "\n" "static void pcc_memory_recycler__term(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler) {\n" - " char *start = recycler->base;\n" - " char *end = recycler->base + (recycler->allocated * recycler->element_size);\n" + " char *const start = recycler->base;\n" + " char *const end = (char *)recycler->base + (recycler->allocated * recycler->element_size);\n" " while (recycler->list) {\n" - " pcc_recycling_list_t *tmp = recycler->list;\n" + " pcc_recycling_list_t *const tmp = recycler->list;\n" " recycler->list = tmp->next;\n" " if (start <= (char *)tmp && (char *)tmp < end)\n" " continue;\n" From 142660fb1b4082b5cbaa1dcccbc6f98c3d6c9e41 Mon Sep 17 00:00:00 2001 From: Arihiro Yoshida Date: Fri, 29 Apr 2022 21:37:26 +0900 Subject: [PATCH 6/6] Reduce memory allocation frequency --- src/packcc.c | 81 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/src/packcc.c b/src/packcc.c index 75eac1d..de7a799 100644 --- a/src/packcc.c +++ b/src/packcc.c @@ -72,7 +72,7 @@ static size_t strnlen_(const char *str, size_t maxlen) { #undef TRUE /* to avoid macro definition conflicts with the system header file of IBM AIX */ #undef FALSE -#define VERSION "1.7.2" +#define VERSION "1.8.0" #ifndef BUFFER_MIN_SIZE #define BUFFER_MIN_SIZE 256 @@ -3374,6 +3374,10 @@ static bool_t generate(context_t *ctx) { "#define PCC_ARRAY_MIN_SIZE 2\n" "#endif /* !PCC_ARRAY_MIN_SIZE */\n" "\n" + "#ifndef PCC_POOL_MIN_SIZE\n" + "#define PCC_POOL_MIN_SIZE 65536\n" + "#endif /* !PCC_POOL_MIN_SIZE */\n" + "\n" "#define PCC_DBG_EVALUATE 0\n" "#define PCC_DBG_MATCH 1\n" "#define PCC_DBG_NOMATCH 2\n" @@ -3577,16 +3581,23 @@ static bool_t generate(context_t *ctx) { ); stream__puts( &sstream, - "typedef struct pcc_recycling_list_tag {\n" - " struct pcc_recycling_list_tag *next;\n" - "} pcc_recycling_list_t;\n" + "typedef struct pcc_memory_entry_tag pcc_memory_entry_t;\n" + "typedef struct pcc_memory_pool_tag pcc_memory_pool_t;\n" + "\n" + "struct pcc_memory_entry_tag {\n" + " pcc_memory_entry_t *next;\n" + "};\n" + "\n" + "struct pcc_memory_pool_tag {\n" + " pcc_memory_pool_t *next;\n" + " size_t allocated;\n" + " size_t unused;\n" + "};\n" "\n" "typedef struct pcc_memory_recycler_tag {\n" - " pcc_recycling_list_t *list;\n" + " pcc_memory_pool_t *pool_list;\n" + " pcc_memory_entry_t *entry_list;\n" " size_t element_size;\n" - " size_t allocated;\n" - " size_t inuse;\n" - " void *base;\n" "} pcc_memory_recycler_t;\n" "\n" ); @@ -3894,45 +3905,49 @@ static bool_t generate(context_t *ctx) { stream__puts( &sstream, "static void pcc_memory_recycler__init(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler, size_t element_size) {\n" - " recycler->list = NULL;\n" + " recycler->pool_list = NULL;\n" + " recycler->entry_list = NULL;\n" " recycler->element_size = element_size;\n" - " recycler->allocated = 1024 * 1024;\n" - " recycler->inuse = 0;\n" - " recycler->base = PCC_MALLOC(auxil, recycler->element_size * recycler->allocated);\n" "}\n" "\n" "static void *pcc_memory_recycler__supply(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler) {\n" - " if (recycler->list) {\n" - " pcc_recycling_list_t *const tmp = recycler->list;\n" - " recycler->list = tmp->next;\n" + " if (recycler->entry_list) {\n" + " pcc_memory_entry_t *const tmp = recycler->entry_list;\n" + " recycler->entry_list = tmp->next;\n" " return tmp;\n" " }\n" - " if (recycler->inuse < recycler->allocated) {\n" - " char *const base = recycler->base;\n" - " char *const tmp = base + (recycler->inuse * recycler->element_size);\n" - " recycler->inuse++;\n" - " return tmp;\n" + " if (!recycler->pool_list || recycler->pool_list->unused == 0) {\n" + " size_t size = PCC_POOL_MIN_SIZE;\n" + " if (recycler->pool_list) {\n" + " size = recycler->pool_list->allocated << 1;\n" + " if (size == 0) size = recycler->pool_list->allocated;\n" + " }\n" + " {\n" + " pcc_memory_pool_t *const pool = (pcc_memory_pool_t *)PCC_MALLOC(\n" + " auxil, sizeof(pcc_memory_pool_t) + recycler->element_size * size\n" + " );\n" + " pool->allocated = size;\n" + " pool->unused = size;\n" + " pool->next = recycler->pool_list;\n" + " recycler->pool_list = pool;\n" + " }\n" " }\n" - " return PCC_MALLOC(auxil, recycler->element_size);\n" + " recycler->pool_list->unused--;\n" + " return (char *)recycler->pool_list + sizeof(pcc_memory_pool_t) + recycler->element_size * recycler->pool_list->unused;\n" "}\n" "\n" - "static void pcc_memory_recycler__recycle(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler, void *obj) {\n" - " pcc_recycling_list_t *const tmp = obj;\n" - " tmp->next = recycler->list;\n" - " recycler->list = tmp;\n" + "static void pcc_memory_recycler__recycle(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler, void *ptr) {\n" + " pcc_memory_entry_t *const tmp = (pcc_memory_entry_t *)ptr;\n" + " tmp->next = recycler->entry_list;\n" + " recycler->entry_list = tmp;\n" "}\n" "\n" "static void pcc_memory_recycler__term(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler) {\n" - " char *const start = recycler->base;\n" - " char *const end = (char *)recycler->base + (recycler->allocated * recycler->element_size);\n" - " while (recycler->list) {\n" - " pcc_recycling_list_t *const tmp = recycler->list;\n" - " recycler->list = tmp->next;\n" - " if (start <= (char *)tmp && (char *)tmp < end)\n" - " continue;\n" + " while (recycler->pool_list) {\n" + " pcc_memory_pool_t *const tmp = recycler->pool_list;\n" + " recycler->pool_list = tmp->next;\n" " PCC_FREE(auxil, tmp);\n" " }\n" - " PCC_FREE(auxil, recycler->base);\n" "}\n" "\n" );