error handling

Marc Glisse marc.glisse at inria.fr
Tue Dec 16 00:03:15 UTC 2014


On Mon, 10 Nov 2014, Marc Glisse wrote:

> I am still planning to implement the second paragraph of this message:
> https://gmplib.org/list-archives/gmp-devel/2012-February/002210.html

One thing that is a bit painful is that many tests look like:

   TMP_DECL;
   TMP_MARK;
   tests_start ();

... use TMP_ALLOC

   TMP_FREE;
   tests_end ();

But if I move the functionality of TMP_FREE to the destructor of an object 
declared in TMP_DECL or TMP_MARK, it now happens after tests_end(), which 
complains that some memory was not released. So for many tests I need to 
add { before TMP_DECL and } after TMP_FREE, or split the tests to a 
separate function.

mpf_get_str is the only function in the library (ignoring tests) that 
fails to compile if I ignore TMP_DECL and do the declaration in TMP_MARK 
(because of a goto). Adding { and } solves it. There may be other places 
where some extra {} to invoke the destructor earlier would help.

If WANT_TMP_DEBUG, some variables are initialized by TMP_DECL instead of 
TMP_MARK. I don't understand why, and it complicates the code if I want to 
handle C++03 and not just C++11 or later, so I'll move those 
initializations if noone gives a reason.


+/* Handle C++ exceptions.  */
+#ifdef __cplusplus
+struct gmp_tmp_salloc_t {
+  /* May warn in pedantic mode when TMP_SDECL is empty.  */
+  TMP_SDECL;
+  gmp_tmp_salloc_t(){ TMP_SMARK; }
+  void*salloc(size_t n){ return TMP_SALLOC(n); }
+  ~gmp_tmp_salloc_t(){ TMP_SFREE; }
+};
+struct gmp_tmp_alloc_t {
+  TMP_DECL;
+  gmp_tmp_alloc_t(){ TMP_MARK; }
+  void*salloc(size_t n){ return TMP_SALLOC(n); }
+  void*balloc(size_t n){ return TMP_BALLOC(n); }
+  void*alloc(size_t n){ return TMP_ALLOC(n); }
+  ~gmp_tmp_alloc_t(){ TMP_FREE; }
+};
+#undef TMP_SDECL
+#undef TMP_DECL
+#undef TMP_SMARK
+#undef TMP_MARK
+#undef TMP_SALLOC
+#undef TMP_BALLOC
+#undef TMP_ALLOC
+#undef TMP_SFREE
+#undef TMP_FREE
+#define TMP_SDECL
+#define TMP_DECL
+#define TMP_SMARK	gmp_tmp_salloc_t gmp_tmp_alloc
+#define TMP_MARK	gmp_tmp_alloc_t gmp_tmp_alloc
+#define TMP_SALLOC(n)	gmp_tmp_alloc.salloc(n)
+#define TMP_BALLOC(n)	gmp_tmp_alloc.balloc(n)
+#define TMP_ALLOC(n)	gmp_tmp_alloc.alloc(n)
+#define TMP_SFREE
+#define TMP_FREE
+#endif

-- 
Marc Glisse


More information about the gmp-discuss mailing list