From: Thibaut VARÈNE Date: 2021年8月14日 13:05:00 +0000 (+0200) Subject: parser: don't malloc() X-Git-Tag: v1.0~28 X-Git-Url: http://vcs.slashdirt.org/git/?a=commitdiff_plain;h=863984e93f6ceadf5b3e912c812025f53199b84a;p=sw%2Ftic2json.git parser: don't malloc() At the cost of a slight stack usage increase, this drastically reduces overall memory usage and improves performance since we don't incur the malloc() penalty. Sample figures from a single run over a 100KB test capture: - Before: thread 1 finished and used 17151 bytes out of 8388608 on its stack. Margin: 8371457 bytes. total heap usage: 13,996 allocs, 13,996 frees, 255,865 bytes allocated - After: thread 1 finished and used 21967 bytes out of 8388608 on its stack. Margin: 8366641 bytes. total heap usage: 9,516 allocs, 9,516 frees, 112,505 bytes allocated --- diff --git a/tic.h b/tic.h new file mode 100644 index 0000000..66bbfd4 --- /dev/null +++ b/tic.h @@ -0,0 +1,24 @@ +// +// tic.h +// +// +// (C) 2021 Thibaut VARENE +// License: GPLv2 - http://www.gnu.org/licenses/gpl-2.0.html +// + +#ifndef tic_h +#define tic_h + +enum f_type { F_STRING, F_INT, F_HEX }; + +struct tic_field { + enum f_type type; + char *label; + char *horodate; + union { + char *s; + int i; + } data; +}; + +#endif /* tic_h */ diff --git a/tic.l b/tic.l index c2eeafc..29b6e85 100644 --- a/tic.l +++ b/tic.l @@ -30,6 +30,7 @@ CHKSUM [\x20-\x5f] SEP \x09 %{ + #include "tic.h" #include "tic.tab.h" static uint8_t checksum; diff --git a/tic.y b/tic.y index 15dc00e..e9ed428 100644 --- a/tic.y +++ b/tic.y @@ -20,6 +20,7 @@ #include #include #include + #include "tic.h" int yylex(); int yylex_destroy(); @@ -30,25 +31,10 @@ static int hooked; static char fdelim; static int mask_allzeros; -enum f_type { F_STRING, F_INT, F_HEX }; - -struct tic_field { - enum f_type type; - char *label; - char *horodate; - union { - char *s; - int i; - } data; -}; - -struct tic_field *make_field(enum f_type type, char *label, char *horodate, char *data) +void make_field(struct tic_field *field, enum f_type type, char *label, char *horodate, char *data) { - struct tic_field *field; - - field = malloc(sizeof(*field)); if (!field) - return NULL; + return; field->label = label; field->horodate = horodate; @@ -68,8 +54,6 @@ struct tic_field *make_field(enum f_type type, char *label, char *horodate, char default: break; } - - return field; } void print_field(struct tic_field *field) @@ -103,14 +87,13 @@ void free_field(struct tic_field *field) default: break; } - free(field); } %} %union { char *text; - struct tic_field *field; + struct tic_field field; } %verbose @@ -133,7 +116,7 @@ void free_field(struct tic_field *field) %type field_horodate field_nodate field %destructor { free($$); } -%destructor { free_field($$); } +%destructor { free_field(&$$); } %destructor { } %% @@ -166,14 +149,13 @@ datasets: dataset: FIELD_START field FIELD_OK { - if (!2ドル) YYABORT; // OOM if (hooked) { - print_field(2ドル); + print_field(&2ドル); fdelim = ','; } - free_field(2ドル); + free_field(&2ドル); } - | FIELD_START field FIELD_KO { if (!2ドル) YYABORT; fprintf(stderr, "dataset invalid checksum\n"); free_field(2ドル); } + | FIELD_START field FIELD_KO { fprintf(stderr, "dataset invalid checksum\n"); free_field(&2ドル); } | FIELD_START error FIELD_OK { fprintf(stderr, "unrecognized dataset\n"); yyerrok; } ; @@ -182,15 +164,15 @@ field: field_horodate ; field_horodate: - etiquette_str_horodate TOK_SEP TOK_HDATE TOK_SEP TOK_SEP { $$ = make_field(F_STRING, 1,ドル 3,ドル NULL); } - | etiquette_str_horodate TOK_SEP TOK_HDATE TOK_SEP TOK_DATA TOK_SEP { $$ = make_field(F_STRING, 1,ドル 3,ドル 5ドル); } - | etiquette_int_horodate TOK_SEP TOK_HDATE TOK_SEP TOK_DATA TOK_SEP { $$ = make_field(F_INT, 1,ドル 3,ドル 5ドル); } + etiquette_str_horodate TOK_SEP TOK_HDATE TOK_SEP TOK_SEP { make_field(&$,ドル F_STRING, 1,ドル 3,ドル NULL); } + | etiquette_str_horodate TOK_SEP TOK_HDATE TOK_SEP TOK_DATA TOK_SEP { make_field(&$,ドル F_STRING, 1,ドル 3,ドル 5ドル); } + | etiquette_int_horodate TOK_SEP TOK_HDATE TOK_SEP TOK_DATA TOK_SEP { make_field(&$,ドル F_INT, 1,ドル 3,ドル 5ドル); } ; field_nodate: - etiquette_str_nodate TOK_SEP TOK_DATA TOK_SEP { $$ = make_field(F_STRING, 1,ドル NULL, 3ドル); } - | etiquette_int_nodate TOK_SEP TOK_DATA TOK_SEP { $$ = make_field(F_INT, 1,ドル NULL, 3ドル); } - | etiquette_hex_nodate TOK_SEP TOK_DATA TOK_SEP { $$ = make_field(F_HEX, 1,ドル NULL, 3ドル); } + etiquette_str_nodate TOK_SEP TOK_DATA TOK_SEP { make_field(&$,ドル F_STRING, 1,ドル NULL, 3ドル); } + | etiquette_int_nodate TOK_SEP TOK_DATA TOK_SEP { make_field(&$,ドル F_INT, 1,ドル NULL, 3ドル); } + | etiquette_hex_nodate TOK_SEP TOK_DATA TOK_SEP { make_field(&$,ドル F_HEX, 1,ドル NULL, 3ドル); } ; etiquette_str_horodate:

AltStyle によって変換されたページ (->オリジナル) /