[Gmp-commit] /var/hg/gmp: mini-gmp: Use custom memory allocation for testsuite.
mercurial at gmplib.org
mercurial at gmplib.org
Fri Jan 18 21:59:26 CET 2013
details: /var/hg/gmp/rev/66f52077e390
changeset: 15304:66f52077e390
user: Niels M?ller <nisse at lysator.liu.se>
date: Fri Jan 18 21:59:23 2013 +0100
description:
mini-gmp: Use custom memory allocation for testsuite.
diffstat:
ChangeLog | 12 ++++++
mini-gmp/tests/t-double.c | 2 +-
mini-gmp/tests/t-gcd.c | 5 ++
mini-gmp/tests/t-str.c | 2 +-
mini-gmp/tests/testutils.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++
mini-gmp/tests/testutils.h | 3 +
6 files changed, 108 insertions(+), 2 deletions(-)
diffs (173 lines):
diff -r 8794a8f80fc0 -r 66f52077e390 ChangeLog
--- a/ChangeLog Fri Jan 18 21:15:02 2013 +0100
+++ b/ChangeLog Fri Jan 18 21:59:23 2013 +0100
@@ -1,5 +1,17 @@
2013-01-18 Niels Möller <nisse at lysator.liu.se>
+ * mini-gmp/tests/t-gcd.c (gcdext_valid_p): Fixed memory leak.
+
+ * mini-gmp/tests/t-double.c (testmain): Call tu_free rather than
+ free, for storage allocated by mpz_get_str.
+ * mini-gmp/tests/t-str.c (testmain): Likewise.
+
+ * mini-gmp/tests/testutils.c (block_init, block_check): New
+ functions.
+ (tu_alloc, tu_realloc, tu_free): New functions.
+ (main): Use mp_set_memory_functions.
+ * mini-gmp/tests/testutils.h (tu_free): Declare.
+
* mini-gmp/tests/testutils.h: New file, declarations for test
programs.
diff -r 8794a8f80fc0 -r 66f52077e390 mini-gmp/tests/t-double.c
--- a/mini-gmp/tests/t-double.c Fri Jan 18 21:15:02 2013 +0100
+++ b/mini-gmp/tests/t-double.c Fri Jan 18 21:59:23 2013 +0100
@@ -75,7 +75,7 @@
values[i].d, s, values[i].s);
abort ();
}
- free(s);
+ tu_free(s, 0);
mpz_clear (x);
}
diff -r 8794a8f80fc0 -r 66f52077e390 mini-gmp/tests/t-gcd.c
--- a/mini-gmp/tests/t-gcd.c Fri Jan 18 21:15:02 2013 +0100
+++ b/mini-gmp/tests/t-gcd.c Fri Jan 18 21:59:23 2013 +0100
@@ -100,6 +100,11 @@
if (mpz_cmpabs (r, ta) > 0)
return 0;
}
+
+ mpz_clear (ta);
+ mpz_clear (tb);
+ mpz_clear (r);
+
return 1;
}
diff -r 8794a8f80fc0 -r 66f52077e390 mini-gmp/tests/t-str.c
--- a/mini-gmp/tests/t-str.c Fri Jan 18 21:15:02 2013 +0100
+++ b/mini-gmp/tests/t-str.c Fri Jan 18 21:59:23 2013 +0100
@@ -170,7 +170,7 @@
}
}
free (ap);
- free (bp);
+ tu_free (bp, 0);
}
}
mpz_clear (a);
diff -r 8794a8f80fc0 -r 66f52077e390 mini-gmp/tests/testutils.c
--- a/mini-gmp/tests/testutils.c Fri Jan 18 21:15:02 2013 +0100
+++ b/mini-gmp/tests/testutils.c Fri Jan 18 21:59:23 2013 +0100
@@ -23,13 +23,99 @@
works. */
#include "../mini-gmp.c"
+static size_t total_alloc = 0;
+
+/* Custom memory allocation to track memory usage, and add a small red
+ zone.
+
+ About alignment: In general, getting a block from malloc, and
+ incrementing it by sizeof(size_t), like we do here, might give a
+ pointer which is not properlt unaligned for all types. But the
+ largest type we allocate space for is unsigned long (mp_limb_t),
+ which shouldn't have stricter alignment requirements than
+ size_t. */
+
+static char block_end[8] =
+ { 0x7c, 0x37, 0xd6, 0x12, 0xa8, 0x6c, 0x01, 0xd1 };
+
+static void *
+block_init (size_t *block, size_t size)
+{
+ char *p;
+ *block++ = size;
+
+ p = (char *) block;
+ memcpy (p + size, block_end, sizeof(block_end));
+
+ total_alloc += size;
+ return p;
+}
+
+/* Check small redzone, return pointer to malloced block. */
+size_t *
+block_check (char *p)
+{
+ size_t *block = (size_t *) p - 1;
+ size_t size = block[0];
+
+ if (memcmp (p + size, block_end, sizeof(block_end)) != 0)
+ {
+ fprintf (stderr, "red zone overwritten.\n");
+ abort ();
+ }
+ total_alloc -= size;
+ return block;
+}
+
+static void *
+tu_alloc (size_t size)
+{
+ size_t *block = malloc (sizeof(size_t) + size + sizeof(block_end));
+ if (!block)
+ {
+ fprintf (stderr, "Virtual memory exhausted.\n");
+ abort ();
+ }
+
+ return block_init (block, size);
+}
+
+static void *
+tu_realloc (void *p, size_t old_size, size_t new_size)
+{
+ size_t *block = block_check (p);
+ block = realloc (block, sizeof(size_t) + new_size + sizeof(block_end));
+ if (!block)
+ {
+ fprintf (stderr, "Virtual memory exhausted.\n");
+ abort ();
+ }
+
+ return block_init (block, new_size);
+}
+
+void
+tu_free (void *p, size_t old_size)
+{
+ free (block_check (p));
+}
+
int
main (int argc, char **argv)
{
hex_random_init ();
+
+ mp_set_memory_functions (tu_alloc, tu_realloc, tu_free);
+
/* Currently, t-comb seems to be the only program accepting any
arguments. It might make sense to parse common arguments here. */
testmain (argc, argv);
+ if (total_alloc != 0)
+ {
+ fprintf (stderr, "Memory leaked: %lu bytes.\n",
+ (unsigned long) total_alloc);
+ abort ();
+ }
return 0;
}
diff -r 8794a8f80fc0 -r 66f52077e390 mini-gmp/tests/testutils.h
--- a/mini-gmp/tests/testutils.h Fri Jan 18 21:15:02 2013 +0100
+++ b/mini-gmp/tests/testutils.h Fri Jan 18 21:59:23 2013 +0100
@@ -20,3 +20,6 @@
#include "mini-random.h"
void testmain (int argc, char **argv);
+
+void
+tu_free (void *p, size_t old_size);
More information about the gmp-commit
mailing list