Changeset: 47adc866f4a0 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB/rev/47adc866f4a0 Modified Files: clients/examples/C/murltest.c clients/examples/C/murltest.h clients/examples/C/testsfile.c clients/mapilib/Tests/murltest.py Branch: odbc_loader Log Message:
Add test for custom msettings allocator diffs (145 lines): diff --git a/clients/examples/C/murltest.c b/clients/examples/C/murltest.c --- a/clients/examples/C/murltest.c +++ b/clients/examples/C/murltest.c @@ -19,7 +19,7 @@ #include <stdlib.h> #include <string.h> -char *USAGE = "Usage: murltest TESTFILES.."; +char *USAGE = "Usage: murltest [-c] [-v[v[v]]] TESTFILES.."; static bool run_file(const char *filename, int verbose) @@ -71,7 +71,9 @@ main(int argc, char **argv) *next_slot++ = arg; continue; } - if (arg[1] == 'v') { + if (strcmp(arg, "-c") == 0) { + use_custom_allocator(); + }else if (arg[1] == 'v') { char *p = &arg[1]; while (*p == 'v') { p++; diff --git a/clients/examples/C/murltest.h b/clients/examples/C/murltest.h --- a/clients/examples/C/murltest.h +++ b/clients/examples/C/murltest.h @@ -11,7 +11,10 @@ */ #include "monetdb_config.h" +#include "stream.h" +#include "msettings.h" -#include "stream.h" + +void use_custom_allocator(void); bool run_tests(stream *s, int verbose); diff --git a/clients/examples/C/testsfile.c b/clients/examples/C/testsfile.c --- a/clients/examples/C/testsfile.c +++ b/clients/examples/C/testsfile.c @@ -27,6 +27,7 @@ static int start_line = -1; static int nstarted = 0; static msettings *mp = NULL; +static msettings_allocator allocator = NULL; static bool verify_roundtrip(const char *location) @@ -54,7 +55,7 @@ bool verify_roundtrip(const char *locati return false; } - msettings *tmp = msettings_create(); + msettings *tmp = msettings_create_with(allocator, NULL); if (tmp == NULL) { fprintf(stderr, "malloc failed\n"); return false; @@ -378,7 +379,7 @@ handle_line(int lineno, const char *loca // block starts here nstarted++; start_line = lineno; - mp = msettings_create(); + mp = msettings_create_with(allocator, NULL); if (mp == NULL) { fprintf(stderr, "%s: malloc failed\n", location); return false; @@ -534,3 +535,49 @@ run_tests(stream *s, int verbose) } return ok; } + +// Our custom allocator stores a magic cookie before every +// allocation. +// +// This allows us to detect that the memory we're free'ing +// wasn't allocated by us. +// Also, if the standard allocator free's memory allocated by +// us, it or Valgrind will hopefully notice that the pointer +// is wrong. +static void * +custom_allocator(void *state, void *old, size_t size) +{ + (void)state; + const size_t prefix_size = 64; + const char cookie[] = "AllocCookie"; + const size_t cookie_size = sizeof(cookie); + assert(cookie_size == 5 + 6 + 1); + + if (old) { + old = (char*)old - prefix_size; + // check for cookie and erase it + if (memcmp(old, cookie, cookie_size) != 0) { + assert(0 && "custom allocator cookie missing"); + abort(); + } + memset(old, '\0', cookie_size); + } + + char *new_allocation = realloc(old, size > 0 ? size + prefix_size: 0); + assert(size > 0 || new_allocation == NULL); + + if (new_allocation) { + // set magic cookie + memcpy(new_allocation, cookie, cookie_size); + new_allocation += prefix_size; + } + + return new_allocation; +} + + +void +use_custom_allocator(void) +{ + allocator = custom_allocator; +} diff --git a/clients/mapilib/Tests/murltest.py b/clients/mapilib/Tests/murltest.py --- a/clients/mapilib/Tests/murltest.py +++ b/clients/mapilib/Tests/murltest.py @@ -17,10 +17,17 @@ default_tests_file = os.path.join(test_d tests_file = os.environ.get('TST_TESTS_MD', default_tests_file) -cmd = [ 'murltest', tests_file ] -try: - subprocess.run(cmd, check=True) -except subprocess.CalledProcessError as e: - print(f"Command {cmd} failed.", file=sys.stderr) - print(f"Add -v, -vv or -vvv to get more context", file=sys.stderr) - exit(1) +commands = [ + # with standard allocator + [ 'murltest', tests_file ], + # with custom allocator + [ 'murltest', '-c', tests_file ], +] + +for cmd in commands: + try: + subprocess.run(cmd, check=True) + except subprocess.CalledProcessError as e: + print(f"Command {cmd} failed.", file=sys.stderr) + print(f"Add -v, -vv or -vvv to get more context", file=sys.stderr) + exit(1) _______________________________________________ checkin-list mailing list -- checkin-list@monetdb.org To unsubscribe send an email to checkin-list-le...@monetdb.org