Patchwork [v3] libstdc++/57920

login
register
mail settings
Submitter Paolo Carlini
Date July 23, 2013, 9:56 a.m.
Message ID <51EE533D.1050600@oracle.com>
Download mbox | patch
Permalink /patch/261022/
State New
Headers show

Comments

Paolo Carlini - July 23, 2013, 9:56 a.m.
... in the future, when we manage to actually avoid including <cstdio> 
from <string> I think we can avoid including it from <random> if we play 
a bit with void*. Should be safe aliasing-wise. Something like the 
below, completely untested.

Paolo.

////////////////
Paolo Carlini - July 23, 2013, 10:10 a.m.
On 07/23/2013 11:56 AM, Paolo Carlini wrote:
> ... in the future, when we manage to actually avoid including <cstdio> 
> from <string>
The C++11 string conversions could be exported by the *.so, aren't 
templates.

Paolo.
Chung-Ju Wu - July 23, 2013, 10:45 a.m.
2013/7/23 Paolo Carlini <paolo.carlini@oracle.com>:
> ... in the future, when we manage to actually avoid including <cstdio> from
> <string> I think we can avoid including it from <random> if we play a bit
> with void*. Should be safe aliasing-wise. Something like the below,
> completely untested.

I tested it on my target.  At least the elf toolchain can be
successfully built with such changes.


Best regards,
jasonwucj

Patch

Index: include/bits/random.h
===================================================================
--- include/bits/random.h	(revision 201148)
+++ include/bits/random.h	(working copy)
@@ -1638,10 +1638,10 @@ 
 
     union
     {
-    FILE*        _M_file;
-    mt19937      _M_mt;
+      void*      _M_file;
+      mt19937    _M_mt;
+    };
   };
-  };
 
   /* @} */ // group random_generators
 
Index: include/std/random
===================================================================
--- include/std/random	(revision 201160)
+++ include/std/random	(working copy)
@@ -36,7 +36,6 @@ 
 #else
 
 #include <cmath>
-#include <cstdio> // For FILE
 #include <cstdlib>
 #include <string>
 #include <iosfwd>
Index: src/c++11/random.cc
===================================================================
--- src/c++11/random.cc	(revision 201160)
+++ src/c++11/random.cc	(working copy)
@@ -30,6 +30,8 @@ 
 # include <cpuid.h>
 #endif
 
+#include <cstdio>
+
 #ifdef _GLIBCXX_HAVE_UNISTD_H
 # include <unistd.h>
 #endif
@@ -102,7 +104,7 @@ 
       std::__throw_runtime_error(__N("random_device::"
 				     "random_device(const std::string&)"));
 
-    _M_file = std::fopen(fname, "rb");
+    _M_file = reinterpret_cast<void*>(std::fopen(fname, "rb"));
     if (! _M_file)
       goto fail;
   }
@@ -117,7 +119,7 @@ 
   random_device::_M_fini()
   {
     if (_M_file)
-      std::fclose(_M_file);
+      std::fclose(reinterpret_cast<FILE*>(_M_file));
   }
 
   random_device::result_type
@@ -130,10 +132,11 @@ 
 
     result_type __ret;
 #ifdef _GLIBCXX_HAVE_UNISTD_H
-    read(fileno(_M_file), reinterpret_cast<void*>(&__ret), sizeof(result_type));
+    read(fileno(reinterpret_cast<FILE*>(_M_file)),
+	 reinterpret_cast<void*>(&__ret), sizeof(result_type));
 #else
     std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
-	       1, _M_file);
+	       1, reinterpret_cast<FILE*>(_M_file));
 #endif
     return __ret;
   }