From eac1febfad3e3c90d7fb1c79338f3f2388fa36c0 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Fri, 2 Nov 2018 14:24:31 +0000 Subject: [PATCH] cleaned up code --- moo/lib/comp.c | 6 --- moo/lib/err.c | 129 +++++++++++++++++++++++----------------------- moo/lib/main.c | 135 ++++++++++++++++++++++++++++++++----------------- moo/lib/moo.h | 4 -- 4 files changed, 154 insertions(+), 120 deletions(-) diff --git a/moo/lib/comp.c b/moo/lib/comp.c index 511dfac..15f48d0 100644 --- a/moo/lib/comp.c +++ b/moo/lib/comp.c @@ -9355,9 +9355,3 @@ int moo_compile (moo_t* moo, moo_ioimpl_t io) return n; } - -void moo_getsynerr (moo_t* moo, moo_synerr_t* synerr) -{ - MOO_ASSERT (moo, moo->c != MOO_NULL); - if (synerr) *synerr = moo->c->synerr; -} diff --git a/moo/lib/err.c b/moo/lib/err.c index 7bcc014..d467691 100644 --- a/moo/lib/err.c +++ b/moo/lib/err.c @@ -26,10 +26,6 @@ #include "moo-prv.h" -#if defined(MOO_ENABLE_LIBUNWIND) -# include -#endif - /* BEGIN: GENERATED WITH generr.moo */ static moo_ooch_t errstr_0[] = {'n','o',' ','e','r','r','o','r','\0'}; @@ -184,52 +180,9 @@ static const moo_ooch_t* synerr_to_errstr (moo_synerrnum_t errnum) } #endif -/* -------------------------------------------------------------------------- - * SYSTEM DEPENDENT FUNCTIONS - * -------------------------------------------------------------------------- */ - -#if defined(HAVE_EXECINFO_H) -# include -# include -#endif - -#if defined(_WIN32) -# include -# include -#elif defined(__OS2__) -# define INCL_DOSPROCESS -# define INCL_DOSFILEMGR -# define INCL_DOSERRORS -# include -#elif defined(__DOS__) -# include -# if defined(_INTELC32_) -# define DOS_EXIT 0x4C -# else -# include -# endif -# include -#elif defined(vms) || defined(__vms) -# define __NEW_STARLET 1 -# include /* (SYS$...) */ -# include /* (SS$...) */ -# include /* (lib$...) */ -#elif defined(macintosh) -# include -# include -# include -# include -#else -# include -# include -# include -# include -#endif - /* -------------------------------------------------------------------------- * ERROR NUMBER/MESSAGE HANDLING * -------------------------------------------------------------------------- */ - const moo_ooch_t* moo_geterrstr (moo_t* moo) { return moo_errnum_to_errstr (moo->errnum); @@ -262,13 +215,13 @@ void moo_seterrwithsyserr (moo_t* moo, int syserr_type, int syserr_code) if (moo->vmprim.syserrstrb) { - errnum = moo->vmprim.syserrstrb (moo, syserr_type, syserr_code, moo->errmsg.tmpbuf.bch, MOO_COUNTOF(moo->errmsg.tmpbuf.bch)); + errnum = moo->vmprim.syserrstrb(moo, syserr_type, syserr_code, moo->errmsg.tmpbuf.bch, MOO_COUNTOF(moo->errmsg.tmpbuf.bch)); moo_seterrbfmt (moo, errnum, "%hs", moo->errmsg.tmpbuf.bch); } else { MOO_ASSERT (moo, moo->vmprim.syserrstru != MOO_NULL); - errnum = moo->vmprim.syserrstru (moo, syserr_type, syserr_code, moo->errmsg.tmpbuf.uch, MOO_COUNTOF(moo->errmsg.tmpbuf.uch)); + errnum = moo->vmprim.syserrstru(moo, syserr_type, syserr_code, moo->errmsg.tmpbuf.uch, MOO_COUNTOF(moo->errmsg.tmpbuf.uch)); moo_seterrbfmt (moo, errnum, "%ls", moo->errmsg.tmpbuf.uch); } } @@ -405,13 +358,69 @@ void moo_setsynerr (moo_t* moo, moo_synerrnum_t num, const moo_ioloc_t* loc, con { moo_setsynerrbfmt (moo, num, loc, tgt, MOO_NULL); } + +void moo_getsynerr (moo_t* moo, moo_synerr_t* synerr) +{ + MOO_ASSERT (moo, moo->c != MOO_NULL); + if (synerr) *synerr = moo->c->synerr; +} #endif /* -------------------------------------------------------------------------- - * STACK FRAME BACKTRACE + * ASSERTION SUPPORT * -------------------------------------------------------------------------- */ + +#if defined(MOO_BUILD_RELEASE) + +void moo_assertfailed (moo_t* moo, const moo_bch_t* expr, const moo_bch_t* file, moo_oow_t line) +{ + /* do nothing */ +} + +#else /* defined(MOO_BUILD_RELEASE) */ + #if defined(MOO_ENABLE_LIBUNWIND) -void moo_backtrace (moo_t* moo) +# include +#elif defined(HAVE_EXECINFO_H) +# include +# include +#endif + +#if defined(_WIN32) +# include +# include +#elif defined(__OS2__) +# define INCL_DOSPROCESS +# define INCL_DOSFILEMGR +# define INCL_DOSERRORS +# include +#elif defined(__DOS__) +# include +# if defined(_INTELC32_) +# define DOS_EXIT 0x4C +# else +# include +# endif +# include +#elif defined(vms) || defined(__vms) +# define __NEW_STARLET 1 +# include /* (SYS$...) */ +# include /* (SS$...) */ +# include /* (lib$...) */ +#elif defined(macintosh) +# include +# include +# include +# include +#else +# include +# include +# include +# include +#endif + +#if defined(MOO_ENABLE_LIBUNWIND) +static void backtrace_stack_frames (moo_t* moo) { unw_cursor_t cursor; unw_context_t context; @@ -440,7 +449,7 @@ void moo_backtrace (moo_t* moo) } } #elif defined(HAVE_BACKTRACE) -void moo_backtrace (moo_t* moo) +static void backtrace_stack_frames (moo_t* moo) { void* btarray[128]; moo_oow_t btsize; @@ -461,24 +470,16 @@ void moo_backtrace (moo_t* moo) } } #else -void moo_backtrace (moo_t* moo) +static void backtrace_stack_frames (moo_t* moo) { /* do nothing. not supported */ } #endif -/* -------------------------------------------------------------------------- - * ASSERTION FAILURE HANDLER - * -------------------------------------------------------------------------- */ - - void moo_assertfailed (moo_t* moo, const moo_bch_t* expr, const moo_bch_t* file, moo_oow_t line) { -#if defined(MOO_BUILD_RELEASE) - /* do nothing */ -#else moo_logbfmt (moo, MOO_LOG_UNTYPED | MOO_LOG_FATAL, "ASSERTION FAILURE: %s at %s:%zu\n", expr, file, line); - moo_backtrace (moo); + backtrace_stack_frames (moo); #if defined(_WIN32) ExitProcess (249); @@ -505,6 +506,6 @@ void moo_assertfailed (moo_t* moo, const moo_bch_t* expr, const moo_bch_t* file, kill (getpid(), SIGABRT); _exit (1); #endif - -#endif } + +#endif /* defined(MOO_BUILD_RELEASE) */ diff --git a/moo/lib/main.c b/moo/lib/main.c index c40af2d..374b8b8 100644 --- a/moo/lib/main.c +++ b/moo/lib/main.c @@ -43,16 +43,12 @@ # include # include # include +# include # if defined(MOO_HAVE_CFG_H) && defined(MOO_ENABLE_LIBLTDL) # include # define USE_LTDL # else # define USE_WIN_DLL -# define sys_dl_error() win_dlerror() -# define sys_dl_open(x) LoadLibraryExA(x, MOO_NULL, 0) -# define sys_dl_openext(x) LoadLibraryExA(x, MOO_NULL, 0) -# define sys_dl_close(x) FreeLibrary(x) -# define sys_dl_getsym(x,n) GetProcAddress(x,n) # endif # include "poll-msw.h" @@ -103,27 +99,12 @@ # if defined(MOO_ENABLE_LIBLTDL) # include # define USE_LTDL -# define sys_dl_error() lt_dlerror() -# define sys_dl_open(x) lt_dlopen(x) -# define sys_dl_openext(x) lt_dlopenext(x) -# define sys_dl_close(x) lt_dlclose(x) -# define sys_dl_getsym(x,n) lt_dlsym(x,n) # elif defined(HAVE_DLFCN_H) # include # define USE_DLFCN -# define sys_dl_error() dlerror() -# define sys_dl_open(x) dlopen(x,RTLD_NOW) -# define sys_dl_openext(x) dlopen(x,RTLD_NOW) -# define sys_dl_close(x) dlclose(x) -# define sys_dl_getsym(x,n) dlsym(x,n) # elif defined(__APPLE__) || defined(__MACOSX__) # define USE_MACH_O_DYLD # include -# define sys_dl_error() mach_dlerror() -# define sys_dl_open(x) mach_dlopen(x) -# define sys_dl_openext(x) mach_dlopen(x) -# define sys_dl_close(x) mach_dlclose(x) -# define sys_dl_getsym(x,n) mach_dlsym(x,n) # else # error UNSUPPORTED DYNAMIC LINKER # endif @@ -880,7 +861,37 @@ static moo_errnum_t errno_to_errnum (int errcode) #if defined(_WIN32) static moo_errnum_t winerr_to_errnum (DWORD errcode) { - return MOO_ESYSERR; + switch (errcode) + { + case ERROR_NOT_ENOUGH_MEMORY: + case ERROR_OUTOFMEMORY: + return MOO_ESYSMEM; + + case ERROR_INVALID_PARAMETER: + case ERROR_INVALID_NAME: + return MOO_EINVAL; + + case ERROR_INVALID_HANDLE: + return MOO_EBADHND; + + case ERROR_ACCESS_DENIED: + case ERROR_SHARING_VIOLATION: + return MOO_EACCES; + + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + return MOO_ENOENT; + + case ERROR_ALREADY_EXISTS: + case ERROR_FILE_EXISTS: + return MOO_EEXIST; + + case ERROR_BROKEN_PIPE: + return MOO_EPIPE; + + default: + return MOO_ESYSERR; + } } #endif @@ -894,10 +905,12 @@ static moo_errnum_t os2err_to_errnum (APIRET errcode) return MOO_ESYSMEM; case ERROR_INVALID_PARAMETER: - case ERROR_INVALID_HANDLE: case ERROR_INVALID_NAME: return MOO_EINVAL; + case ERROR_INVALID_HANDLE: + return MOO_EBADHND; + case ERROR_ACCESS_DENIED: case ERROR_SHARING_VIOLATION: return MOO_EACCES; @@ -947,21 +960,24 @@ static moo_errnum_t syserrstrb (moo_t* moo, int syserr_type, int syserr_code, mo { case 1: #if defined(_WIN32) - DWORD rc; - rc = FormatMessageA ( - FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, syserr_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - buf, len, MOO_NULL - ); - while (rc > 0 && buf[rc - 1] == '\r' || buf[rc - 1] == '\n') buf[--rc] = '\0'; + if (buf) + { + DWORD rc; + rc = FormatMessageA ( + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, syserr_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + buf, len, MOO_NULL + ); + while (rc > 0 && buf[rc - 1] == '\r' || buf[rc - 1] == '\n') buf[--rc] = '\0'; + } return winerr_to_errnum(syserr_code); #elif defined(__OS2__) /* TODO: convert code to string */ - moo_copy_bcstr (buf, len, "system error"); + if (buf) moo_copy_bcstr (buf, len, "system error"); return os2err_to_errnum(syserr_code); #elif defined(macintosh) /* TODO: convert code to string */ - moo_copy_bcstr (buf, len, "system error"); + if (buf) moo_copy_bcstr (buf, len, "system error"); return os2err_to_errnum(syserr_code); #else /* in other systems, errno is still the native system error code. @@ -970,21 +986,49 @@ static moo_errnum_t syserrstrb (moo_t* moo, int syserr_type, int syserr_code, mo case 0: #if defined(HAVE_STRERROR_R) - strerror_r (syserr_code, buf, len); + if (buf) strerror_r (syserr_code, buf, len); #else /* this is not thread safe */ - moo_copy_bcstr (buf, len, strerror(syserr_code)); + if (buf) moo_copy_bcstr (buf, len, strerror(syserr_code)); #endif return errno_to_errnum(syserr_code); } - moo_copy_bcstr (buf, len, "system error"); + if (buf) moo_copy_bcstr (buf, len, "system error"); return MOO_ESYSERR; } - /* ========================================================================= */ +#if defined(USE_LTDL) +# define sys_dl_error() lt_dlerror() +# define sys_dl_open(x) lt_dlopen(x) +# define sys_dl_openext(x) lt_dlopenext(x) +# define sys_dl_close(x) lt_dlclose(x) +# define sys_dl_getsym(x,n) lt_dlsym(x,n) + +#elif defined(USE_DLFCN) +# define sys_dl_error() dlerror() +# define sys_dl_open(x) dlopen(x,RTLD_NOW) +# define sys_dl_openext(x) dlopen(x,RTLD_NOW) +# define sys_dl_close(x) dlclose(x) +# define sys_dl_getsym(x,n) dlsym(x,n) + +#elif defined(USE_WIN_DLL) +# define sys_dl_error() win_dlerror() +# define sys_dl_open(x) LoadLibraryExA(x, MOO_NULL, 0) +# define sys_dl_openext(x) LoadLibraryExA(x, MOO_NULL, 0) +# define sys_dl_close(x) FreeLibrary(x) +# define sys_dl_getsym(x,n) GetProcAddress(x,n) + +#elif defined(USE_MACH_O_DYLD) +# define sys_dl_error() mach_dlerror() +# define sys_dl_open(x) mach_dlopen(x) +# define sys_dl_openext(x) mach_dlopen(x) +# define sys_dl_close(x) mach_dlclose(x) +# define sys_dl_getsym(x,n) mach_dlsym(x,n) +#endif + #if defined(USE_WIN_DLL) static const char* win_dlerror (void) @@ -1857,15 +1901,6 @@ static void vm_gettime (moo_t* moo, moo_ntime_t* now) #endif } -#if defined(__DOS__) -# if defined(_INTELC32_) - void _halt_cpu (void); -# elif defined(__WATCOMC__) - void _halt_cpu (void); -# pragma aux _halt_cpu = "hlt" -# endif -#endif - static int vm_muxadd (moo_t* moo, moo_ooi_t io_handle, moo_ooi_t mask) { int event_mask; @@ -2290,6 +2325,15 @@ static void vm_muxwait (moo_t* moo, const moo_ntime_t* dur, moo_vmprim_muxwait_c #endif /* USE_THREAD */ } +#if defined(__DOS__) +# if defined(_INTELC32_) + void _halt_cpu (void); +# elif defined(__WATCOMC__) + void _halt_cpu (void); +# pragma aux _halt_cpu = "hlt" +# endif +#endif + static void vm_sleep (moo_t* moo, const moo_ntime_t* dur) { #if defined(_WIN32) @@ -2297,8 +2341,7 @@ static void vm_sleep (moo_t* moo, const moo_ntime_t* dur) if (xtn->waitable_timer) { LARGE_INTEGER li; -goto normal_sleep; - li.QuadPart = -MOO_SECNSEC_TO_NSEC(dur->sec, dur->nsec); + li.QuadPart = -(MOO_SECNSEC_TO_NSEC(dur->sec, dur->nsec) / 100); /* in 100 nanoseconds */ if(SetWaitableTimer(xtn->waitable_timer, &li, 0, MOO_NULL, MOO_NULL, FALSE) == FALSE) goto normal_sleep; WaitForSingleObject(xtn->waitable_timer, INFINITE); } diff --git a/moo/lib/moo.h b/moo/lib/moo.h index 7fa6bfc..3b533bf 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -2288,10 +2288,6 @@ MOO_EXPORT int moo_decode ( const moo_oocs_t* classfqn ); -MOO_EXPORT void moo_backtrace ( - moo_t* moo -); - MOO_EXPORT void moo_assertfailed ( moo_t* moo, const moo_bch_t* expr,