From 84b79160c8ed3037bfc4affedb8c195157e28d78 Mon Sep 17 00:00:00 2001 From: Vlasta Vesely Date: Sun, 20 May 2018 08:29:33 +0200 Subject: [PATCH] do not allocate translit. contexts, use initialized static vars --- iast-czech.c | 15 +++++++-------- iast-czech.h | 2 +- iast.c | 16 +++++++--------- iast.h | 2 +- main.c | 12 +++++------- transliteration.c | 7 +------ transliteration.h | 4 +--- 7 files changed, 23 insertions(+), 35 deletions(-) diff --git a/iast-czech.c b/iast-czech.c index 57d8fee..e2dab57 100644 --- a/iast-czech.c +++ b/iast-czech.c @@ -163,13 +163,12 @@ static const transliteration_filter_t filters[] = { NULL }; -struct transliteration_context *transliteration_context_iast_czech_alloc() +static const struct transliteration_context context = { + .table = table, + .filters = filters +}; + +const struct transliteration_context *get_iast_czech_transliteration_context() { - struct transliteration_context *context; - - context = malloc(sizeof(*context)); - context->table = table; - context->filters = filters; - - return context; + return &context; } diff --git a/iast-czech.h b/iast-czech.h index 7931273..fc3bad2 100644 --- a/iast-czech.h +++ b/iast-czech.h @@ -5,6 +5,6 @@ #include "transliteration.h" -struct transliteration_context *transliteration_context_iast_czech_alloc(); +const struct transliteration_context *get_iast_czech_transliteration_context(); #endif /* __IAST_CZECH_H */ diff --git a/iast.c b/iast.c index 5a2fe84..20c129d 100644 --- a/iast.c +++ b/iast.c @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0 */ #include -#include #include "iast.h" #include "transliteration.h" @@ -103,13 +102,12 @@ static const transliteration_filter_t filters[] = { NULL }; -struct transliteration_context *transliteration_context_iast_alloc() +static const struct transliteration_context context = { + .table = table, + .filters = filters +}; + +const struct transliteration_context *get_iast_transliteration_context() { - struct transliteration_context *context; - - context = malloc(sizeof(*context)); - context->table = table; - context->filters = filters; - - return context; + return &context; } diff --git a/iast.h b/iast.h index 90f0a06..9c0ef50 100644 --- a/iast.h +++ b/iast.h @@ -5,6 +5,6 @@ #include "transliteration.h" -struct transliteration_context *transliteration_context_iast_alloc(); +const struct transliteration_context *get_iast_transliteration_context(); #endif /* __IAST_H */ diff --git a/main.c b/main.c index b822371..a8adc69 100644 --- a/main.c +++ b/main.c @@ -36,7 +36,6 @@ const char *usage_str = static void usage() { fprintf(stdout, "%s\n", usage_str); - exit(0); } static char *stdin_read() @@ -67,7 +66,7 @@ int main(int argc, const char **argv) const char *arg; const char *queue[argc]; char *input, *output; - struct transliteration_context *context; + const struct transliteration_context *context; for (i = 1; i < argc; i++) { arg = argv[i]; @@ -85,6 +84,7 @@ int main(int argc, const char **argv) continue; case 'h': usage(); + goto out; } fprintf(stderr, "error: unknown option '%s'\n", arg); @@ -104,15 +104,15 @@ int main(int argc, const char **argv) } context = (flags & FLAG_CZECH) - ? transliteration_context_iast_czech_alloc() - : transliteration_context_iast_alloc(); + ? get_iast_czech_transliteration_context() + : get_iast_transliteration_context(); if (flags & FLAG_STDIN) { input = stdin_read(); if (input == NULL) { fprintf(stderr, "[iast] failed to read from STDIN.\n"); retval = -1; - goto drop_context; + goto out; } output = transliterate_devanagari_to_latin(input, context); @@ -127,8 +127,6 @@ int main(int argc, const char **argv) free(output); } -drop_context: - transliteration_context_drop(context); out: return retval; } diff --git a/transliteration.c b/transliteration.c index 1b85cd2..cc2626f 100644 --- a/transliteration.c +++ b/transliteration.c @@ -48,7 +48,7 @@ static void apply_transliteration_filters(struct syllable *head, } char *transliterate_devanagari_to_latin(const char *text, - struct transliteration_context *context) + const struct transliteration_context *context) { unsigned int length = strlen(text); const char *ptr = text; @@ -90,8 +90,3 @@ char *transliterate_devanagari_to_latin(const char *text, return tmp; } - -void transliteration_context_drop(struct transliteration_context *context) -{ - free(context); -} diff --git a/transliteration.h b/transliteration.h index 18fbcbd..a805c15 100644 --- a/transliteration.h +++ b/transliteration.h @@ -23,9 +23,7 @@ struct transliteration_context { }; char *transliterate_devanagari_to_latin(const char *text, - struct transliteration_context *context); - -void transliteration_context_drop(struct transliteration_context *context); + const struct transliteration_context *context); static inline int is_devanagari(unsigned int code) {