Skip to content

Commit

Permalink
Eliminate global variable mem_os
Browse files Browse the repository at this point in the history
Required for vnmakarov#12.
  • Loading branch information
TheCount committed Mar 1, 2022
1 parent 7019672 commit d0e378f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 42 deletions.
30 changes: 12 additions & 18 deletions src/yaep.c
Original file line number Diff line number Diff line change
Expand Up @@ -6341,24 +6341,24 @@ yaep_free_tree (struct yaep_tree_node *root, void (*parse_free) (void *),

#ifdef YAEP_TEST

/* All parse_alloc memory is contained here. */
#ifndef __cplusplus
static os_t mem_os;
#else
static os_t *mem_os;
#endif

static void *
test_parse_alloc (int size)
{
void *result;

OS_TOP_EXPAND (mem_os, size);
result = OS_TOP_BEGIN (mem_os);
OS_TOP_FINISH (mem_os);
assert ((size > 0) && ((unsigned int) size == (size_t) size));
result = malloc (size);
assert (result != NULL);

return result;
}

static void
test_parse_free (void * mem)
{
free( mem );
}

/* The following variable is the current number of next input grammar
terminal. */
static int nterm;
Expand Down Expand Up @@ -6509,7 +6509,6 @@ use_functions (int argc, char **argv)
fprintf (stderr, "No memory\n");
exit (1);
}
OS_CREATE (mem_os, grammar->alloc, 0);
yaep_set_one_parse_flag (g, FALSE);
if (argc > 1)
yaep_set_lookahead_level (g, atoi (argv[1]));
Expand All @@ -6524,14 +6523,12 @@ use_functions (int argc, char **argv)
if (yaep_read_grammar (g, TRUE, read_terminal, read_rule) != 0)
{
fprintf (stderr, "%s\n", yaep_error_message (g));
OS_DELETE (mem_os);
exit (1);
}
ntok = 0;
if (yaep_parse (g, test_read_token, test_syntax_error, test_parse_alloc,
NULL, &root, &ambiguous_p))
test_parse_free, &root, &ambiguous_p))
fprintf (stderr, "yaep_parse: %s\n", yaep_error_message (g));
OS_DELETE (mem_os);
yaep_free_grammar (g);
}
#endif
Expand Down Expand Up @@ -6563,7 +6560,6 @@ use_description (int argc, char **argv)
fprintf (stderr, "yaep_create_grammar: No memory\n");
exit (1);
}
OS_CREATE (mem_os, grammar->alloc, 0);
yaep_set_one_parse_flag (g, FALSE);
if (argc > 1)
yaep_set_lookahead_level (g, atoi (argv[1]));
Expand All @@ -6578,13 +6574,11 @@ use_description (int argc, char **argv)
if (yaep_parse_grammar (g, TRUE, description) != 0)
{
fprintf (stderr, "%s\n", yaep_error_message (g));
OS_DELETE (mem_os);
exit (1);
}
if (yaep_parse (g, test_read_token, test_syntax_error, test_parse_alloc,
NULL, &root, &ambiguous_p))
test_parse_free, &root, &ambiguous_p))
fprintf (stderr, "yaep_parse: %s\n", yaep_error_message (g));
OS_DELETE (mem_os);
yaep_free_grammar (g);
}
#endif
Expand Down
28 changes: 4 additions & 24 deletions src/yaep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,12 @@ use_functions (int argc, char **argv)
struct yaep_tree_node *root;
int ambiguous_p;

YaepAllocator *alloc = yaep_alloc_new (NULL, NULL, NULL, NULL);
if (alloc == NULL)
{
exit (1);
}
nterm = nrule = 0;
OS_CREATE (mem_os, alloc, 0);
fprintf (stderr, "Use functions\n");
e = new yaep ();
if (e == NULL)
{
fprintf (stderr, "yaep::yaep: No memory\n");
OS_DELETE (mem_os);
exit (1);
}
e->set_one_parse_flag (FALSE);
Expand All @@ -211,16 +204,13 @@ use_functions (int argc, char **argv)
if (e->read_grammar (TRUE, read_terminal, read_rule) != 0)
{
fprintf (stderr, "%s\n", e->error_message ());
OS_DELETE (mem_os);
exit (1);
}
ntok = 0;
if (e->parse (test_read_token, test_syntax_error, test_parse_alloc, NULL,
&root, &ambiguous_p))
if (e->parse (test_read_token, test_syntax_error, test_parse_alloc,
test_parse_free, &root, &ambiguous_p))
fprintf (stderr, "yaep parse: %s\n", e->error_message ());
delete e;
OS_DELETE (mem_os);
yaep_alloc_del (alloc);
}

static void
Expand All @@ -230,18 +220,11 @@ use_description (int argc, char **argv)
struct yaep_tree_node *root;
int ambiguous_p;

YaepAllocator *alloc = yaep_alloc_new (NULL, NULL, NULL, NULL);
if (alloc == NULL)
{
exit (1);
}
fprintf (stderr, "Use description\n");
OS_CREATE (mem_os, alloc, 0);
e = new yaep ();
if (e == NULL)
{
fprintf (stderr, "yaep::yaep: No memory\n");
OS_DELETE (mem_os);
exit (1);
}
e->set_one_parse_flag (FALSE);
Expand All @@ -258,15 +241,12 @@ use_description (int argc, char **argv)
if (e->parse_grammar (TRUE, description) != 0)
{
fprintf (stderr, "%s\n", e->error_message ());
OS_DELETE (mem_os);
exit (1);
}
if (e->parse (test_read_token, test_syntax_error, test_parse_alloc, NULL,
&root, &ambiguous_p))
if (e->parse (test_read_token, test_syntax_error, test_parse_alloc,
test_parse_free, &root, &ambiguous_p))
fprintf (stderr, "yaep::parse: %s\n", e->error_message ());
delete e;
OS_DELETE (mem_os);
yaep_alloc_del (alloc);
}

#endif /* #ifdef YAEP_TEST */

0 comments on commit d0e378f

Please sign in to comment.