summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Ryde <user42@zip.com.au>2001-11-19 22:13:42 +0100
committerKevin Ryde <user42@zip.com.au>2001-11-19 22:13:42 +0100
commit9061b62c3a43ce5fd7e5d0f0b7dfeb223fcbebed (patch)
tree036977250eb8246de5cae39b58b4fc7a5051b92b
parent4f579b097d6eb8ff649f780105347d9045ea7231 (diff)
downloadgmp-9061b62c3a43ce5fd7e5d0f0b7dfeb223fcbebed.tar.gz
* tal-debug.c, gmp-impl.h: More checks of TMP_DECL/TMP_MARK/TMP_FREE
consistency.
-rw-r--r--gmp-impl.h53
-rw-r--r--tal-debug.c37
2 files changed, 71 insertions, 19 deletions
diff --git a/gmp-impl.h b/gmp-impl.h
index ee5d05d20..77d969c0d 100644
--- a/gmp-impl.h
+++ b/gmp-impl.h
@@ -233,21 +233,52 @@ struct tmp_debug_entry_t {
size_t size;
};
void __gmp_tmp_debug_mark _PROTO ((const char *, int, struct tmp_debug_t **,
- struct tmp_debug_t *));
-void *__gmp_tmp_debug_alloc _PROTO ((const char *, int, struct tmp_debug_t **,
+ struct tmp_debug_t *,
+ const char *, const char *));
+void *__gmp_tmp_debug_alloc _PROTO ((const char *, int, int,
+ struct tmp_debug_t **, const char *,
size_t)) ATTRIBUTE_MALLOC;
-void __gmp_tmp_debug_free _PROTO ((const char *, int, struct tmp_debug_t **));
-/* don't demand NULL, just cast a zero */
-#define TMP_DECL(marker) \
+void __gmp_tmp_debug_free _PROTO ((const char *, int, int,
+ struct tmp_debug_t **,
+ const char *, const char *));
+#if HAVE_STRINGIZE
+#define TMP_DECL(marker) TMP_DECL_NAME(marker, #marker)
+#define TMP_MARK(marker) TMP_MARK_NAME(marker, #marker)
+#define TMP_FREE(marker) TMP_FREE_NAME(marker, #marker)
+#else
+#define TMP_DECL(marker) TMP_DECL_NAME(marker, "marker")
+#define TMP_MARK(marker) TMP_MARK_NAME(marker, "marker")
+#define TMP_FREE(marker) TMP_FREE_NAME(marker, "marker")
+#endif
+/* The marker variable is designed to provoke an uninitialized varialble
+ warning from the compiler if TMP_FREE is used without a TMP_MARK.
+ __tmp_marker_inscope does the same for TMP_ALLOC. Runtime tests pick
+ these things up too. */
+#define TMP_DECL_NAME(marker, marker_name) \
+ int marker; \
+ int __tmp_marker_inscope; \
+ const char *__tmp_marker_name = marker_name; \
struct tmp_debug_t __tmp_marker_struct; \
+ /* don't demand NULL, just cast a zero */ \
struct tmp_debug_t *__tmp_marker = (struct tmp_debug_t *) 0
-#define TMP_MARK(marker) \
- __gmp_tmp_debug_mark (ASSERT_FILE, ASSERT_LINE, \
- &__tmp_marker, &__tmp_marker_struct)
+#define TMP_MARK_NAME(marker, marker_name) \
+ do { \
+ marker = 1; \
+ __tmp_marker_inscope = 1; \
+ __gmp_tmp_debug_mark (ASSERT_FILE, ASSERT_LINE, \
+ &__tmp_marker, &__tmp_marker_struct, \
+ __tmp_marker_name, marker_name); \
+ } while (0)
#define TMP_ALLOC(size) \
- __gmp_tmp_debug_alloc (ASSERT_FILE, ASSERT_LINE, &__tmp_marker, size)
-#define TMP_FREE(marker) \
- __gmp_tmp_debug_free (ASSERT_FILE, ASSERT_LINE, &__tmp_marker)
+ __gmp_tmp_debug_alloc (ASSERT_FILE, ASSERT_LINE, \
+ __tmp_marker_inscope, \
+ &__tmp_marker, __tmp_marker_name, size)
+#define TMP_FREE_NAME(marker, marker_name) \
+ do { \
+ __gmp_tmp_debug_free (ASSERT_FILE, ASSERT_LINE, \
+ marker, &__tmp_marker, \
+ __tmp_marker_name, marker_name); \
+ } while (0)
#endif
diff --git a/tal-debug.c b/tal-debug.c
index 3606cf574..59453cd45 100644
--- a/tal-debug.c
+++ b/tal-debug.c
@@ -49,12 +49,21 @@ MA 02111-1307, USA. */
void
__gmp_tmp_debug_mark (const char *file, int line,
- struct tmp_debug_t **markp, struct tmp_debug_t *mark)
+ struct tmp_debug_t **markp, struct tmp_debug_t *mark,
+ const char *decl_name, const char *mark_name)
{
+ if (strcmp (mark_name, decl_name) != 0)
+ {
+ __gmp_assert_header (file, line);
+ fprintf (stderr, "GNU MP: TMP_MARK(%s) but TMP_DECL(%s) is in scope\n",
+ mark_name, decl_name);
+ abort ();
+ }
+
if (*markp != NULL)
{
__gmp_assert_header (file, line);
- fprintf (stderr, "GNU MP: Duplicate TMP_MARK\n");
+ fprintf (stderr, "GNU MP: Repeat of TMP_MARK(%s)\n", mark_name);
if (mark->file != NULL && mark->file[0] != '\0' && mark->line != -1)
{
__gmp_assert_header (mark->file, mark->line);
@@ -68,10 +77,11 @@ __gmp_tmp_debug_mark (const char *file, int line,
mark->line = line;
mark->list = NULL;
}
-
+
void *
-__gmp_tmp_debug_alloc (const char *file, int line,
- struct tmp_debug_t **markp, size_t size)
+__gmp_tmp_debug_alloc (const char *file, int line, int dummy,
+ struct tmp_debug_t **markp,
+ const char *decl_name, size_t size)
{
struct tmp_debug_t *mark = *markp;
struct tmp_debug_entry_t *p;
@@ -81,7 +91,7 @@ __gmp_tmp_debug_alloc (const char *file, int line,
if (mark == NULL)
{
__gmp_assert_header (file, line);
- fprintf (stderr, "GNU MP: TMP_ALLOC without TMP_MARK\n");
+ fprintf (stderr, "GNU MP: TMP_ALLOC without TMP_MARK(%s)\n", decl_name);
abort ();
}
@@ -94,7 +104,9 @@ __gmp_tmp_debug_alloc (const char *file, int line,
}
void
-__gmp_tmp_debug_free (const char *file, int line, struct tmp_debug_t **markp)
+__gmp_tmp_debug_free (const char *file, int line, int dummy,
+ struct tmp_debug_t **markp,
+ const char *decl_name, const char *free_name)
{
struct tmp_debug_t *mark = *markp;
struct tmp_debug_entry_t *p, *next;
@@ -102,7 +114,16 @@ __gmp_tmp_debug_free (const char *file, int line, struct tmp_debug_t **markp)
if (mark == NULL)
{
__gmp_assert_header (file, line);
- fprintf (stderr, "GNU MP: TMP_FREE without TMP_MARK\n");
+ fprintf (stderr, "GNU MP: TMP_FREE(%s) without TMP_MARK(%s)\n",
+ free_name, decl_name);
+ abort ();
+ }
+
+ if (strcmp (free_name, decl_name) != 0)
+ {
+ __gmp_assert_header (file, line);
+ fprintf (stderr, "GNU MP: TMP_FREE(%s) when TMP_DECL(%s) is in scope\n",
+ free_name, decl_name);
abort ();
}