Commit 9c39d340 authored by Vivien Kraus's avatar Vivien Kraus
Browse files

Pack up the allocator

parent 3b2f64d1
Pipeline #66284 passed with stage
in 44 seconds
......@@ -42,4 +42,8 @@
-I@abs_top_builddir@/src/lexer
-I@abs_top_srcdir@/src/parser
-I@abs_top_builddir@/src/parser
-I@abs_top_srcdir@/src/application
-I@abs_top_builddir@/src/application
-I@abs_top_srcdir@/src/program
-I@abs_top_builddir@/src/program
"))))
......@@ -28,9 +28,13 @@ extern "C"
{
#endif /* __cplusplus */
typedef void *(*TarotFmalloc) (size_t, void *);
typedef void *(*TarotFrealloc) (void *, size_t, void *);
typedef void (*TarotFfree) (void *, void *);
typedef struct
{
void *(*f_malloc) (void *data, size_t size);
void *(*f_realloc) (void *data, void *mem, size_t new_size);
void (*f_free) (void *data, void *mem);
void *data;
} TarotAllocator;
#ifdef __cplusplus
}
......
......@@ -38,7 +38,7 @@ typedef struct {
} Argument;
static void *
my_malloc (size_t bytes, void *data)
my_malloc (void *data, size_t bytes)
{
Argument *arg = (Argument *) data;
MemoryBlock * next = arg->stack;
......@@ -76,7 +76,7 @@ find_parent (MemoryBlock *root, MemoryBlock *block)
}
static void *
my_realloc (void *memory, size_t bytes, void *data)
my_realloc (void *data, void *memory, size_t bytes)
{
Argument *arg = (Argument *) data;
MemoryBlock * root = arg->stack;
......@@ -97,7 +97,7 @@ my_realloc (void *memory, size_t bytes, void *data)
}
static void
my_free (void *memory, void *data)
my_free (void *data, void *memory)
{
Argument *arg = (Argument *) data;
MemoryBlock * root = arg->stack;
......@@ -144,6 +144,13 @@ test_case (const char *input, size_t n_exp, ExpectedToken *expected)
unsigned int value;
int actual;
FILE *file = fmemopen ((char *) input, strlen (input), "r");
TarotAllocator allocator =
{
.f_malloc = my_malloc,
.f_realloc = my_realloc,
.f_free = my_free,
.data = &arg
};
if (file == NULL)
{
fprintf (stderr, "Could not fmemopen.\n");
......@@ -151,7 +158,7 @@ test_case (const char *input, size_t n_exp, ExpectedToken *expected)
}
arg.stack = NULL;
tarot_lexer_location_start (&loc);
lexer = tarot_lexer_alloc (file, &my_malloc, &my_realloc, &my_free, &arg);
lexer = tarot_lexer_alloc (file, allocator);
if (lexer == NULL)
{
fprintf (stderr, "Could not allocate the lexer.\n");
......
......@@ -38,10 +38,7 @@ int u8_mblen (unsigned char *, size_t);
typedef struct
{
TarotFmalloc m_malloc;
TarotFrealloc m_realloc;
TarotFfree m_free;
void *user_data;
TarotAllocator m_allocator;
} scanner_interface;
static unsigned int parse_card (const char *text);
......@@ -188,14 +185,15 @@ void *
_tarot_lexer_alloc (size_t bytes, void *yyscanner)
{
struct yyguts_t *yyg = (struct yyguts_t *) yyscanner;
return yyextra.m_malloc (bytes, yyextra.user_data);
return yyextra.m_allocator.f_malloc (yyextra.m_allocator.data, bytes);
}
void *
_tarot_lexer_realloc (void *memory, size_t bytes, void *yyscanner)
{
struct yyguts_t *yyg = (struct yyguts_t *) yyscanner;
return yyextra.m_realloc (memory, bytes, yyextra.user_data);
return yyextra.m_allocator.f_realloc (yyextra.m_allocator.data, memory,
bytes);
}
void
......@@ -205,7 +203,7 @@ _tarot_lexer_free (void *memory, void *yyscanner)
if (memory != NULL)
{
/* the scanner does not ensure the argument is non-null */
yyextra.m_free (memory, yyextra.user_data);
yyextra.m_allocator.f_free (yyextra.m_allocator.data, memory);
}
}
......@@ -215,23 +213,19 @@ struct TarotLexer
};
TarotLexer *
tarot_lexer_alloc (FILE * input, TarotFmalloc fmalloc, TarotFrealloc frealloc,
TarotFfree ffree, void *user_data)
tarot_lexer_alloc (FILE * input, TarotAllocator allocator)
{
TarotLexer *ret = NULL;
scanner_interface f;
f.m_malloc = fmalloc;
f.m_realloc = frealloc;
f.m_free = ffree;
f.user_data = user_data;
ret = fmalloc (sizeof (TarotLexer), user_data);
f.m_allocator = allocator;
ret = allocator.f_malloc (allocator.data, sizeof (TarotLexer));
if (ret != NULL)
{
int error = _tarot_lexer_lex_init_extra (f, &(ret->scan));
_tarot_lexer_set_in (input, ret->scan);
if (error != 0)
{
ffree (ret, user_data);
allocator.f_free (allocator.data, ret);
ret = NULL;
}
}
......@@ -251,10 +245,9 @@ tarot_lexer_free (TarotLexer * lexer)
void *yyscanner = lexer->scan;
struct yyguts_t *yyg = (struct yyguts_t *) yyscanner;
scanner_interface interface = yyextra;
TarotFfree m_free = interface.m_free;
void *user_data = interface.user_data;
TarotAllocator allocator = interface.m_allocator;
_tarot_lexer_lex_destroy (lexer->scan);
m_free (lexer, user_data);
allocator.f_free (allocator.data, lexer);
}
void
......
......@@ -67,9 +67,7 @@ extern "C"
unsigned int last_column;
} TarotLexerLType;
TarotLexer *tarot_lexer_alloc (FILE * file, TarotFmalloc fmalloc,
TarotFrealloc frealloc, TarotFfree ffree,
void *user_data);
TarotLexer *tarot_lexer_alloc (FILE * file, TarotAllocator allocator);
void tarot_lexer_location_start (TarotLexerLType * loc);
......
......@@ -53,7 +53,7 @@ fake_lex (void *argument, unsigned int *value, TarotLexerLType *loc)
}
static void *
fake_malloc (size_t bytes, void *data)
fake_malloc (void *data, size_t bytes)
{
Argument *arg = (Argument *) data;
void *ret = malloc (bytes);
......@@ -67,14 +67,14 @@ fake_malloc (size_t bytes, void *data)
}
static void *
fake_realloc (void *memory, size_t bytes, void *data)
fake_realloc (void *data, void *memory, size_t bytes)
{
(void) data;
return realloc (memory, bytes);
}
static void
fake_free (void *memory, void *data)
fake_free (void *data, void *memory)
{
Argument *arg = (Argument *) data;
free (memory);
......@@ -367,12 +367,19 @@ test_case (size_t n_tokens, const int *tokens, const unsigned int *values, const
TarotLexerLType actual_location = { 42, 42, 42, 42 };
char *cmd_ptr = NULL;
TarotCommand *cmd = NULL;
TarotAllocator allocator =
{
.f_malloc = fake_malloc,
.f_realloc = fake_realloc,
.f_free = fake_free,
.data = &arg
};
arg.n_allocs = 0;
arg.n_lex = n_tokens;
arg.lex = tokens;
arg.values = values;
arg.locations = locations;
parser = tarot_parser_alloc (fake_lex, fake_malloc, fake_realloc, fake_free, &arg);
parser = tarot_parser_alloc (fake_lex, &arg, allocator);
assert (parser != NULL);
tarot_lexer_location_start (&actual_location);
error = tarot_parser_parse (parser, &actual_location);
......
......@@ -35,10 +35,8 @@ typedef struct
struct TarotParser
{
TarotFlex flex;
TarotFmalloc fmalloc;
TarotFrealloc frealloc;
TarotFfree ffree;
void *user_data;
void *flex_data;
TarotAllocator allocator;
TarotCommand *command;
TarotLexerLType position;
TarotLexerLType next_token_position;
......@@ -83,8 +81,8 @@ static TarotLexerLType location_extent (TarotLexerLType before,
TarotLexerLType after);
#define yyparse _tarot_parser_yyparse
#define YYMALLOC(what) parser->fmalloc (what, parser->user_data)
#define YYFREE(what) parser->ffree (what, parser->user_data)
#define YYMALLOC(what) parser->allocator.f_malloc (parser->allocator.data, what)
#define YYFREE(what) parser->allocator.f_free (parser->allocator.data, what)
#define YYLTYPE TarotLexerLType
#include "tarot.c"
......@@ -540,7 +538,7 @@ yylex (void *yylval_ptr, void *loc_ptr, TarotParser * parser)
YYSTYPE *yylval = (YYSTYPE *) yylval_ptr;
YYLTYPE *yylloc = (YYLTYPE *) loc_ptr;
tk =
parser->flex (parser->user_data, &value, &(parser->next_token_position));
parser->flex (parser->flex_data, &value, &(parser->next_token_position));
*yylloc = parser->next_token_position;
switch (tk)
{
......@@ -601,18 +599,15 @@ yyerror (void *yylloc_ptr, TarotParser * parser, const char *str)
}
TarotParser *
tarot_parser_alloc (TarotFlex flex, TarotFmalloc fmalloc,
TarotFrealloc frealloc, TarotFfree ffree, void *user_data)
tarot_parser_alloc (TarotFlex flex, void *flex_data, TarotAllocator allocator)
{
TarotParser *ret = NULL;
ret = fmalloc (sizeof (TarotParser), user_data);
ret = allocator.f_malloc (allocator.data, sizeof (TarotParser));
if (ret != NULL)
{
ret->flex = flex;
ret->fmalloc = fmalloc;
ret->frealloc = frealloc;
ret->ffree = ffree;
ret->user_data = user_data;
ret->flex_data = flex_data;
ret->allocator = allocator;
ret->command = NULL;
tarot_lexer_location_start (&(ret->next_token_position));
}
......@@ -656,11 +651,10 @@ tarot_parser_get_location (const TarotParser * parser, TarotLexerLType * loc)
void
tarot_parser_free (TarotParser * parser)
{
TarotFfree ffree = parser->ffree;
void *user_data = parser->user_data;
TarotAllocator allocator = parser->allocator;
if (parser->command != NULL)
{
ffree (parser->command, user_data);
YYFREE (parser->command);
}
ffree (parser, user_data);
allocator.f_free (allocator.data, parser);
}
......@@ -37,9 +37,8 @@ extern "C"
typedef int (*TarotFlex) (void *, unsigned int *, TarotLexerLType *);
TarotParser *tarot_parser_alloc (TarotFlex flex, TarotFmalloc fmalloc,
TarotFrealloc frealloc, TarotFfree ffree,
void *user_data);
TarotParser *tarot_parser_alloc (TarotFlex flex, void *flex_data,
TarotAllocator allocator);
int tarot_parser_parse (TarotParser * parser, const TarotLexerLType * loc);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment