]> vcs.slashdirt.org Git - sw/tic2json.git/commitdiff

vcs.slashdirt.org Git - sw/tic2json.git/commitdiff

git git / sw / tic2json.git / commitdiff
? search:
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 00fd77b)
parser: don't malloc()
2021年8月14日 13:05:00 +0000 (15:05 +0200)
2021年8月15日 11:21:02 +0000 (13:21 +0200)
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

tic.h [new file with mode: 0644] patch | blob

diff --git a/tic.h b/tic.h
new file mode 100644 (file)
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 c2eeafce9427b9ba3255b6ced9f3dfecbf994cfd..29b6e851659c4445984b9e68cf5c44637cfdeb0d 100644 (file)
--- 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 15dc00eeddc920cebd7a3133ca83d66dad27087b..e9ed428eac236f3753adbd043b9a9e9d299ea407 100644 (file)
--- a/tic.y
+++ b/tic.y
@@ -20,6 +20,7 @@
#include <string.h>
#include <inttypes.h>
#include <unistd.h>
+ #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> field_horodate field_nodate field
%destructor { free($$); } <text>
-%destructor { free_field($$); } <field>
+%destructor { free_field(&$$); } <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:
tic2json TIC parser/converter
RSS Atom

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