summaryrefslogtreecommitdiff
path: root/mini-gmp
diff options
context:
space:
mode:
authorNiels M?ller <nisse@lysator.liu.se>2013-01-18 21:59:23 +0100
committerNiels M?ller <nisse@lysator.liu.se>2013-01-18 21:59:23 +0100
commita24a3fcc3478407f68459e1a7c61ce72cff8767f (patch)
treefa65b813b2ff74b1b40225f2d26d93cf2ab82fc7 /mini-gmp
parent216e8e2ae8986c07aea3b0717bef5efe84b42b9d (diff)
downloadgmp-a24a3fcc3478407f68459e1a7c61ce72cff8767f.tar.gz
mini-gmp: Use custom memory allocation for testsuite.
Diffstat (limited to 'mini-gmp')
-rw-r--r--mini-gmp/tests/t-double.c2
-rw-r--r--mini-gmp/tests/t-gcd.c5
-rw-r--r--mini-gmp/tests/t-str.c2
-rw-r--r--mini-gmp/tests/testutils.c86
-rw-r--r--mini-gmp/tests/testutils.h3
5 files changed, 96 insertions, 2 deletions
diff --git a/mini-gmp/tests/t-double.c b/mini-gmp/tests/t-double.c
index 6c6bac2bc..36a621845 100644
--- a/mini-gmp/tests/t-double.c
+++ b/mini-gmp/tests/t-double.c
@@ -75,7 +75,7 @@ testmain (int argc, char **argv)
values[i].d, s, values[i].s);
abort ();
}
- free(s);
+ tu_free(s, 0);
mpz_clear (x);
}
diff --git a/mini-gmp/tests/t-gcd.c b/mini-gmp/tests/t-gcd.c
index b0e3c83f4..5090b3511 100644
--- a/mini-gmp/tests/t-gcd.c
+++ b/mini-gmp/tests/t-gcd.c
@@ -100,6 +100,11 @@ gcdext_valid_p (const mpz_t a, const mpz_t b,
if (mpz_cmpabs (r, ta) > 0)
return 0;
}
+
+ mpz_clear (ta);
+ mpz_clear (tb);
+ mpz_clear (r);
+
return 1;
}
diff --git a/mini-gmp/tests/t-str.c b/mini-gmp/tests/t-str.c
index d333adc31..8248cfd33 100644
--- a/mini-gmp/tests/t-str.c
+++ b/mini-gmp/tests/t-str.c
@@ -170,7 +170,7 @@ testmain (int argc, char **argv)
}
}
free (ap);
- free (bp);
+ tu_free (bp, 0);
}
}
mpz_clear (a);
diff --git a/mini-gmp/tests/testutils.c b/mini-gmp/tests/testutils.c
index 60fd238ee..dd63f2ba6 100644
--- a/mini-gmp/tests/testutils.c
+++ b/mini-gmp/tests/testutils.c
@@ -23,13 +23,99 @@ the GNU MP Library test suite. If not, see http://www.gnu.org/licenses/. */
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 --git a/mini-gmp/tests/testutils.h b/mini-gmp/tests/testutils.h
index 199f0a7cc..25032d389 100644
--- a/mini-gmp/tests/testutils.h
+++ b/mini-gmp/tests/testutils.h
@@ -20,3 +20,6 @@ the GNU MP Library test suite. If not, see http://www.gnu.org/licenses/. */
#include "mini-random.h"
void testmain (int argc, char **argv);
+
+void
+tu_free (void *p, size_t old_size);