Usermode/libc++ - Exceptions mostly
authorJohn Hodge <[email protected]>
Wed, 21 May 2014 15:23:10 +0000 (23:23 +0800)
committerJohn Hodge <[email protected]>
Wed, 21 May 2014 15:23:10 +0000 (23:23 +0800)
Usermode/Libraries/libc++.so_src/exception_handling.cc
Usermode/Libraries/libc++.so_src/exception_handling.h [new file with mode: 0644]
Usermode/Libraries/libc++.so_src/exceptions.cc
Usermode/Libraries/libc++.so_src/include_exp/allocator
Usermode/Libraries/libc++.so_src/include_exp/cstdlib [new file with mode: 0644]
Usermode/Libraries/libc++.so_src/include_exp/cstring [new file with mode: 0644]
Usermode/Libraries/libc++.so_src/include_exp/exception
Usermode/Libraries/libc++.so_src/include_exp/vector

index 2303980..44d029b 100644 (file)
@@ -2,66 +2,79 @@
  */
 #include <typeinfo>
 #include <cstdint>
+#include <cstddef>
+#include <cstdlib>
+#include <cstring>
+#include <exception>
+#include "exception_handling.h"
 
-typedef void *(unexpected_handler)(void);
-typedef void *(terminate_handler)(void);
 
-struct _Unwind_Context;
+ int   uncaught_exception_count;
+__cxa_exception        *uncaught_exception_top;
 
-typedef enum {
-       _URC_NO_REASON = 0,
-       _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
-       _URC_FATAL_PHASE2_ERROR = 2,
-       _URC_FATAL_PHASE1_ERROR = 3,
-       _URC_NORMAL_STOP = 4,
-       _URC_END_OF_STACK = 5,
-       _URC_HANDLER_FOUND = 6,
-       _URC_INSTALL_CONTEXT = 7,
-       _URC_CONTINUE_UNWIND = 8
-} _Unwind_Reason_Code;
+/*__thread*/ struct {
+       __cxa_exception info;
+       char    buf[32];
+} emergency_exception;
+/*__thread*/ bool      emergency_exception_used;
 
-typedef void   (*_Unwind_Exception_Cleanup_Fn)(_Unwind_Reason_Code reason, struct _Unwind_Exception *exc);
+static bool TEST_AND_SET(bool& flag) {
+       bool ret = flag;
+       flag = true;
+       return ret;
+}
 
-struct _Unwind_Exception
+// === CODE ===
+extern "C" void __cxa_call_unexpected(void *)
 {
-       uint64_t        exception_class;
-       _Unwind_Exception_Cleanup_Fn    exception_cleanup;
-       uint64_t        private_1;
-       uint64_t        private_2;
-};
+       // An unexpected exception was thrown from a function that lists its possible exceptions
+       for(;;);
+}
 
-struct __cxa_exception
+extern "C" void *__cxa_allocate_exception(size_t thrown_size)
 {
-       std::type_info* exceptionType;
-       void (*exceptionDestructor)(void *);
-       unexpected_handler      unexpectedHandler;
-       terminate_handler       terminateHandler;
-       __cxa_exception*        nextException;
-       
-       int     handlerCount;
-       
-       int     handlerSwitchValue;
-       const char*     actionRecord;
-       const char*     languageSpecificData;
-       void*   catchTemp;
-       void*   adjustedPtr;
-       
-       _Unwind_Exception       unwindHeader;
-};
+       __cxa_exception *ret = static_cast<__cxa_exception*>( malloc( sizeof(__cxa_exception) + thrown_size ) );
+       if( !ret ) {
+               if( thrown_size <= sizeof(emergency_exception.buf) && TEST_AND_SET(emergency_exception_used) )
+               {
+                       ret = &emergency_exception.info;
+               }
+       }
+       if( !ret ) {
+               ::std::terminate();
+       }
+       return ret;
+}
 
- int   uncaught_exception_count;
-__cxa_exception        *uncaught_exception_top;
+extern "C" void __cxa_free_exception(void *thrown_exception)
+{
+       if(thrown_exception == &emergency_exception.buf) {
+               //assert(emergency_exception_used);
+               emergency_exception_used = false;
+       }
+       else {
+               __cxa_exception *except = static_cast<__cxa_exception*>( thrown_exception ) - 1;
+               free(except);
+       }
+}
 
-extern "C" void __cxa_call_unexpected(void *)
+extern "C" void __cxa_throw(void *thrown_exception, std::type_info *tinfo, void (*dest)(void*))
 {
-       // An unexpected exception was thrown from a function that lists its possible exceptions
-       for(;;);
+       __cxa_exception *except = static_cast<__cxa_exception*>( thrown_exception ) - 1;
+       
+       except->exceptionType = tinfo;
+       except->exceptionDestructor = dest;
+       memcpy(&except->unwindHeader.exception_class, "Ac20C++\0", 8);
+       uncaught_exception_top ++;
+       
+       _Unwind_RaiseException(thrown_exception);
+       
+       ::std::terminate();
 }
 
 extern "C" void *__cxa_begin_catch(void *exceptionObject)
 {
-       struct __cxa_exception  *except = static_cast<__cxa_exception*>( exceptionObject );
-       except --;
+       __cxa_exception *except = static_cast<__cxa_exception*>( exceptionObject ) - 1;
        
        except->handlerCount ++;
        
@@ -80,7 +93,7 @@ extern "C" void __cxa_end_catch()
        uncaught_exception_top = except->nextException;
        
        if( except->handlerCount == 0 ) {
-               //__cxa_free_exception(except+1);
+               __cxa_free_exception(except+1);
        }
 }
 
diff --git a/Usermode/Libraries/libc++.so_src/exception_handling.h b/Usermode/Libraries/libc++.so_src/exception_handling.h
new file mode 100644 (file)
index 0000000..b1feae5
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ */
+#ifndef _EXCEPTION_HANLDING_H_
+#define _EXCEPTION_HANLDING_H_
+
+typedef void *(unexpected_handler)(void);
+typedef void *(terminate_handler)(void);
+
+struct _Unwind_Context;
+
+typedef enum {
+       _URC_NO_REASON = 0,
+       _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
+       _URC_FATAL_PHASE2_ERROR = 2,
+       _URC_FATAL_PHASE1_ERROR = 3,
+       _URC_NORMAL_STOP = 4,
+       _URC_END_OF_STACK = 5,
+       _URC_HANDLER_FOUND = 6,
+       _URC_INSTALL_CONTEXT = 7,
+       _URC_CONTINUE_UNWIND = 8
+} _Unwind_Reason_Code;
+
+typedef void   (*_Unwind_Exception_Cleanup_Fn)(_Unwind_Reason_Code reason, struct _Unwind_Exception *exc);
+
+struct _Unwind_Exception
+{
+       uint64_t        exception_class;
+       _Unwind_Exception_Cleanup_Fn    exception_cleanup;
+       uint64_t        private_1;
+       uint64_t        private_2;
+};
+
+struct __cxa_exception
+{
+       std::type_info* exceptionType;
+       void (*exceptionDestructor)(void *);
+       unexpected_handler      unexpectedHandler;
+       terminate_handler       terminateHandler;
+       __cxa_exception*        nextException;
+       
+       int     handlerCount;
+       
+       int     handlerSwitchValue;
+       const char*     actionRecord;
+       const char*     languageSpecificData;
+       void*   catchTemp;
+       void*   adjustedPtr;
+       
+       _Unwind_Exception       unwindHeader;
+};
+
+
+extern "C" void __cxa_call_unexpected(void *);
+extern "C" void *__cxa_allocate_exception(size_t thrown_size);
+extern "C" void __cxa_free_exception(void *thrown_exception);
+extern "C" void __cxa_throw(void *thrown_exception, std::type_info *tinfo, void (*dest)(void*));
+extern "C" void *__cxa_begin_catch(void *exceptionObject);
+extern "C" void __cxa_end_catch();
+
+extern "C" void _Unwind_RaiseException(void *thrown_exception);
+
+#endif
+
index 2b66dce..593ce3a 100644 (file)
@@ -8,6 +8,7 @@
 #include <string>
 #include <exception>
 #include <stdexcept>
+#include <acess/sys.h>
 
 // === CODE ===
 ::std::exception::exception() throw():
@@ -35,6 +36,12 @@ const char* ::std::exception::what() const throw()
        return m_what_str.c_str();
 }
 
+void ::std::terminate()
+{
+       _exit(0);
+}
+
+// --- Standar Exceptions ---
 ::std::logic_error::logic_error(const ::std::string& what_str):
        exception(what_str)
 {
index d9faae9..943dc0f 100644 (file)
@@ -53,6 +53,20 @@ public:
                n=n;
                ::operator delete(p);
        }
+       size_type max_size() {
+               return ((size_type)-1) / sizeof(value_type);
+       }
+       void construct( pointer p, const_reference val ) {
+               ::new ((void*)p) value_type (val);
+       }
+       // C++11
+       //template<class U, class... Args>
+       //void construct( U* p, Args&&... args ) {
+       //      ::new ((void*)p) U (forward<Args>(args));
+       //}
+       void destroy(pointer p) {
+               p->~value_type();
+       }
 };
 
 };
diff --git a/Usermode/Libraries/libc++.so_src/include_exp/cstdlib b/Usermode/Libraries/libc++.so_src/include_exp/cstdlib
new file mode 100644 (file)
index 0000000..7d119a7
--- /dev/null
@@ -0,0 +1,17 @@
+/*
+ * Acess2 C++ Library
+ * - By John Hodge (thePowersGang)
+ *
+ * cstdlib (header)
+ * - C++ wrapper around stdlib.h
+ */
+#ifndef _LIBCXX_CSTDLIB_
+#define _LIBCXX_CSTDLIB_
+
+extern "C" {
+#include <stdlib.h>
+};
+
+#endif
+
+
diff --git a/Usermode/Libraries/libc++.so_src/include_exp/cstring b/Usermode/Libraries/libc++.so_src/include_exp/cstring
new file mode 100644 (file)
index 0000000..66149e0
--- /dev/null
@@ -0,0 +1,17 @@
+/*
+ * Acess2 C++ Library
+ * - By John Hodge (thePowersGang)
+ *
+ * cstring (header)
+ * - C++ wrapper around string.h
+ */
+#ifndef _LIBCXX_CSTRING_
+#define _LIBCXX_CSTRING_
+
+extern "C" {
+#include <string.h>
+};
+
+#endif
+
+
index 0beb016..63e35bd 100644 (file)
@@ -27,6 +27,22 @@ protected:
        exception(const string& what_str) noexcept;
 };
 
+class bad_exception:
+       public exception
+{
+public:
+       bad_exception() noexcept;
+};
+
+typedef void (*terminate_handler)();
+typedef void (*unexpected_handler)();
+
+extern void set_terminate(terminate_handler f) throw();
+extern void set_unexpected(unexpected_handler f) throw();
+extern void terminate();
+extern void unexpected();
+extern bool uncaught_exception() throw();
+
 };     // namespace std
 
 #endif
index e69de29..c130123 100644 (file)
@@ -0,0 +1,73 @@
+/*
+ * Acess2 C++ Library
+ * - By John Hodge (thePowersGang)
+ *
+ * string (header)
+ * - C++'s String type
+ */
+#ifndef _LIBCXX_VECTOR_
+#define _LIBCXX_VECTOR_
+
+#include <allocator>
+
+namespace std {
+
+namespace _bits {
+template <class T> class vector_iterator;
+}
+
+template <class T, class Alloc = allocator<T> >
+class vector
+{
+public:
+       typedef T       value_type;
+       typedef Alloc   allocator_type;
+       typedef typename allocator_type::reference      reference;
+       typedef typename allocator_type::const_reference        const_reference;
+       typedef typename allocator_type::pointer        pointer;
+       typedef typename allocator_type::const_pointer  const_pointer;
+       typedef ::std::_bits::vector_iterator<T>        iterator;
+       typedef ::std::_bits::vector_iterator<const T>  const_iterator;
+       typedef int     difference_type;
+       typedef size_t  size_type;
+
+private:
+       allocator_type  m_alloc;
+       size_type       m_size;
+       size_type       m_capacity;
+       value_type*     m_data;
+
+public:        
+       vector(const allocator_type& alloc = allocator_type()):
+               m_alloc(alloc),
+               m_size(0),
+               m_capacity(0),
+               m_data(0)
+       {
+       }
+       vector(size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type()):
+               vector(alloc)
+       {
+               
+       }
+       vector(const vector& x):
+               vector(x.m_alloc)
+       {
+               *this = x;
+       }
+       
+       ~vector()
+       {
+               for( size_type i = 0; i < m_size; i ++ ) {
+                       m_alloc.destroy( &m_data[i] );
+               }
+       }
+       
+       
+};
+
+};     // namespace std
+
+#endif
+// vim: ft=cpp
+

UCC git Repository :: git.ucc.asn.au