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

[tyndur-devel] [PATCH 4/6] + 18.4 Language support library, Start and termination: <cstdlib>



---
 STATUS                          |    2 +-
 include/cstdlib                 |  140 +++++++++++++++++++++++++++++++++++++++
 include/lightlibc++/doxygen.hpp |    2 +
 source/cstdlib.cpp              |   71 ++++++++++++++++++++
 4 files changed, 214 insertions(+), 1 deletions(-)
 create mode 100644 include/cstdlib
 create mode 100644 source/cstdlib.cpp

diff --git a/STATUS b/STATUS
index 813580e..3b8d47d 100644
--- a/STATUS
+++ b/STATUS
@@ -6,7 +6,7 @@ This file documents the status of the implementation.
 18.1 Types                                                           [X]
 18.2 Implementation properties                                       [ ]
 18.3 Integer types                                                   [X]
-18.4 Start and termination                                           [ ]
+18.4 Start and termination                                           [X]
 18.5 Dynamic memory management <new>                                 [ ]
 18.6 Type identi�cation                                              [ ]
 18.7 Exception handling                                              [ ]
diff --git a/include/cstdlib b/include/cstdlib
new file mode 100644
index 0000000..235a635
--- /dev/null
+++ b/include/cstdlib
@@ -0,0 +1,140 @@
+/*
+Permission is granted to use, modify, and / or redistribute at will.
+
+This includes removing authorship notices, re-use of code parts in
+other software (with or without giving credit), and / or creating a
+commercial product based on it.
+
+This permission is not revocable by the author.
+
+This software is provided as-is. Use it at your own risk. There is
+no warranty whatsoever, neither expressed nor implied, and by using
+this software you accept that the author(s) shall not be held liable
+for any loss of data, loss of service, or other damages, be they
+incidental or consequential. Your only option other than accepting
+this is not to use the software at all.
+*/
+#ifndef _LIGHTLIBCPP_CSTDLIB
+#define _LIGHTLIBCPP_CSTDLIB
+
+
+
+#include <stdlib.h>          // abort, atexit, exit
+
+
+
+// NOTE: macros EXIT_FAILURE, EXIT_SUCCESS, RAND_MAX, MB_CUR_MAX taken from C Standard library
+
+
+
+namespace std
+{
+  /** \addtogroup lightlibcpp_18_4 */
+  /*@{*/
+
+
+
+  /* Types from the C Standard library header stdlib.h */
+
+  /** div_t type from the C Standard library */
+  typedef ::div_t            div_t;
+  /** ldiv_t type from the C Standard library */
+  typedef ::ldiv_t           ldiv_t;
+  /** lldiv_t type from the C Standard library */
+  typedef ::lldiv_t          lldiv_t;
+
+
+
+  /* Functions from the C Standard library header stdlib.h */
+
+  using ::atof;
+  using ::atoi;
+  using ::atol;
+  using ::atoll;
+
+  using ::strtod;
+  using ::strtof;
+  using ::strtold;
+
+  using ::strtol;
+  using ::strtoll;
+  using ::strtoul;
+  using ::strtoull;
+
+  using ::rand;
+  using ::srand;
+
+  using ::calloc;
+  using ::free;
+  using ::malloc;
+  using ::realloc;
+
+  using ::abort;
+  using ::atexit;
+  using ::exit;
+  using ::_Exit;
+  using ::getenv;
+  using ::system;
+
+  using ::bsearch;
+  using ::qsort;
+
+  using ::abs;
+  using ::labs;
+  using ::llabs;
+
+  using ::div;
+  using ::ldiv;
+  using ::lldiv;
+
+  using ::mblen;
+  using ::mbtowc;
+  using ::wctomb;
+  using ::mbstowcs;
+  using ::wcstombs;
+
+
+
+#ifndef _LIGHTLIB_CPP03_STRICT
+
+  /** The at_quick_exit() functions register the function pointed to by f to be
+   *  called without arguments when quick_exit is called. The at_quick_exit()
+   *  functions shall be thread safe.
+   *
+   *  The at_quick_exit registrations are distinct from the atexit
+   *  registrations, and applications may need to call both registration
+   *  functions with the same argument.
+   *
+   *  Implementation limits: The implementation shall support the registration
+   *  of at least 32 functions.
+   *
+   *  \param[in] f pointer to the function that should be registered
+   *  \return zero if the registration succeeds, non-zero if it fails.
+   */
+  int at_quick_exit(void (*f)(void));
+
+  /** Functions registered by calls to at_quick_exit are called in the reverse
+   *  order of their registration, except that a function shall be called
+   *  after any previously registered functions that had already been called
+   *  at the time it was registered. Objects shall not be destroyed as a result
+   *  of calling quick_exit. If control leaves a registered function called by
+   *  quick_exit because the function does not provide a handler for a thrown
+   *  exception, terminate() shall be called.
+   *
+   *  After calling registered functions, quick_exit shall call _Exit(status).
+   *
+   *  The function quick_exit() never returns to its caller.
+   *  \param[in] status the status code that is passed to _Exit
+   */
+  void quick_exit(int status);
+
+#endif
+
+
+
+  /*@}*/
+}
+
+
+
+#endif
diff --git a/include/lightlibc++/doxygen.hpp b/include/lightlibc++/doxygen.hpp
index bdf9b31..14def2a 100644
--- a/include/lightlibc++/doxygen.hpp
+++ b/include/lightlibc++/doxygen.hpp
@@ -20,3 +20,5 @@ this is not to use the software at all.
  *  \ingroup lightlibcpp_18 */
 /** \defgroup lightlibcpp_18_3 18.3 Integer types
  *  \ingroup lightlibcpp_18 */
+/** \defgroup lightlibcpp_18_4 18.4 Start and termination
+ *  \ingroup lightlibcpp_18 */
diff --git a/source/cstdlib.cpp b/source/cstdlib.cpp
new file mode 100644
index 0000000..2f30780
--- /dev/null
+++ b/source/cstdlib.cpp
@@ -0,0 +1,71 @@
+/*
+Permission is granted to use, modify, and / or redistribute at will.
+
+This includes removing authorship notices, re-use of code parts in
+other software (with or without giving credit), and / or creating a
+commercial product based on it.
+
+This permission is not revocable by the author.
+
+This software is provided as-is. Use it at your own risk. There is
+no warranty whatsoever, neither expressed nor implied, and by using
+this software you accept that the author(s) shall not be held liable
+for any loss of data, loss of service, or other damages, be they
+incidental or consequential. Your only option other than accepting
+this is not to use the software at all.
+*/
+#include <cstdlib>
+#include <cstddef>           // std::size_t
+#include <exception>         // std::terminate
+
+
+
+/*
+ * file-local variables
+ */
+typedef void (*quick_exit_function_t)(void);
+quick_exit_function_t quick_exit_functions[32];
+std::size_t quick_exit_functions_index = 0;
+
+
+
+/*
+ * functions
+ */
+
+int std::at_quick_exit(void (*f)(void))
+{
+  // TODO: Ensure thread-safety
+
+  if (quick_exit_functions_index == 32)
+    return -1;
+
+  quick_exit_functions[quick_exit_functions_index++] = f;
+  return 0;
+}
+
+void std::quick_exit(int status)
+{
+  // TODO: except that a function shall be called after any previously
+  //       registered functions that had already been called at the time it was
+  //       registered
+  for (;quick_exit_functions_index > 0;--quick_exit_functions_index)
+  {
+    #ifndef _LIGHTLIBCPP_NO_EXCEPTIONS
+      try
+      {
+    #endif
+
+        quick_exit_functions[quick_exit_functions_index - 1]();
+
+    #ifndef _LIGHTLIBCPP_NO_EXCEPTIONS
+      }
+      catch (...)
+      {
+        std::terminate();
+      }
+    #endif
+  }
+
+  std::_Exit(status);
+}
-- 
1.6.0.4