[beast: 21/57] SFI: Bse: ensure stdint types, provide breakpoint, string_format, printerr, etc



commit c4e0b3bd04c5e6acf2af1b81f870868fbe37f6b2
Author: Tim Janik <timj gnu org>
Date:   Mon Jul 17 11:13:28 2017 +0200

    SFI: Bse: ensure stdint types, provide breakpoint, string_format, printerr, etc
    
    Signed-off-by: Tim Janik <timj gnu org>

 sfi/bcore.cc |   60 ++++++++++++++++++++++++++-----
 sfi/bcore.hh |  110 ++++++++++++++++++++++++++++++++++++---------------------
 2 files changed, 119 insertions(+), 51 deletions(-)
---
diff --git a/sfi/bcore.cc b/sfi/bcore.cc
index 5dfb402..8f07349 100644
--- a/sfi/bcore.cc
+++ b/sfi/bcore.cc
@@ -1,15 +1,57 @@
 // Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
 #include "bcore.hh"
+#include <cstring>
 #include "platform.hh"
 #include <unistd.h>     // _exit
 #include <sys/time.h>   // gettimeofday
 
-namespace Bse {
-using namespace Rapicorn;
+// == limits.h & float.h checks ==
+// assert several assumptions the code makes
+static_assert (CHAR_BIT     == +8, "");
+static_assert (SCHAR_MIN    == -128, "");
+static_assert (SCHAR_MAX    == +127, "");
+static_assert (UCHAR_MAX    == +255, "");
+static_assert (SHRT_MIN     == -32768, "");
+static_assert (SHRT_MAX     == +32767, "");
+static_assert (USHRT_MAX    == +65535, "");
+static_assert (INT_MIN      == -2147483647 - 1, "");
+static_assert (INT_MAX      == +2147483647, "");
+static_assert (UINT_MAX     == +4294967295U, "");
+static_assert (INT64_MIN    == -9223372036854775807LL - 1, "");
+static_assert (INT64_MAX    == +9223372036854775807LL, "");
+static_assert (UINT64_MAX   == +18446744073709551615LLU, "");
+static_assert (LDBL_MIN     <= 1E-37, "");
+static_assert (LDBL_MAX     >= 1E+37, "");
+static_assert (LDBL_EPSILON <= 1E-9, "");
+static_assert (FLT_MIN      <= 1E-37, "");
+static_assert (FLT_MAX      >= 1E+37, "");
+static_assert (FLT_EPSILON  <= 1E-5, "");
+static_assert (DBL_MIN      <= 1E-37, "");
+static_assert (DBL_MAX      >= 1E+37, "");
+static_assert (DBL_EPSILON  <= 1E-9, "");
 
+namespace Bse {
 
 namespace Internal {
 
+void
+printout_string (const String &string)
+{
+  // some platforms (_WIN32) don't properly flush on '\n'
+  fflush (stderr); // preserve ordering
+  fputs (string.c_str(), stdout);
+  fflush (stdout);
+}
+
+void
+printerr_string (const String &string)
+{
+  // some platforms (_WIN32) don't properly flush on '\n'
+  fflush (stdout); // preserve ordering
+  fputs (string.c_str(), stderr);
+  fflush (stderr);
+}
+
 bool debug_any_enabled = true; // initialized by debug_key_enabled()
 
 bool
@@ -43,7 +85,7 @@ debug_key_enabled (const char *conditional)
   return false;
 }
 
-void RAPICORN_NORETURN
+void BSE_NORETURN
 force_abort ()
 {
   // ensure the program halts on error conditions
@@ -58,11 +100,9 @@ diagnostic (char kind, const std::string &message)
   String prefix;
   switch (kind) {
   case 'W':     prefix = "WARNING: ";   break;
+  case 'I':     prefix = "INFO: ";      break;
   case 'D':     prefix = "DEBUG: ";     break;
   case ' ':     prefix = "";            break;
-  case 'I':
-    prefix = program_alias() + ": ";
-    break;
   case 'F':
     prefix = program_alias() + ": FATAL: ";
     break;
@@ -84,11 +124,11 @@ debug_diagnostic (const char *prefix, const std::string &message)
   printerr ("%u.%06u %s: %s%s", tv.tv_sec, tv.tv_usec, pprefix, message, newline);
 }
 
-struct DebugStartup {
-  DebugStartup()
+struct EarlyStartup101 {
+  EarlyStartup101()
   {
     if (debug_key_enabled ("") ||       // force debug_any_enabled initialization
-        debug_any_enabled)              // run DebugStartup if *any* debugging is enabled
+        debug_any_enabled)              // print startup time if *any* debugging is enabled
       {
         const time_t now = time (NULL);
         struct tm gtm = { 0, };
@@ -100,7 +140,7 @@ struct DebugStartup {
   }
 };
 
-static DebugStartup _debug_startup __attribute__ ((init_priority (101)));
+static EarlyStartup101 _early_startup_101 __attribute__ ((init_priority (101)));
 
 } // Internal
 
diff --git a/sfi/bcore.hh b/sfi/bcore.hh
index 3548df7..930cf28 100644
--- a/sfi/bcore.hh
+++ b/sfi/bcore.hh
@@ -3,50 +3,80 @@
 #define __BSE_BCORE_HH__
 
 #include <sfi/cxxaux.hh>
-#include <rapicorn-core.hh>
+#include <sfi/strings.hh>
 #include <sfi/glib-extra.hh>
 
 namespace Bse {
-using namespace Rapicorn;
-using Rapicorn::string_format;
-using Rapicorn::printout;
-using Rapicorn::printerr;
-namespace Path = Rapicorn::Path;
-
-// == Path Name Macros ==
-#ifdef  _WIN32 // includes _WIN64
-#undef  BSE_UNIX_PATHS                                                  ///< Undefined on _WIN32 and _WIN64, 
defined on Unix.
-#define BSE_DOS_PATHS                1                                  ///< Undefined on Unix-like systems, 
defined on _WIN32 and _WIN64.
-#else   // !_WIN32
-#define BSE_UNIX_PATHS               1                                  ///< Undefined on _WIN32 and _WIN64, 
defined on Unix.
-#undef  BSE_DOS_PATHS                                                   ///< Undefined on Unix-like systems, 
defined on _WIN32 and _WIN64.
-#endif  // !_WIN32
-#define BSE_DIR_SEPARATOR               RAPICORN_DIR_SEPARATOR          ///< A '/' on Unix-like systems, a 
'\\' on _WIN32.
-#define BSE_DIR_SEPARATOR_S             RAPICORN_DIR_SEPARATOR_S        ///< A "/" on Unix-like systems, a 
"\\" on _WIN32.
-#define BSE_SEARCHPATH_SEPARATOR        RAPICORN_SEARCHPATH_SEPARATOR   ///< A ':' on Unix-like systems, a 
';' on _WIN32.
-#define BSE_SEARCHPATH_SEPARATOR_S      RAPICORN_SEARCHPATH_SEPARATOR_S ///< A ":" on Unix-like systems, a 
";" on _WIN32.
-#define BSE_IS_ABSPATH(p)               RAPICORN_IS_ABSPATH (p)         ///< Checks root directory path 
component, plus drive letter on _WIN32.
+
+// == type aliases ==
+typedef uint8_t         uint8;          ///< An 8-bit unsigned integer.
+typedef uint16_t        uint16;         ///< A 16-bit unsigned integer.
+typedef uint32_t        uint32;         ///< A 32-bit unsigned integer.
+typedef uint64_t        uint64;         ///< A 64-bit unsigned integer, use PRI*64 in format strings.
+typedef int8_t          int8;           ///< An 8-bit signed integer.
+typedef int16_t         int16;          ///< A 16-bit signed integer.
+typedef int32_t         int32;          ///< A 32-bit signed integer.
+typedef int64_t         int64;          ///< A 64-bit unsigned integer, use PRI*64 in format strings.
+typedef uint32_t        unichar;        ///< A 32-bit unsigned integer used for Unicode characters.
+static_assert (sizeof (uint8) == 1 && sizeof (uint16) == 2 && sizeof (uint32) == 4 && sizeof (uint64) == 8, 
"");
+static_assert (sizeof (int8)  == 1 && sizeof (int16)  == 2 && sizeof (int32)  == 4 && sizeof (int64)  == 8, 
"");
+static_assert (sizeof (int) == 4 && sizeof (uint) == 4 && sizeof (unichar) == 4, "");
+using   std::map;
+using   std::vector;
+typedef std::string String;             ///< Convenience alias for std::string.
+typedef vector<String> StringVector;    ///< Convenience alias for a std::vector<std::string>.
 
 // == Diagnostics ==
-template<class ...Args> void        fatal             (const char *format, const Args &...args) 
RAPICORN_NORETURN;
-template<class ...Args> void        warning           (const char *format, const Args &...args);
-template<class ...Args> void        warn              (const char *format, const Args &...args);
-template<class ...Args> void        info              (const char *format, const Args &...args);
-template<class ...Args> inline void dump              (const char *conditional, const char *format, const 
Args &...args) RAPICORN_ALWAYS_INLINE;
-template<class ...Args> inline void debug             (const char *conditional, const char *format, const 
Args &...args) RAPICORN_ALWAYS_INLINE;
-inline bool                         debug_enabled     (const char *conditional) RAPICORN_ALWAYS_INLINE 
BSE_PURE;
-
-// == Internal Implementation Details ==
+template<class... Args> String      string_format        (const char *format, const Args &...args) 
BSE_PRINTF (1, 0);
+template<class... Args> String      string_locale_format (const char *format, const Args &...args) 
BSE_PRINTF (1, 0);
+template<class... Args> void        printout             (const char *format, const Args &...args) 
BSE_PRINTF (1, 0);
+template<class... Args> void        printerr             (const char *format, const Args &...args) 
BSE_PRINTF (1, 0);
+template<class ...Args> void        fatal                (const char *format, const Args &...args) 
BSE_NORETURN;
+template<class ...Args> void        warning              (const char *format, const Args &...args);
+template<class ...Args> void        warn                 (const char *format, const Args &...args);
+template<class ...Args> void        info                 (const char *format, const Args &...args);
+template<class ...Args> inline void dump                 (const char *conditional, const char *format, const 
Args &...args) BSE_ALWAYS_INLINE;
+template<class ...Args> inline void debug                (const char *conditional, const char *format, const 
Args &...args) BSE_ALWAYS_INLINE;
+inline bool                         debug_enabled        (const char *conditional) BSE_ALWAYS_INLINE 
BSE_PURE;
+
+// == Development Aids ==
+extern inline void breakpoint () BSE_ALWAYS_INLINE;                    ///< Cause a debugging breakpoint, 
for development only.
+
+// == Implementation Details ==
+#if (defined __i386__ || defined __x86_64__)
+inline void breakpoint() { __asm__ __volatile__ ("int $03"); }
+#elif defined __alpha__ && !defined __osf__
+inline void breakpoint() { __asm__ __volatile__ ("bpt"); }
+#else   // !__i386__ && !__alpha__
+inline void breakpoint() { __builtin_trap(); }
+#endif
+
 namespace Internal {
 extern bool                         debug_any_enabled;  //< Indicates if $BSE_DEBUG enables some debug 
settings.
 bool                                debug_key_enabled (const char *conditional) BSE_PURE;
 void                                diagnostic        (char kind, const std::string &message);
 void                                debug_diagnostic  (const char *prefix, const std::string &message);
-void                                force_abort       () RAPICORN_NORETURN;
+void                                force_abort       () BSE_NORETURN;
+void                                printout_string   (const String &string);
+void                                printerr_string   (const String &string);
 } // Internal
 
+/// Print a message on stdout (and flush stdout) ala printf(), using the POSIX/C locale.
+template<class... Args> void
+printout (const char *format, const Args &...args)
+{
+  Internal::printout_string (string_format (format, args...));
+}
+
+/// Print a message on stderr (and flush stderr) ala printf(), using the POSIX/C locale.
+template<class... Args> void
+printerr (const char *format, const Args &...args)
+{
+  Internal::printerr_string (string_format (format, args...));
+}
+
 /// Issue a printf-like message if @a conditional is enabled by $BSE_DEBUG.
-template<class ...Args> inline void RAPICORN_ALWAYS_INLINE
+template<class ...Args> inline void BSE_ALWAYS_INLINE
 dump (const char *conditional, const char *format, const Args &...args)
 {
   if (BSE_UNLIKELY (Internal::debug_any_enabled) && Internal::debug_key_enabled (conditional))
@@ -54,7 +84,7 @@ dump (const char *conditional, const char *format, const Args &...args)
 }
 
 /// Issue a printf-like debugging message if @a conditional is enabled by $BSE_DEBUG.
-template<class ...Args> inline void RAPICORN_ALWAYS_INLINE
+template<class ...Args> inline void BSE_ALWAYS_INLINE
 debug (const char *conditional, const char *format, const Args &...args)
 {
   if (BSE_UNLIKELY (Internal::debug_any_enabled) && Internal::debug_key_enabled (conditional))
@@ -62,7 +92,7 @@ debug (const char *conditional, const char *format, const Args &...args)
 }
 
 /// Check if @a conditional is enabled by $BSE_DEBUG.
-inline bool RAPICORN_ALWAYS_INLINE BSE_PURE
+inline bool BSE_ALWAYS_INLINE BSE_PURE
 debug_enabled (const char *conditional)
 {
   if (BSE_UNLIKELY (Internal::debug_any_enabled))
@@ -71,7 +101,7 @@ debug_enabled (const char *conditional)
 }
 
 /// Issue a printf-like message and abort the program, this function will not return.
-template<class ...Args> void RAPICORN_NORETURN
+template<class ...Args> void BSE_NORETURN
 fatal (const char *format, const Args &...args)
 {
   Internal::diagnostic ('F', string_format (format, args...));
@@ -79,21 +109,21 @@ fatal (const char *format, const Args &...args)
 }
 
 /// Issue a printf-like warning message.
-template<class ...Args> void RAPICORN_NORETURN
+template<class ...Args> void BSE_NORETURN
 warn (const char *format, const Args &...args)
 {
   Internal::diagnostic ('W', string_format (format, args...));
 }
 
 /// Issue a printf-like warning message.
-template<class ...Args> void RAPICORN_NORETURN
+template<class ...Args> void BSE_NORETURN
 warning (const char *format, const Args &...args)
 {
   Internal::diagnostic ('W', string_format (format, args...));
 }
 
 /// Issue an informative printf-like message.
-template<class ...Args> void RAPICORN_NORETURN
+template<class ...Args> void BSE_NORETURN
 info (const char *format, const Args &...args)
 {
   Internal::diagnostic ('I', string_format (format, args...));
@@ -101,9 +131,9 @@ info (const char *format, const Args &...args)
 
 // == Assertions ==
 /// Return from the current function if @a cond is unmet and issue an assertion warning.
-#define BSE_ASSERT_RETURN(cond, ...)            AIDA_ASSERT_RETURN (cond, __VA_ARGS__)
+#define BSE_ASSERT_RETURN(cond, ...)     do { if (BSE_ISLIKELY (cond)) break; ::Bse::assertion_failed 
(__FILE__, __LINE__, #cond); return __VA_ARGS__; } while (0)
 /// Return from the current function and issue an assertion warning.
-#define BSE_ASSERT_RETURN_UNREACHED(...)        AIDA_ASSERT_RETURN_UNREACHED (__VA_ARGS__)
+#define BSE_ASSERT_RETURN_UNREACHED(...) do { ::Bse::assertion_failed (__FILE__, __LINE__, NULL); return 
__VA_ARGS__; } while (0)
 #ifdef BSE_CONVENIENCE
 /// Return from the current function if @a cond is unmet and issue an assertion warning.
 #define assert_return(cond, ...)        BSE_ASSERT_RETURN (cond, __VA_ARGS__)
@@ -116,8 +146,6 @@ info (const char *format, const Args &...args)
 /// Return silently if @a cond does not evaluate to true with return value @a ...
 #define return_unless(cond, ...)        BSE_RETURN_UNLESS (cond, __VA_ARGS__)
 #endif // BSE_CONVENIENCE
-using Rapicorn::Aida::assertion_failed_hook;
-using Rapicorn::breakpoint;
 
 } // Bse
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]