From 7dabb65d28e0174a6814d706095692f57ab9a635 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sat, 16 May 2015 02:38:45 -0400 Subject: [PATCH 01/50] Add Fatal Error Handler, rewrite Error Handler as. This commit adds a Fatal Error Notification Subsystem implementation and rewrites the pre-existing error handler code to be C code with C++ bindings. Do note that the transition to C code means that an API change cannot be avoided. Although attempts have been made to make the transition as easy as possible. Also the error table API version numbers have been updated. In addition to adding an API call to check what those version numbers are. --- CMakeLists.txt | 2 + src/Common/Src/Error_Handler/CMakeLists.txt | 59 ++- .../Src/Error_Handler/Common_Error_Handler.c | 370 ++++++++++++++++++ .../Error_Handler/Common_Error_Handler.cpp | 136 ------- .../Src/Error_Handler/Common_Error_Handler.h | 226 ++++++++++- .../Common_Error_Handler_CPP_Bindings.cpp | 118 ++++++ .../Common_Error_Handler_Internal.h | 12 +- .../Common_Error_Handler_Structures.c | 148 +++++++ .../Common_Error_Handler_Structures.h | 247 +++++++----- ..._Error_Handler_Structures_CPP_Bindings.cpp | 41 ++ src/Common/Src/Error_Handler/Error_Struct.h | 25 +- ...le.cpp => Posix_Error_Translation_Table.c} | 37 +- .../Posix_Error_Translation_Table.h | 162 ++++++-- ...x_Error_Translation_Table_CPP_Bindings.cpp | 45 +++ 14 files changed, 1302 insertions(+), 326 deletions(-) create mode 100644 src/Common/Src/Error_Handler/Common_Error_Handler.c delete mode 100644 src/Common/Src/Error_Handler/Common_Error_Handler.cpp create mode 100644 src/Common/Src/Error_Handler/Common_Error_Handler_CPP_Bindings.cpp create mode 100644 src/Common/Src/Error_Handler/Common_Error_Handler_Structures.c create mode 100644 src/Common/Src/Error_Handler/Common_Error_Handler_Structures_CPP_Bindings.cpp rename src/Common/Src/Error_Handler/{Posix_Error_Translation_Table.cpp => Posix_Error_Translation_Table.c} (60%) create mode 100644 src/Common/Src/Error_Handler/Posix_Error_Translation_Table_CPP_Bindings.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e083325..7b86078 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,8 @@ endif() # Option Defaults. option (BUILD_DYNAMIC_LIBRARY_SUBSYSTEM "Whether or not to build the Dynamic Library Subsystem. (Defaults to yes.)" ON) +option (BUILD_COMMON_ERROR_HANDLER "Whether or not to build the Common Error Handler. (Defaults to yes.)" ON) +option (BUILD_FATAL_ERROR_NOTIFY_SUPPORT "Whether or not to build the Common Error Handler's fatal error notification support. (Defaults to yes.)" ON) option (BUILD_INTERNAL_MUTEX_SUPPORT "Whether or not to build the internal mutex support library. (Defaults to yes.)" ON) option (BUILD_INTERNAL_MUTEX_THREAD_SUBSYS_WRAPPER "Whether or not the Threading Subsystem library will have Internal Mutex Support built in. (Defaults to yes, but is disabled currently due to changes to Thread_Utils that have not been commited yet.)" OFF) option (BUILD_INTERNAL_MUTEX_THREAD_SUBSYS_WRAPPER_PLUGIN "Whether or not to build the Threading Subsystem plugin for Internal Mutex Support. (Defaults to yes, but is disabled currently due to changes to Thread_Utils that have not been commited yet.)" OFF) diff --git a/src/Common/Src/Error_Handler/CMakeLists.txt b/src/Common/Src/Error_Handler/CMakeLists.txt index 44119e0..ed2b7e5 100644 --- a/src/Common/Src/Error_Handler/CMakeLists.txt +++ b/src/Common/Src/Error_Handler/CMakeLists.txt @@ -2,8 +2,59 @@ set(LIBRARY_OUTPUT_PATH ${L_OUTPUT_DIR}) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${O_OUTPUT_DIR}) -# Create the error handler. -set (COMMON_ERROR_HANDLER_INCLUDES Common_Error_Handler.cpp -Posix_Error_Translation_Table.cpp) +# Build the error handler if needed. +if (BUILD_COMMON_ERROR_HANDLER) + # Check for internal mutex support as the error handler uses it. + if (BUILD_FATAL_ERROR_NOTIFY_SUPPORT) + if (BUILD_INTERNAL_MUTEX_SUPPORT) + # Enable the needed flags. + set (COMMON_ERROR_HANDLER_DEFINES MSYS_BUILD_FATAL_ERROR_SUPPORT) -add_library(Common_Error_Handler_Multiverse_Engine SHARED ${COMMON_ERROR_HANDLER_INCLUDES}) + # Define the link libraries. + set(COMMON_ERROR_HANDLER_LINK_LIBS Common_Mutexes_Multiverse_Engine) + else(BUILD_INTERNAL_MUTEX_SUPPORT) + # No internal mutex support, so skip building the error handler's fatal error notification support. + message(SEND_ERROR "ERROR: Internal mutex support is disabled, and is REQUIRED to build the Common Error Handler's fatal error notification support. Skipping.") + endif(BUILD_INTERNAL_MUTEX_SUPPORT) + endif (BUILD_FATAL_ERROR_NOTIFY_SUPPORT) + + # Create the error handler. (Nasty hack to avoid a warning from GCC about invalid -std:gnu99 flag below....) + set (COMMON_ERROR_HANDLER_C_INCLUDES Common_Error_Handler.c + Posix_Error_Translation_Table.c + ) + set (COMMON_ERROR_HANDLER_CXX_INCLUDES Common_Error_Handler_Structures_CPP_Bindings.cpp + Common_Error_Handler_CPP_Bindings.cpp + Posix_Error_Translation_Table_CPP_Bindings.cpp + ) + + # Because we broke up the object code build from the shared library linking, we need to break up the flags and arguments too. + set (COMMON_ERROR_HANDLER_COMPILER_FLAGS -fPIC) + set (COMMON_ERROR_HANDLER_C_FLAGS ${COMMON_ERROR_HANDLER_COMPILER_FLAGS}) + set (COMMON_ERROR_HANDLER_CXX_FLAGS ${COMMON_ERROR_HANDLER_COMPILER_FLAGS}) + set (COMMON_ERROR_HANDLER_LINK_FLAGS -fPIC) + + # Check for gcc and enable gnu99 extensions if needed. + if (${CMAKE_COMPILER_IS_GNUCC}) + message (STATUS "GCC compiler detected, enabling gnu99 extensions for Common Error Handler.") + set (COMMON_ERROR_HANDLER_C_FLAGS ${COMMON_ERROR_HANDLER_C_FLAGS} -std=gnu99) + endif(${CMAKE_COMPILER_IS_GNUCC}) + + # First add the static C object library... + add_library(Common_Error_Handler_Multiverse_Engine_C OBJECT ${COMMON_ERROR_HANDLER_C_INCLUDES}) + set_property(TARGET Common_Error_Handler_Multiverse_Engine_C APPEND PROPERTY COMPILE_DEFINITIONS ${COMMON_ERROR_HANDLER_DEFINES}) + target_compile_options (Common_Error_Handler_Multiverse_Engine_C BEFORE PRIVATE ${COMMON_ERROR_HANDLER_C_FLAGS}) # Allows us to put the args to the compiler in the correct order. + + # Now add the C++ Bindings object library. + add_library(Common_Error_Handler_Multiverse_Engine_CXX OBJECT ${COMMON_ERROR_HANDLER_CXX_INCLUDES}) + set_property(TARGET Common_Error_Handler_Multiverse_Engine_CXX APPEND PROPERTY COMPILE_DEFINITIONS ${COMMON_ERROR_HANDLER_DEFINES}) + target_compile_options (Common_Error_Handler_Multiverse_Engine_CXX BEFORE PRIVATE ${COMMON_ERROR_HANDLER_CXX_FLAGS}) # Allows us to put the args to the compiler in the correct order. + + # Now the final shared library. (What we really want.) + add_library(Common_Error_Handler_Multiverse_Engine SHARED $ + $ + ) + target_link_libraries(Common_Error_Handler_Multiverse_Engine ${COMMON_ERROR_HANDLER_LINK_LIBS}) + set_property(TARGET Common_Error_Handler_Multiverse_Engine APPEND PROPERTY COMPILE_DEFINITIONS ${COMMON_ERROR_HANDLER_DEFINES}) + set_property(TARGET Common_Error_Handler_Multiverse_Engine APPEND PROPERTY LINK_FLAGS ${COMMON_ERROR_HANDLER_LINK_FLAGS}) + +endif(BUILD_COMMON_ERROR_HANDLER) diff --git a/src/Common/Src/Error_Handler/Common_Error_Handler.c b/src/Common/Src/Error_Handler/Common_Error_Handler.c new file mode 100644 index 0000000..eb462fe --- /dev/null +++ b/src/Common/Src/Error_Handler/Common_Error_Handler.c @@ -0,0 +1,370 @@ +/*! + Multiverse Engine Project 23/6/2014 Common Common_Error_Handler.c + + Copyright (C) 2014 Multiverse Engine Project + + This program is free software; + you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; + either version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with this program; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Official source repository and project information can be found at + https://github.com/codebase7/mengine +*/ + +// Internal includes. +#include "Common_Error_Handler.h" // Main header. +#include "Common_Error_Handler_Internal.h" // Private internal header that defines the structure used for error logging. + +// Only include the mutex header if needed by the fatal error handler. +#ifdef MSYS_BUILD_FATAL_ERROR_SUPPORT +#ifdef __win32__ +#include "..\Mutexes\MSYS_Mutexes.h" // Internal mutex support. +#else +#include "../Mutexes/MSYS_Mutexes.h" // Internal mutex support. +#endif // __win32__ +#endif // MSYS_BUILD_FATAL_ERROR_SUPPORT + +// Enable C linkage if needed. +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +/*! + * static int Common_commonLastErrorCode + * + * Contains the last error code encountered by a Common namespace function. + * + * Note: Calling Common_GetErrorMessage() or Common_GetErrorTableSize() + * will NOT clear this variable. + * Calling any other Common namespace function WILL clear this variable. + */ +static int Common_commonLastErrorCode = COMMON_ERROR_SUCCESS; + +void Common_Set_Error_Log_Level(const unsigned int logLevel) +{ + // Set the log level. + commonErrorLoggingData.errorLogLevel = logLevel; + + // Exit function. + return; +} + +unsigned int Common_Get_Error_Log_Level() +{ + // Return the current log level. + return commonErrorLoggingData.errorLogLevel; +} + +void Common_Register_Error_Log_Callback(void (*loggingFunction)(const unsigned int logLevel, const char * errorMsg)) +{ + // Check and see if the pointer is NULL. + if (loggingFunction == NULL) + { + // Set the log level to ERROR_DISABLE. + commonErrorLoggingData.errorLogLevel = ERROR_DISABLE; + commonErrorLoggingData.loggingFunct = NULL; + } + else + { + // Set the pointer. + commonErrorLoggingData.loggingFunct = loggingFunction; + } + + // Exit function. + return; +} + +// Build the Fatal Error Handler if needed. +#ifdef MSYS_BUILD_FATAL_ERROR_SUPPORT +/*! + * static unsigned int registeredFatalErrorCallbackFunctionsSize + * + * This unsigned int is used to remember the allocated size of the registeredFatalErrorCallbackFunctions array. + */ +static unsigned int registeredFatalErrorCallbackFunctionsSize = 0; + +/* + * static Common_pErrorCallBackFunction * registeredFatalErrorCallbackFunctions + * + * This pointer to an array contains the callback functions (registered with Common::Register_Fatal_Error_Callback()) + * used to notify the engine's subsystems and the application that a fatal error has occured and the engine's + * process is about to be terminated by the host system. + */ +static Common_pErrorCallBackFunction * registeredFatalErrorCallbackFunctions = NULL; + +/*! + * static MSYS_Mutex * fatalErrorHandlerMutex + * + * This pointer is to an MSYS_Mutex object that is used to control access to the registered fatal error handler + * function pointer list and it's size value. + */ +static MSYS_Mutex * fatalErrorHandlerMutex = NULL; + +bool Common_Register_Fatal_Error_Callback(const Common_pErrorCallBackFunction fatalErrorNotifyFunction) +{ + // Init vars. + bool ret = false; // The result of this function. + size_t previousErrorListSize = registeredFatalErrorCallbackFunctionsSize; // The size of the previous error list. + size_t newErrorListSize = 0; // The size of the new error list we are creating. + Common_pErrorCallBackFunction * previousErrorList = registeredFatalErrorCallbackFunctions; // The previous error list. + Common_pErrorCallBackFunction * newErrorList = NULL; // The new error list we are creating. + MSYS_Mutex * retFromLockMutex = NULL; // The result from the call to MSYS_Lock_Mutex(). + + // Check for a valid mutex. + if (fatalErrorHandlerMutex == NULL) + { + // Allocate the mutex. + fatalErrorHandlerMutex = MSYS_Create_Mutex(); + } + + // Lock the error handler mutex. + retFromLockMutex = MSYS_Lock_Mutex(fatalErrorHandlerMutex); + if ((retFromLockMutex != NULL) && (retFromLockMutex == fatalErrorHandlerMutex)) + { + // Check for valid function pointer. + if (fatalErrorNotifyFunction != NULL) + { + // Check for a error function list. + if ((previousErrorList != NULL) && (previousErrorListSize > 0)) + { + // Re-allocate the error list. + newErrorList = (Common_pErrorCallBackFunction *)malloc((sizeof(Common_pErrorCallBackFunction) * (previousErrorListSize + 1))); + if (newErrorList != NULL) + { + // Update the size info. + newErrorListSize = (previousErrorListSize + 1); + + // Copy the data. + for (size_t x = 0; x < previousErrorListSize; x++) + { + newErrorList[x] = previousErrorList[x]; + } + } + } + else + { + // Allocate the error list. + newErrorList = (Common_pErrorCallBackFunction *)malloc(sizeof(Common_pErrorCallBackFunction)); + if (newErrorList != NULL) + { + // Update the size info. + newErrorListSize = 1; + } + } + + // Check for a valid list. + if ((previousErrorListSize + 1) == newErrorListSize) + { + // Register the function. + newErrorList[(previousErrorListSize)] = fatalErrorNotifyFunction; + + // Copy the new list pointer, and size info. + registeredFatalErrorCallbackFunctions = newErrorList; + registeredFatalErrorCallbackFunctionsSize = newErrorListSize; + + // Check and see if we need to deallocate the previousErrorList. + if ((previousErrorList != NULL) && (previousErrorList != newErrorList) && (previousErrorListSize > 0)) + { + // Null out the old pointer list. + for (size_t x = 0; x < previousErrorListSize; x++) + { + previousErrorList[x] = NULL; + } + + // Deallocate the old array. + free(previousErrorList); + previousErrorList = NULL; + previousErrorListSize = 0; + } + + // We are done. + ret = true; + } + } + + // Release the error handler mutex. + MSYS_Unlock_Mutex(fatalErrorHandlerMutex); + } + + // Return the result. + return ret; +} + +bool Common_Unregister_Fatal_Error_Callback(const Common_pErrorCallBackFunction fatalErrorNotifyFunction) +{ + // Init vars. + bool ret = false; // The result of this function. + size_t previousErrorListSize = registeredFatalErrorCallbackFunctionsSize; // The size of the previous error list. + size_t newErrorListSize = 0; // The size of the new error list we are creating. + Common_pErrorCallBackFunction * previousErrorList = registeredFatalErrorCallbackFunctions; // The previous error list. + Common_pErrorCallBackFunction * newErrorList = NULL; // The new error list we are creating. + MSYS_Mutex * retFromLockMutex = NULL; // The result from the call to MSYS_Lock_Mutex(). + + // Lock the error handler mutex. + retFromLockMutex = MSYS_Lock_Mutex(fatalErrorHandlerMutex); + if ((retFromLockMutex != NULL) && (retFromLockMutex == fatalErrorHandlerMutex)) + { + // Check for valid function pointer. + if (fatalErrorNotifyFunction != NULL) + { + // Check for a error function list. + if ((previousErrorList != NULL) && (previousErrorListSize > 0)) + { + // Check the existing list for that function. + for (size_t x = 0; ((newErrorList == NULL) && (x < previousErrorListSize)); x++) + { + // Check for the correct function, to see if the function was registered previously. + if (previousErrorList[x] == fatalErrorNotifyFunction) + { + // Found the function, so re-allocate the error list so we can remove it. + newErrorList = (Common_pErrorCallBackFunction *)malloc((sizeof(Common_pErrorCallBackFunction) * (previousErrorListSize - 1))); + if (newErrorList != NULL) + { + // Update the size info. + newErrorListSize = (previousErrorListSize - 1); + } + } + } + + // Only continue if the list was reallocated due to us finding the function to remove. + if ((newErrorList != NULL) && (newErrorListSize == (previousErrorListSize - 1))) + { + // Copy the data. + for (size_t x = 0, y = 0; ((x < previousErrorListSize) && (y < newErrorListSize)); x++) + { + // Make sure we don't copy the pointer we are removing. + if (previousErrorList[x] != fatalErrorNotifyFunction) + { + // Copy the pointer, and increment y. (Yes, y can be different than x, because it will not contain every pointer.) + newErrorList[y] = previousErrorList[x]; + y++; + } + } + + // Now that the data is copied, copy the pointers. + registeredFatalErrorCallbackFunctions = newErrorList; + registeredFatalErrorCallbackFunctionsSize = newErrorListSize; + + // Deallocate the old list. + if ((previousErrorList != NULL) && (previousErrorList != newErrorList) && (previousErrorListSize > 0)) + { + // Null out the old pointer list. + for (size_t x = 0; x < previousErrorListSize; x++) + { + previousErrorList[x] = NULL; + } + + free(previousErrorList); + previousErrorList = NULL; + previousErrorListSize = 0; + } + + // Done. + ret = true; + } + } + } + + // Release the error handler mutex. + MSYS_Unlock_Mutex(fatalErrorHandlerMutex); + } + + // Return the result. + return ret; +} + +void Common_Fatal_Error_Notify() +{ + // Init vars. + MSYS_Mutex * retFromLockMutex = NULL; // The result from the call to MSYS_Lock_Mutex(). + + // Lock the error handler mutex. + retFromLockMutex = MSYS_Lock_Mutex(fatalErrorHandlerMutex); + if ((retFromLockMutex != NULL) && (retFromLockMutex == fatalErrorHandlerMutex)) + { + // Check for registered fatal error callbacks. + if (registeredFatalErrorCallbackFunctionsSize > 0) + { + // Begin vector iteration loop. + for (size_t x = 0; (x < registeredFatalErrorCallbackFunctionsSize); x++) + { + // Trigger each function. + if (registeredFatalErrorCallbackFunctions[x] != NULL) + { + registeredFatalErrorCallbackFunctions[x](); + } + } + } + + // Release the error handler mutex. + MSYS_Unlock_Mutex(fatalErrorHandlerMutex); + } + + // Exit function. + return; +} + +#endif // MSYS_BUILD_FATAL_ERROR_SUPPORT + +void COMMON_LOG_ERROR(const unsigned int loggingLevel, const char * errorMsg) +{ + /* + * Only do something if the log is enabled, + * the error is at or below our current log level, + * and the logging callback function is defined. + * + * Note: The lower the log level, the higher severity of the error. + */ + if ((commonErrorLoggingData.errorLogLevel != ERROR_DISABLE) && + (commonErrorLoggingData.loggingFunct != NULL) && + (loggingLevel <= commonErrorLoggingData.errorLogLevel)) + { + // Call the callback. (Hope it returns....) + commonErrorLoggingData.loggingFunct(loggingLevel, errorMsg); + } + + // Exit function. + return; +} + +void COMMON_LOG_CRITICAL(const char * errorMsg) +{ + COMMON_LOG_ERROR(ERROR_CRITICAL, errorMsg); + return; +} + +void COMMON_LOG_WARNING(const char * errorMsg) +{ + COMMON_LOG_ERROR(ERROR_WARNING, errorMsg); + return; +} + +void COMMON_LOG_INFO(const char * errorMsg) +{ + COMMON_LOG_ERROR(ERROR_INFO, errorMsg); + return; +} + +void COMMON_LOG_DEBUG(const char * errorMsg) +{ + COMMON_LOG_ERROR(ERROR_DEBUG, errorMsg); + return; +} + +void COMMON_LOG_VERBOSE(const char * errorMsg) +{ + COMMON_LOG_ERROR(ERROR_VERBOSE, errorMsg); + return; +} + +// End C Linkage if needed. +#ifdef __cplusplus +} +#endif // __cplusplus diff --git a/src/Common/Src/Error_Handler/Common_Error_Handler.cpp b/src/Common/Src/Error_Handler/Common_Error_Handler.cpp deleted file mode 100644 index 1ea3719..0000000 --- a/src/Common/Src/Error_Handler/Common_Error_Handler.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/*! - Multiverse Engine Project 23/6/2014 Common Common_Error_Handler.cpp - - Copyright (C) 2014 Multiverse Engine Project - - This program is free software; - you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; - either version 2 of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; - without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along with this program; - if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - - Official source repository and project information can be found at - https://github.com/codebase7/mengine -*/ - -#include "Common_Error_Handler.h" // Main header. -#include "Common_Error_Handler_Internal.h" // Private internal header that defines the structure used for error logging. - -const unsigned int Common::Get_Error_Table_Size() -{ - return (sizeof(Common::commonErrorTable) / sizeof(Common_Error_Object)); -} - -const char * Common::Get_Error_Message(const int & errorCode) -{ - // Init vars. - const char * result = NULL; // Result of this function. - const size_t errorTableSize = Common::Get_Error_Table_Size(); // Size of the Common error lookup table. - - // Begin lookup loop. - for (size_t x = 0; ((x < errorTableSize) && (result == NULL)); x++) - { - // Check for the correct error code. - if (Common::commonErrorTable[x].errorCode == errorCode) - { - // Found the correct error code. - result = Common::commonErrorTable[x].error; - } - } - - // If we still can't find the error message to return, use COMMON_UNKNOWN_ERROR - if (result == NULL) - { - result = Common::UNKNOWN_ERROR_MSG; - } - - // Return the result. - return result; -} - -void Common::Set_Error_Log_Level(const unsigned int & logLevel) -{ - // Set the log level. - commonErrorLoggingData.errorLogLevel = logLevel; - - // Exit function. - return; -} - -unsigned int Common::Get_Error_Log_Level() -{ - // Return the current log level. - return commonErrorLoggingData.errorLogLevel; -} - -void Common::Register_Error_Log_Callback(void (*loggingFunction)(const unsigned int logLevel, const char * errorMsg)) -{ - // Check and see if the pointer is NULL. - if (loggingFunction == NULL) - { - // Set the log level to ERROR_DISABLE. - commonErrorLoggingData.errorLogLevel = ERROR_DISABLE; - } - - // Set the pointer. - commonErrorLoggingData.loggingFunct = loggingFunction; - - // Exit function. - return; -} - -void COMMON_LOG_ERROR(const unsigned int loggingLevel, const char * errorMsg) -{ - /* - * Only do something if the log is enabled, - * the error is at or below our current log level, - * and the logging callback function is defined. - * - * Note: The lower the log level, the higher severity of the error. - */ - if ((commonErrorLoggingData.errorLogLevel != ERROR_DISABLE) && - (commonErrorLoggingData.loggingFunct != NULL) && - (loggingLevel <= commonErrorLoggingData.errorLogLevel)) - { - // Call the callback. (Hope it returns....) - commonErrorLoggingData.loggingFunct(loggingLevel, errorMsg); - } - - // Exit function. - return; -} - -void COMMON_LOG_CRITICAL(const char * errorMsg) -{ - COMMON_LOG_ERROR(ERROR_CRITICAL, errorMsg); - return; -} - -void COMMON_LOG_WARNING(const char * errorMsg) -{ - COMMON_LOG_ERROR(ERROR_WARNING, errorMsg); - return; -} - -void COMMON_LOG_INFO(const char * errorMsg) -{ - COMMON_LOG_ERROR(ERROR_INFO, errorMsg); - return; -} - -void COMMON_LOG_DEBUG(const char * errorMsg) -{ - COMMON_LOG_ERROR(ERROR_DEBUG, errorMsg); - return; -} - -void COMMON_LOG_VERBOSE(const char * errorMsg) -{ - COMMON_LOG_ERROR(ERROR_VERBOSE, errorMsg); - return; -} diff --git a/src/Common/Src/Error_Handler/Common_Error_Handler.h b/src/Common/Src/Error_Handler/Common_Error_Handler.h index 9292da7..cefe259 100644 --- a/src/Common/Src/Error_Handler/Common_Error_Handler.h +++ b/src/Common/Src/Error_Handler/Common_Error_Handler.h @@ -24,6 +24,9 @@ // External includes. #include // Defines NULL. +#ifndef __cplusplus +#include // Defines bool data type. (For C compilers.) +#endif // __cplusplus // Project includes. #ifdef __win32 // Needed for different path seperator in Windows. @@ -35,30 +38,137 @@ #include "Common_Error_Handler_Structures.h" // Defines the error codes, error lookup table error lookup table version number, and Common::commonLastErrorCode. #include "Posix_Error_Translation_Table.h" // Defines the POSIX errno to Common namespace error translation table and functions. +// Enable C linkage if needed. +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +// Define the C bindings for the error handler. + +/*! + * void Common_Set_Error_Log_Level(const unsigned int & logLevel) + * + * Sets the error logging level for the Common namespace functions. + * + * By default it sets the error logging level to ERROR_DISABLE. + * (Disables all logging. See Core/Src/Panic.h for a list of + * valid logging levels.) + */ +void Common_Set_Error_Log_Level(const unsigned int logLevel); + +/*! + * unsigned int Common_Get_Error_Log_Level() + * + * Returns the current error logging level for the Common namespace functions. + * + * See Core/Src/Panic.h for a list of valid logging levels. + */ +unsigned int Common_Get_Error_Log_Level(); + +/*! + * void Common_Register_Error_Log_Callback(void (*loggingFunction)(const unsigned int logLevel, const char * errorMsg)) + * + * WARNING: The callback function MUST return control back to + * the caller, as the caller will be blocked until the callback + * function returns. + * + * Sets the logging function to call when an error is generated + * by a Common namespace function. + * + * For example, this function can be used to send the generated + * errors to the console in a multi-threaded enviroment. + * + * Passing a NULL pointer to this function (the default) will + * disable calling another function when an error is generated. + * In addition the logging level will be reset to ERROR_DISABLE. + */ +void Common_Register_Error_Log_Callback(void (*loggingFunction)(const unsigned int logLevel, const char * errorMsg)); +#ifdef MSYS_BUILD_FATAL_ERROR_SUPPORT + +/*! + * typedef void(*Common_pErrorCallBackFunction)(void) + * + * Defines the function pointer type for use as arguments by the Common fatal error handler functions. + */ +typedef void(*Common_pErrorCallBackFunction)(void); + +/*! + * bool Common_Register_Fatal_Error_Callback(const Common_pErrorCallBackFunction fatalErrorNotifyFunction) + * + * WARNING: The callback function MUST return control back to + * the caller, as the caller will be blocked until the callback + * function returns. This means any other registered callbacks + * will not be triggered, which may lead to data loss. The only exception + * to this is if the host system will kill the engine's process after a set + * amount of time. In this one specific instance, nothing can be done to + * prevent the host system from killing the engine. As such the function + * registered with this call should attempt to clean up and return as + * fast as possible. + * + * Registers a callback function for notification of an engine subsystem + * triggering the host system to kill the engine's process. + * + * Note: The registered callback is not guaranteed to be called at all prior to + * the engine process being terminated. (The host system reserves the right to + * kill the engine without warning it of the impending termination.) + * As such this should be considered an informal notification and not something + * to be relied on if data preservation / security or proper cleanup is required + * prior to engine shutdown. + * + * Returns true if the regisration completed successfully. + * Returns false otherwise. + */ +bool Common_Register_Fatal_Error_Callback(const Common_pErrorCallBackFunction fatalErrorNotifyFunction); + +/*! + * bool Common_Unregister_Fatal_Error_Callback(const Common_pErrorCallBackFunction fatalErrorNotifyFunction) + * + * Unregisters the given fatal error callback function from the list of fatal error + * callback functions to be triggered in the event of a fatal error being generated. + * + * (I.e. If unregistered, a given callback function will not be called if a fatal + * error occurs.) + * + * The callback function pointer given to this function must match a function pointer + * given to Common_Register_Fatal_Error_Callback() (Or Common::Register_Fatal_Error_Callback()) previously, + * otherwise this function will fail. + * + * Returns true if the unregisration completed successfully. + * Returns false otherwise. + */ +bool Common_Unregister_Fatal_Error_Callback(const Common_pErrorCallBackFunction fatalErrorNotifyFunction); + +/*! + * void Common_Fatal_Error_Notify() + * + * This function triggers the registered fatal error callback functions + * registered with Common_Register_Fatal_Error_Callback() (Or Common::Register_Fatal_Error_Callback()), + * in an attempt to notify all need to know sections of the engine, and application, that the engine is + * about to be terminated. + * + * When this function returns to it's caller, it is expected that the caller + * will terminate the engine's process if it does not happen automaticly. + * + * This function does not return any data to it's caller. + */ +void Common_Fatal_Error_Notify(); +#endif // MSYS_BUILD_FATAL_ERROR_SUPPORT + +// End C Linkage if needed. +#ifdef __cplusplus +} +#endif // __cplusplus + +// Define C++ Bindings. +#ifdef __cplusplus // Define namespaces. namespace Common { - /*! - * const unsigned int Common::Get_Error_Table_Size() - * - * Returns the size of the common error table. - */ - const unsigned int Get_Error_Table_Size(); - - /*! - * const char * Common::Get_Error_Message(const int & errorCode) - * - * This function takes the given error code and returns a pointer to a human - * readable string describing the meaning of the given error code. - * - * Returns a valid pointer if the given error code is in the common error table. - * Returns the message for Common::COMMON_UNKNOWN_ERROR otherwise. - */ - const char * Get_Error_Message(const int & errorCode); - /*! * void Common::Set_Error_Log_Level(const unsigned int & logLevel) - * + * + * (C++ Binding) + * * Sets the error logging level for the Common namespace functions. * * By default it sets the error logging level to ERROR_DISABLE. @@ -69,7 +179,9 @@ namespace Common /*! * unsigned int Common::Get_Error_Log_Level() - * + * + * (C++ Binding) + * * Returns the current error logging level for the Common namespace functions. * * See Core/Src/Panic.h for a list of valid logging levels. @@ -78,7 +190,9 @@ namespace Common /*! * void Common::Register_Error_Log_Callback(void (*loggingFunction)(const unsigned int logLevel, const char * errorMsg)) - * + * + * (C++ Binding) + * * WARNING: The callback function MUST return control back to * the caller, as the caller will be blocked until the callback * function returns. @@ -94,7 +208,77 @@ namespace Common * In addition the logging level will be reset to ERROR_DISABLE. */ void Register_Error_Log_Callback(void (*loggingFunction)(const unsigned int logLevel, const char * errorMsg) = NULL); + +#ifdef MSYS_BUILD_FATAL_ERROR_SUPPORT + /*! + * bool Common::Register_Fatal_Error_Callback(const Common_pErrorCallBackFunction fatalErrorNotifyFunction) + * + * (C++ Binding) + * + * WARNING: The callback function MUST return control back to + * the caller, as the caller will be blocked until the callback + * function returns. This means any other registered callbacks + * will not be triggered, which may lead to data loss. The only exception + * to this is if the host system will kill the engine's process after a set + * amount of time. In this one specific instance, nothing can be done to + * prevent the host system from killing the engine. As such the function + * registered with this call should attempt to clean up and return as + * fast as possible. + * + * Registers a callback function for notification of an engine subsystem + * triggering the host system to kill the engine's process. + * + * Note: The registered callback is not guaranteed to be called at all prior to + * the engine process being terminated. (The host system reserves the right to + * kill the engine without warning it of the impending termination.) + * As such this should be considered an informal notification and not something + * to be relied on if data preservation / security or proper cleanup is required + * prior to engine shutdown. + * + * Returns true if the regisration completed successfully. + * Returns false otherwise. + */ + bool Register_Fatal_Error_Callback(const Common_pErrorCallBackFunction fatalErrorNotifyFunction); + + /*! + * bool Common::Unregister_Fatal_Error_Callback(const Common_pErrorCallBackFunction fatalErrorNotifyFunction) + * + * (C++ Binding) + * + * Unregisters the given fatal error callback function from the list of fatal error + * callback functions to be triggered in the event of a fatal error being generated. + * + * (I.e. If unregistered, a given callback function will not be called if a fatal + * error occurs.) + * + * The callback function pointer given to this function must match a function pointer + * given to Common_Register_Fatal_Error_Callback() (Or Common::Register_Fatal_Error_Callback()) previously, + * otherwise this function will fail. + * + * Returns true if the unregisration completed successfully. + * Returns false otherwise. + */ + bool Unregister_Fatal_Error_Callback(const Common_pErrorCallBackFunction fatalErrorNotifyFunction); + + /*! + * void Common::Fatal_Error_Notify() + * + * (C++ Binding) + * + * This function triggers the registered fatal error callback functions + * registered with Or Common_Register_Fatal_Error_Callback() (Common::Register_Fatal_Error_Callback()), + * in an attempt to notify all need to know sections of the engine, and application, that the engine is + * about to be terminated. + * + * When this function returns to it's caller, it is expected that the caller + * will terminate the engine's process if it does not happen automaticly. + * + * This function does not return any data to it's caller. + */ + void Fatal_Error_Notify(); +#endif // MSYS_BUILD_FATAL_ERROR_SUPPORT }; +#endif // __cplusplus #endif // COMMON_ERROR_HANDLER_H diff --git a/src/Common/Src/Error_Handler/Common_Error_Handler_CPP_Bindings.cpp b/src/Common/Src/Error_Handler/Common_Error_Handler_CPP_Bindings.cpp new file mode 100644 index 0000000..f880379 --- /dev/null +++ b/src/Common/Src/Error_Handler/Common_Error_Handler_CPP_Bindings.cpp @@ -0,0 +1,118 @@ +/*! + Multiverse Engine Project 13/5/2015 Common Common_Error_Handler_CPP_Bindings.cpp + + Copyright (C) 2014 Multiverse Engine Project + + This program is free software; + you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; + either version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with this program; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Official source repository and project information can be found at + https://github.com/codebase7/mengine +*/ + +// Define C++ Bindings. +#ifdef __cplusplus + +// Internal includes. +#include "Common_Error_Handler.h" // Main header. +#include "Common_Error_Handler_Internal.h" // Private internal header that defines the structure used for error logging. + +// External includes. +#include + +void Common::Set_Error_Log_Level(const unsigned int & logLevel) +{ + Common_Set_Error_Log_Level(logLevel); +} + +unsigned int Common::Get_Error_Log_Level() +{ + return Common_Get_Error_Log_Level(); +} + +void Common::Register_Error_Log_Callback(void (*loggingFunction)(const unsigned int logLevel, const char * errorMsg)) +{ + Common_Register_Error_Log_Callback(loggingFunction); +} + +// Build the Fatal Error Handler if needed. +#ifdef MSYS_BUILD_FATAL_ERROR_SUPPORT + +bool Common::Register_Fatal_Error_Callback(const Common_pErrorCallBackFunction fatalErrorNotifyFunction) +{ + // Init vars. + bool ret = false; // The result of this function. + + // Begin try block. + try { + // Call real function. + ret = Common_Register_Fatal_Error_Callback(fatalErrorNotifyFunction); + } + catch (...) + { + /* + * Great probably a memory allocation error, if so, we don't do anything as the original list is not modifyed + * unless the operations succeed. + * + * If not, well we just destroyed the list of functions we needed to call when a fatal error happened, + * so maybe we should call terminate() here? + */ + ret = false; + } + + // Return the result. + return ret; +} + +bool Common::Unregister_Fatal_Error_Callback(const Common_pErrorCallBackFunction fatalErrorNotifyFunction) +{ + // Init vars. + bool ret = false; // The result of this function. + + // Begin try block. + try { + // Call real function. + ret = Common_Unregister_Fatal_Error_Callback(fatalErrorNotifyFunction); + } + catch (...) + { + /* + * Great probably a memory allocation error, if so, we don't do anything as the original list is not modifyed + * unless the operations succeed. + * + * If not, well we just destroyed the list of functions we needed to call when a fatal error happened, + * so maybe we should call terminate() here? + */ + ret = false; + } + + // Return the result. + return ret; +} + +void Common::Fatal_Error_Notify() +{ + // Begin try block. + try { + // Call real function. + Common_Fatal_Error_Notify(); + } + catch (std::exception & ex) + { + // Well, not much to do here, we are terminating anyway. + COMMON_LOG_CRITICAL("Common::Fatal_Error_Notify(): Exception occured while notifying engine subsystems / application that a fatal error occured."); + } +} + +#endif // MSYS_BUILD_FATAL_ERROR_SUPPORT + +#endif // __cplusplus + diff --git a/src/Common/Src/Error_Handler/Common_Error_Handler_Internal.h b/src/Common/Src/Error_Handler/Common_Error_Handler_Internal.h index 1847783..a1db502 100644 --- a/src/Common/Src/Error_Handler/Common_Error_Handler_Internal.h +++ b/src/Common/Src/Error_Handler/Common_Error_Handler_Internal.h @@ -34,6 +34,11 @@ //#error "This header is for internal engine use only. It should not be linked against, as it is NOT a part of the public API." //#endif // COMMON_ERROR_HANDLER_H +// Enable C linkage if needed. +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + /* * struct CommonErrorLogData * @@ -57,7 +62,7 @@ struct CommonErrorLogData { * * The actual data structure that contains the error * logging data for the Common namespace functions. - * + * * Once again, DO NOT USE THIS STRUCTURE DIRECTLY! * This structure is NOT a part of the public API, and * is subject to change at anytime. @@ -137,6 +142,11 @@ void COMMON_LOG_DEBUG(const char * errorMsg); */ void COMMON_LOG_VERBOSE(const char * errorMsg); +// End C Linkage if needed. +#ifdef __cplusplus +} +#endif // __cplusplus + #endif // COMMON_ERROR_HANDLER_INTERNAL_H // End of Common_Error_Handler_Internal.h diff --git a/src/Common/Src/Error_Handler/Common_Error_Handler_Structures.c b/src/Common/Src/Error_Handler/Common_Error_Handler_Structures.c new file mode 100644 index 0000000..0c682f4 --- /dev/null +++ b/src/Common/Src/Error_Handler/Common_Error_Handler_Structures.c @@ -0,0 +1,148 @@ +/*! + Multiverse Engine Project 16/5/2015 Common Common_Error_Handler_Structures.c + + Copyright (C) 2015 Multiverse Engine Project + + This program is free software; + you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; + either version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with this program; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Official source repository and project information can be found at + https://github.com/codebase7/mengine +*/ + +// Internal Includes. +#include "Common_Error_Handler.h" + +// Enable C linkage if needed. +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +/*! + * const char * COMMON_ERROR_UNKNOWN_ERROR_MSG + * + * This is the definition for the generic unknown error + * code. + * + * It is defined here to allow Common_Get_Error_Message() + * to reference it without needing to use another for loop + * to locate it's offset in the error message table. + */ +const char COMMON_ERROR_UNKNOWN_ERROR_MSG[] = COMMON_UNKNOWN_ERROR_MSG_DEF; + +/*! + * const Common_Error_Object Common_commonErrorTable[] + * + * This array contains all of the error codes that a Common namespace + * function may return, in addition to a human readable string describing + * the error code's meaning. (This is a work in progress, some error codes + * conflict with error codes in other functions, as such these are slowly + * being corrected.) + * + * The easiest way of fetching the human readable string from this table + * is to call Common::GetErrorMessage(). + * + * Do not reuse an existing error code. (If the error is generic then use a + * generic error code.) + * + * Do not change an existing error code. (This is to keep the API stable + * between revisions.) + */ +const Common_Error_Object Common_commonErrorTable[] = { + // Define the initilizers for the lookup table. + {COMMON_ERROR_UNKNOWN_ERROR, COMMON_ERROR_UNKNOWN_ERROR_MSG}, + {COMMON_ERROR_SYSTEM_SPECIFIC, "System specific error code. Check log."}, + {COMMON_ERROR_SUCCESS, "Success."}, + {COMMON_ERROR_INVALID_ARGUMENT, "Invalid argument."}, + {COMMON_ERROR_FUNCTION_NOT_IMPLEMENTED, "Function not implemented."}, + {COMMON_ERROR_ACCESS_DENIED, "Access Denied."}, + {COMMON_ERROR_EXCEPTION_THROWN, "Execption thrown."}, + {COMMON_ERROR_INTERNAL_ERROR, "Internal error."}, + {COMMON_ERROR_IO_ERROR, "Input / Output error. (Normally this is a hardware issue.)"}, + {COMMON_ERROR_RANGE_ERROR, "Given range is invalid."}, + {COMMON_ERROR_MEMORY_ERROR, "Memory error (Could not allocate memory / Control loop out of bounds.)"}, + {COMMON_ERROR_INVALID_LIBRARY_ID, "Given LibraryID does not match a supported library or a loaded plugin."}, + {COMMON_ERROR_PEBKAC_INVALID_OPERATION_ORDER, "Caller is attempting to do something that requires another call / process to be completed first. (Check the order in which you are doing things.)"}, + {COMMON_ERROR_CANNOT_GET_SYSTEM_TIME, "Could not get the system's current time."}, + {COMMON_ERROR_SUBSYSTEM_OBJECT_NOT_INITED, "Given object has not been inited yet."}, + {COMMON_ERROR_SUBSYSTEM_OBJECT_ALREADY_INITED, "Given object has already been inited."}, + // Rendering Subsystem error codes. + {RENDERER_ERROR_UNABLE_TO_ALLOC_OI_BUF, "Could not allocate memory for overlay image buffer."}, + {RENDERER_ERROR_UNABLE_TO_ALLOC_TD_BUF, "Could not allocate memory for transparency data buffer."}, + {RENDERER_ERROR_MEM_BUF_ALLOC_EXCEPTION, "Exception thrown while attempting to allocate memory buffer(s)."}, + {RENDERER_ERROR_DUPE_OVERLAY_EXCEPTION, "Exception thrown while duplicating overlay, clearing dest overlay."}, + {RENDERER_ERROR_INVAL_OVERLAY_SELF_OVERWRITE, "Given overlays are the same. Cannot overwrite an overlay with itself."}, + {RENDERER_ERROR_TRANSPARENCY_DISABLED, "Transparency is disabled on given overlay."}, + // Threading Subsystem (Thread_Utils) error codes. + {THREAD_UTILS_ERROR_EXCEPTION_THROWN, "Exception thrown in threading subsystem."}, + {THREAD_UTILS_ERROR_PLUGIN_LOAD_FAILURE, "Unable to load plugin(s). Internal error."}, + {THREAD_UTILS_ERROR_THREAD_COULD_NOT_START, "Could not start new thread."}, + {THREAD_UTILS_ERROR_THREAD_COULD_NOT_DETACH, "Could not detach thread."}, + {THREAD_UTILS_ERROR_THREAD_COULD_NOT_JOIN, "Could not join thread."}, + {THREAD_UTILS_ERROR_MUTEX_ALREADY_LOCKED, "The given mutex is already locked."}, + {THREAD_UTILS_ERROR_CONDITION_WAIT_TIMEOUT_REACHED, "Given timeout period was exceeded while waiting for the condition variable to signal."}, + {THREAD_UTILS_ERROR_CONDITION_CANNOT_LOCK_MUTEX, "Could not lock internal mutex in condition variable object."}, + // FileUtills. + {FILEUTILLS_ERROR_EXISTANT, "The path exists."}, + {FILEUTILLS_ERROR_NON_EXISTANT, "The path (or a component of the path) does not exist."}, + {FILEUTILLS_ERROR_READ_ONLY, "The path is read only."}, + {FILEUTILLS_ERROR_PATH_LENGTH_INVALID, "The path's length is beyond the filesystem's maximum length."}, + {FILEUTILLS_ERROR_PATH_FILE_AS_DIRECTORY, "The path has a file in it that is being treated as a directory."}, + {FILEUTILLS_ERROR_PATH_IS_A_FILE, "Given path is a file."}, + {FILEUTILLS_ERROR_PATH_IS_A_DIRECTORY, "Given path is a directory."}, + {FILEUTILLS_ERROR_FILESYSTEM_FULL, "Given filesystem is full."}, + {FILEUTILLS_ERROR_FILESYSTEM_QUOTA_REACHED, "User's disk usage quota for the given filesystem has been reached."}, + {FILEUTILLS_ERROR_EMPTY_DIRECTORY, "The given path is an empty directory."}, + {FILEUTILLS_ERROR_NON_EMPTY_DIRECTORY, "The given path is a non-empty directory."}, + // UI Subsystem/ + {UI_SUBSYSTEM_ERROR_EXCEPTION_THROWN, "An exception was thrown in the UI Subsystem."}, + // TODO: Need to add the error codes from all common namespace functions. +}; + +const unsigned int Common_Get_Error_Table_API_Version() +{ + return COMMON_ERROR_TABLE_ABI_VER; +} + +const unsigned int Common_Get_Error_Table_Size() +{ + return (sizeof(Common_commonErrorTable) / sizeof(Common_Error_Object)); +} + +const char * Common_Get_Error_Message(const int errorCode) +{ + // Init vars. + const char * result = COMMON_ERROR_UNKNOWN_ERROR_MSG; // Result of this function. + const size_t errorTableSize = Common_Get_Error_Table_Size(); // Size of the Common error lookup table. + + // Check for COMMON_UNKNOWN_ERROR. + if (errorCode != COMMON_ERROR_UNKNOWN_ERROR) + { + // Begin lookup loop. + for (size_t x = 0; ((x < errorTableSize) && (result == COMMON_ERROR_UNKNOWN_ERROR_MSG)); x++) + { + // Check for the correct error code. + if (Common_commonErrorTable[x].errorCode == errorCode) + { + // Found the correct error code. + result = Common_commonErrorTable[x].error; + } + } + } + + // Return the result. + return result; +} + +// End C Linkage if needed. +#ifdef __cplusplus +} +#endif // __cplusplus diff --git a/src/Common/Src/Error_Handler/Common_Error_Handler_Structures.h b/src/Common/Src/Error_Handler/Common_Error_Handler_Structures.h index 24fefa6..ad6bf04 100644 --- a/src/Common/Src/Error_Handler/Common_Error_Handler_Structures.h +++ b/src/Common/Src/Error_Handler/Common_Error_Handler_Structures.h @@ -1,128 +1,161 @@ +/*! + Multiverse Engine Project 04/7/2014 Common Common_Error_Handler_Structures.h + Copyright (C) 2014 Multiverse Engine Project + This program is free software; + you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; + either version 2 of the License, or (at your option) any later version. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with this program; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Official source repository and project information can be found at + https://github.com/codebase7/mengine +*/ + +// Include guard. +#ifndef COMMON_ERROR_ERROR_HANDLER_STRUCTURES_H +#define COMMON_ERROR_ERROR_HANDLER_STRUCTURES_H + +// Internal includes. #include "Error_Struct.h" // Structure used to create error lookup table. -namespace Common { - // Common namespace error code definitions. - // Error code table ABI version. - const unsigned int COMMON_ERROR_ABI_VER = 1; // Increment this if you alter the table. +// Define the Common unknown error message. (It has to be replicated in two places, once for the C code and once for the C++ bindings.) +#define COMMON_UNKNOWN_ERROR_MSG_DEF "Unknown error code." - /*! - * const static char * Common::UNKNOWN_ERROR_MSG - * - * This is the definition for the generic unknown error - * code. - * - * It is defined here to allow Common::Get_Error_Message() - * to reference it without needing to use another for loop - * to locate it's offset in the error message table. - */ - const static char * UNKNOWN_ERROR_MSG = "Unknown error code."; +// Error code table ABI version. +#define COMMON_ERROR_TABLE_ABI_VER 2 // Increment this if you alter the table. - // Error code table. +// Enable C linkage if needed. +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +// Common namespace error code definitions. (Human-readable error message translation table is located in Common_Error_Handler_Structures.c) enum { // Generic error codes. - COMMON_SYSTEM_SPECIFIC = 99, - COMMON_SUCCESS = 0, - COMMON_UNKNOWN_ERROR = -1, - COMMON_INVALID_ARGUMENT = -2, - COMMON_FUNCTION_NOT_IMPLEMENTED = -3, - COMMON_ACCESS_DENIED = -4, - COMMON_EXCEPTION_THROWN = -5, - COMMON_INTERNAL_ERROR = -6, - COMMON_IO_ERROR = -7, - COMMON_RANGE_ERROR = -8, - COMMON_MEMORY_ERROR = -9, + COMMON_ERROR_SYSTEM_SPECIFIC = 99, + COMMON_ERROR_SUCCESS = 0, + COMMON_ERROR_UNKNOWN_ERROR = -1, + COMMON_ERROR_INVALID_ARGUMENT = -2, + COMMON_ERROR_FUNCTION_NOT_IMPLEMENTED = -3, + COMMON_ERROR_ACCESS_DENIED = -4, + COMMON_ERROR_EXCEPTION_THROWN = -5, + COMMON_ERROR_INTERNAL_ERROR = -6, + COMMON_ERROR_IO_ERROR = -7, + COMMON_ERROR_RANGE_ERROR = -8, + COMMON_ERROR_MEMORY_ERROR = -9, + COMMON_ERROR_INVALID_LIBRARY_ID = -10, // Formerly THREAD_UTILS_INVALID_LIBRARY_ID. + COMMON_ERROR_PEBKAC_INVALID_OPERATION_ORDER = -11, + COMMON_ERROR_CANNOT_GET_SYSTEM_TIME = -12, + COMMON_ERROR_SUBSYSTEM_OBJECT_NOT_INITED = -13, + COMMON_ERROR_SUBSYSTEM_OBJECT_ALREADY_INITED = -14, + // Rendering Subsystem error codes. - RENDERER_UNABLE_TO_ALLOC_OI_BUF = -12, // Overlay image buffer. - RENDERER_UNABLE_TO_ALLOC_TD_BUF = -13, // Transparency data buffer. - RENDERER_MEM_BUF_ALLOC_EXCEPTION = -14, - RENDERER_DUPE_OVERLAY_EXCEPTION = -15, - RENDERER_INVAL_OVERLAY_SELF_OVERWRITE = -16, - RENDERER_TRANSPARENCY_DISABLED = -17, + RENDERER_ERROR_UNABLE_TO_ALLOC_OI_BUF = -21, // Overlay image buffer. + RENDERER_ERROR_UNABLE_TO_ALLOC_TD_BUF = -22, // Transparency data buffer. + RENDERER_ERROR_MEM_BUF_ALLOC_EXCEPTION = -23, + RENDERER_ERROR_DUPE_OVERLAY_EXCEPTION = -24, + RENDERER_ERROR_INVAL_OVERLAY_SELF_OVERWRITE = -25, + RENDERER_ERROR_TRANSPARENCY_DISABLED = -26, // Threading Subsystem error codes. - THREAD_UTILS_EXCEPTION_THROWN = -31, - THREAD_UTILS_INVALID_LIBRARY_ID = -32, - THREAD_UTILS_PLUGIN_LOAD_FAILURE = -33, + THREAD_UTILS_ERROR_EXCEPTION_THROWN = -31, + THREAD_UTILS_ERROR_PLUGIN_LOAD_FAILURE = -32, + THREAD_UTILS_ERROR_THREAD_COULD_NOT_START = -33, + THREAD_UTILS_ERROR_THREAD_COULD_NOT_DETACH = -34, + THREAD_UTILS_ERROR_THREAD_COULD_NOT_JOIN = -35, + THREAD_UTILS_ERROR_MUTEX_ALREADY_LOCKED = -36, + THREAD_UTILS_ERROR_CONDITION_CANNOT_LOCK_MUTEX = -37, + THREAD_UTILS_ERROR_CONDITION_WAIT_TIMEOUT_REACHED = -38, // FileUtills error codes. - FILEUTILLS_EXISTANT = -60, - FILEUTILLS_NON_EXISTANT = -61, - FILEUTILLS_READ_ONLY = -62, - FILEUTILLS_PATH_LENGTH_INVALID = -63, - FILEUTILLS_PATH_FILE_AS_DIRECTORY = -64, - FILEUTILLS_PATH_IS_A_FILE = -65, - FILEUTILLS_PATH_IS_A_DIRECTORY = -66, - FILEUTILLS_FILESYSTEM_FULL = -67, - FILEUTILLS_FILESYSTEM_QUOTA_REACHED = -68, - FILEUTILLS_EMPTY_DIRECTORY = -69, - FILEUTILLS_NON_EMPTY_DIRECTORY = -70, + FILEUTILLS_ERROR_EXISTANT = -60, + FILEUTILLS_ERROR_NON_EXISTANT = -61, + FILEUTILLS_ERROR_READ_ONLY = -62, + FILEUTILLS_ERROR_PATH_LENGTH_INVALID = -63, + FILEUTILLS_ERROR_PATH_FILE_AS_DIRECTORY = -64, + FILEUTILLS_ERROR_PATH_IS_A_FILE = -65, + FILEUTILLS_ERROR_PATH_IS_A_DIRECTORY = -66, + FILEUTILLS_ERROR_FILESYSTEM_FULL = -67, + FILEUTILLS_ERROR_FILESYSTEM_QUOTA_REACHED = -68, + FILEUTILLS_ERROR_EMPTY_DIRECTORY = -69, + FILEUTILLS_ERROR_NON_EMPTY_DIRECTORY = -70, + // UI Subsystem. + UI_SUBSYSTEM_ERROR_EXCEPTION_THROWN = -90, }; +/*! + * const unsigned int Common_Get_Error_Table_API_Version() + * + * Returns the API version number of the common error table. + */ +const unsigned int Common_Get_Error_Table_API_Version(); + +/*! + * const unsigned int Common_Get_Error_Table_Size() + * + * Returns the size of the common error table. + */ +const unsigned int Common_Get_Error_Table_Size(); + +/*! + * const char * Common_Get_Error_Message(const int & errorCode) + * + * This function takes the given error code and returns a pointer to a human + * readable string describing the meaning of the given error code. + * + * Returns a valid pointer if the given error code is in the common error table. + * Returns the message for Common_COMMON_UNKNOWN_ERROR otherwise. + */ +const char * Common_Get_Error_Message(const int errorCode); + +// Define C++ Bindings. +#ifdef __cplusplus +// Define namespaces. +namespace Common +{ /*! - * const Common_Error_Object Common::commonErrorTable[] - * - * This array contains all of the error codes that a Common namespace - * function may return, in addition to a human readable string describing - * the error code's meaning. (This is a work in progress, some error codes - * conflict with error codes in other functions, as such these are slowly - * being corrected.) - * - * The easiest way of fetching the human readable string from this table - * is to call Common::GetErrorMessage(). - * - * Do not reuse an existing error code. (If the error is generic then use a - * generic error code.) - * - * Do not change an existing error code. (This is to keep the API stable - * between revisions.) + * const unsigned int Common::Get_Error_Table_API_Version() + * + * (C++ Binding) + * + * Returns the API version number of the common error table. */ - const Common_Error_Object commonErrorTable[] = { - {COMMON_UNKNOWN_ERROR, UNKNOWN_ERROR_MSG}, - {COMMON_SYSTEM_SPECIFIC, "System specific error code. Check log."}, - {COMMON_SUCCESS, "Success."}, - {COMMON_INVALID_ARGUMENT, "Invalid argument."}, - {COMMON_FUNCTION_NOT_IMPLEMENTED, "Function not implemented."}, - {COMMON_ACCESS_DENIED, "Access Denied."}, - {COMMON_EXCEPTION_THROWN, "Execption thrown."}, - {COMMON_INTERNAL_ERROR, "Internal error."}, - {COMMON_IO_ERROR, "Input / Output error. (Normally this is a hardware issue.)"}, - {COMMON_RANGE_ERROR, "Given range is invalid."}, - {COMMON_MEMORY_ERROR, "Memory error (Could not allocate memory / Control loop out of bounds.)"}, - // Rendering Subsystem error codes. - {RENDERER_UNABLE_TO_ALLOC_OI_BUF, "Could not allocate memory for overlay image buffer."}, - {RENDERER_UNABLE_TO_ALLOC_TD_BUF, "Could not allocate memory for transparency data buffer."}, - {RENDERER_MEM_BUF_ALLOC_EXCEPTION, "Exception thrown while attempting to allocate memory buffer(s)."}, - {RENDERER_DUPE_OVERLAY_EXCEPTION, "Exception thrown while duplicating overlay, clearing dest overlay."}, - {RENDERER_INVAL_OVERLAY_SELF_OVERWRITE, "Given overlays are the same. Cannot overwrite an overlay with itself."}, - {RENDERER_TRANSPARENCY_DISABLED, "Transparency is disabled on given overlay."}, - // Threading Subsystem (Thread_Utils) error codes. - {THREAD_UTILS_EXCEPTION_THROWN, "Exception thrown in threading subsystem."}, - {THREAD_UTILS_INVALID_LIBRARY_ID, "Given LibraryID does not match a supported library or a loaded plugin."}, - {THREAD_UTILS_PLUGIN_LOAD_FAILURE, "Unable to load plugin(s). Internal error."}, - // FileUtills. - {FILEUTILLS_EXISTANT, "The path exists."}, - {FILEUTILLS_NON_EXISTANT, "The path (or a component of the path) does not exist."}, - {FILEUTILLS_READ_ONLY, "The path is read only."}, - {FILEUTILLS_PATH_LENGTH_INVALID, "The path's length is beyond the filesystem's maximum length."}, - {FILEUTILLS_PATH_FILE_AS_DIRECTORY, "The path has a file in it that is being treated as a directory."}, - {FILEUTILLS_PATH_IS_A_FILE, "Given path is a file."}, - {FILEUTILLS_PATH_IS_A_DIRECTORY, "Given path is a directory."}, - {FILEUTILLS_FILESYSTEM_FULL, "Given filesystem is full."}, - {FILEUTILLS_FILESYSTEM_QUOTA_REACHED, "User's disk usage quota for the given filesystem has been reached."}, - {FILEUTILLS_EMPTY_DIRECTORY, "The given path is an empty directory."}, - {FILEUTILLS_NON_EMPTY_DIRECTORY, "The given path is a non-empty directory."}, - // TODO: Need to add the error codes from all common namespace functions. - }; + const unsigned int Get_Error_Table_API_Version(); + + /*! + * const unsigned int Common::Get_Error_Table_Size() + * + * (C++ Binding) + * + * Returns the size of the common error table. + */ + const unsigned int Get_Error_Table_Size(); /*! - * static int Common::commonLastErrorCode + * const char * Common::Get_Error_Message(const int & errorCode) + * + * (C++ Binding) * - * Contains the last error code encountered by a Common namespace function. + * This function takes the given error code and returns a pointer to a human + * readable string describing the meaning of the given error code. * - * Note: Calling Common::GetErrorMessage() or Common::GetErrorTableSize() - * will NOT clear this variable. - * Calling any other Common namespace function WILL clear this variable. + * Returns a valid pointer if the given error code is in the common error table. + * Returns the message for Common_COMMON_UNKNOWN_ERROR otherwise. */ - static int commonLastErrorCode = COMMON_SUCCESS; -}; + const char * Get_Error_Message(const int & errorCode); +} +#endif // __cplusplus + +// End C Linkage if needed. +#ifdef __cplusplus +} +#endif // __cplusplus + +#endif // COMMON_ERROR_ERROR_HANDLER_STRUCTURES_H + +// End of Common_Error_Handler_Structures.h diff --git a/src/Common/Src/Error_Handler/Common_Error_Handler_Structures_CPP_Bindings.cpp b/src/Common/Src/Error_Handler/Common_Error_Handler_Structures_CPP_Bindings.cpp new file mode 100644 index 0000000..0fd7237 --- /dev/null +++ b/src/Common/Src/Error_Handler/Common_Error_Handler_Structures_CPP_Bindings.cpp @@ -0,0 +1,41 @@ +/*! + Multiverse Engine Project 16/5/2015 Common Common_Error_Handler_Structures_CPP_Bindings.cpp + + Copyright (C) 2015 Multiverse Engine Project + + This program is free software; + you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; + either version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with this program; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Official source repository and project information can be found at + https://github.com/codebase7/mengine +*/ + +// Internal Includes. +#include "Common_Error_Handler.h" + +// Define C++ Bindings. +#ifdef __cplusplus + +const unsigned int Common::Get_Error_Table_API_Version() +{ + return Common_Get_Error_Table_API_Version(); +} + +const unsigned int Common::Get_Error_Table_Size() +{ + return Common_Get_Error_Table_Size(); +} + +const char * Common::Get_Error_Message(const int & errorCode) +{ + return Common_Get_Error_Message(errorCode); +} +#endif // __cplusplus diff --git a/src/Common/Src/Error_Handler/Error_Struct.h b/src/Common/Src/Error_Handler/Error_Struct.h index d551a0a..e08b1b0 100644 --- a/src/Common/Src/Error_Handler/Error_Struct.h +++ b/src/Common/Src/Error_Handler/Error_Struct.h @@ -18,14 +18,31 @@ https://github.com/codebase7/mengine */ +// Include guard. #ifndef ERROR_STRUCT_H #define ERROR_STRUCT_H -struct Common_Error_Object { +// Enable C linkage if needed. +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +/*! + * typedef struct Common_Error_Object_T + * + * Defines the data structure used by the Common Error Handler functions + * to look up human-readable strings and their assoicated error codes. + */ +typedef struct Common_Error_Object_T { short errorCode; const char * error; -}; +} Common_Error_Object; + +// End C Linkage if needed. +#ifdef __cplusplus +} +#endif // __cplusplus -#endif +#endif // ERROR_STRUCT_H -// End of Error_Struct.h \ No newline at end of file +// End of Error_Struct.h diff --git a/src/Common/Src/Error_Handler/Posix_Error_Translation_Table.cpp b/src/Common/Src/Error_Handler/Posix_Error_Translation_Table.c similarity index 60% rename from src/Common/Src/Error_Handler/Posix_Error_Translation_Table.cpp rename to src/Common/Src/Error_Handler/Posix_Error_Translation_Table.c index edfe8c1..e33fe41 100644 --- a/src/Common/Src/Error_Handler/Posix_Error_Translation_Table.cpp +++ b/src/Common/Src/Error_Handler/Posix_Error_Translation_Table.c @@ -1,5 +1,5 @@ /*! - Multiverse Engine Project 05/7/2014 Common Posix_Error_Translation_Table.cpp + Multiverse Engine Project 05/7/2014 Common Posix_Error_Translation_Table.c Copyright (C) 2014 Multiverse Engine Project @@ -22,33 +22,48 @@ #include "Common_Error_Handler_Structures.h" #include "Posix_Error_Translation_Table.h" -unsigned int Common::Get_Posix_Error_Translation_Table_Size() +// Include Extern C if needed. +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +const unsigned int Common_Get_Posix_Error_Translation_Table_API_Version() +{ + return COMMON_POSIX_ERROR_TRANSLATION_TABLE_API_VERSION; +} + +const unsigned int Common_Get_Posix_Error_Translation_Table_Size() { - return (sizeof(Common::posixErrorTranslationTable) / sizeof(Common::posixErrnoTableEntry)); + return (sizeof(Common_posixErrorTranslationTable) / sizeof(Common_posixErrnoTableEntry)); } -int Common::Translate_Posix_Errno_To_Common_Error_Code(const int & err) +int Common_Translate_Posix_Errno_To_Common_Error_Code(const int err) { // Init result. - int ret = COMMON_SYSTEM_SPECIFIC; /* - * Default is COMMON_SYSTEM_SPECIFIC, as + int ret = COMMON_ERROR_SYSTEM_SPECIFIC; /* + * Default is COMMON_ERROR_SYSTEM_SPECIFIC, as * not all POSIX errno(s) are / can be * reperesented in the Common namespace * error code table. */ // Run loop. - for (unsigned int x = 0; ((x < Common::Get_Posix_Error_Translation_Table_Size()) && - (ret == COMMON_SYSTEM_SPECIFIC)); x++) + for (unsigned int x = 0; ((x < Common_Get_Posix_Error_Translation_Table_Size()) && + (ret == COMMON_ERROR_SYSTEM_SPECIFIC)); x++) { // Check for a match in the error code translation table. - if (Common::posixErrorTranslationTable[x].posixErrorNo == err) + if (Common_posixErrorTranslationTable[x].posixErrorNo == err) { // Match found, return the Common namespace error code. - ret = Common::posixErrorTranslationTable[x].commonErrorCode; + ret = Common_posixErrorTranslationTable[x].commonErrorCode; } } // Return the result. return ret; -} \ No newline at end of file +} + +// Terminate C linkage if needed. +#ifdef __cplusplus +} +#endif // __cplusplus diff --git a/src/Common/Src/Error_Handler/Posix_Error_Translation_Table.h b/src/Common/Src/Error_Handler/Posix_Error_Translation_Table.h index 25d3ab6..671704b 100644 --- a/src/Common/Src/Error_Handler/Posix_Error_Translation_Table.h +++ b/src/Common/Src/Error_Handler/Posix_Error_Translation_Table.h @@ -25,66 +25,143 @@ // Include POSIX errno header. #include +// Enable C linkage if needed. +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +/*! + * #define COMMON_POSIX_ERROR_TRANSLATION_TABLE_API_VERSION + * + * Defines the API version of the posix errno to + * common namespace error code translation table. + * + * If you change the table increment the version + * number. + */ +#define COMMON_POSIX_ERROR_TRANSLATION_TABLE_API_VERSION 1 + +/*! + * typedef struct Common_posixErrnoTableEntry_T Common_posixErrnoTableEntry + * + * (C Binding.) + * + * Used to construct the errno translation table. + * for POSIX compliant systems. + */ +typedef struct Common_posixErrnoTableEntry_T { + int posixErrorNo; + int commonErrorCode; +} Common_posixErrnoTableEntry; + +/*! + * const static Common_posixErrnoTableEntry Common_posixErrorTranslationTable[] + * + * A table that contains (some) POSIX errno codes and their + * Common namespace error code equilivants. + * + * Not all POSIX errno(s) are present in this table, as + * some POSIX errno(s) are specific to POSIX compliant + * systems. As such there is no Common namespace error + * code (beyond COMMON_SYSTEM_SPECIFIC) to represent them. + */ +const static Common_posixErrnoTableEntry Common_posixErrorTranslationTable[] = { + {EACCES, COMMON_ERROR_ACCESS_DENIED}, + {ENOENT, FILEUTILLS_ERROR_NON_EXISTANT}, + {EEXIST, FILEUTILLS_ERROR_EXISTANT}, + {EROFS, FILEUTILLS_ERROR_READ_ONLY}, + {EINVAL, COMMON_ERROR_INVALID_ARGUMENT}, + {ENAMETOOLONG, FILEUTILLS_ERROR_PATH_LENGTH_INVALID}, + {ENOTDIR, FILEUTILLS_ERROR_PATH_FILE_AS_DIRECTORY}, + {ENOMEM, COMMON_ERROR_MEMORY_ERROR}, + {EFAULT, COMMON_ERROR_INVALID_ARGUMENT}, + {ENOSPC, FILEUTILLS_ERROR_FILESYSTEM_FULL}, + {EDQUOT, FILEUTILLS_ERROR_FILESYSTEM_QUOTA_REACHED}, + {ENOTEMPTY, FILEUTILLS_ERROR_NON_EMPTY_DIRECTORY}, +}; + +/*! + * const unsigned int Common_Get_Posix_Error_Translation_Table_API_Version() + * + * Returns the API Version number of the Common_posixErrorTranslationTable + * array. + */ +const unsigned int Common_Get_Posix_Error_Translation_Table_API_Version(); + +/*! + * const unsigned int Common_Get_Posix_Error_Translation_Table_Size() + * + * Returns the size of the Common::posixErrorTranslationTable + * array. + */ +const unsigned int Common_Get_Posix_Error_Translation_Table_Size(); + +/*! + * int Common_Translate_Posix_Errno_To_Common_Error_Code(const int err) + * + * Translates the given POSIX errno to it's Common namespace + * error code equilivant. (If applicable.) + * + * WARNING: Because the POSIX errno variable is global, you + * should never call this function passing errno directly. + * + * Instead, copy the errno to a temporary variable and then + * pass the temporary variable to this function. + * + * (Otherwise just calling this function could change errno + * before it is checked.) + * + * @pram const int err, the POSIX error code to translate. + * + * Returns the translated Common namespace error code if applicable. + * + * Returns COMMON_SYSTEM_SPECIFIC if the POSIX errno does not have a + * Common namespace error code translation. + */ +int Common_Translate_Posix_Errno_To_Common_Error_Code(const int err); + +// End C Linkage if needed. +#ifdef __cplusplus +} +#endif // __cplusplus + +// Check for a C compiler. +#ifdef __cplusplus namespace Common { /*! - * struct Common::posixErrnoTableEntry - * + * typedef Common_posixErrnoTableEntry Common::posixErrnoTableEntry + * + * (C++ Binding.) + * * Used to construct the errno translation table. * for POSIX compliant systems. */ - struct posixErrnoTableEntry{ - int posixErrorNo; - int commonErrorCode; - }; + typedef Common_posixErrnoTableEntry posixErrnoTableEntry; /*! - * const int posixErrorTranslationTableAPIVersion - * - * Defines the API version of the posix errno to - * common namespace error code translation table. - * - * If you change the table increment the version - * number. - */ - const int posixErrorTranslationTableAPIVersion = 1; - - /*! - * const Common::posixErrnoTableEntry Common::posixErrorTranslationTable[] - * - * A table that contains (some) POSIX errno codes and their - * Common namespace error code equilivants. + * const unsigned int Common::Get_Posix_Error_Translation_Table_API_Version() * - * Not all POSIX errno(s) are present in this table, as - * some POSIX errno(s) are specific to POSIX compliant - * systems. As such there is no Common namespace error - * code (beyond COMMON_SYSTEM_SPECIFIC) to represent them. + * Returns the API Version number of the Common_posixErrorTranslationTable + * array. */ - const posixErrnoTableEntry posixErrorTranslationTable[] = { - {EACCES, Common::COMMON_ACCESS_DENIED}, - {ENOENT, Common::FILEUTILLS_NON_EXISTANT}, - {EEXIST, Common::FILEUTILLS_EXISTANT}, - {EROFS, Common::FILEUTILLS_READ_ONLY}, - {EINVAL, Common::COMMON_INVALID_ARGUMENT}, - {ENAMETOOLONG, Common::FILEUTILLS_PATH_LENGTH_INVALID}, - {ENOTDIR, Common::FILEUTILLS_PATH_FILE_AS_DIRECTORY}, - {ENOMEM, Common::COMMON_MEMORY_ERROR}, - {EFAULT, Common::COMMON_INVALID_ARGUMENT}, - {ENOSPC, Common::FILEUTILLS_FILESYSTEM_FULL}, - {EDQUOT, Common::FILEUTILLS_FILESYSTEM_QUOTA_REACHED}, - }; + const unsigned int Get_Posix_Error_Translation_Table_API_Version(); /*! - * unsigned int Common::Get_Posix_Error_Translation_Table_Size() - * + * const unsigned int Common::Get_Posix_Error_Translation_Table_Size() + * + * (C++ Binding) + * * Returns the size of the Common::posixErrorTranslationTable * array. */ - unsigned int Get_Posix_Error_Translation_Table_Size(); + const unsigned int Get_Posix_Error_Translation_Table_Size(); /*! * int Common::Translate_Posix_Errno_To_Common_Error_Code(const int & err) - * + * + * (C++ Binding) + * * Translates the given POSIX errno to it's Common namespace * error code equilivant. (If applicable.) * @@ -106,6 +183,7 @@ namespace Common */ int Translate_Posix_Errno_To_Common_Error_Code(const int & err); }; +#endif // __cplusplus #endif // POSIX_ERROR_TRANSLATION_TABLE_H diff --git a/src/Common/Src/Error_Handler/Posix_Error_Translation_Table_CPP_Bindings.cpp b/src/Common/Src/Error_Handler/Posix_Error_Translation_Table_CPP_Bindings.cpp new file mode 100644 index 0000000..96c7adb --- /dev/null +++ b/src/Common/Src/Error_Handler/Posix_Error_Translation_Table_CPP_Bindings.cpp @@ -0,0 +1,45 @@ +/*! + Multiverse Engine Project 15/5/2015 Common Posix_Error_Translation_Table_CPP_Bindings.cpp + + Copyright (C) 2015 Multiverse Engine Project + + This program is free software; + you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; + either version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with this program; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Official source repository and project information can be found at + https://github.com/codebase7/mengine +*/ + +// C++ Bindings. +#ifdef __cplusplus + +// Internal includes. +#include "Common_Error_Handler_Structures.h" +#include "Posix_Error_Translation_Table.h" + +// External includes. +#include + +const unsigned int Common::Get_Posix_Error_Translation_Table_API_Version() +{ + return Common_Get_Posix_Error_Translation_Table_API_Version(); +} + +const unsigned int Common::Get_Posix_Error_Translation_Table_Size() +{ + return Common_Get_Posix_Error_Translation_Table_Size(); +} + +int Common::Translate_Posix_Errno_To_Common_Error_Code(const int & err) +{ + return Common_Translate_Posix_Errno_To_Common_Error_Code(err); +} +#endif // __cplusplus From 93199aa7662b72c19aa42a554de09fa34e82cd86 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Thu, 11 Jun 2015 14:18:37 -0400 Subject: [PATCH 02/50] Move fake stdbool.h This commit moves the fake stdbool.h from src/Common/Src/Mutexes to src/Common/Src. --- src/Common/Src/Mutexes/Windows/MSYS_Mutexes_MSVC.h | 2 +- src/Common/Src/{Mutexes => }/stdbool.h | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/Common/Src/{Mutexes => }/stdbool.h (100%) diff --git a/src/Common/Src/Mutexes/Windows/MSYS_Mutexes_MSVC.h b/src/Common/Src/Mutexes/Windows/MSYS_Mutexes_MSVC.h index 83d9af3..f1e625b 100644 --- a/src/Common/Src/Mutexes/Windows/MSYS_Mutexes_MSVC.h +++ b/src/Common/Src/Mutexes/Windows/MSYS_Mutexes_MSVC.h @@ -35,7 +35,7 @@ extern "C" { #endif /* __cplusplus */ /* Define stdbool. */ -#include "..\stdbool.h" +#include "..\..\stdbool.h" /* Define the functions. */ diff --git a/src/Common/Src/Mutexes/stdbool.h b/src/Common/Src/stdbool.h similarity index 100% rename from src/Common/Src/Mutexes/stdbool.h rename to src/Common/Src/stdbool.h From 23ba84692495683be3d86d8fb664034c56533b12 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Thu, 11 Jun 2015 15:19:39 -0400 Subject: [PATCH 03/50] MSVC fix for Error_Handler.c This commit is a compile fix for Common_Error_Handler.c under MSVC. Also changes the data type for registeredFatalErrorCallbackFunctionsSize from an unsigned int to a size_t. --- .../Src/Error_Handler/Common_Error_Handler.c | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Common/Src/Error_Handler/Common_Error_Handler.c b/src/Common/Src/Error_Handler/Common_Error_Handler.c index eb462fe..ca2aefe 100644 --- a/src/Common/Src/Error_Handler/Common_Error_Handler.c +++ b/src/Common/Src/Error_Handler/Common_Error_Handler.c @@ -84,11 +84,11 @@ void Common_Register_Error_Log_Callback(void (*loggingFunction)(const unsigned i // Build the Fatal Error Handler if needed. #ifdef MSYS_BUILD_FATAL_ERROR_SUPPORT /*! - * static unsigned int registeredFatalErrorCallbackFunctionsSize + * static size_t registeredFatalErrorCallbackFunctionsSize * - * This unsigned int is used to remember the allocated size of the registeredFatalErrorCallbackFunctions array. + * This size_t is used to remember the allocated size of the registeredFatalErrorCallbackFunctions array. */ -static unsigned int registeredFatalErrorCallbackFunctionsSize = 0; +static size_t registeredFatalErrorCallbackFunctionsSize = 0; /* * static Common_pErrorCallBackFunction * registeredFatalErrorCallbackFunctions @@ -113,6 +113,7 @@ bool Common_Register_Fatal_Error_Callback(const Common_pErrorCallBackFunction fa bool ret = false; // The result of this function. size_t previousErrorListSize = registeredFatalErrorCallbackFunctionsSize; // The size of the previous error list. size_t newErrorListSize = 0; // The size of the new error list we are creating. + size_t x = 0; /* Counter used in for loops. */ Common_pErrorCallBackFunction * previousErrorList = registeredFatalErrorCallbackFunctions; // The previous error list. Common_pErrorCallBackFunction * newErrorList = NULL; // The new error list we are creating. MSYS_Mutex * retFromLockMutex = NULL; // The result from the call to MSYS_Lock_Mutex(). @@ -142,7 +143,7 @@ bool Common_Register_Fatal_Error_Callback(const Common_pErrorCallBackFunction fa newErrorListSize = (previousErrorListSize + 1); // Copy the data. - for (size_t x = 0; x < previousErrorListSize; x++) + for (x = 0; x < previousErrorListSize; x++) { newErrorList[x] = previousErrorList[x]; } @@ -173,7 +174,7 @@ bool Common_Register_Fatal_Error_Callback(const Common_pErrorCallBackFunction fa if ((previousErrorList != NULL) && (previousErrorList != newErrorList) && (previousErrorListSize > 0)) { // Null out the old pointer list. - for (size_t x = 0; x < previousErrorListSize; x++) + for (x = 0; x < previousErrorListSize; x++) { previousErrorList[x] = NULL; } @@ -203,6 +204,8 @@ bool Common_Unregister_Fatal_Error_Callback(const Common_pErrorCallBackFunction bool ret = false; // The result of this function. size_t previousErrorListSize = registeredFatalErrorCallbackFunctionsSize; // The size of the previous error list. size_t newErrorListSize = 0; // The size of the new error list we are creating. + size_t x = 0; /* Counter used in top level for loops. */ + size_t y = 0; /* Counter used in sub level 1 for loops. */ Common_pErrorCallBackFunction * previousErrorList = registeredFatalErrorCallbackFunctions; // The previous error list. Common_pErrorCallBackFunction * newErrorList = NULL; // The new error list we are creating. MSYS_Mutex * retFromLockMutex = NULL; // The result from the call to MSYS_Lock_Mutex(). @@ -218,7 +221,7 @@ bool Common_Unregister_Fatal_Error_Callback(const Common_pErrorCallBackFunction if ((previousErrorList != NULL) && (previousErrorListSize > 0)) { // Check the existing list for that function. - for (size_t x = 0; ((newErrorList == NULL) && (x < previousErrorListSize)); x++) + for (x = 0; ((newErrorList == NULL) && (x < previousErrorListSize)); x++) { // Check for the correct function, to see if the function was registered previously. if (previousErrorList[x] == fatalErrorNotifyFunction) @@ -237,7 +240,7 @@ bool Common_Unregister_Fatal_Error_Callback(const Common_pErrorCallBackFunction if ((newErrorList != NULL) && (newErrorListSize == (previousErrorListSize - 1))) { // Copy the data. - for (size_t x = 0, y = 0; ((x < previousErrorListSize) && (y < newErrorListSize)); x++) + for (x = 0, y = 0; ((x < previousErrorListSize) && (y < newErrorListSize)); x++) { // Make sure we don't copy the pointer we are removing. if (previousErrorList[x] != fatalErrorNotifyFunction) @@ -256,7 +259,7 @@ bool Common_Unregister_Fatal_Error_Callback(const Common_pErrorCallBackFunction if ((previousErrorList != NULL) && (previousErrorList != newErrorList) && (previousErrorListSize > 0)) { // Null out the old pointer list. - for (size_t x = 0; x < previousErrorListSize; x++) + for (x = 0; x < previousErrorListSize; x++) { previousErrorList[x] = NULL; } @@ -284,6 +287,7 @@ void Common_Fatal_Error_Notify() { // Init vars. MSYS_Mutex * retFromLockMutex = NULL; // The result from the call to MSYS_Lock_Mutex(). + size_t x = 0; /* Counter used in loops. */ // Lock the error handler mutex. retFromLockMutex = MSYS_Lock_Mutex(fatalErrorHandlerMutex); @@ -293,7 +297,7 @@ void Common_Fatal_Error_Notify() if (registeredFatalErrorCallbackFunctionsSize > 0) { // Begin vector iteration loop. - for (size_t x = 0; (x < registeredFatalErrorCallbackFunctionsSize); x++) + for (x = 0; (x < registeredFatalErrorCallbackFunctionsSize); x++) { // Trigger each function. if (registeredFatalErrorCallbackFunctions[x] != NULL) From 55e9861feda9030111abc5c20c424a163ce6ee22 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Thu, 11 Jun 2015 15:20:55 -0400 Subject: [PATCH 04/50] Remove unused exception var This commit removes the unused execption variable from Common::Fatal_Error_Notify(). (Get rid of compile warning.) --- .../Src/Error_Handler/Common_Error_Handler_CPP_Bindings.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Common/Src/Error_Handler/Common_Error_Handler_CPP_Bindings.cpp b/src/Common/Src/Error_Handler/Common_Error_Handler_CPP_Bindings.cpp index f880379..f355d93 100644 --- a/src/Common/Src/Error_Handler/Common_Error_Handler_CPP_Bindings.cpp +++ b/src/Common/Src/Error_Handler/Common_Error_Handler_CPP_Bindings.cpp @@ -105,7 +105,7 @@ void Common::Fatal_Error_Notify() // Call real function. Common_Fatal_Error_Notify(); } - catch (std::exception & ex) + catch (...) { // Well, not much to do here, we are terminating anyway. COMMON_LOG_CRITICAL("Common::Fatal_Error_Notify(): Exception occured while notifying engine subsystems / application that a fatal error occured."); From 7b918ff838a218c37f9ed26478d155989c86b284 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Thu, 11 Jun 2015 15:22:22 -0400 Subject: [PATCH 05/50] MSVC fix for Error_Handler_Struct This commit is a compile fix for Common_Error_Handler_Structures.c under MSVC. --- src/Common/Src/Error_Handler/Common_Error_Handler_Structures.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Common/Src/Error_Handler/Common_Error_Handler_Structures.c b/src/Common/Src/Error_Handler/Common_Error_Handler_Structures.c index 0c682f4..c693f92 100644 --- a/src/Common/Src/Error_Handler/Common_Error_Handler_Structures.c +++ b/src/Common/Src/Error_Handler/Common_Error_Handler_Structures.c @@ -122,12 +122,13 @@ const char * Common_Get_Error_Message(const int errorCode) // Init vars. const char * result = COMMON_ERROR_UNKNOWN_ERROR_MSG; // Result of this function. const size_t errorTableSize = Common_Get_Error_Table_Size(); // Size of the Common error lookup table. + size_t x = 0; /* Counter used in for loop. */ // Check for COMMON_UNKNOWN_ERROR. if (errorCode != COMMON_ERROR_UNKNOWN_ERROR) { // Begin lookup loop. - for (size_t x = 0; ((x < errorTableSize) && (result == COMMON_ERROR_UNKNOWN_ERROR_MSG)); x++) + for (x = 0; ((x < errorTableSize) && (result == COMMON_ERROR_UNKNOWN_ERROR_MSG)); x++) { // Check for the correct error code. if (Common_commonErrorTable[x].errorCode == errorCode) From 4e1f6353ac90824489a1ba1592caff6afe784be4 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Thu, 11 Jun 2015 15:31:27 -0400 Subject: [PATCH 06/50] MSVC fix Common_Error_Handler.h This commit is a compile fix for Common_Error_Handler.h under MSVC. (Fix missing stdbool.h with false header.) --- src/Common/Src/Error_Handler/Common_Error_Handler.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Common/Src/Error_Handler/Common_Error_Handler.h b/src/Common/Src/Error_Handler/Common_Error_Handler.h index 02f4949..03267f4 100644 --- a/src/Common/Src/Error_Handler/Common_Error_Handler.h +++ b/src/Common/Src/Error_Handler/Common_Error_Handler.h @@ -25,8 +25,12 @@ // External includes. #include // Defines NULL. #ifndef __cplusplus -#include // Defines bool data type. (For C compilers.) -#endif // __cplusplus +#ifdef _MSC_FULL_VER /* VC is special. */ +#include "..\stdbool.h" /* Defines bool data type. (For C compilers.) */ +#else +#include +#endif /* _MSC_FULL_VER */ +#endif /* __cplusplus */ // Project includes. #ifdef _WIN32 // Needed for different path seperator in Windows. From ff80017ce633810bbde571239c6fe9a8d82d7c7b Mon Sep 17 00:00:00 2001 From: codebase7 Date: Thu, 11 Jun 2015 21:19:12 -0400 Subject: [PATCH 07/50] Commit missing file. This commit adds the missing Dynamic_Library_Subsystem_Windows.c file. (It's defined to be built in master, but not present. We are creating this branch as it requires the fake stdbool header from the error_handler branch to build under MSVC. Also due to the code being untested, we are moving it here to avoid messing up master.) --- .../Dynamic_Library_Subsystem_Windows.c | 212 ++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c new file mode 100644 index 0000000..0691225 --- /dev/null +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c @@ -0,0 +1,212 @@ +/*! + Multiverse Engine Project 28/3/2014 Dynamic_Library_Subsystem Dynamic_Library_Subsystem_Windows.c + + Copyright (C) 2014 Multiverse Engine Project + + This program is free software; + you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; + either version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with this program; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Official source repository and project information can be found at + https://github.com/codebase7/mengine +*/ + +#include "Dynamic_Library_Subsystem.h" +#include + +#ifdef __cplusplus +// Define extern C. +extern "C" { +#endif + int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib) + { + // Init vars. + int result = 0; // The result of this function. + HMODULE callResult = NULL; // The result of the call to LoadLibraryEx(). + + // Check to see if the pointer to the management structure is valid. + if (lib != NULL) + { + // Check to see if pathToLibrary is NULL. + if (pathToLibrary != NULL) + { + // Check to see if the library is loaded. + if ((!lib->bIsLoaded) || (reloadLibrary)) + { + // Check and see if the library is loaded. + if (lib->bIsLoaded) + { + // Call Unload_Library. + result = Common_Dynamic_Library_Subsystem_Unload_Library(lib); + } + + // Only continue if the library was unloaded, or if we did not need to unload the library. + if (result == 0) + { + // Set the values in lib. + lib->bIsLoaded = false; + lib->bLastCallEncounteredAnError = false; + lib->osSpecificPointerData = NULL; + lib->pathToLibrary = pathToLibrary; + + // Call LoadLibraryEx(). + callResult = LoadLibraryEx(lib->pathToLibrary, NULL, 0); + + // Check the callResult. + if (callResult == NULL) + { + // Could not load the library. + result = -1; + lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Could not load the library.\n"); + } + else + { + // Cast the OS specific data structure pointer to void*. + lib->osSpecificPointerData = (void*)callResult; + + // Set bIsLoaded. + lib->bIsLoaded = true; + } + } + else + { + // Encountered an error during the unload. + result = -2; + lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Unable to reload library.\n"); + } + } + else + { + // Library is already loaded. + result = 1; + } + } + else + { + // pathToLibrary is NULL. + result = -3; + lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): No path to the library was given. Unable to load a library without the path to it.\n"); + } + } + else + { + // Management structure is invalid. + result = -4; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): The engine's library structure for the given library is invalid. Unable to load a library without a valid library structure.\n"); + } + + // Return result. + return result; + } + + int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib) + { + // Init vars. + int result = 0; // The result of this function. + + // Check to see if the pointer to the management structure is valid. + if (lib != NULL) + { + // Reset bLastCallEncounteredAnError. + lib->bLastCallEncounteredAnError = false; + + // Check and see if the OS specific data structure pointer is valid. + if ((lib->bIsLoaded) && (lib->osSpecificPointerData != NULL)) + { + // Call FreeLibrary. + if (FreeLibrary((HMODULE)lib->osSpecificPointerData)) + { + // The library was unloaded successfully. + lib->bIsLoaded = false; + lib->osSpecificPointerData = NULL; + } + else + { + // Could not unload the library. + result = -2; + lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not unload the library.\n"); + } + } + else + { + // Library is not loaded. + result = -1; + lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The library is not loaded.\n"); + } + } + else + { + // Management structure is invalid. + result = -4; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The engine's library structure for the given library is invalid. Unable to unload a library without a valid library structure.\n"); + } + + // Return result. + return result; + } + + void * Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const char * symbolName) + { + // Init vars. + void * result = NULL; // The result of this function. + + // Check to see if the pointer to the management structure is valid. + if (lib != NULL) + { + // Reset bLastCallEncounteredAnError. + lib->bLastCallEncounteredAnError = false; + + // Check to see if symbolName is NULL. + if (symbolName != NULL) + { + // Check for a loaded library. + if (((lib->bIsLoaded) && (lib->osSpecificPointerData != NULL))) + { + // Get the address. + result = (void*)GetProcAddress((HMODULE)lib->osSpecificPointerData, symbolName); + if (result == NULL) + { + // An error occured fetching the symbol. + lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Unable to retrive symbol.\n"); + } + } + else + { + // Library is not loaded. + lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The library is not loaded.\n"); + } + } + else + { + // symbolName is NULL. + lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): No symbol name was given, cannot load a symbol without a name to identifiy it.\n"); + } + } + else + { + // Library structure is invalid. + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The engine's library structure for the given library is invalid. Unable to lookup function without a valid library structure.\n"); + } + + // Return result. + return result; + } +#ifdef __cplusplus +} // End of extern C. +#endif + From 9dfbcb97167c0b4394b1a6795de30d011818a21f Mon Sep 17 00:00:00 2001 From: codebase7 Date: Thu, 11 Jun 2015 21:20:10 -0400 Subject: [PATCH 08/50] MSVC Compile Fix This commit is a compile fix for Dynamic_Library_Subsystem.h under MSVC. (Fake bool header.) --- .../Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h index 7042a1e..d972e8f 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h @@ -22,7 +22,12 @@ #ifndef MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_H #define MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_H +/* Check for MSVC. */ +#ifdef _MSC_FULL_VER +#include "..\stdbool.h" /* bool. (MSVC is special.) */ +#else #include // bool. +#endif /* _MSC_FULL_VER */ #include // NULL. #include // Malloc. From 2f65d1903ef8bf2f98a52c5552613528a608a684 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Thu, 11 Jun 2015 21:36:39 -0400 Subject: [PATCH 09/50] Fix warnings. This commit fixes the missing const statement from the function arguments for the following functions: Common_Dynamic_Library_Subsystem_Load_Library(), Common_Dynamic_Library_Subsystem_Unload_Library(), and Common_Dynamic_Library_Subsystem_Get_Symbol() in Dynamic_Library_Subsystem_Windows.c. --- .../Dynamic_Library_Subsystem_Windows.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c index 0691225..4e24acc 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c @@ -25,7 +25,7 @@ // Define extern C. extern "C" { #endif - int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib) + int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) { // Init vars. int result = 0; // The result of this function. @@ -109,7 +109,7 @@ extern "C" { return result; } - int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib) + int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) { // Init vars. int result = 0; // The result of this function. @@ -157,7 +157,7 @@ extern "C" { return result; } - void * Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const char * symbolName) + void * Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName) { // Init vars. void * result = NULL; // The result of this function. From f3294ea925fbdfa7871966c85e7889eeaa109d92 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sat, 13 Jun 2015 12:28:39 -0400 Subject: [PATCH 10/50] Define DLL exports for Dynamic Library Subsystem This commit defines the needed DLL exports for the Dynamic Library Subsystem. --- .../Dynamic_Library_Subsystem.h | 11 +++++++---- .../Dynamic_Library_Subsystem_Data_Structures.h | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h index d972e8f..8a03248 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h @@ -22,6 +22,9 @@ #ifndef MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_H #define MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_H +/* Internal includes. */ +#include "../../../DLL_PORT.h" /* Defines MSYS_DLL_EXPORT, and MSYS_DLL_IMPORT_TEMPLATE. */ + /* Check for MSVC. */ #ifdef _MSC_FULL_VER #include "..\stdbool.h" /* bool. (MSVC is special.) */ @@ -72,8 +75,8 @@ extern "C" { * Returns -4 if the given Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library pointer was NULL. * Returns 1 if the library was already loaded. (Only possible if reloadLibrary is false.) (The value of lib.bLastCallEncounteredAnError will be false in this case as well.) */ - int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const bool reloadLibrary, - Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib); + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const bool reloadLibrary, + Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib); /*! * int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) @@ -101,7 +104,7 @@ extern "C" { * Returns -2 if the library unload call returned unsuccessful. * Returns -4 if the given Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library pointer was NULL. */ - int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib); + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib); /*! * void * Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, @@ -126,7 +129,7 @@ extern "C" { * Returns a NULL pointer if the lookup failed for some reason. (The value of lib.bLastCallEncounteredAnError will be true in this case.) * Returns a NULL pointer if the given Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library pointer was NULL. */ - void * Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName); + MSYS_DLL_EXPORT void * Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName); #ifdef __cplusplus } // End of extern C. #endif diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h index f5beb16..1273b4f 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h @@ -65,7 +65,7 @@ extern "C" { * Returns a pointer to a Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library data structure if successful. * Otherwise returns NULL. */ - Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library(); + MSYS_DLL_EXPORT Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library(); /*! * void Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib) @@ -77,7 +77,7 @@ extern "C" { * * This function has no return. */ - void Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib); + MSYS_DLL_EXPORT void Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib); #ifdef __cplusplus } // End of extern C. #endif From dad999f63a1b133b10f5eed9e56b90f1a9d27596 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 04:11:01 -0500 Subject: [PATCH 11/50] Fix comment style in Dynamic_Library_Subsystem.h This commit fixes the code comment style in Dynamic_Library_Subsystem.h. --- .../Dynamic_Library_Subsystem.h | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h index 8a03248..4da3817 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h @@ -18,7 +18,7 @@ https://github.com/codebase7/mengine */ -// Include guard. +/* Include guard. */ #ifndef MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_H #define MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_H @@ -29,21 +29,21 @@ #ifdef _MSC_FULL_VER #include "..\stdbool.h" /* bool. (MSVC is special.) */ #else -#include // bool. +#include /* bool. */ #endif /* _MSC_FULL_VER */ -#include // NULL. -#include // Malloc. +#include /* NULL. */ +#include /* Malloc. */ -// Internal includes. -#include "Dynamic_Library_Subsystem_Data_Structures.h" // Contains data structures used internally. +/* Internal includes. */ +#include "Dynamic_Library_Subsystem_Data_Structures.h" /* Contains data structures used internally. */ #ifdef __win32 -#include "..\Error_Handler\Common_Error_Handler_Internal.h" // Contains the function defintions for calling the common error handler. +#include "..\Error_Handler\Common_Error_Handler_Internal.h" /* Contains the function defintions for calling the common error handler. */ #else -#include "../Error_Handler/Common_Error_Handler_Internal.h" // Contains the function defintions for calling the common error handler. -#endif // __win32 +#include "../Error_Handler/Common_Error_Handler_Internal.h" /* Contains the function defintions for calling the common error handler. */ +#endif /* __win32 */ -// Defines for the default library extension. +/* Defines for the default library extension. */ #ifdef __win32 #define DL_EXTENSION ".dll" #elif __linux__ @@ -51,7 +51,7 @@ #endif #ifdef __cplusplus -// Define extern C. +/* Define extern C. */ extern "C" { #endif /*! @@ -131,9 +131,9 @@ extern "C" { */ MSYS_DLL_EXPORT void * Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName); #ifdef __cplusplus -} // End of extern C. -#endif +} /* End of extern C. */ +#endif /* __cplusplus */ -#endif +#endif /* MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_H */ -// End of Dynamic_Library_Subsystem.h +/* End of Dynamic_Library_Subsystem.h */ From bd4d69564005572579092fd70d8722e0193dd29c Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 04:18:01 -0500 Subject: [PATCH 12/50] Fix return codes for Common_Dynamic_Library_Subsystem_Load_Library() (Windows) This commit fixes the return codes for Common_Dynamic_Library_Subsystem_Load_Library() (Windows) to use the Common Namespace Error Codes. (In addition to translating returned host error codes.) Also this commit fixes the code comment style in Common_Dynamic_Library_Subsystem_Load_Library() (Windows). --- .../Dynamic_Library_Subsystem.h | 12 +-- .../Dynamic_Library_Subsystem_Windows.c | 75 +++++++++++-------- 2 files changed, 51 insertions(+), 36 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h index 4da3817..de37c76 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h @@ -68,12 +68,12 @@ extern "C" { * Pram: Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, A pointer to a properly constructed * management data structure used internally. * - * Returns 0 if the library was successfully loaded. (The value of lib.bLastCallEncounteredAnError will be false in this case as well.) - * Returns -1 if the library could not be loaded. (The value of lib.bLastCallEncounteredAnError will be true in this case as well.) - * Returns -2 if the attempt to unload the library failed. (Only possible if reloadLibrary is true.) (The value of lib.bLastCallEncounteredAnError will be true in this case as well.) - * Returns -3 if the pathToLibrary pointer was NULL. (The value of lib.bLastCallEncounteredAnError will be true in this case as well.) - * Returns -4 if the given Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library pointer was NULL. - * Returns 1 if the library was already loaded. (Only possible if reloadLibrary is false.) (The value of lib.bLastCallEncounteredAnError will be false in this case as well.) + * Returns COMMON_ERROR_SUCCESS if the library was successfully loaded. (The value of lib.bLastCallEncounteredAnError will be false in this case as well.) + * Returns Translated host system error code if the library could not be loaded. (The value of lib.bLastCallEncounteredAnError will be true in this case as well.) + * Returns All errors returned by Common_Dynamic_Library_Subsystem_Unload_Library() if the attempt to unload the library failed. (Only possible if reloadLibrary is true.) (The value of lib.bLastCallEncounteredAnError will be true in this case as well.) + * Returns COMMON_ERROR_INVALID_ARGUMENT if the pathToLibrary pointer was NULL. (The value of lib.bLastCallEncounteredAnError will be true in this case as well.) + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library pointer was NULL. + * Returns DYNLIB_ERROR_LIBRARY_ALREADY_LOADED if the library was already loaded. (Only possible if reloadLibrary is false.) (The value of lib.bLastCallEncounteredAnError will be false in this case as well.) */ MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib); diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c index 4e24acc..1038185 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c @@ -18,95 +18,110 @@ https://github.com/codebase7/mengine */ +/* Internal includes. */ #include "Dynamic_Library_Subsystem.h" +#include "../Error_Handler/Common_Error_Handler_Structures.h" +#include "../Error_Handler/Windows_Error_Translation_Table.h" #include #ifdef __cplusplus -// Define extern C. +/* Define extern C. */ extern "C" { #endif int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) { - // Init vars. - int result = 0; // The result of this function. - HMODULE callResult = NULL; // The result of the call to LoadLibraryEx(). + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ + HMODULE callResult = NULL; /* The result of the call to LoadLibraryEx(). */ + DWORD retLLEX = 0; /* Error code from LoadLibraryEx(). */ - // Check to see if the pointer to the management structure is valid. + /* Check to see if the pointer to the management structure is valid. */ if (lib != NULL) { - // Check to see if pathToLibrary is NULL. + /* Check to see if pathToLibrary is NULL. */ if (pathToLibrary != NULL) { - // Check to see if the library is loaded. + /* Check to see if the library is loaded. */ if ((!lib->bIsLoaded) || (reloadLibrary)) { - // Check and see if the library is loaded. + /* Check and see if the library is loaded. */ if (lib->bIsLoaded) { - // Call Unload_Library. - result = Common_Dynamic_Library_Subsystem_Unload_Library(lib); + /* Call Unload_Library. */ + retFromCall = Common_Dynamic_Library_Subsystem_Unload_Library(lib); } - // Only continue if the library was unloaded, or if we did not need to unload the library. - if (result == 0) + /* Only continue if the library was unloaded, or if we did not need to unload the library. */ + if ((retFromCall == COMMON_ERROR_UNKNOWN_ERROR) || (retFromCall == COMMON_ERROR_SUCCESS)) { - // Set the values in lib. + /* Set the values in lib. */ lib->bIsLoaded = false; lib->bLastCallEncounteredAnError = false; lib->osSpecificPointerData = NULL; lib->pathToLibrary = pathToLibrary; - // Call LoadLibraryEx(). + /* Call LoadLibraryEx(). */ callResult = LoadLibraryEx(lib->pathToLibrary, NULL, 0); - // Check the callResult. + /* Check the callResult. */ if (callResult == NULL) { - // Could not load the library. - result = -1; + /* Get the last error. */ + retLLEX = GetLastError(); + retFromCall = Common_Translate_Windows_Error_Code_To_Common_Error_Code(retLLEX); + + /* Could not load the library. */ + ret = retFromCall; lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Could not load the library.\n"); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Could not load <"); + COMMON_LOG_VERBOSE(lib->pathToLibrary); + COMMON_LOG_VERBOSE("> Host function returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); } else { - // Cast the OS specific data structure pointer to void*. + /* Cast the OS specific data structure pointer to void*. */ lib->osSpecificPointerData = (void*)callResult; - // Set bIsLoaded. + /* Set bIsLoaded. */ lib->bIsLoaded = true; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; } } else { - // Encountered an error during the unload. - result = -2; + /* Encountered an error during the unload. */ + ret = retFromCall; lib->bLastCallEncounteredAnError = true; COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Unable to reload library.\n"); } } else { - // Library is already loaded. - result = 1; + /* Library is already loaded. */ + ret = DYNLIB_ERROR_LIBRARY_ALREADY_LOADED; } } else { - // pathToLibrary is NULL. - result = -3; + /* pathToLibrary is NULL. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; lib->bLastCallEncounteredAnError = true; COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): No path to the library was given. Unable to load a library without the path to it.\n"); } } else { - // Management structure is invalid. - result = -4; + /* Management structure is invalid. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): The engine's library structure for the given library is invalid. Unable to load a library without a valid library structure.\n"); } - // Return result. - return result; + /* Exit function. */ + return ret; } int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) From ba2d9c7504dc1b063659235bb521cf561f1f507d Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 04:34:58 -0500 Subject: [PATCH 13/50] Fix return codes for Common_Dynamic_Library_Subsystem_Load_Library() (Posix) This commit fixes the return codes for Common_Dynamic_Library_Subsystem_Load_Library() (Posix) to use the Common Namespace Error Codes. (In addition to translating returned host error codes.) Also this commit fixes the code comment style in Common_Dynamic_Library_Subsystem_Load_Library() (Posix), in addition to adding verbose logging support. --- .../Dynamic_Library_Subsystem_POSIX.c | 91 ++++++++++++------- 1 file changed, 60 insertions(+), 31 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c index d5fe56f..a7363ed 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c @@ -17,93 +17,122 @@ Official source repository and project information can be found at https://github.com/codebase7/mengine */ - + +/* Check for linux. */ #ifdef __linux__ + +/* Internal includes */ #include "Dynamic_Library_Subsystem.h" +#include "../Error_Handler/Common_Error_Handler_Structures.h" +#include "../Error_Handler/Posix_Error_Translation_Table.h" + +/* External includes. */ #include // dlopen, dlclose, dlsym, dlerror. +/* Check for C++ Compiler. */ #ifdef __cplusplus -// Define extern C. +/* Define extern C. */ extern "C" { #endif int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) { - // Init vars. - int result = 0; // The result of this function. - void * callResult = NULL; // The result of the call to dlopen(). + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ + int errcpy = 0; /* The current errno. */ + void * callResult = NULL; /* The result of the call to dlopen(). */ - // Check to see if the pointer to the management structure is valid. + /* Check to see if the pointer to the management structure is valid. */ if (lib != NULL) { - // Check to see if pathToLibrary is NULL. + /* Check to see if pathToLibrary is NULL. */ if (pathToLibrary != NULL) { - // Check to see if the library is loaded. + /* Check to see if the library is loaded. */ if ((!lib->bIsLoaded) || (reloadLibrary)) { - // Check and see if the library is loaded. + /* Check and see if the library is loaded. */ if (lib->bIsLoaded) { - // Call Unload_Library. - result = Common_Dynamic_Library_Subsystem_Unload_Library(lib); + /* Call Unload_Library. */ + retFromCall = Common_Dynamic_Library_Subsystem_Unload_Library(lib); } - // Only continue if the library was unloaded, or if we did not need to unload the library. - if (result == 0) + /* Only continue if the library was unloaded, or if we did not need to unload the library. */ + if ((retFromCall == COMMON_ERROR_UNKNOWN_ERROR) || (retFromCall == COMMON_ERROR_SUCCESS)) { - // Set the values in lib. + /* Set the values in lib. */ lib->bIsLoaded = false; lib->bLastCallEncounteredAnError = false; lib->osSpecificPointerData = NULL; lib->pathToLibrary = pathToLibrary; - // Call dlopen(). + /* Call dlopen(). */ callResult = dlopen(lib->pathToLibrary, RTLD_LAZY | RTLD_LOCAL); - // Check the callResult. + /* Check the callResult. */ if (callResult == NULL) { - // Could not load the library. - result = -1; + /* Fetch the errno. */ + errcpy = errno; + + /* Translate the error code. */ + retFromCall = Common_Translate_Posix_Errno_To_Common_Error_Code(errcpy); + + /* Could not load the library. */ + ret = retFromCall; lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Could not load <"); + COMMON_LOG_VERBOSE(lib->pathToLibrary); + COMMON_LOG_VERBOSE("> Host function returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); } else { - // Cast the OS specific data structure pointer to void*. + /* Cast the OS specific data structure pointer to void. */ lib->osSpecificPointerData = callResult; - // Set bIsLoaded. + /* Set bIsLoaded. */ lib->bIsLoaded = true; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): <"); + COMMON_LOG_VERBOSE(lib->pathToLibrary); + COMMON_LOG_VERBOSE("> loaded."); } } else { - // Encountered an error during the unload. - result = -2; + /* Encountered an error during the unload. */ + ret = retFromCall; lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Unable to reload library."); } } else { - // Library is already loaded. - result = 1; + /* Library is already loaded. */ + ret = DYNLIB_ERROR_LIBRARY_ALREADY_LOADED; } } else { - // pathToLibrary is NULL. - result = -3; + /* pathToLibrary is NULL. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): No path to the library was given. Unable to load a library without the path to it."); } } else { - // Management structure is invalid. - result = -4; + /* Management structure is invalid. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): The engine's library structure for the given library is invalid. Unable to load a library without a valid library structure."); } - // Return result. - return result; + /* Return result. */ + return ret; } int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) @@ -199,4 +228,4 @@ extern "C" { #ifdef __cplusplus } // End of extern C. #endif -#endif +#endif From d1b8b73cdef49864ae8d82e814bf8380a11511e5 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 04:37:44 -0500 Subject: [PATCH 14/50] Fix verbose logging in Common_Dynamic_Library_Subsystem_Load_Library() (Windows) This commit fixes the verbose logging in Common_Dynamic_Library_Subsystem_Load_Library() (Windows). (Remove new lines, add library loaded message.) --- .../Dynamic_Library_Subsystem_Windows.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c index 1038185..17f187b 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c @@ -89,6 +89,9 @@ extern "C" { /* Success. */ ret = COMMON_ERROR_SUCCESS; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): <"); + COMMON_LOG_VERBOSE(lib->pathToLibrary); + COMMON_LOG_VERBOSE("> loaded."); } } else @@ -96,7 +99,7 @@ extern "C" { /* Encountered an error during the unload. */ ret = retFromCall; lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Unable to reload library.\n"); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Unable to reload library."); } } else @@ -110,14 +113,14 @@ extern "C" { /* pathToLibrary is NULL. */ ret = COMMON_ERROR_INVALID_ARGUMENT; lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): No path to the library was given. Unable to load a library without the path to it.\n"); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): No path to the library was given. Unable to load a library without the path to it."); } } else { /* Management structure is invalid. */ ret = COMMON_ERROR_INVALID_ARGUMENT; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): The engine's library structure for the given library is invalid. Unable to load a library without a valid library structure.\n"); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): The engine's library structure for the given library is invalid. Unable to load a library without a valid library structure."); } /* Exit function. */ From 83898c5bfc75511649037a4a128465041b17a54a Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 04:40:19 -0500 Subject: [PATCH 15/50] More comment fixes for the loaders. This commit adds some missing comments to Dynamic_Library_Subsystem_Windows.c and Dynamic_Library_Subsystem_POSIX.c. --- .../Dynamic_Library_Subsystem_POSIX.c | 2 +- .../Dynamic_Library_Subsystem_Windows.c | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c index a7363ed..e8a69d2 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c @@ -33,7 +33,7 @@ #ifdef __cplusplus /* Define extern C. */ extern "C" { -#endif +#endif /* __cplusplus */ int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) { /* Init vars. */ diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c index 17f187b..7cc944a 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c @@ -22,12 +22,15 @@ #include "Dynamic_Library_Subsystem.h" #include "../Error_Handler/Common_Error_Handler_Structures.h" #include "../Error_Handler/Windows_Error_Translation_Table.h" + +/* External includes. */ #include +/* Check for C++ Compiler. */ #ifdef __cplusplus /* Define extern C. */ extern "C" { -#endif +#endif /* __cplusplus */ int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) { /* Init vars. */ From 96266d9abce056126f430279bbfc96cc76c86b69 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 04:42:44 -0500 Subject: [PATCH 16/50] Fix comment style in Common_Dynamic_Library_Subsystem_Unload_Library() (Windows) This commit fixes the code comment style in Common_Dynamic_Library_Subsystem_Unload_Library() (Windows). --- .../Dynamic_Library_Subsystem_Windows.c | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c index 7cc944a..35f8c96 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c @@ -132,28 +132,28 @@ extern "C" { int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) { - // Init vars. - int result = 0; // The result of this function. + /* Init vars. */ + int result = 0; /* The result of this function. */ - // Check to see if the pointer to the management structure is valid. + /* Check to see if the pointer to the management structure is valid. */ if (lib != NULL) { - // Reset bLastCallEncounteredAnError. + /* Reset bLastCallEncounteredAnError. */ lib->bLastCallEncounteredAnError = false; - // Check and see if the OS specific data structure pointer is valid. + /* Check and see if the OS specific data structure pointer is valid. */ if ((lib->bIsLoaded) && (lib->osSpecificPointerData != NULL)) { - // Call FreeLibrary. + /* Call FreeLibrary. */ if (FreeLibrary((HMODULE)lib->osSpecificPointerData)) { - // The library was unloaded successfully. + /* The library was unloaded successfully. */ lib->bIsLoaded = false; lib->osSpecificPointerData = NULL; } else { - // Could not unload the library. + /* Could not unload the library. */ result = -2; lib->bLastCallEncounteredAnError = true; COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not unload the library.\n"); @@ -161,7 +161,7 @@ extern "C" { } else { - // Library is not loaded. + /* Library is not loaded. */ result = -1; lib->bLastCallEncounteredAnError = true; COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The library is not loaded.\n"); @@ -169,12 +169,12 @@ extern "C" { } else { - // Management structure is invalid. + /* Management structure is invalid. */ result = -4; COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The engine's library structure for the given library is invalid. Unable to unload a library without a valid library structure.\n"); } - // Return result. + /* Return result. */ return result; } From a49f1e034f113ab6bc3bc94f02e1fbd4668f5998 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 04:44:25 -0500 Subject: [PATCH 17/50] Fix comment style in Common_Dynamic_Library_Subsystem_Unload_Library() (Posix) This commit fixes the code comment style in Common_Dynamic_Library_Subsystem_Unload_Library() (Posix). --- .../Dynamic_Library_Subsystem_POSIX.c | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c index e8a69d2..32aa27b 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c @@ -137,49 +137,49 @@ extern "C" { int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) { - // Init vars. - int result = 0; // The result of this function. + /* Init vars. */ + int result = 0; /* The result of this function. */ - // Check to see if the pointer to the management structure is valid. + /* Check to see if the pointer to the management structure is valid. */ if (lib != NULL) { - // Reset bLastCallEncounteredAnError. + /* Reset bLastCallEncounteredAnError. */ lib->bLastCallEncounteredAnError = false; - // Check for a valid handle. + /* Check for a valid handle. */ if ((lib->bIsLoaded) && (lib->osSpecificPointerData != NULL)) { - // Call dlclose(). + /* Call dlclose(). */ result = dlclose(lib->osSpecificPointerData); - // Check result. + /* Check result. */ if (result != 0) { - // An error occured. + /* An error occured. */ result = -2; lib->bLastCallEncounteredAnError = true; } else { - // Library unloaded successfully. + /* Library unloaded successfully. */ lib->osSpecificPointerData = NULL; lib->bIsLoaded = false; } } else { - // Library is not loaded. + /* Library is not loaded. */ result = -1; lib->bLastCallEncounteredAnError = true; } } else { - // Management structure is invalid. + /* Management structure is invalid. */ result = -4; } - // Return result. + /* Return result. */ return result; } From b967f2788e20c5c3922538313be3aa6d7046b186 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 05:05:41 -0500 Subject: [PATCH 18/50] Fix return codes for Common_Dynamic_Library_Subsystem_Unload_Library() (Windows) This commit fixes the return codes for Common_Dynamic_Library_Subsystem_Unload_Library() (Windows) to use the Common Namespace Error Codes. (In addition to translating returned host error codes.) Also this commit fixes the verbose logging in Common_Dynamic_Library_Subsystem_Unload_Library() (Windows) in addition to fixing a bug involving not clearing the lib->bLastCallEncounteredAnError flag apon success. --- .../Dynamic_Library_Subsystem_Windows.c | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c index 35f8c96..398c474 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c @@ -133,7 +133,9 @@ extern "C" { int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) { /* Init vars. */ - int result = 0; /* The result of this function. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ + DWORD retFL = 0; /* Error code from FreeLibrary(). */ /* Check to see if the pointer to the management structure is valid. */ if (lib != NULL) @@ -150,32 +152,48 @@ extern "C" { /* The library was unloaded successfully. */ lib->bIsLoaded = false; lib->osSpecificPointerData = NULL; + lib->bLastCallEncounteredAnError = false; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): <"; + COMMON_LOG_VERBOSE(lib->pathToLibrary); + COMMON_LOG_VERBOSE("> unloaded."); } else { + /* Get the last error. */ + retFL = GetLastError(); + retFromCall = Common_Translate_Windows_Error_Code_To_Common_Error_Code(retFL); + /* Could not unload the library. */ - result = -2; + ret = retFromCall; lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not unload the library.\n"); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not unload <"); + COMMON_LOG_VERBOSE(lib->pathToLibrary); + COMMON_LOG_VERBOSE("> Host function returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); } } else { /* Library is not loaded. */ - result = -1; + ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The library is not loaded.\n"); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The given library <"); + COMMON_LOG_VERBOSE(lib->pathToLibrary); + COMMON_LOG_VERBOSE("> is not loaded."); } } else { /* Management structure is invalid. */ - result = -4; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The engine's library structure for the given library is invalid. Unable to unload a library without a valid library structure.\n"); + ret = COMMON_ERROR_INVALID_ARGUMENT_ERROR; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The engine's library structure for the given library is invalid. Unable to unload a library without a valid library structure."); } /* Return result. */ - return result; + return ret; } void * Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName) From 7ad49e1d726e4a4458653c0145e6d9ca2103badc Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 05:07:07 -0500 Subject: [PATCH 19/50] Fix return codes for Common_Dynamic_Library_Subsystem_Unload_Library() (Posix) This commit fixes the return codes for Common_Dynamic_Library_Subsystem_Unload_Library() (Posix) to use the Common Namespace Error Codes. (In addition to translating returned host error codes.) Also this commit fixes the verbose logging (lack thereof) in Common_Dynamic_Library_Subsystem_Unload_Library() (Posix) in addition to fixing a bug involving not clearing the lib->bLastCallEncounteredAnError flag apon success. --- .../Dynamic_Library_Subsystem_POSIX.c | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c index 32aa27b..e421bb4 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c @@ -138,7 +138,9 @@ extern "C" { int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) { /* Init vars. */ - int result = 0; /* The result of this function. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ + int errcpy = 0; /* The current errno. */ /* Check to see if the pointer to the management structure is valid. */ if (lib != NULL) @@ -150,37 +152,58 @@ extern "C" { if ((lib->bIsLoaded) && (lib->osSpecificPointerData != NULL)) { /* Call dlclose(). */ - result = dlclose(lib->osSpecificPointerData); + retFromCall = dlclose(lib->osSpecificPointerData); /* Check result. */ - if (result != 0) + if (retFromCall != 0) { - /* An error occured. */ - result = -2; + /* Copy the errno. */ + errcpy = errno; + + /* Translate the error code. */ + retFromCall = Common_Translate_Posix_Errno_To_Common_Error_Code(errcpy); + + /* Could not load the library. */ + ret = retFromCall; lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not unload <"); + COMMON_LOG_VERBOSE(lib->pathToLibrary); + COMMON_LOG_VERBOSE("> Host function returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); } else { /* Library unloaded successfully. */ lib->osSpecificPointerData = NULL; lib->bIsLoaded = false; + lib->bLastCallEncounteredAnError = false; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): <"; + COMMON_LOG_VERBOSE(lib->pathToLibrary); + COMMON_LOG_VERBOSE("> unloaded."); } } else { /* Library is not loaded. */ - result = -1; + ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The given library <"); + COMMON_LOG_VERBOSE(lib->pathToLibrary); + COMMON_LOG_VERBOSE("> is not loaded."); } } else { /* Management structure is invalid. */ - result = -4; + ret = COMMON_ERROR_INVALID_ARGUMENT_ERROR; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The engine's library structure for the given library is invalid. Unable to unload a library without a valid library structure."); } /* Return result. */ - return result; + return ret; } void * Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName) From e7a864bd2e3db6fffe53b92e201259163dd44826 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 05:09:38 -0500 Subject: [PATCH 20/50] Fix output bug in Common_Dynamic_Library_Subsystem_Load_Library() (Posix) This commit fixes an output bug in Common_Dynamic_Library_Subsystem_Load_Library() (Posix) where the lib->bLastCallEncounteredAnError flag was not cleared on success. --- .../Dynamic_Library_Subsystem_POSIX.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c index e421bb4..e815ca0 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c @@ -95,6 +95,9 @@ extern "C" { /* Set bIsLoaded. */ lib->bIsLoaded = true; + /* Clear lib->bLastCallEncounteredAnError. */ + lib->bLastCallEncounteredAnError = false; + /* Success. */ ret = COMMON_ERROR_SUCCESS; COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): <"); From 4a3108bfb84aa3a6f658ed5c6643303211e808b1 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 05:11:35 -0500 Subject: [PATCH 21/50] Fix output bug in Common_Dynamic_Library_Subsystem_Load_Library() (Windows) This commit fixes an output bug in Common_Dynamic_Library_Subsystem_Load_Library() (Windows) where the lib->bLastCallEncounteredAnError flag was not cleared on success. --- .../Dynamic_Library_Subsystem_Windows.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c index 398c474..fd93da2 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c @@ -90,6 +90,9 @@ extern "C" { /* Set bIsLoaded. */ lib->bIsLoaded = true; + /* Clear lib->bLastCallEncounteredAnError. */ + lib->bLastCallEncounteredAnError = false; + /* Success. */ ret = COMMON_ERROR_SUCCESS; COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): <"); From 4a74351b13150800cd8fe05837745301a35ad1db Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 05:17:16 -0500 Subject: [PATCH 22/50] Fix comment style in Common_Dynamic_Library_Subsystem_Get_Symbol() (Windows) This commit fixes the code comment style in Common_Dynamic_Library_Subsystem_Get_Symbol() (Windows). --- .../Dynamic_Library_Subsystem_Windows.c | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c index fd93da2..69cc9d0 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c @@ -201,54 +201,54 @@ extern "C" { void * Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName) { - // Init vars. - void * result = NULL; // The result of this function. + /* Init vars. */ + void * result = NULL; /* The result of this function. */ - // Check to see if the pointer to the management structure is valid. + /* Check to see if the pointer to the management structure is valid. */ if (lib != NULL) { - // Reset bLastCallEncounteredAnError. + /* Reset bLastCallEncounteredAnError. */ lib->bLastCallEncounteredAnError = false; - // Check to see if symbolName is NULL. + /* Check to see if symbolName is NULL. */ if (symbolName != NULL) { - // Check for a loaded library. + /* Check for a loaded library. */ if (((lib->bIsLoaded) && (lib->osSpecificPointerData != NULL))) { - // Get the address. + /* Get the address. */ result = (void*)GetProcAddress((HMODULE)lib->osSpecificPointerData, symbolName); if (result == NULL) { - // An error occured fetching the symbol. + /* An error occured fetching the symbol. */ lib->bLastCallEncounteredAnError = true; COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Unable to retrive symbol.\n"); } } else { - // Library is not loaded. + /* Library is not loaded. */ lib->bLastCallEncounteredAnError = true; COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The library is not loaded.\n"); } } else { - // symbolName is NULL. + /* symbolName is NULL. */ lib->bLastCallEncounteredAnError = true; COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): No symbol name was given, cannot load a symbol without a name to identifiy it.\n"); } } else { - // Library structure is invalid. + /* Library structure is invalid. */ COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The engine's library structure for the given library is invalid. Unable to lookup function without a valid library structure.\n"); } - // Return result. + /* Return result. */ return result; } #ifdef __cplusplus -} // End of extern C. -#endif +} /* End of extern C. */ +#endif /* __cplusplus */ From d8f6ee863b777a7c626705ca3f44a28f3afb412e Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 05:18:36 -0500 Subject: [PATCH 23/50] Fix comment style in Common_Dynamic_Library_Subsystem_Get_Symbol() (Posix) This commit fixes the code comment style in Common_Dynamic_Library_Subsystem_Get_Symbol() (Posix). --- .../Dynamic_Library_Subsystem_POSIX.c | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c index e815ca0..f804273 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c @@ -211,31 +211,31 @@ extern "C" { void * Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName) { - // Init vars. - void * result = NULL; // The result of this function. + /* Init vars. */ + void * result = NULL; /* The result of this function. */ - // Check to see if the pointer to the management structure is valid. + /* Check to see if the pointer to the management structure is valid. */ if (lib != NULL) { - // Reset bLastCallEncounteredAnError. + /* Reset bLastCallEncounteredAnError. */ lib->bLastCallEncounteredAnError = false; - // Check to see if symbolName is NULL. + /* Check to see if symbolName is NULL. */ if (symbolName != NULL) { - // Check to see if we have a valid handle. + /* Check to see if we have a valid handle. */ if ((lib->bIsLoaded) && (lib->osSpecificPointerData != NULL)) { - // Call dlerror to clear the error state. + /* Call dlerror to clear the error state. */ dlerror(); - // Call dlsym. + /* Call dlsym. */ result = dlsym(lib->osSpecificPointerData, symbolName); - // Call dlerror again to check for an error. + /* Call dlerror again to check for an error. */ if (dlerror() != NULL) { - // An error occured. + /* An error occured. */ result = NULL; lib->bLastCallEncounteredAnError = true; } @@ -243,15 +243,15 @@ extern "C" { } else { - // symbolName is NULL. + /* symbolName is NULL. */ lib->bLastCallEncounteredAnError = true; } } - // Return result. + /* Return result. */ return result; } #ifdef __cplusplus -} // End of extern C. -#endif -#endif +} /* End of extern C. */ +#endif /* __cplusplus */ +#endif /* __linux__ */ From 21faf1d1e838333beb2408cbe5bb25bc2d1ba3e2 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 05:41:25 -0500 Subject: [PATCH 24/50] Fix Common_Dynamic_Library_Subsystem_Get_Symbol() (POSIX) to be compliant with the new coding style. This commit fixes Common_Dynamic_Library_Subsystem_Get_Symbol() (POSIX) to be compliant with the new coding style. - Return Common Namespace error codes. - Additional output variables as function arguments. - Verbose logging. - C-Style code comments. --- .../Dynamic_Library_Subsystem_POSIX.c | 62 +++++++++++++++---- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c index f804273..0d7e437 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c @@ -209,17 +209,22 @@ extern "C" { return ret; } - void * Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName) + int Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName, void ** retSym) { /* Init vars. */ - void * result = NULL; /* The result of this function. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + void * pSym = NULL; /* The returned symbol pointer. */ + char * hostErr = NULL; /* The result returned from dlerror(). */ /* Check to see if the pointer to the management structure is valid. */ if (lib != NULL) { - /* Reset bLastCallEncounteredAnError. */ - lib->bLastCallEncounteredAnError = false; + /* Reset bLastCallEncounteredAnError. */ + lib->bLastCallEncounteredAnError = false; + /* Check and see if retSym is valid. */ + if (retSym != NULL) + { /* Check to see if symbolName is NULL. */ if (symbolName != NULL) { @@ -230,26 +235,61 @@ extern "C" { dlerror(); /* Call dlsym. */ - result = dlsym(lib->osSpecificPointerData, symbolName); + pSym = dlsym(lib->osSpecificPointerData, symbolName); /* Call dlerror again to check for an error. */ - if (dlerror() != NULL) + hostErr = dlerror(); + if (hostErr != NULL) { - /* An error occured. */ - result = NULL; + /* An error occured. + There is no clean way to check the error given here, as dlerror() returns a human-readable string. + In addition, dlsym() does not have any defined error codes in the POSIX standard. + As such we have no way of returning the specific error encountered to the caller, + so we must return COMMON_ERROR_SYSTEM_SPECIFIC. + */ + ret = COMMON_ERROR_SYSTEM_SPECIFIC; lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Error returned from host: "); + COMMON_LOG_VERBOSE(hostErr); + } + else + { + /* Copy pSym to retSym. */ + (*retSym) = pSym; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; } } } else { - /* symbolName is NULL. */ - lib->bLastCallEncounteredAnError = true; + /* symbolName is NULL. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); } + } + else + { + /* retSym is NULL. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + } + } + else + { + /* Invalid management structure. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); } /* Return result. */ - return result; + return ret; } #ifdef __cplusplus } /* End of extern C. */ From 4c3be6d5de5a5e8f35fd0624383c9469cd4ba362 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 05:46:27 -0500 Subject: [PATCH 25/50] Fix host error handling Common_Dynamic_Library_Subsystem_Unload_Library() (Posix) This commit fixes host error handling in Common_Dynamic_Library_Subsystem_Unload_Library() (Posix). (The POSIX standard does not define error codes for dlclose().) --- .../Dynamic_Library_Subsystem_POSIX.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c index 0d7e437..51b0bfe 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c @@ -143,7 +143,7 @@ extern "C" { /* Init vars. */ int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ - int errcpy = 0; /* The current errno. */ + char * hostErr = NULL; /* The result returned from dlerror(). */ /* Check to see if the pointer to the management structure is valid. */ if (lib != NULL) @@ -160,19 +160,21 @@ extern "C" { /* Check result. */ if (retFromCall != 0) { - /* Copy the errno. */ - errcpy = errno; - - /* Translate the error code. */ - retFromCall = Common_Translate_Posix_Errno_To_Common_Error_Code(errcpy); + /* An error occured. + There is no clean way to check the error given here, as dlerror() returns a human-readable string. + In addition, dlclose() does not have any defined error codes in the POSIX standard. + As such we have no way of returning the specific error encountered to the caller, + so we must return COMMON_ERROR_SYSTEM_SPECIFIC. + */ + ret = COMMON_ERROR_SYSTEM_SPECIFIC; /* Could not load the library. */ - ret = retFromCall; + hostErr = dlerror(); lib->bLastCallEncounteredAnError = true; COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not unload <"); COMMON_LOG_VERBOSE(lib->pathToLibrary); COMMON_LOG_VERBOSE("> Host function returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + COMMON_LOG_VERBOSE(hostErr); } else { From f3aa28a462c1c31ffa20bdc20219b51f0523bd6a Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 05:50:04 -0500 Subject: [PATCH 26/50] Fix host error handling Common_Dynamic_Library_Subsystem_Load_Library() (Posix) This commit fixes host error handling in Common_Dynamic_Library_Subsystem_Load_Library() (Posix). (The POSIX standard does not define error codes for dlopen().) --- .../Dynamic_Library_Subsystem_POSIX.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c index 51b0bfe..dcf0d14 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c @@ -24,7 +24,6 @@ /* Internal includes */ #include "Dynamic_Library_Subsystem.h" #include "../Error_Handler/Common_Error_Handler_Structures.h" -#include "../Error_Handler/Posix_Error_Translation_Table.h" /* External includes. */ #include // dlopen, dlclose, dlsym, dlerror. @@ -39,7 +38,7 @@ extern "C" { /* Init vars. */ int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ - int errcpy = 0; /* The current errno. */ + char * hostErr = NULL; /* The result returned from dlerror(). */ void * callResult = NULL; /* The result of the call to dlopen(). */ /* Check to see if the pointer to the management structure is valid. */ @@ -73,19 +72,21 @@ extern "C" { /* Check the callResult. */ if (callResult == NULL) { - /* Fetch the errno. */ - errcpy = errno; - - /* Translate the error code. */ - retFromCall = Common_Translate_Posix_Errno_To_Common_Error_Code(errcpy); + /* An error occured. + There is no clean way to check the error given here, as dlerror() returns a human-readable string. + In addition, dlopen() does not have any defined error codes in the POSIX standard. + As such we have no way of returning the specific error encountered to the caller, + so we must return COMMON_ERROR_SYSTEM_SPECIFIC. + */ + ret = COMMON_ERROR_SYSTEM_SPECIFIC; /* Could not load the library. */ - ret = retFromCall; + hostErr = dlerror(); lib->bLastCallEncounteredAnError = true; COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Could not load <"); COMMON_LOG_VERBOSE(lib->pathToLibrary); COMMON_LOG_VERBOSE("> Host function returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + COMMON_LOG_VERBOSE(hostErr); } else { From ee0b26bcf92faed1b8e4b42e2ec6d2a469c8c8af Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 05:58:43 -0500 Subject: [PATCH 27/50] Fix missing error code to Common_Dynamic_Library_Subsystem_Get_Symbol() (Posix) This commit fixes the missing error code for when Common_Dynamic_Library_Subsystem_Get_Symbol() (Posix) gets called on library that is not currently loaded. --- .../Dynamic_Library_Subsystem_POSIX.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c index dcf0d14..2afa20c 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c @@ -264,6 +264,15 @@ extern "C" { ret = COMMON_ERROR_SUCCESS; } } + else + { + /* Library is not loaded. */ + ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; + lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The given library <"); + COMMON_LOG_VERBOSE(lib->pathToLibrary); + COMMON_LOG_VERBOSE("> is not loaded."); + } } else { From e251fb9880c7be21889ee028d772f611fe33eafa Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 06:04:50 -0500 Subject: [PATCH 28/50] Fix Common_Dynamic_Library_Subsystem_Get_Symbol() (Windows) to be compliant with the new coding style. This commit fixes Common_Dynamic_Library_Subsystem_Get_Symbol() (Windows) to be compliant with the new coding style. - Return Common Namespace error codes. - Additional output variables as function arguments. - Verbose logging. - C-Style code comments. --- .../Dynamic_Library_Subsystem_Windows.c | 62 +++++++++++++++---- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c index 69cc9d0..3b876c0 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c @@ -199,17 +199,23 @@ extern "C" { return ret; } - void * Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName) + int Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName, void ** retSym) { /* Init vars. */ - void * result = NULL; /* The result of this function. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ + void * pSym = NULL; /* The returned symbol pointer. */ + DWORD retGPA = 0; /* Error code from GetProcAddress(). */ /* Check to see if the pointer to the management structure is valid. */ if (lib != NULL) { - /* Reset bLastCallEncounteredAnError. */ - lib->bLastCallEncounteredAnError = false; + /* Reset bLastCallEncounteredAnError. */ + lib->bLastCallEncounteredAnError = false; + /* Check and see if retSym is valid. */ + if (retSym != NULL) + { /* Check to see if symbolName is NULL. */ if (symbolName != NULL) { @@ -217,36 +223,66 @@ extern "C" { if (((lib->bIsLoaded) && (lib->osSpecificPointerData != NULL))) { /* Get the address. */ - result = (void*)GetProcAddress((HMODULE)lib->osSpecificPointerData, symbolName); - if (result == NULL) + pSym = (void*)GetProcAddress((HMODULE)lib->osSpecificPointerData, symbolName); + if (pSym == NULL) { + /* Get the last error. */ + retGPA = GetLastError(); + retFromCall = Common_Translate_Windows_Error_Code_To_Common_Error_Code(retGPA); + /* An error occured fetching the symbol. */ + ret = retFromCall; lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Unable to retrive symbol.\n"); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not fetch symbol in <"); + COMMON_LOG_VERBOSE(lib->pathToLibrary); + COMMON_LOG_VERBOSE("> Host function returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + else + { + /* Copy pSym to retSym. */ + (*retSym) = pSym; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; } } else { /* Library is not loaded. */ + ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The library is not loaded.\n"); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The given library <"); + COMMON_LOG_VERBOSE(lib->pathToLibrary); + COMMON_LOG_VERBOSE("> is not loaded."); } } else { - /* symbolName is NULL. */ - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): No symbol name was given, cannot load a symbol without a name to identifiy it.\n"); + /* symbolName is NULL. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): No symbol name was given, cannot load a symbol without a name to identifiy it."); } + } + else + { + /* retSym is NULL. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + lib->bLastCallEncounteredAnError = true; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + } } else { /* Library structure is invalid. */ - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The engine's library structure for the given library is invalid. Unable to lookup function without a valid library structure.\n"); + ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The engine's library structure for the given library is invalid. Unable to lookup function without a valid library structure."); } /* Return result. */ - return result; + return ret; } #ifdef __cplusplus } /* End of extern C. */ From 7e0ff7795391abba88f2c4a5cd923fbc4d10e3f9 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 06:23:01 -0500 Subject: [PATCH 29/50] Fix code comments and make Common_Dynamic_Library_Subsystem_Get_Symbol() compliant with the new coding style This commit fixes the code comments / documentation in Dynamic_Library_Subsystem.h. In addition to making Common_Dynamic_Library_Subsystem_Get_Symbol()'s declaration compliant with the new coding style. - The code documentation for Common_Dynamic_Library_Subsystem_Load_Library(), Common_Dynamic_Library_Subsystem_Unload_Library(), and Common_Dynamic_Library_Subsystem_Get_Symbol() has been updated with their new return codes. - Common_Dynamic_Library_Subsystem_Get_Symbol() now returns Common Namespace Error Codes. - The symbol pointer from Common_Dynamic_Library_Subsystem_Get_Symbol() is now returned to the caller by a new double pointer argument (retSym). --- .../Dynamic_Library_Subsystem.h | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h index de37c76..50e60fa 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h @@ -60,9 +60,6 @@ extern "C" { * * This function calls the system specific dynamic library handler and attempts to load the requested library. * - * Note: Due to the nature of the Dynamic_Library_Subsystem being a generic wrapper over the system specific calls, - * we cannot return more informative error messages. (Not without a generic error lookup table anyway....) - * * Pram: const char * pathToLibrary, a pointer to the c-string that contains the path to the library on disk. * Pram: const bool reloadLibrary, whether or not to reload the library if it is already loaded. * Pram: Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, A pointer to a properly constructed @@ -83,9 +80,6 @@ extern "C" { * * This function calls the system specific dynamic library handler and attempts to unload the requested library. * - * Note: Due to the nature of the Dynamic_Library_Subsystem being a generic wrapper over the system specific calls, - * we cannot return more informative error messages. (Not without a generic error lookup table anyway....) - * * Note: Due to the underlying system, even if this call is successful, the library may still be loaded in memory. * Internally, the Dynamic_Library_Subsystem blocks attempts to load the library multiple times via calls to Common_Dynamic_Library_Subsystem_Load_Library(). * (I.e. Load a library that is already loaded.) This is to try and prevent specific systems that use reference counts @@ -99,23 +93,20 @@ extern "C" { * Pram: Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, A pointer to a properly constructed * management data structure used internally. * - * Returns 0 if the library unload call returned successful. - * Returns -1 if the library was not loaded according to the given management data structure. - * Returns -2 if the library unload call returned unsuccessful. - * Returns -4 if the given Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library pointer was NULL. + * Returns COMMON_ERROR_SUCCESS if the library unload call returned successful. + * Returns DYNLIB_ERROR_LIBRARY_NOT_LOADED if the library was not loaded according to the given management data structure. + * Returns Translated host system error code if the library unload call returned unsuccessful. + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library pointer was NULL. */ MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib); /*! - * void * Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, - * const char * symbolName) + * int Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, + * const char * symbolName, void ** retSym) * * This function calls the system specific dynamic library handler and attempts to fetch a pointer to the first byte * of the given symbol. * - * Note: Due to the nature of the Dynamic_Library_Subsystem being a generic wrapper over the system specific calls, - * we cannot return more informative error messages. (Not without a generic error lookup table anyway....) - * * Note: To check for an error result from this function, check the value of lib.bLastCallEncounteredAnError. * If lib.bLastCallEncounteredAnError is false, no error occured during this function. Otherwise an error occured. * @@ -123,13 +114,17 @@ extern "C" { * management data structure used internally. * Pram: const char * symbolName, A pointer to a c-string that contains the name of the requested symbol to * search for. + * Pram: void ** retSym, A double pointer that will hold the address to the first byte of the requested symbol if the function succeeds. + * (retSym will NOT be modified if this function returns an error.) * - * Returns a valid pointer if the resulting lookup returned a valid pointer. (The value of lib.bLastCallEncounteredAnError will be false in this case.) + * Returns COMMON_ERROR_SUCCESS if the resulting lookup returned a valid pointer. (The value of lib.bLastCallEncounteredAnError will be false in this case.) * Returns a NULL pointer if the resulting lookup returned a NULL pointer. (The value of lib.bLastCallEncounteredAnError will be false in this case as well.) - * Returns a NULL pointer if the lookup failed for some reason. (The value of lib.bLastCallEncounteredAnError will be true in this case.) - * Returns a NULL pointer if the given Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library pointer was NULL. + * (Note: Some systems may not support NULL symbols, in this case the result is the same as if the lookup failed, see below.) + * Returns Translated host system error code if the lookup failed for some reason. (The value of lib.bLastCallEncounteredAnError will be true in this case.) + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library, symbolName, or retSym pointer(s) was / were set to NULL. + * Returns DYNLIB_ERROR_LIBRARY_NOT_LOADED if the library was not loaded according to the given management data structure. */ - MSYS_DLL_EXPORT void * Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName); + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName, void ** retSym); #ifdef __cplusplus } /* End of extern C. */ #endif /* __cplusplus */ From 3670d46bfac834aec5a5bb5d546d62de9ff2ce68 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 07:28:38 -0500 Subject: [PATCH 30/50] Compile fix for Dynamic_Library_Subsystem_Windows.c This commit is a compile fix for Dynamic_Library_Subsystem_Windows.c. --- .../Dynamic_Library_Subsystem_Windows.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c index 3b876c0..798d3be 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c @@ -159,7 +159,7 @@ extern "C" { /* Success. */ ret = COMMON_ERROR_SUCCESS; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): <"; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): <"); COMMON_LOG_VERBOSE(lib->pathToLibrary); COMMON_LOG_VERBOSE("> unloaded."); } @@ -191,7 +191,7 @@ extern "C" { else { /* Management structure is invalid. */ - ret = COMMON_ERROR_INVALID_ARGUMENT_ERROR; + ret = COMMON_ERROR_INVALID_ARGUMENT; COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The engine's library structure for the given library is invalid. Unable to unload a library without a valid library structure."); } From e7625fddad22bcaa16834d2ae172b7092e4aedce Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sun, 17 Jan 2016 18:33:02 -0500 Subject: [PATCH 31/50] Fix commenting style in Dynamic_Library_Subsystem_Data_Structures.h. This commit fixes the code commenting style in Dynamic_Library_Subsystem_Data_Structures.h. --- ...ynamic_Library_Subsystem_Data_Structures.h | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h index 1273b4f..9875140 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h @@ -18,24 +18,25 @@ https://github.com/codebase7/mengine */ -// Include guard. +/* Include guard. */ #ifndef MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_H #error "You must include the Dynamic_Library_Subsystem.h header file. It will include all of the other needed headers." #else #ifndef MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_DATA_STRUCTURES_H #define MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_DATA_STRUCTURES_H -// Define the supported API level. +/* Define the supported API level. */ #define MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_LEVEL 0 +/* Check for C++ Compiler. */ #ifdef __cplusplus -// Define extern C. +/* Define extern C. */ extern "C" { -#endif - // Define Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library. +#endif /* __cplusplus */ + /* Define Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library. */ struct Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library { - bool bIsLoaded; // Whether or not this library is loaded. + bool bIsLoaded; /* Whether or not this library is loaded. */ bool bLastCallEncounteredAnError; /* Whether or not this library has encountered an error in the last call. (Should reset to false when a new call is made and no error occurs.) @@ -46,9 +47,10 @@ extern "C" { (For example, Windows defines loaded libraries with an HMODULE pointer, which is needed to unload the library or to search it.) */ - const char * pathToLibrary; // Path to the dynamic library file on disk. + const char * pathToLibrary; /* Path to the dynamic library file on disk. */ }; + /* Create user defined data type for Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library structure. */ typedef struct Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library; /*! @@ -79,10 +81,10 @@ extern "C" { */ MSYS_DLL_EXPORT void Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib); #ifdef __cplusplus -} // End of extern C. -#endif +} /* End of extern C. */ +#endif /* __cplusplus */ -#endif -#endif +#endif /* MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_DATA_STRUCTURES_H */ +#endif /* MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_H */ -// End of Dynamic_Library_Subsystem_Data_Structures.h +/* End of Dynamic_Library_Subsystem_Data_Structures.h */ From bf0e2457023fd326a39a0cd6e71afb96cd2ef91f Mon Sep 17 00:00:00 2001 From: codebase7 Date: Fri, 22 Jan 2016 03:02:06 -0500 Subject: [PATCH 32/50] Add Dynamic_Library_Subsystem_Data_Structures_Private_API.c This commit adds Dynamic_Library_Subsystem_Data_Structures_Private_API.c to the Dynamic Library Subsystem. --- .../Dynamic_Library_Subsystem/CMakeLists.txt | 8 +- ...ry_Subsystem_Data_Structures_Private_API.c | 330 ++++++++++++++++++ ...ry_Subsystem_Data_Structures_Private_API.h | 249 +++++++++++++ 3 files changed, 585 insertions(+), 2 deletions(-) create mode 100644 src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c create mode 100644 src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h diff --git a/src/Common/Src/Dynamic_Library_Subsystem/CMakeLists.txt b/src/Common/Src/Dynamic_Library_Subsystem/CMakeLists.txt index 56eb522..7f1299c 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/CMakeLists.txt +++ b/src/Common/Src/Dynamic_Library_Subsystem/CMakeLists.txt @@ -7,12 +7,14 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${O_OUTPUT_DIR}) IF(${CMAKE_SYSTEM_NAME} MATCHES "Windows") # Building on windows. set(DYNAMIC_LIBRARY_SUBSYSTEM_INCLUDES Dynamic_Library_Subsystem_Data_Structures.c + Dynamic_Library_Subsystem_Data_Structures_Private_API.c Dynamic_Library_Subsystem_Windows.c) unset(DYNAMIC_LIBRARY_SUBSYSTEM_LINK_LIBS) ELSEIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux") # Building on Linux. set(DYNAMIC_LIBRARY_SUBSYSTEM_INCLUDES Dynamic_Library_Subsystem_Data_Structures.c + Dynamic_Library_Subsystem_Data_Structures_Private_API.c Dynamic_Library_Subsystem_POSIX.c) set(DYNAMIC_LIBRARY_SUBSYSTEM_LINK_LIBS dl) @@ -25,8 +27,10 @@ ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Windows") # Actually build it. add_library(Dynamic_Library_Subsystem_Multiverse_Engine_Static STATIC ${DYNAMIC_LIBRARY_SUBSYSTEM_INCLUDES}) target_link_libraries(Dynamic_Library_Subsystem_Multiverse_Engine_Static ${DYNAMIC_LIBRARY_SUBSYSTEM_LINK_LIBS} -Common_Error_Handler_Multiverse_Engine_Static) +Common_Error_Handler_Multiverse_Engine_Static +Core_Multiverse_Engine_Static) add_library(Dynamic_Library_Subsystem_Multiverse_Engine SHARED ${DYNAMIC_LIBRARY_SUBSYSTEM_INCLUDES}) target_link_libraries(Dynamic_Library_Subsystem_Multiverse_Engine ${DYNAMIC_LIBRARY_SUBSYSTEM_LINK_LIBS} -Common_Error_Handler_Multiverse_Engine) +Common_Error_Handler_Multiverse_Engine +Core_Multiverse_Engine) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c new file mode 100644 index 0000000..f576119 --- /dev/null +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c @@ -0,0 +1,330 @@ +/*! + Multiverse Engine Project 22/1/2016 Dynamic_Library_Subsystem Dynamic_Library_Subsystem_Data_Structures_Private_API.c + + Copyright (C) 2016 Multiverse Engine Project + + This program is free software; + you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; + either version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with this program; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Official source repository and project information can be found at + https://github.com/codebase7/mengine +*/ + +/* Internal includes */ +#include "Dynamic_Library_Subsystem.h" +#include "Dynamic_Library_Subsystem_Data_Structures_Private_API.h" +#include "../../../Core/Src/DataProcess.h" + +/* Check for C++ Compiler. */ +#ifdef __cplusplus +/* Define extern C. */ +extern "C" { +#endif /* __cplusplus */ + int Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private ** lib) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; + Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * result = NULL; + + /* Check for valid arguments. */ + if (lib != NULL) + { + /* Create the referece. */ + retFromCall = DataProcess_Reallocate_C_String(((char**)(&result)), 0, sizeof(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)); + + /* Check for a valid result. */ + if ((retFromCall == COMMON_ERROR_SUCCESS) && (result != NULL)) + { + /* Initilize the vars. */ + result->pathToLibrary = NULL; /* Avoid potential non-NULL causing a memory deallocation attempt for a pointer that is not allocated. */ + Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library_Private(result); + + /* Copy the pointer to lib. */ + (*lib) = result; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + } + else + { + /* Could not init structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + } + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Return result. */ + return ret; + } + + void Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private ** lib) + { + /* Check for valid pointer. */ + if ((lib != NULL) && ((*lib) != NULL)) + { + /* Free the structure. */ + DataProcess_Deallocate_CString(((char **)(lib))); + } + + /* Exit function. */ + return; + } + + void Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib) + { + /* Check for valid pointer. */ + if (lib != NULL) + { + /* Clear the data. */ + result->bIsLoaded = false; + result->bLastCallEncounteredAnError = false; + result->osSpecificPointerData = NULL; + + /* Check for set pathToLibrary. */ + if (result->pathToLibrary != NULL) + { + /* Deallocate memory for the pathToLibrary variable. */ + DataProcess_Deallocate_CString(&(result->pathToLibrary)); + } + } + + /* Exit function. */ + return; + } + + int Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + + /* Check for valid pointer. */ + if (lib != NULL) + { + /* Check bIsLoaded. */ + ret = ((lib->bIsLoaded) ? (COMMON_ERROR_TRUE) : (COMMON_ERROR_FALSE)); + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const bool value) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + + /* Check for valid pointer. */ + if (lib != NULL) + { + /* Set bIsLoaded. */ + lib->bIsLoaded = value; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Get_LastCallEncounteredError_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + + /* Check for valid pointer. */ + if (lib != NULL) + { + /* Check bLastCallEncounteredError. */ + ret = ((lib->bLastCallEncounteredError) ? (COMMON_ERROR_TRUE) : (COMMON_ERROR_FALSE)); + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const bool value) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + + /* Check for valid pointer. */ + if (lib != NULL) + { + /* Set bLastCallEncounteredError. */ + lib->bLastCallEncounteredError = value; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, void ** retVar) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + + /* Check for valid pointer. */ + if ((lib != NULL) && (retVar != NULL)) + { + /* Get the osSpecificPointerData and copy it to retVar. */ + (*retVar) = lib->osSpecificPointerData; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const void * value) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + + /* Check for valid pointer. */ + if (lib != NULL) + { + /* Copy the pointer value to osSpecificPointerData. */ + lib->osSpecificPointerData = value; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const char ** retVar) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + + /* Check for valid pointer. */ + if ((lib != NULL) && (retVar != NULL)) + { + /* Get the pathToLibrary pointer and copy it to retVar. */ + (*retVar) = lib->pathToLibrary; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const char * value, const size_t valueLength) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; + char * tempPath = NULL; + + /* Check for valid pointer. */ + if ((lib != NULL) && (((value == NULL) && (valueLength == 0)) || ((value != NULL) && (valueLength > 0)))) + { + /* OK, allocate memory. If needed. */ + if ((value != NULL) && (valueLength > 0)) + { + /* Allocate memory. */ + retFromCall = DataProcess_Reallocate_CString(tempPath, 0, valueLength); + if ((retFromCall == COMMON_ERROR_SUCCESS) && (tempPath != NULL)) + { + /* Copy the path string. */ + memcpy(tempPath, value, valueLength); + } + else + { + /* Could not allocate memory for path string. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + } + } + + /* Check for unknown error. */ + if (ret == COMMON_ERROR_UNKNOWN_ERROR) + { + /* Check for allocated pathToLibrary. */ + if (lib->pathToLibrary != NULL) + { + /* Deallocate the path string. */ + DataProcess_Deallocate_CString(&(lib->pathToLibrary)); + } + + /* Check and see if we need to copy the tempPath pointer. */ + if (tempPath != NULL) + { + /* Copy the tempPath pointer to the structure. */ + lib->pathToLibrary = tempPath; + } + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + } + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + +#ifdef __cplusplus +} /* End of extern C. */ +#endif /* __cplusplus */ diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h new file mode 100644 index 0000000..84d572d --- /dev/null +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h @@ -0,0 +1,249 @@ +/*! + Multiverse Engine Project 17/1/2016 Dynamic_Library_Subsystem Dynamic_Library_Subsystem_Data_Structures_Private_API.h + + Copyright (C) 2016 Multiverse Engine Project + + This program is free software; + you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; + either version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with this program; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Official source repository and project information can be found at + https://github.com/codebase7/mengine +*/ + +/* Include guard. */ +#ifndef MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_H +#error "You must include the Dynamic_Library_Subsystem.h header file. It will include all of the other needed headers." +#else +#ifndef MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_DATA_STRUCTURES_PRIVATE_API_H +#define MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_DATA_STRUCTURES_PRIVATE_API_H + +#error "FIXME: Abstraction required for Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library data structure." + +/* Check for C++ Compiler. */ +#ifdef __cplusplus +/* Define extern C. */ +extern "C" { +#endif /* __cplusplus */ + /*! + struct Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private + + This structure is an internal engine structure used to manage dynamicly + loaded libraries that were loaded at runtime via the Dynamic Library Subsystem. + + This structure should NOT be accessed directly as it is subject to change without + warning. You should use the Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library + wrapper instead. + */ + struct Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private + { + bool bIsLoaded; /* Whether or not this library is loaded. */ + bool bLastCallEncounteredAnError; /* + Whether or not this library has encountered an error in the last call. + (Should reset to false when a new call is made and no error occurs.) + */ + + void * osSpecificPointerData; /* + Pointer to an OS specific data structure. + (For example, Windows defines loaded libraries with an HMODULE pointer, + which is needed to unload the library or to search it.) + */ + const char * pathToLibrary; /* Path to the dynamic library file on disk. */ + }; + + /* Create user defined data type for Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private structure. */ + typedef struct Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private; + + /*! + * int Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private ** lib) + * + * Creates a Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private data structure in a neutral state. + * + * This function should be called when constructing a Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private object. + * As it will correctly setup said object. + * + * WARNING: The caller is responsible for calling + * Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library_Private() on the successfully created structure. + * + * WARNING: This function will NOT deallocate a preexisting structure, and will overwrite the given pointer if it is + * non-NULL if this function succeeds. Therefore if you need to keep the pointer, copy it elsewhere before calling this + * function. + * + * Returns COMMON_ERROR_SUCCESS if successful. (The created structure will be pointed to by lib.) + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given double pointer is NULL. + * Returns COMMON_ERROR_MEMORY_ERROR if the data structure could not be allocated. + * Otherwise returns the appropriate error code. + * + * No-alteration clause: + * - In case of error, this function will not modify it's arguments. + */ + int Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private ** lib); + + /*! + * void Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private ** lib) + * + * Destroys (frees) a created Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private data structure and set's it's pointer to NULL. + * + * Pram: Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private ** lib, a double pointer to + * a successfully created Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private data structure. + * + * This function has no return. + */ + void Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private ** lib); + + /*! + * void Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib) + * + * Blanks the given Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private data structure's members. (Deallocating memory for them if needed.) + * + * If given an invalid pointer this function will silently fail. + * + * This function has no return. + */ + void Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib); + + /*! + * int Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib) + * + * ACCESSOR FUNCTION. + * + * Returns the status of the bIsLoaded member variable. + * + * Returns COMMON_ERROR_TRUE if the library is loaded. + * Returns COMMON_ERROR_FALSE if the library is NOT loaded. + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. + * Otherwise returns the appropriate error code. + */ + int Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib); + + /*! + * int Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const bool value) + * + * ACCESSOR FUNCTION. + * + * Sets the status of the bIsLoaded member variable to the given value. + * + * Returns COMMON_ERROR_SUCCESS if the status was set to the given value successfully. + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. + * Otherwise returns the appropriate error code. + */ + int Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const bool value); + + /*! + * int Common_Dynamic_Library_Subsystem_Get_LastCallEncounteredError_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib) + * + * ACCESSOR FUNCTION. + * + * Returns the status of the bLastCallEncounteredAnError member variable. + * + * Returns COMMON_ERROR_TRUE if the last operation on this library encountered an error. + * Returns COMMON_ERROR_FALSE if the last operation on this library did NOT encounter an error. + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. + * Otherwise returns the appropriate error code. + */ + int Common_Dynamic_Library_Subsystem_Get_LastCallEncounteredError_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib); + + /*! + * int Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const bool value) + * + * ACCESSOR FUNCTION. + * + * Sets the status of the bLastCallEncounteredAnError member variable to the given value. + * + * Returns COMMON_ERROR_SUCCESS if the status was set to the given value successfully. + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. + * Otherwise returns the appropriate error code. + */ + int Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const bool value); + + /*! + * int Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, void ** retVar) + * + * ACCESSOR FUNCTION. + * + * Returns the pointer for the osSpecificPointerData member variable. + * + * WARNING: This function will NOT deallocate a preexisting structure, and will overwrite the given pointer if it is + * non-NULL if this function succeeds. Therefore if you need to keep the pointer, copy it elsewhere before calling this + * function. + * + * WARNING: The caller should NOT deallocate the returned pointer. + * + * Returns COMMON_ERROR_SUCCESS if the data was returned successfully. (retVar will point to the pointer value in this case.) + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. + * Otherwise returns the appropriate error code. + */ + int Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, void ** retVar); + + /*! + * int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const void * value) + * + * ACCESSOR FUNCTION. + * + * Sets the pointer for the osSpecificPointerData member variable to the given value. + * + * Returns COMMON_ERROR_SUCCESS if the status was set to the given value successfully. + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. + * Otherwise returns the appropriate error code. + */ + int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const void * value); + + /*! + * int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const char ** retVar) + * + * ACCESSOR FUNCTION. + * + * Returns the pointer for the pathToLibrary member variable. + * + * WARNING: This function will NOT deallocate a preexisting structure, and will overwrite the given pointer if it is + * non-NULL if this function succeeds. Therefore if you need to keep the pointer, copy it elsewhere before calling this + * function. + * + * WARNING: The caller should NOT deallocate the returned pointer. + * + * Returns COMMON_ERROR_SUCCESS if the data was returned successfully. (retVar will point to the pointer value in this case.) + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. + * Otherwise returns the appropriate error code. + */ + int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const char ** retVar); + + /*! + * int Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const char * value, const size_t valueLength) + * + * ACCESSOR FUNCTION. + * + * Sets the pointer for the pathToLibrary member variable to the given value. + * + * Note this function serves two purposes, to allocate and copy the needed path string into the structure, + * and to deallocate a pre-existing path string in the structure. The mode that is chosen is dependant on the + * arguments given to the function. + * + * I.e. If value is non-NULL and valueLength is greater than zero, then the given path string will be copied. + * If value is NULL, and valueLength is equal to zero, then any pre-existing path string in the structure will be + * deallocated. Any other combination will generate a COMMON_ERROR_INVALID_ARGUMENT error code and the structure + * will not be modified. + * + * Returns COMMON_ERROR_SUCCESS if the status was set to the given value successfully. + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointers or length is NULL. + * Otherwise returns the appropriate error code. + * + * No-alteration clause: + * - In case of error, this function will not modify it's arguments. + */ + int Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const char * value, const size_t valueLength); + +#ifdef __cplusplus +} /* End of extern C. */ +#endif /* __cplusplus */ + +#endif /* MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_DATA_STRUCTURES_PRIVATE_API_H */ +#endif /* MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_H */ + +/* End of Dynamic_Library_Subsystem_Data_Structures.h */ From e7f67be1f127a20f89fab9eae9e9b383a545f2c1 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Fri, 22 Jan 2016 03:32:38 -0500 Subject: [PATCH 33/50] Update Dynamic_Library_Subsystem_Data_Structures to use Dynamic_Library_Subsystem_Data_Structures_Private_API. This commit makes Dynamic_Library_Subsystem_Data_Structures use the Dynamic_Library_Subsystem_Data_Structures_Private_API code for hiding the implementation details for Dynamic_Library_Subsystem_Data_Structures. --- ...ynamic_Library_Subsystem_Data_Structures.c | 257 ++++++++++++++++-- ...ynamic_Library_Subsystem_Data_Structures.h | 189 +++++++++++-- 2 files changed, 401 insertions(+), 45 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c index 7b1dc8c..57cbd36 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c @@ -18,44 +18,257 @@ https://github.com/codebase7/mengine */ +/* Internal includes */ #include "Dynamic_Library_Subsystem.h" +#include "Dynamic_Library_Subsystem_Data_Structures_Private_API.h" +#include "../../../Core/Src/DataProcess.h" +/* Check for C++ Compiler. */ #ifdef __cplusplus -// Define extern C. +/* Define extern C. */ extern "C" { -#endif - Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library() +#endif /* __cplusplus */ + int Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library ** lib) { - // Create the referece. + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * result = NULL; - result = ((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library*)(malloc(sizeof(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library)))); - // Check for a valid result. - if (result != NULL) + /* Check for valid arguments. */ + if (lib != NULL) + { + /* Create the referece. */ + retFromCall = DataProcess_Reallocate_C_String(((char**)(&result)), 0, sizeof(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library)); + + /* Check for a valid result. */ + if ((retFromCall == COMMON_ERROR_SUCCESS) && (result != NULL)) + { + /* Set the pointer to NULL. */ + result->pointer = NULL; + + /* Call the real function. */ + ret = Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)(&(result->pointer)))); + } + else + { + /* Could not init structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + } + } + else { - // Initilize the vars. - result->bIsLoaded = false; - result->bLastCallEncounteredAnError = false; - result->osSpecificPointerData = NULL; - result->pathToLibrary = NULL; + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; } - // Return result. - return result; + /* Return result. */ + return ret; } - void Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib) + void Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library ** lib) { - // Check for valid pointer. - if (lib != NULL) + /* Check for valid pointer. */ + if ((lib != NULL) && ((*lib) != NULL)) + { + /* Check for allocated pointer. */ + if ((*lib)->pointer != NULL) + { + /* Deallocate the pointer. */ + Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)(&((*lib)->pointer)))); + } + + /* Free the structure. */ + DataProcess_Deallocate_CString(((char **)(lib))); + } + + /* Exit function. */ + return; + } + + void Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib) + { + /* Check for valid pointers. */ + if ((lib != NULL) && (lib->pointer != NULL)) { - // Free the structure. - free(lib); + /* Call real function. */ + Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer))); } - // Exit function. + /* Exit function. */ return; } + + int Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + + /* Check for valid pointers. */ + if ((lib != NULL) && (lib->pointer != NULL)) + { + /* Call real function. */ + ret = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer))); + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const bool value) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + + /* Check for valid pointers. */ + if ((lib != NULL) && (lib->pointer != NULL)) + { + /* Call real function. */ + ret = Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer)), value); + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Get_LastCallEncounteredError_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + + /* Check for valid pointers. */ + if ((lib != NULL) && (lib->pointer != NULL)) + { + /* Call real function. */ + ret = Common_Dynamic_Library_Subsystem_Get_LastCallEncounteredError_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer))); + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const bool value) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + + /* Check for valid pointers. */ + if ((lib != NULL) && (lib->pointer != NULL)) + { + /* Call real function. */ + ret = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer)), value); + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, void ** retVar) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + + /* Check for valid pointers. */ + if ((lib != NULL) && (lib->pointer != NULL) && (retVar != NULL)) + { + /* Call real function. */ + ret = Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer)), retVar); + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const void * value) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + + /* Check for valid pointers. */ + if ((lib != NULL) && (lib->pointer != NULL)) + { + /* Call real function. */ + ret = Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer)), value); + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const char ** retVar) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + + /* Check for valid pointers. */ + if ((lib != NULL) && (lib->pointer != NULL) && (retVar != NULL)) + { + /* Call real function. */ + ret = Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer)), retVar); + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const char * value, const size_t valueLength) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; + char * tempPath = NULL; + + /* Check for valid pointers. */ + if ((lib != NULL) && (lib->pointer != NULL) && (((value == NULL) && (valueLength == 0)) || ((value != NULL) && (valueLength > 0)))) + { + /* Call real function. */ + ret = Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer)), value, valueLength); + } + else + { + /* Invalid lib pointer. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + } + + /* Exit function. */ + return ret; + } + #ifdef __cplusplus -} // End of extern C. -#endif +} /* End of extern C. */ +#endif /* __cplusplus */ diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h index 9875140..19e9e5e 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h @@ -28,6 +28,9 @@ /* Define the supported API level. */ #define MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_LEVEL 0 +/* Internal includes. */ +#include "../../../DLL_PORT.h" + /* Check for C++ Compiler. */ #ifdef __cplusplus /* Define extern C. */ @@ -36,50 +39,190 @@ extern "C" { /* Define Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library. */ struct Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library { - bool bIsLoaded; /* Whether or not this library is loaded. */ - bool bLastCallEncounteredAnError; /* - Whether or not this library has encountered an error in the last call. - (Should reset to false when a new call is made and no error occurs.) - */ - - void * osSpecificPointerData; /* - Pointer to an OS specific data structure. - (For example, Windows defines loaded libraries with an HMODULE pointer, - which is needed to unload the library or to search it.) - */ - const char * pathToLibrary; /* Path to the dynamic library file on disk. */ + void * pointer; }; /* Create user defined data type for Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library structure. */ typedef struct Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library; /*! - * Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library() + * int Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library ** lib) * * Creates a Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library data structure in a neutral state. * * This function should be called when constructing a Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library object. * As it will correctly setup said object. * - * WARNING: The caller is responsible for calling free() or - * Destroy_Loaded_Dynamic_Library() on the successfully created structure. + * WARNING: The caller is responsible for calling + * Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library() on the successfully created structure. + * + * WARNING: This function will NOT deallocate a preexisting structure, and will overwrite the given pointer if it is + * non-NULL if this function succeeds. Therefore if you need to keep the pointer, copy it elsewhere before calling this + * function. + * + * Returns COMMON_ERROR_SUCCESS if successful. (The created structure will be pointed to by lib.) + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given double pointer is NULL. + * Returns COMMON_ERROR_MEMORY_ERROR if the data structure could not be allocated. + * Otherwise returns the appropriate error code. * - * Returns a pointer to a Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library data structure if successful. - * Otherwise returns NULL. + * No-alteration clause: + * - In case of error, this function will not modify it's arguments. */ - MSYS_DLL_EXPORT Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library(); + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library ** lib); /*! - * void Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib) + * void Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library ** lib) * - * Destroys (frees) a created Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library data structure. + * Destroys (frees) a created Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library data structure and set's it's pointer to NULL. * - * Pram: Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, a pointer to - * a successfully created Loaded_Dynamic_Library data structure. + * Pram: Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library ** lib, a double pointer to + * a successfully created Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library data structure. * * This function has no return. */ - MSYS_DLL_EXPORT void Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib); + MSYS_DLL_EXPORT void Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library ** lib); + + /*! + * void Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib) + * + * Blanks the given Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library data structure's members. (Deallocating memory for them if needed.) + * + * If given an invalid pointer this function will silently fail. + * + * This function has no return. + */ + MSYS_DLL_EXPORT void Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib); + + /*! + * int Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib) + * + * ACCESSOR FUNCTION. + * + * Returns the status of the bIsLoaded member variable. + * + * Returns COMMON_ERROR_TRUE if the library is loaded. + * Returns COMMON_ERROR_FALSE if the library is NOT loaded. + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. + * Otherwise returns the appropriate error code. + */ + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib); + + /*! + * int Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const bool value) + * + * ACCESSOR FUNCTION. + * + * Sets the status of the bIsLoaded member variable to the given value. + * + * Returns COMMON_ERROR_SUCCESS if the status was set to the given value successfully. + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. + * Otherwise returns the appropriate error code. + */ + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const bool value); + + /*! + * int Common_Dynamic_Library_Subsystem_Get_LastCallEncounteredError_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib) + * + * ACCESSOR FUNCTION. + * + * Returns the status of the bLastCallEncounteredAnError member variable. + * + * Returns COMMON_ERROR_TRUE if the last operation on this library encountered an error. + * Returns COMMON_ERROR_FALSE if the last operation on this library did NOT encounter an error. + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. + * Otherwise returns the appropriate error code. + */ + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Get_LastCallEncounteredError_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib); + + /*! + * int Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const bool value) + * + * ACCESSOR FUNCTION. + * + * Sets the status of the bLastCallEncounteredAnError member variable to the given value. + * + * Returns COMMON_ERROR_SUCCESS if the status was set to the given value successfully. + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. + * Otherwise returns the appropriate error code. + */ + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const bool value); + + /*! + * int Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, void ** retVar) + * + * ACCESSOR FUNCTION. + * + * Returns the pointer for the osSpecificPointerData member variable. + * + * WARNING: This function will NOT deallocate a preexisting structure, and will overwrite the given pointer if it is + * non-NULL if this function succeeds. Therefore if you need to keep the pointer, copy it elsewhere before calling this + * function. + * + * WARNING: The caller should NOT deallocate the returned pointer. + * + * Returns COMMON_ERROR_SUCCESS if the data was returned successfully. (retVar will point to the pointer value in this case.) + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. + * Otherwise returns the appropriate error code. + */ + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, void ** retVar); + + /*! + * int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const void * value) + * + * ACCESSOR FUNCTION. + * + * Sets the pointer for the osSpecificPointerData member variable to the given value. + * + * Returns COMMON_ERROR_SUCCESS if the status was set to the given value successfully. + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. + * Otherwise returns the appropriate error code. + */ + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const void * value); + + /*! + * int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const char ** retVar) + * + * ACCESSOR FUNCTION. + * + * Returns the pointer for the pathToLibrary member variable. + * + * WARNING: This function will NOT deallocate a preexisting structure, and will overwrite the given pointer if it is + * non-NULL if this function succeeds. Therefore if you need to keep the pointer, copy it elsewhere before calling this + * function. + * + * WARNING: The caller should NOT deallocate the returned pointer. + * + * Returns COMMON_ERROR_SUCCESS if the data was returned successfully. (retVar will point to the pointer value in this case.) + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. + * Otherwise returns the appropriate error code. + */ + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const char ** retVar); + + /*! + * int Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const char * value, const size_t valueLength) + * + * ACCESSOR FUNCTION. + * + * Sets the pointer for the pathToLibrary member variable to the given value. + * + * Note this function serves two purposes, to allocate and copy the needed path string into the structure, + * and to deallocate a pre-existing path string in the structure. The mode that is chosen is dependant on the + * arguments given to the function. + * + * I.e. If value is non-NULL and valueLength is greater than zero, then the given path string will be copied. + * If value is NULL, and valueLength is equal to zero, then any pre-existing path string in the structure will be + * deallocated. Any other combination will generate a COMMON_ERROR_INVALID_ARGUMENT error code and the structure + * will not be modified. + * + * Returns COMMON_ERROR_SUCCESS if the status was set to the given value successfully. + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointers or length is NULL. + * Otherwise returns the appropriate error code. + * + * No-alteration clause: + * - In case of error, this function will not modify it's arguments. + */ + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const char * value, const size_t valueLength); + #ifdef __cplusplus } /* End of extern C. */ #endif /* __cplusplus */ From b434413b98ad05f6245f75330dca0b9f0cfd1f14 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Fri, 22 Jan 2016 03:35:49 -0500 Subject: [PATCH 34/50] Add Common_Dynamic_Library_Subsystem_Get_API_Version() This commit adds the Common_Dynamic_Library_Subsystem_Get_API_Version() function to the Dynamic Library Subsystem. --- .../Dynamic_Library_Subsystem_Data_Structures.c | 6 ++++++ .../Dynamic_Library_Subsystem_Data_Structures.h | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c index 57cbd36..654a468 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c @@ -28,6 +28,12 @@ /* Define extern C. */ extern "C" { #endif /* __cplusplus */ + int Common_Dynamic_Library_Subsystem_Get_API_Version() + { + /* Return the API Version number. */ + return MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_LEVEL; + } + int Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library ** lib) { /* Init vars. */ diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h index 19e9e5e..5425546 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h @@ -45,6 +45,13 @@ extern "C" { /* Create user defined data type for Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library structure. */ typedef struct Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library; + /*! + * int Common_Dynamic_Library_Subsystem_Get_API_Version() + * + * Returns the API version for the Dynamic Library Subsystem. + */ + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Get_API_Version(); + /*! * int Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library ** lib) * From b7bf161257bc5c18f810bc9a11b746f168c0d52b Mon Sep 17 00:00:00 2001 From: codebase7 Date: Fri, 22 Jan 2016 03:44:47 -0500 Subject: [PATCH 35/50] Delete error preprocessor macro from Dynamic_Library_Subsystem_Data_Structures_Private_API.h This commit removes a bad error preprocessor macro that I was using as a note to implement the private API code in Dynamic_Library_Subsystem_Data_Structures_Private_API.h. (Yes, it's a personal reminder. So I guess you get to see some of my internal coding practices. :P ) --- .../Dynamic_Library_Subsystem_Data_Structures_Private_API.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h index 84d572d..59b289f 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h @@ -25,8 +25,6 @@ #ifndef MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_DATA_STRUCTURES_PRIVATE_API_H #define MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_DATA_STRUCTURES_PRIVATE_API_H -#error "FIXME: Abstraction required for Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library data structure." - /* Check for C++ Compiler. */ #ifdef __cplusplus /* Define extern C. */ From e22caac005d055dd1468fa83a37414a455a5d75a Mon Sep 17 00:00:00 2001 From: codebase7 Date: Fri, 22 Jan 2016 03:53:58 -0500 Subject: [PATCH 36/50] Add missing includes for error codes to Dynamic Library Subsystem. This commit adds the missing header for the Common namespace error codes to the Dynamic Library Subsystem Structures* source files. --- .../Dynamic_Library_Subsystem_Data_Structures.c | 1 + .../Dynamic_Library_Subsystem_Data_Structures_Private_API.c | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c index 654a468..7da67af 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c @@ -22,6 +22,7 @@ #include "Dynamic_Library_Subsystem.h" #include "Dynamic_Library_Subsystem_Data_Structures_Private_API.h" #include "../../../Core/Src/DataProcess.h" +#include "../Error_Handler/Common_Error_Handler_Structures.h" /* Check for C++ Compiler. */ #ifdef __cplusplus diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c index f576119..d3cc1e6 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c @@ -22,6 +22,7 @@ #include "Dynamic_Library_Subsystem.h" #include "Dynamic_Library_Subsystem_Data_Structures_Private_API.h" #include "../../../Core/Src/DataProcess.h" +#include "../Error_Handler/Common_Error_Handler_Structures.h" /* Check for C++ Compiler. */ #ifdef __cplusplus From 4c5ac993d7aef8cb716f7813e0ae0265ff70fba7 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Fri, 22 Jan 2016 04:12:21 -0500 Subject: [PATCH 37/50] Compile fixes for Dynamic_Library_Subsystem_Data_Structures This commit is a set of compile fixes for Dynamic_Library_Subsystem_Data_Structures.c Dynamic_Library_Subsystem_Data_Structures_Private_API.c and Dynamic_Library_Subsystem_Data_Structures_Private_API.h. --- ...ynamic_Library_Subsystem_Data_Structures.c | 24 +++++++++---------- ...ry_Subsystem_Data_Structures_Private_API.c | 22 ++++++++--------- ...ry_Subsystem_Data_Structures_Private_API.h | 6 ++--- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c index 7da67af..977158b 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c @@ -55,7 +55,7 @@ extern "C" { result->pointer = NULL; /* Call the real function. */ - ret = Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)(&(result->pointer)))); + ret = Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private **)(&(result->pointer)))); } else { @@ -82,7 +82,7 @@ extern "C" { if ((*lib)->pointer != NULL) { /* Deallocate the pointer. */ - Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)(&((*lib)->pointer)))); + Common_Dynamic_Library_Subsystem_Destroy_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private **)(&((*lib)->pointer)))); } /* Free the structure. */ @@ -99,7 +99,7 @@ extern "C" { if ((lib != NULL) && (lib->pointer != NULL)) { /* Call real function. */ - Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer))); + Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer))); } /* Exit function. */ @@ -115,7 +115,7 @@ extern "C" { if ((lib != NULL) && (lib->pointer != NULL)) { /* Call real function. */ - ret = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer))); + ret = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer))); } else { @@ -136,7 +136,7 @@ extern "C" { if ((lib != NULL) && (lib->pointer != NULL)) { /* Call real function. */ - ret = Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer)), value); + ret = Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer)), value); } else { @@ -157,7 +157,7 @@ extern "C" { if ((lib != NULL) && (lib->pointer != NULL)) { /* Call real function. */ - ret = Common_Dynamic_Library_Subsystem_Get_LastCallEncounteredError_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer))); + ret = Common_Dynamic_Library_Subsystem_Get_LastCallEncounteredError_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer))); } else { @@ -178,7 +178,7 @@ extern "C" { if ((lib != NULL) && (lib->pointer != NULL)) { /* Call real function. */ - ret = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer)), value); + ret = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer)), value); } else { @@ -199,7 +199,7 @@ extern "C" { if ((lib != NULL) && (lib->pointer != NULL) && (retVar != NULL)) { /* Call real function. */ - ret = Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer)), retVar); + ret = Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer)), retVar); } else { @@ -211,7 +211,7 @@ extern "C" { return ret; } - int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const void * value) + int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, void * value) { /* Init vars. */ int ret = COMMON_ERROR_UNKNOWN_ERROR; @@ -220,7 +220,7 @@ extern "C" { if ((lib != NULL) && (lib->pointer != NULL)) { /* Call real function. */ - ret = Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer)), value); + ret = Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer)), value); } else { @@ -241,7 +241,7 @@ extern "C" { if ((lib != NULL) && (lib->pointer != NULL) && (retVar != NULL)) { /* Call real function. */ - ret = Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer)), retVar); + ret = Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer)), retVar); } else { @@ -264,7 +264,7 @@ extern "C" { if ((lib != NULL) && (lib->pointer != NULL) && (((value == NULL) && (valueLength == 0)) || ((value != NULL) && (valueLength > 0)))) { /* Call real function. */ - ret = Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private)((*lib)->pointer)), value, valueLength); + ret = Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer)), value, valueLength); } else { diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c index d3cc1e6..7600466 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c @@ -90,15 +90,15 @@ extern "C" { if (lib != NULL) { /* Clear the data. */ - result->bIsLoaded = false; - result->bLastCallEncounteredAnError = false; - result->osSpecificPointerData = NULL; + lib->bIsLoaded = false; + lib->bLastCallEncounteredAnError = false; + lib->osSpecificPointerData = NULL; /* Check for set pathToLibrary. */ - if (result->pathToLibrary != NULL) + if (lib->pathToLibrary != NULL) { /* Deallocate memory for the pathToLibrary variable. */ - DataProcess_Deallocate_CString(&(result->pathToLibrary)); + DataProcess_Deallocate_CString(&(lib->pathToLibrary)); } } @@ -159,8 +159,8 @@ extern "C" { /* Check for valid pointer. */ if (lib != NULL) { - /* Check bLastCallEncounteredError. */ - ret = ((lib->bLastCallEncounteredError) ? (COMMON_ERROR_TRUE) : (COMMON_ERROR_FALSE)); + /* Check bLastCallEncounteredAnError. */ + ret = ((lib->bLastCallEncounteredAnError) ? (COMMON_ERROR_TRUE) : (COMMON_ERROR_FALSE)); } else { @@ -180,8 +180,8 @@ extern "C" { /* Check for valid pointer. */ if (lib != NULL) { - /* Set bLastCallEncounteredError. */ - lib->bLastCallEncounteredError = value; + /* Set bLastCallEncounteredAnError. */ + lib->bLastCallEncounteredAnError = value; /* Success. */ ret = COMMON_ERROR_SUCCESS; @@ -220,7 +220,7 @@ extern "C" { return ret; } - int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const void * value) + int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, void * value) { /* Init vars. */ int ret = COMMON_ERROR_UNKNOWN_ERROR; @@ -282,7 +282,7 @@ extern "C" { if ((value != NULL) && (valueLength > 0)) { /* Allocate memory. */ - retFromCall = DataProcess_Reallocate_CString(tempPath, 0, valueLength); + retFromCall = DataProcess_Reallocate_C_String(&tempPath, 0, valueLength); if ((retFromCall == COMMON_ERROR_SUCCESS) && (tempPath != NULL)) { /* Copy the path string. */ diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h index 59b289f..fcb8977 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h @@ -53,7 +53,7 @@ extern "C" { (For example, Windows defines loaded libraries with an HMODULE pointer, which is needed to unload the library or to search it.) */ - const char * pathToLibrary; /* Path to the dynamic library file on disk. */ + char * pathToLibrary; /* Path to the dynamic library file on disk. */ }; /* Create user defined data type for Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private structure. */ @@ -181,7 +181,7 @@ extern "C" { int Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, void ** retVar); /*! - * int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const void * value) + * int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, void * value) * * ACCESSOR FUNCTION. * @@ -191,7 +191,7 @@ extern "C" { * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. * Otherwise returns the appropriate error code. */ - int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const void * value); + int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, void * value); /*! * int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const char ** retVar) From be7f3aa8413132f6075f318e52d3eca992e27c71 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Fri, 22 Jan 2016 21:38:40 -0500 Subject: [PATCH 38/50] Add length argument for pathToLibrary's length to Common_Dynamic_Library_Subsystem_Load_Library(). This commit adds a new length argument for pathToLibrary's length (in bytes) to the Common_Dynamic_Library_Subsystem_Load_Library() function. --- .../Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h index 50e60fa..15be2bf 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h @@ -55,12 +55,13 @@ extern "C" { #endif /*! - * int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const bool reloadLibrary, + * int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const size_t pathToLibraryLength, const bool reloadLibrary, * Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) * * This function calls the system specific dynamic library handler and attempts to load the requested library. * * Pram: const char * pathToLibrary, a pointer to the c-string that contains the path to the library on disk. + * Pram: const size_t pathToLibraryLength, length of the pathToLibrary c-string in bytes. * Pram: const bool reloadLibrary, whether or not to reload the library if it is already loaded. * Pram: Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, A pointer to a properly constructed * management data structure used internally. @@ -72,7 +73,7 @@ extern "C" { * Returns COMMON_ERROR_INVALID_ARGUMENT if the given Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library pointer was NULL. * Returns DYNLIB_ERROR_LIBRARY_ALREADY_LOADED if the library was already loaded. (Only possible if reloadLibrary is false.) (The value of lib.bLastCallEncounteredAnError will be false in this case as well.) */ - MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const bool reloadLibrary, + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const size_t pathToLibraryLength, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib); /*! From e4b7769fce174c5813230065a74b3bc476ebd8f2 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Fri, 22 Jan 2016 21:40:35 -0500 Subject: [PATCH 39/50] Add error logging to Dynamic_Library_Subsystem_Data_Structures.c. This commit adds Common Namespace error logging to Dynamic_Library_Subsystem_Data_Structures.c. --- ...ynamic_Library_Subsystem_Data_Structures.c | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c index 977158b..37a512f 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c @@ -22,6 +22,7 @@ #include "Dynamic_Library_Subsystem.h" #include "Dynamic_Library_Subsystem_Data_Structures_Private_API.h" #include "../../../Core/Src/DataProcess.h" +#include "../Error_Handler/Common_Error_Handler_Internal.h" #include "../Error_Handler/Common_Error_Handler_Structures.h" /* Check for C++ Compiler. */ @@ -61,12 +62,17 @@ extern "C" { { /* Could not init structure. */ ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library(): Memory allocation function returned: "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(retFromCall)); + COMMON_LOG_DEBUG(" Could not allocate memory for management structure."); } } else { /* Invalid lib pointer. */ ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); } /* Return result. */ @@ -116,11 +122,20 @@ extern "C" { { /* Call real function. */ ret = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer))); + if (ret == COMMON_ERROR_INVALID_ARGUMENT) + { + /* That's an error, we didn't set up the private data structure correctly or it's been corrupted. */ + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + COMMON_LOG_DEBUG(" Private API data structure pointer is invalid."); + } } else { /* Invalid lib pointer. */ ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); } /* Exit function. */ @@ -137,11 +152,20 @@ extern "C" { { /* Call real function. */ ret = Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer)), value); + if (ret == COMMON_ERROR_INVALID_ARGUMENT) + { + /* That's an error, we didn't set up the private data structure correctly or it's been corrupted. */ + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + COMMON_LOG_DEBUG(" Private API data structure pointer is invalid."); + } } else { /* Invalid lib pointer. */ ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); } /* Exit function. */ @@ -158,11 +182,20 @@ extern "C" { { /* Call real function. */ ret = Common_Dynamic_Library_Subsystem_Get_LastCallEncounteredError_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer))); + if (ret == COMMON_ERROR_INVALID_ARGUMENT) + { + /* That's an error, we didn't set up the private data structure correctly or it's been corrupted. */ + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Get_LastCallEncounteredError_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + COMMON_LOG_DEBUG(" Private API data structure pointer is invalid."); + } } else { /* Invalid lib pointer. */ ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Get_LastCallEncounteredError_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); } /* Exit function. */ @@ -179,11 +212,20 @@ extern "C" { { /* Call real function. */ ret = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer)), value); + if (ret == COMMON_ERROR_INVALID_ARGUMENT) + { + /* That's an error, we didn't set up the private data structure correctly or it's been corrupted. */ + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + COMMON_LOG_DEBUG(" Private API data structure pointer is invalid."); + } } else { /* Invalid lib pointer. */ ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); } /* Exit function. */ @@ -200,11 +242,20 @@ extern "C" { { /* Call real function. */ ret = Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer)), retVar); + if (ret == COMMON_ERROR_INVALID_ARGUMENT) + { + /* That's an error, we didn't set up the private data structure correctly or it's been corrupted. */ + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + COMMON_LOG_DEBUG(" Private API data structure pointer is invalid."); + } } else { /* Invalid lib pointer. */ ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); } /* Exit function. */ @@ -221,11 +272,20 @@ extern "C" { { /* Call real function. */ ret = Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer)), value); + if (ret == COMMON_ERROR_INVALID_ARGUMENT) + { + /* That's an error, we didn't set up the private data structure correctly or it's been corrupted. */ + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + COMMON_LOG_DEBUG(" Private API data structure pointer is invalid."); + } } else { /* Invalid lib pointer. */ ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); } /* Exit function. */ @@ -242,11 +302,20 @@ extern "C" { { /* Call real function. */ ret = Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer)), retVar); + if (ret == COMMON_ERROR_INVALID_ARGUMENT) + { + /* That's an error, we didn't set up the private data structure correctly or it's been corrupted. */ + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + COMMON_LOG_DEBUG(" Private API data structure pointer is invalid."); + } } else { /* Invalid lib pointer. */ ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); } /* Exit function. */ @@ -265,11 +334,20 @@ extern "C" { { /* Call real function. */ ret = Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer)), value, valueLength); + if (ret == COMMON_ERROR_INVALID_ARGUMENT) + { + /* That's an error, we didn't set up the private data structure correctly or it's been corrupted. */ + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + COMMON_LOG_DEBUG(" Private API data structure pointer is invalid."); + } } else { /* Invalid lib pointer. */ ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library(): "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); } /* Exit function. */ From e63b4663f9c32bcfa868f263cbc04bfde31f5926 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Fri, 22 Jan 2016 21:41:59 -0500 Subject: [PATCH 40/50] Add error logging to Dynamic_Library_Subsystem_Data_Structures_Private_API.c. This commit adds Common Namespace error logging to Dynamic_Library_Subsystem_Data_Structures_Private_API.c. --- ...Dynamic_Library_Subsystem_Data_Structures_Private_API.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c index 7600466..7d08e75 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c @@ -22,6 +22,7 @@ #include "Dynamic_Library_Subsystem.h" #include "Dynamic_Library_Subsystem_Data_Structures_Private_API.h" #include "../../../Core/Src/DataProcess.h" +#include "../Error_Handler/Common_Error_Handler_Internal.h" #include "../Error_Handler/Common_Error_Handler_Structures.h" /* Check for C++ Compiler. */ @@ -59,6 +60,9 @@ extern "C" { { /* Could not init structure. */ ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library_Private(): Memory allocation function returned: "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(retFromCall)); + COMMON_LOG_DEBUG(" Could not allocate memory for management structure."); } } else @@ -292,6 +296,9 @@ extern "C" { { /* Could not allocate memory for path string. */ ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library_Private(): Memory allocation function returned: "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(retFromCall)); + COMMON_LOG_DEBUG(" Could not allocate memory for library path."); } } From 59cafb1a5ba50d8b004f25803cdabf792a46e142 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Fri, 22 Jan 2016 21:46:08 -0500 Subject: [PATCH 41/50] Make Dynamic_Library_Subsystem_Windows.c use the new dynlib management structure code. This commit makes Dynamic_Library_Subsystem_Windows.c use the new API code for interacting with the loaded dynamic library structures, instead of accessing them directly. --- .../Dynamic_Library_Subsystem_Windows.c | 588 ++++++++++++------ 1 file changed, 404 insertions(+), 184 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c index 798d3be..314807f 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c @@ -31,259 +31,479 @@ /* Define extern C. */ extern "C" { #endif /* __cplusplus */ - int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const size_t pathToLibraryLength, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ + HMODULE callResult = NULL; /* The result of the call to LoadLibraryEx(). */ + DWORD retLLEX = 0; /* Error code from LoadLibraryEx(). */ + char * tempPath = NULL; /* Used to load the library. */ + + /* Check to see if the pointer to the management structure is valid. */ + if (lib != NULL) { - /* Init vars. */ - int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ - int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ - HMODULE callResult = NULL; /* The result of the call to LoadLibraryEx(). */ - DWORD retLLEX = 0; /* Error code from LoadLibraryEx(). */ - - /* Check to see if the pointer to the management structure is valid. */ - if (lib != NULL) + /* Check to see if pathToLibrary is NULL. */ + if (pathToLibrary != NULL) + { + /* Check to see if the library is loaded. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(lib); + if ((retFromCall == COMMON_ERROR_FALSE) || (retFromCall == COMMON_ERROR_INVALID_ARGUMENT) || (reloadLibrary)) { - /* Check to see if pathToLibrary is NULL. */ - if (pathToLibrary != NULL) + /* Check and see if the library is loaded. */ + if (retFromCall == COMMON_ERROR_TRUE) + { + /* Call Unload_Library. */ + retFromCall = Common_Dynamic_Library_Subsystem_Unload_Library(lib); + } + else + { + /* Reset retFromCall. */ + retFromCall = COMMON_ERROR_UNKNOWN_ERROR; + } + + /* Only continue if the library was unloaded, or if we did not need to unload the library. */ + if ((retFromCall == COMMON_ERROR_UNKNOWN_ERROR) || (retFromCall == COMMON_ERROR_SUCCESS)) + { + /* Blank the values in lib. */ + Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library(lib); + + /* Copy the path string into lib. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library(lib, pathToLibrary, pathToLibraryLength); + if (retFromCall == COMMON_ERROR_SUCCESS) { - /* Check to see if the library is loaded. */ - if ((!lib->bIsLoaded) || (reloadLibrary)) + /* Get the path back. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &tempPath); + if ((retFromCall == COMMON_ERROR_SUCCESS) && (tempPath != NULL)) + { + /* Call LoadLibraryEx(). */ + callResult = LoadLibraryEx(tempPath, NULL, 0); + + /* Check the callResult. */ + if (callResult == NULL) { - /* Check and see if the library is loaded. */ - if (lib->bIsLoaded) - { - /* Call Unload_Library. */ - retFromCall = Common_Dynamic_Library_Subsystem_Unload_Library(lib); - } - - /* Only continue if the library was unloaded, or if we did not need to unload the library. */ - if ((retFromCall == COMMON_ERROR_UNKNOWN_ERROR) || (retFromCall == COMMON_ERROR_SUCCESS)) + /* Get the last error. */ + retLLEX = GetLastError(); + retFromCall = Common_Translate_Windows_Error_Code_To_Common_Error_Code(retLLEX); + + /* Could not load the library. */ + ret = retFromCall; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Could not load <"); + COMMON_LOG_VERBOSE(tempPath); + COMMON_LOG_VERBOSE("> Host function returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + /* Log additional error. */ + COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + else + { + /* Cast the OS specific data structure pointer to void*. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(lib, ((void*)callResult)); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + /* Set bIsLoaded. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library(lib, true); + if (retFromCall == COMMON_ERROR_SUCCESS) { - /* Set the values in lib. */ - lib->bIsLoaded = false; - lib->bLastCallEncounteredAnError = false; - lib->osSpecificPointerData = NULL; - lib->pathToLibrary = pathToLibrary; - - /* Call LoadLibraryEx(). */ - callResult = LoadLibraryEx(lib->pathToLibrary, NULL, 0); - - /* Check the callResult. */ - if (callResult == NULL) - { - /* Get the last error. */ - retLLEX = GetLastError(); - retFromCall = Common_Translate_Windows_Error_Code_To_Common_Error_Code(retLLEX); - - /* Could not load the library. */ - ret = retFromCall; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Could not load <"); - COMMON_LOG_VERBOSE(lib->pathToLibrary); - COMMON_LOG_VERBOSE("> Host function returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - else - { - /* Cast the OS specific data structure pointer to void*. */ - lib->osSpecificPointerData = (void*)callResult; - - /* Set bIsLoaded. */ - lib->bIsLoaded = true; - - /* Clear lib->bLastCallEncounteredAnError. */ - lib->bLastCallEncounteredAnError = false; - - /* Success. */ - ret = COMMON_ERROR_SUCCESS; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): <"); - COMMON_LOG_VERBOSE(lib->pathToLibrary); - COMMON_LOG_VERBOSE("> loaded."); - } + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): <"); + COMMON_LOG_VERBOSE(tempPath); + COMMON_LOG_VERBOSE("> loaded."); } else { - /* Encountered an error during the unload. */ - ret = retFromCall; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Unable to reload library."); + /* Could not set is loaded flag. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); } + } + else + { + /* Could not set os specific pointer data. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + } } - else - { - /* Library is already loaded. */ - ret = DYNLIB_ERROR_LIBRARY_ALREADY_LOADED; - } + } + else + { + /* Could not retrive path from lib structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + } } else { - /* pathToLibrary is NULL. */ - ret = COMMON_ERROR_INVALID_ARGUMENT; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): No path to the library was given. Unable to load a library without the path to it."); + /* Could not copy path data. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); } + } + else + { + /* Encountered an error during the unload. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Unable to reload library."); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + /* Log additional error. */ + COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } } else { - /* Management structure is invalid. */ - ret = COMMON_ERROR_INVALID_ARGUMENT; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): The engine's library structure for the given library is invalid. Unable to load a library without a valid library structure."); + /* Library is already loaded. */ + ret = DYNLIB_ERROR_LIBRARY_ALREADY_LOADED; } - - /* Exit function. */ - return ret; + } + else + { + /* pathToLibrary is NULL. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): No path to the library was given. Unable to load a library without the path to it."); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + /* Log additional error. */ + COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } } - - int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + else { - /* Init vars. */ - int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ - int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ - DWORD retFL = 0; /* Error code from FreeLibrary(). */ + /* Management structure is invalid. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): The engine's library structure for the given library is invalid. Unable to load a library without a valid library structure."); + } + + /* Exit function. */ + return ret; + } - /* Check to see if the pointer to the management structure is valid. */ - if (lib != NULL) + int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ + DWORD retFL = 0; /* Error code from FreeLibrary(). */ + void * osData = NULL; /* The osSpecificPointerData from the management structure. */ + char * pSym = NULL; /* Used to log the library path to the error log. */ + + /* Check to see if the pointer to the management structure is valid. */ + if (lib != NULL) + { + /* Check for a loaded library. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(lib); + if (retFromCall == COMMON_ERROR_TRUE) + { + /* Reset bLastCallEncounteredAnError. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, false); + if (retFromCall == COMMON_ERROR_SUCCESS) { - /* Reset bLastCallEncounteredAnError. */ - lib->bLastCallEncounteredAnError = false; - - /* Check and see if the OS specific data structure pointer is valid. */ - if ((lib->bIsLoaded) && (lib->osSpecificPointerData != NULL)) + /* Get the osSpecificPointerData from the management structure. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library(lib, &osData); + if ((retFromCall == COMMON_ERROR_SUCCESS) && (osData != NULL)) + { + /* Call FreeLibrary. */ + if (FreeLibrary((HMODULE)osData)) { - /* Call FreeLibrary. */ - if (FreeLibrary((HMODULE)lib->osSpecificPointerData)) + /* The library was unloaded successfully. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library(lib, false); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + retFromCall = Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(lib, NULL); + if (retFromCall == COMMON_ERROR_SUCCESS) { - /* The library was unloaded successfully. */ - lib->bIsLoaded = false; - lib->osSpecificPointerData = NULL; - lib->bLastCallEncounteredAnError = false; - - /* Success. */ - ret = COMMON_ERROR_SUCCESS; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): <"); - COMMON_LOG_VERBOSE(lib->pathToLibrary); - COMMON_LOG_VERBOSE("> unloaded."); + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): <"); + + /* Attempt to get the library path for the error log. */ + if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &pSym) == COMMON_ERROR_SUCCESS) && + (pSym != NULL)) + { + COMMON_LOG_VERBOSE(pSym); + } + else + { + COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); + } + pSym = NULL; /* Clear abused pSym. */ + + COMMON_LOG_VERBOSE("> unloaded."); } else { - /* Get the last error. */ - retFL = GetLastError(); - retFromCall = Common_Translate_Windows_Error_Code_To_Common_Error_Code(retFL); - - /* Could not unload the library. */ - ret = retFromCall; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not unload <"); - COMMON_LOG_VERBOSE(lib->pathToLibrary); - COMMON_LOG_VERBOSE("> Host function returned: "); + /* Could not clear os specific pointer data in management structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not clear os specific pointer data in management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* Could not clear is loaded flag in management structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not clear is loaded flag in management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); } + } } else { - /* Library is not loaded. */ - ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The given library <"); - COMMON_LOG_VERBOSE(lib->pathToLibrary); - COMMON_LOG_VERBOSE("> is not loaded."); + /* Get the last error. */ + retFL = GetLastError(); + retFromCall = Common_Translate_Windows_Error_Code_To_Common_Error_Code(retFL); + + /* Could not unload the library. */ + ret = retFromCall; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not unload <"); + + /* Attempt to get the library path for the error log. */ + if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &pSym) == COMMON_ERROR_SUCCESS) && + (pSym != NULL)) + { + COMMON_LOG_VERBOSE(pSym); + } + else + { + COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); + } + pSym = NULL; /* Clear abused pSym. */ + + COMMON_LOG_VERBOSE("> Host function returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } } + } + else + { + /* Could not get osSpecificPointerData from management structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not get os specific data pointer from management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + else + { + /* Could not reset last call incountered an error flag. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not reset management structure's error flag. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + else + { + /* Library is not loaded. */ + ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The given library <"); + + /* Attempt to get the library path for the error log. */ + if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &pSym) == COMMON_ERROR_SUCCESS) && + (pSym != NULL)) + { + COMMON_LOG_VERBOSE(pSym); } else { - /* Management structure is invalid. */ - ret = COMMON_ERROR_INVALID_ARGUMENT; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The engine's library structure for the given library is invalid. Unable to unload a library without a valid library structure."); + COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); } + pSym = NULL; /* Clear abused pSym. */ - /* Return result. */ - return ret; + COMMON_LOG_VERBOSE("> is not loaded."); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* Management structure is invalid. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The engine's library structure for the given library is invalid. Unable to unload a library without a valid library structure."); } - int Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName, void ** retSym) + /* Return result. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName, void ** retSym) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ + void * pSym = NULL; /* The returned symbol pointer. */ + void * osData = NULL; /* The osSpecificPointerData from the management structure. */ + DWORD retGPA = 0; /* Error code from GetProcAddress(). */ + + /* Check to see if the pointer to the management structure is valid. */ + if (lib != NULL) { - /* Init vars. */ - int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ - int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ - void * pSym = NULL; /* The returned symbol pointer. */ - DWORD retGPA = 0; /* Error code from GetProcAddress(). */ - - /* Check to see if the pointer to the management structure is valid. */ - if (lib != NULL) + /* Check and see if retSym is valid. */ + if (retSym != NULL) + { + /* Check to see if symbolName is NULL. */ + if (symbolName != NULL) { - /* Reset bLastCallEncounteredAnError. */ - lib->bLastCallEncounteredAnError = false; - - /* Check and see if retSym is valid. */ - if (retSym != NULL) + /* Check for a loaded library. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(lib); + if (retFromCall == COMMON_ERROR_TRUE) { - /* Check to see if symbolName is NULL. */ - if (symbolName != NULL) + /* Reset bLastCallEncounteredAnError. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, false); + if (retFromCall == COMMON_ERROR_SUCCESS) { - /* Check for a loaded library. */ - if (((lib->bIsLoaded) && (lib->osSpecificPointerData != NULL))) + /* Get the osSpecificPointerData from the management structure. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library(lib, &osData); + if ((retFromCall == COMMON_ERROR_SUCCESS) && (osData != NULL)) + { + /* Get the address. */ + pSym = (void*)GetProcAddress((HMODULE)osData, symbolName); + if (pSym == NULL) { - /* Get the address. */ - pSym = (void*)GetProcAddress((HMODULE)lib->osSpecificPointerData, symbolName); - if (pSym == NULL) + /* Get the last error. */ + retGPA = GetLastError(); + retFromCall = Common_Translate_Windows_Error_Code_To_Common_Error_Code(retGPA); + + /* An error occured fetching the symbol. */ + ret = retFromCall; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not fetch symbol in <"); + + /* Attempt to get the library path for the error log. */ + if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, (char**)&pSym) == COMMON_ERROR_SUCCESS) && + (pSym != NULL)) { - /* Get the last error. */ - retGPA = GetLastError(); - retFromCall = Common_Translate_Windows_Error_Code_To_Common_Error_Code(retGPA); - - /* An error occured fetching the symbol. */ - ret = retFromCall; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not fetch symbol in <"); - COMMON_LOG_VERBOSE(lib->pathToLibrary); - COMMON_LOG_VERBOSE("> Host function returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + COMMON_LOG_VERBOSE((char*)pSym); } else { - /* Copy pSym to retSym. */ - (*retSym) = pSym; + COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); + } + pSym = NULL; /* Clear abused pSym. */ - /* Success. */ - ret = COMMON_ERROR_SUCCESS; + COMMON_LOG_VERBOSE("> Host function returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); } } else { - /* Library is not loaded. */ - ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The given library <"); - COMMON_LOG_VERBOSE(lib->pathToLibrary); - COMMON_LOG_VERBOSE("> is not loaded."); + /* Copy pSym to retSym. */ + (*retSym) = pSym; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; } + } + else + { + /* Could not get osSpecificPointerData from management structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not get os specific data pointer from management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } } else { - /* symbolName is NULL. */ - ret = COMMON_ERROR_INVALID_ARGUMENT; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): No symbol name was given, cannot load a symbol without a name to identifiy it."); + /* Could not reset last call incountered an error flag. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not reset management structure's error flag. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); } } else { - /* retSym is NULL. */ - ret = COMMON_ERROR_INVALID_ARGUMENT; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + /* Library is not loaded. */ + ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The given library <"); + + /* Attempt to get the library path for the error log. */ + if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, (char**)&pSym) == COMMON_ERROR_SUCCESS) && + (pSym != NULL)) + { + COMMON_LOG_VERBOSE((char*)pSym); + } + else + { + COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); + } + pSym = NULL; /* Clear abused pSym. */ + + COMMON_LOG_VERBOSE("> is not loaded."); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } } } else { - /* Library structure is invalid. */ + /* symbolName is NULL. */ ret = COMMON_ERROR_INVALID_ARGUMENT; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The engine's library structure for the given library is invalid. Unable to lookup function without a valid library structure."); + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): No symbol name was given, cannot load a symbol without a name to identifiy it."); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } } - - /* Return result. */ - return ret; + } + else + { + /* retSym is NULL. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } } + else + { + /* Library structure is invalid. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The engine's library structure for the given library is invalid. Unable to lookup function without a valid library structure."); + } + + /* Return result. */ + return ret; + } #ifdef __cplusplus } /* End of extern C. */ #endif /* __cplusplus */ From 99d92e7ce41848adc3189938ed27204636cf246b Mon Sep 17 00:00:00 2001 From: codebase7 Date: Fri, 22 Jan 2016 22:02:16 -0500 Subject: [PATCH 42/50] Make Dynamic_Library_Subsystem_POSIX.c use the new dynlib management structure code. This commit makes Dynamic_Library_Subsystem_POSIX.c use the new API code for interacting with the loaded dynamic library structures, instead of accessing them directly. --- .../Dynamic_Library_Subsystem_POSIX.c | 636 ++++++++++++------ 1 file changed, 429 insertions(+), 207 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c index 2afa20c..f94d0c5 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c @@ -26,283 +26,505 @@ #include "../Error_Handler/Common_Error_Handler_Structures.h" /* External includes. */ -#include // dlopen, dlclose, dlsym, dlerror. +#include /* dlopen, dlclose, dlsym, dlerror. */ /* Check for C++ Compiler. */ #ifdef __cplusplus /* Define extern C. */ extern "C" { #endif /* __cplusplus */ - int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const size_t pathToLibraryLength, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ + void * callResult = NULL; /* The result of the call to dlopen(). */ + char * hostErr = NULL; /* The result returned from dlerror(). */ + char * tempPath = NULL; /* Used to load the library. */ + + /* Check to see if the pointer to the management structure is valid. */ + if (lib != NULL) { - /* Init vars. */ - int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ - int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ - char * hostErr = NULL; /* The result returned from dlerror(). */ - void * callResult = NULL; /* The result of the call to dlopen(). */ - - /* Check to see if the pointer to the management structure is valid. */ - if (lib != NULL) + /* Check to see if pathToLibrary is NULL. */ + if (pathToLibrary != NULL) + { + /* Check to see if the library is loaded. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(lib); + if ((retFromCall == COMMON_ERROR_FALSE) || (retFromCall == COMMON_ERROR_INVALID_ARGUMENT) || (reloadLibrary)) { - /* Check to see if pathToLibrary is NULL. */ - if (pathToLibrary != NULL) + /* Check and see if the library is loaded. */ + if (retFromCall == COMMON_ERROR_TRUE) + { + /* Call Unload_Library. */ + retFromCall = Common_Dynamic_Library_Subsystem_Unload_Library(lib); + } + else + { + /* Reset retFromCall. */ + retFromCall = COMMON_ERROR_UNKNOWN_ERROR; + } + + /* Only continue if the library was unloaded, or if we did not need to unload the library. */ + if ((retFromCall == COMMON_ERROR_UNKNOWN_ERROR) || (retFromCall == COMMON_ERROR_SUCCESS)) + { + /* Blank the values in lib. */ + Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library(lib); + + /* Copy the path string into lib. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library(lib, pathToLibrary, pathToLibraryLength); + if (retFromCall == COMMON_ERROR_SUCCESS) { - /* Check to see if the library is loaded. */ - if ((!lib->bIsLoaded) || (reloadLibrary)) + /* Get the path back. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &tempPath); + if ((retFromCall == COMMON_ERROR_SUCCESS) && (tempPath != NULL)) + { + /* Call dlopen(). */ + callResult = dlopen(tempPath, RTLD_LAZY | RTLD_LOCAL); + + /* Check the callResult. */ + if (callResult == NULL) { - /* Check and see if the library is loaded. */ - if (lib->bIsLoaded) - { - /* Call Unload_Library. */ - retFromCall = Common_Dynamic_Library_Subsystem_Unload_Library(lib); - } + /* An error occured. + There is no clean way to check the error given here, as dlerror() returns a human-readable string. + In addition, dlopen() does not have any defined error codes in the POSIX standard. + As such we have no way of returning the specific error encountered to the caller, + so we must return COMMON_ERROR_SYSTEM_SPECIFIC. + */ + ret = COMMON_ERROR_SYSTEM_SPECIFIC; - /* Only continue if the library was unloaded, or if we did not need to unload the library. */ - if ((retFromCall == COMMON_ERROR_UNKNOWN_ERROR) || (retFromCall == COMMON_ERROR_SUCCESS)) + /* Could not load the library. */ + hostErr = dlerror(); + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Could not load <"); + COMMON_LOG_VERBOSE(tempPath); + COMMON_LOG_VERBOSE("> Host function returned: "); + COMMON_LOG_VERBOSE(hostErr); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + /* Log additional error. */ + COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + else + { + /* Cast the OS specific data structure pointer to void*. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(lib, ((void*)callResult)); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + /* Set bIsLoaded. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library(lib, true); + if (retFromCall == COMMON_ERROR_SUCCESS) { - /* Set the values in lib. */ - lib->bIsLoaded = false; - lib->bLastCallEncounteredAnError = false; - lib->osSpecificPointerData = NULL; - lib->pathToLibrary = pathToLibrary; - - /* Call dlopen(). */ - callResult = dlopen(lib->pathToLibrary, RTLD_LAZY | RTLD_LOCAL); - - /* Check the callResult. */ - if (callResult == NULL) - { - /* An error occured. - There is no clean way to check the error given here, as dlerror() returns a human-readable string. - In addition, dlopen() does not have any defined error codes in the POSIX standard. - As such we have no way of returning the specific error encountered to the caller, - so we must return COMMON_ERROR_SYSTEM_SPECIFIC. - */ - ret = COMMON_ERROR_SYSTEM_SPECIFIC; - - /* Could not load the library. */ - hostErr = dlerror(); - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Could not load <"); - COMMON_LOG_VERBOSE(lib->pathToLibrary); - COMMON_LOG_VERBOSE("> Host function returned: "); - COMMON_LOG_VERBOSE(hostErr); - } - else - { - /* Cast the OS specific data structure pointer to void. */ - lib->osSpecificPointerData = callResult; - - /* Set bIsLoaded. */ - lib->bIsLoaded = true; - - /* Clear lib->bLastCallEncounteredAnError. */ - lib->bLastCallEncounteredAnError = false; - - /* Success. */ - ret = COMMON_ERROR_SUCCESS; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): <"); - COMMON_LOG_VERBOSE(lib->pathToLibrary); - COMMON_LOG_VERBOSE("> loaded."); - } + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): <"); + COMMON_LOG_VERBOSE(tempPath); + COMMON_LOG_VERBOSE("> loaded."); } else { - /* Encountered an error during the unload. */ - ret = retFromCall; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Unable to reload library."); + /* Could not set is loaded flag. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); } + } + else + { + /* Could not set os specific pointer data. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + } } - else - { - /* Library is already loaded. */ - ret = DYNLIB_ERROR_LIBRARY_ALREADY_LOADED; - } + } + else + { + /* Could not retrive path from lib structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + } } else { - /* pathToLibrary is NULL. */ - ret = COMMON_ERROR_INVALID_ARGUMENT; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): No path to the library was given. Unable to load a library without the path to it."); + /* Could not copy path data. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); } + } + else + { + /* Encountered an error during the unload. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Unable to reload library."); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + /* Log additional error. */ + COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } } else { - /* Management structure is invalid. */ - ret = COMMON_ERROR_INVALID_ARGUMENT; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): The engine's library structure for the given library is invalid. Unable to load a library without a valid library structure."); + /* Library is already loaded. */ + ret = DYNLIB_ERROR_LIBRARY_ALREADY_LOADED; } - - /* Return result. */ - return ret; + } + else + { + /* pathToLibrary is NULL. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): No path to the library was given. Unable to load a library without the path to it."); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + /* Log additional error. */ + COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } } - - int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + else { - /* Init vars. */ - int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ - int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ - char * hostErr = NULL; /* The result returned from dlerror(). */ + /* Management structure is invalid. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): The engine's library structure for the given library is invalid. Unable to load a library without a valid library structure."); + } - /* Check to see if the pointer to the management structure is valid. */ - if (lib != NULL) + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ + char * hostErr = NULL; /* The result returned from dlerror(). */ + void * osData = NULL; /* The osSpecificPointerData from the management structure. */ + char * pSym = NULL; /* Used to log the library path to the error log. */ + + /* Check to see if the pointer to the management structure is valid. */ + if (lib != NULL) + { + /* Check for a loaded library. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(lib); + if (retFromCall == COMMON_ERROR_TRUE) + { + /* Reset bLastCallEncounteredAnError. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, false); + if (retFromCall == COMMON_ERROR_SUCCESS) { - /* Reset bLastCallEncounteredAnError. */ - lib->bLastCallEncounteredAnError = false; - - /* Check for a valid handle. */ - if ((lib->bIsLoaded) && (lib->osSpecificPointerData != NULL)) + /* Get the osSpecificPointerData from the management structure. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library(lib, &osData); + if ((retFromCall == COMMON_ERROR_SUCCESS) && (osData != NULL)) + { + /* Call dlclose(). */ + retFromCall = dlclose(lib->osSpecificPointerData); + if (retFromCall == 0) { - /* Call dlclose(). */ - retFromCall = dlclose(lib->osSpecificPointerData); - - /* Check result. */ - if (retFromCall != 0) + /* The library was unloaded successfully. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library(lib, false); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + retFromCall = Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(lib, NULL); + if (retFromCall == COMMON_ERROR_SUCCESS) { - /* An error occured. - There is no clean way to check the error given here, as dlerror() returns a human-readable string. - In addition, dlclose() does not have any defined error codes in the POSIX standard. - As such we have no way of returning the specific error encountered to the caller, - so we must return COMMON_ERROR_SYSTEM_SPECIFIC. - */ - ret = COMMON_ERROR_SYSTEM_SPECIFIC; + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): <"); + + /* Attempt to get the library path for the error log. */ + if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &pSym) == COMMON_ERROR_SUCCESS) && + (pSym != NULL)) + { + COMMON_LOG_VERBOSE(pSym); + } + else + { + COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); + } + pSym = NULL; /* Clear abused pSym. */ - /* Could not load the library. */ - hostErr = dlerror(); - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not unload <"); - COMMON_LOG_VERBOSE(lib->pathToLibrary); - COMMON_LOG_VERBOSE("> Host function returned: "); - COMMON_LOG_VERBOSE(hostErr); + COMMON_LOG_VERBOSE("> unloaded."); } else { - /* Library unloaded successfully. */ - lib->osSpecificPointerData = NULL; - lib->bIsLoaded = false; - lib->bLastCallEncounteredAnError = false; - - /* Success. */ - ret = COMMON_ERROR_SUCCESS; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): <"; - COMMON_LOG_VERBOSE(lib->pathToLibrary); - COMMON_LOG_VERBOSE("> unloaded."); + /* Could not clear os specific pointer data in management structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not clear os specific pointer data in management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } } + } + else + { + /* Could not clear is loaded flag in management structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not clear is loaded flag in management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } } else { - /* Library is not loaded. */ - ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The given library <"); - COMMON_LOG_VERBOSE(lib->pathToLibrary); - COMMON_LOG_VERBOSE("> is not loaded."); + /* An error occured. + There is no clean way to check the error given here, as dlerror() returns a human-readable string. + In addition, dlclose() does not have any defined error codes in the POSIX standard. + As such we have no way of returning the specific error encountered to the caller, + so we must return COMMON_ERROR_SYSTEM_SPECIFIC. + */ + ret = COMMON_ERROR_SYSTEM_SPECIFIC; + + /* Could not load the library. */ + hostErr = dlerror(); + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not unload <"); + + /* Attempt to get the library path for the error log. */ + if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &pSym) == COMMON_ERROR_SUCCESS) && + (pSym != NULL)) + { + COMMON_LOG_VERBOSE(pSym); + } + else + { + COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); + } + pSym = NULL; /* Clear abused pSym. */ + + COMMON_LOG_VERBOSE("> Host function returned: "); + COMMON_LOG_VERBOSE(hostErr); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } } + } + else + { + /* Could not get osSpecificPointerData from management structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not get os specific data pointer from management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + else + { + /* Could not reset last call incountered an error flag. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not reset management structure's error flag. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + else + { + /* Library is not loaded. */ + ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The given library <"); + + /* Attempt to get the library path for the error log. */ + if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &pSym) == COMMON_ERROR_SUCCESS) && + (pSym != NULL)) + { + COMMON_LOG_VERBOSE(pSym); } else { - /* Management structure is invalid. */ - ret = COMMON_ERROR_INVALID_ARGUMENT_ERROR; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The engine's library structure for the given library is invalid. Unable to unload a library without a valid library structure."); + COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); } + pSym = NULL; /* Clear abused pSym. */ - /* Return result. */ - return ret; + COMMON_LOG_VERBOSE("> is not loaded."); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } } - - int Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName, void ** retSym) + else { - /* Init vars. */ - int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ - void * pSym = NULL; /* The returned symbol pointer. */ - char * hostErr = NULL; /* The result returned from dlerror(). */ + /* Management structure is invalid. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The engine's library structure for the given library is invalid. Unable to unload a library without a valid library structure."); + } - /* Check to see if the pointer to the management structure is valid. */ - if (lib != NULL) + /* Return result. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName, void ** retSym) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ + void * pSym = NULL; /* The returned symbol pointer. */ + void * osData = NULL; /* The osSpecificPointerData from the management structure. */ + char * hostErr = NULL; /* The result returned from dlerror(). */ + + /* Check to see if the pointer to the management structure is valid. */ + if (lib != NULL) + { + /* Check and see if retSym is valid. */ + if (retSym != NULL) + { + /* Check to see if symbolName is NULL. */ + if (symbolName != NULL) { - /* Reset bLastCallEncounteredAnError. */ - lib->bLastCallEncounteredAnError = false; - - /* Check and see if retSym is valid. */ - if (retSym != NULL) + /* Check for a loaded library. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(lib); + if (retFromCall == COMMON_ERROR_TRUE) { - /* Check to see if symbolName is NULL. */ - if (symbolName != NULL) + /* Reset bLastCallEncounteredAnError. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, false); + if (retFromCall == COMMON_ERROR_SUCCESS) { - /* Check to see if we have a valid handle. */ - if ((lib->bIsLoaded) && (lib->osSpecificPointerData != NULL)) + /* Get the osSpecificPointerData from the management structure. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library(lib, &osData); + if ((retFromCall == COMMON_ERROR_SUCCESS) && (osData != NULL)) + { + /* Call dlerror to clear the error state. */ + dlerror(); + + /* Call dlsym. */ + pSym = dlsym(osData, symbolName); + + /* Call dlerror again to check for an error. */ + hostErr = dlerror(); + if (hostErr != NULL) { - /* Call dlerror to clear the error state. */ - dlerror(); - - /* Call dlsym. */ - pSym = dlsym(lib->osSpecificPointerData, symbolName); - - /* Call dlerror again to check for an error. */ - hostErr = dlerror(); - if (hostErr != NULL) - { - /* An error occured. - There is no clean way to check the error given here, as dlerror() returns a human-readable string. - In addition, dlsym() does not have any defined error codes in the POSIX standard. - As such we have no way of returning the specific error encountered to the caller, - so we must return COMMON_ERROR_SYSTEM_SPECIFIC. - */ - ret = COMMON_ERROR_SYSTEM_SPECIFIC; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Error returned from host: "); - COMMON_LOG_VERBOSE(hostErr); - } - else - { - /* Copy pSym to retSym. */ - (*retSym) = pSym; - - /* Success. */ - ret = COMMON_ERROR_SUCCESS; - } + /* An error occured. + There is no clean way to check the error given here, as dlerror() returns a human-readable string. + In addition, dlsym() does not have any defined error codes in the POSIX standard. + As such we have no way of returning the specific error encountered to the caller, + so we must return COMMON_ERROR_SYSTEM_SPECIFIC. + */ + ret = COMMON_ERROR_SYSTEM_SPECIFIC; + + /* An error occured fetching the symbol. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not fetch symbol in <"); + + /* Attempt to get the library path for the error log. */ + if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, (char**)&pSym) == COMMON_ERROR_SUCCESS) && + (pSym != NULL)) + { + COMMON_LOG_VERBOSE((char*)pSym); + } + else + { + COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); + } + pSym = NULL; /* Clear abused pSym. */ + + COMMON_LOG_VERBOSE("> Host function returned: "); + COMMON_LOG_VERBOSE(hostErr); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } } else { - /* Library is not loaded. */ - ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The given library <"); - COMMON_LOG_VERBOSE(lib->pathToLibrary); - COMMON_LOG_VERBOSE("> is not loaded."); + /* Copy pSym to retSym. */ + (*retSym) = pSym; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; } + } + else + { + /* Could not get osSpecificPointerData from management structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not get os specific data pointer from management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } } else { - /* symbolName is NULL. */ - ret = COMMON_ERROR_INVALID_ARGUMENT; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + /* Could not reset last call incountered an error flag. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not reset management structure's error flag. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); } } else { - /* retSym is NULL. */ - ret = COMMON_ERROR_INVALID_ARGUMENT; - lib->bLastCallEncounteredAnError = true; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + /* Library is not loaded. */ + ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The given library <"); + + /* Attempt to get the library path for the error log. */ + if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, (char**)&pSym) == COMMON_ERROR_SUCCESS) && + (pSym != NULL)) + { + COMMON_LOG_VERBOSE((char*)pSym); + } + else + { + COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); + } + pSym = NULL; /* Clear abused pSym. */ + + COMMON_LOG_VERBOSE("> is not loaded."); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } } } else { - /* Invalid management structure. */ + /* symbolName is NULL. */ ret = COMMON_ERROR_INVALID_ARGUMENT; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): No symbol name was given, cannot load a symbol without a name to identifiy it."); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } } - - /* Return result. */ - return ret; + } + else + { + /* retSym is NULL. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* Library structure is invalid. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The engine's library structure for the given library is invalid. Unable to lookup function without a valid library structure."); } + + /* Return result. */ + return ret; + } + #ifdef __cplusplus } /* End of extern C. */ #endif /* __cplusplus */ From 5c9e78a30fb33ed27b04f08d1cf4434059d44650 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Fri, 22 Jan 2016 23:13:25 -0500 Subject: [PATCH 43/50] Rename Common_Dynamic_Library_Subsystem_Get_API_Version() toCommon_Dynamic_Library_Subsystem_Get_API_Major_Version_Number() This commit renames Common_Dynamic_Library_Subsystem_Get_API_Version() to Common_Dynamic_Library_Subsystem_Get_API_Major_Version_Number(). --- .../Dynamic_Library_Subsystem_Data_Structures.c | 2 +- .../Dynamic_Library_Subsystem_Data_Structures.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c index 37a512f..d0da9e8 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c @@ -30,7 +30,7 @@ /* Define extern C. */ extern "C" { #endif /* __cplusplus */ - int Common_Dynamic_Library_Subsystem_Get_API_Version() + int Common_Dynamic_Library_Subsystem_Get_API_Major_Version_Number() { /* Return the API Version number. */ return MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_LEVEL; diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h index 5425546..4a9d428 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h @@ -46,11 +46,11 @@ extern "C" { typedef struct Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library; /*! - * int Common_Dynamic_Library_Subsystem_Get_API_Version() + * int Common_Dynamic_Library_Subsystem_Get_API_Major_Version_Number() * - * Returns the API version for the Dynamic Library Subsystem. + * Returns the API major version number for the Dynamic Library Subsystem. */ - MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Get_API_Version(); + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Get_API_Major_Version_Number(); /*! * int Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library ** lib) From b2527e5156c14ef163de723f5c54d018dad12ce3 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Fri, 22 Jan 2016 23:14:47 -0500 Subject: [PATCH 44/50] Rename MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_LEVEL to MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_MAJOR_VER This commit renames MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_LEVEL to MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_MAJOR_VER. --- .../Dynamic_Library_Subsystem_Data_Structures.c | 2 +- .../Dynamic_Library_Subsystem_Data_Structures.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c index d0da9e8..fa95534 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c @@ -33,7 +33,7 @@ extern "C" { int Common_Dynamic_Library_Subsystem_Get_API_Major_Version_Number() { /* Return the API Version number. */ - return MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_LEVEL; + return MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_MAJOR_VER; } int Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library ** lib) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h index 4a9d428..283d437 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h @@ -25,8 +25,8 @@ #ifndef MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_DATA_STRUCTURES_H #define MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_DATA_STRUCTURES_H -/* Define the supported API level. */ -#define MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_LEVEL 0 +/* Define the supported API version numbers. */ +#define MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_MAJOR_VER 0 /* Internal includes. */ #include "../../../DLL_PORT.h" From 1d34c69189bad4ea8d22b98a10edce4142af2553 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Fri, 22 Jan 2016 23:19:33 -0500 Subject: [PATCH 45/50] Add Common_Dynamic_Library_Subsystem_Get_API_Minor_Version_Number() &Common_Dynamic_Library_Subsystem_Get_API_Revision_Version_Number() This commit adds the Common_Dynamic_Library_Subsystem_Get_API_Minor_Version_Number() and Common_Dynamic_Library_Subsystem_Get_API_Revision_Version_Number() functions to the Dynamic Library Subsystem. --- .../Dynamic_Library_Subsystem_Data_Structures.c | 12 ++++++++++++ .../Dynamic_Library_Subsystem_Data_Structures.h | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c index fa95534..90de1f8 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c @@ -36,6 +36,18 @@ extern "C" { return MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_MAJOR_VER; } + int Common_Dynamic_Library_Subsystem_Get_API_Minor_Version_Number() + { + /* Return the API Version number. */ + return MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_Minor_VER; + } + + int Common_Dynamic_Library_Subsystem_Get_API_Revision_Version_Number() + { + /* Return the API Version number. */ + return MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_REVISION_VER; + } + int Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library ** lib) { /* Init vars. */ diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h index 283d437..e481ad3 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h @@ -27,6 +27,8 @@ /* Define the supported API version numbers. */ #define MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_MAJOR_VER 0 +#define MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_MINOR_VER 0 +#define MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_REVISION_VER 0 /* Internal includes. */ #include "../../../DLL_PORT.h" @@ -52,6 +54,20 @@ extern "C" { */ MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Get_API_Major_Version_Number(); + /*! + * int Common_Dynamic_Library_Subsystem_Get_API_Minor_Version_Number() + * + * Returns the API minor version number for the Dynamic Library Subsystem. + */ + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Get_API_Minor_Version_Number(); + + /*! + * int Common_Dynamic_Library_Subsystem_Get_API_Revision_Version_Number() + * + * Returns the API revision version number for the Dynamic Library Subsystem. + */ + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Get_API_Revision_Version_Number(); + /*! * int Common_Dynamic_Library_Subsystem_Create_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library ** lib) * From 5a69d4b68600822b7948a921e97befc6e6164823 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Fri, 22 Jan 2016 23:28:36 -0500 Subject: [PATCH 46/50] Fix typo. (Compile fix.) This commit fixes a typo (lowercase instead of uppercase variable name) preventing successful build in Dynamic_Library_Subsystem_Data_Structures.c. --- .../Dynamic_Library_Subsystem_Data_Structures.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c index 90de1f8..8f29ad0 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c @@ -39,7 +39,7 @@ extern "C" { int Common_Dynamic_Library_Subsystem_Get_API_Minor_Version_Number() { /* Return the API Version number. */ - return MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_Minor_VER; + return MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_API_MINOR_VER; } int Common_Dynamic_Library_Subsystem_Get_API_Revision_Version_Number() From f8c51d2bec6e9545db4108f0dab2b69b39d522fd Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sat, 23 Jan 2016 03:33:30 -0500 Subject: [PATCH 47/50] Add member variable for storing path length to Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private This commit adds pathToLibraryLength as a member variable to Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private to keep track of the length of the pathToLibrary string in bytes. This commit also modifies the required functions to store and return this new information in both the public and private API source files. --- .../Dynamic_Library_Subsystem_Data_Structures.c | 6 +++--- .../Dynamic_Library_Subsystem_Data_Structures.h | 6 +++--- ...rary_Subsystem_Data_Structures_Private_API.c | 17 ++++++++++++++--- ...rary_Subsystem_Data_Structures_Private_API.h | 7 ++++--- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c index 8f29ad0..66c005a 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.c @@ -304,16 +304,16 @@ extern "C" { return ret; } - int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const char ** retVar) + int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const char ** retVar, size_t * retVarLength) { /* Init vars. */ int ret = COMMON_ERROR_UNKNOWN_ERROR; /* Check for valid pointers. */ - if ((lib != NULL) && (lib->pointer != NULL) && (retVar != NULL)) + if ((lib != NULL) && (lib->pointer != NULL) && (retVar != NULL) && (retVarLength != NULL)) { /* Call real function. */ - ret = Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer)), retVar); + ret = Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(((Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private *)(lib->pointer)), retVar, retVarLength); if (ret == COMMON_ERROR_INVALID_ARGUMENT) { /* That's an error, we didn't set up the private data structure correctly or it's been corrupted. */ diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h index e481ad3..45fb749 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures.h @@ -203,11 +203,11 @@ extern "C" { MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const void * value); /*! - * int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const char ** retVar) + * int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const char ** retVar, size_t * retVarLength) * * ACCESSOR FUNCTION. * - * Returns the pointer for the pathToLibrary member variable. + * Returns the pointer for the pathToLibrary member variable, and the value of the pathToLibraryLength member variable. * * WARNING: This function will NOT deallocate a preexisting structure, and will overwrite the given pointer if it is * non-NULL if this function succeeds. Therefore if you need to keep the pointer, copy it elsewhere before calling this @@ -219,7 +219,7 @@ extern "C" { * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. * Otherwise returns the appropriate error code. */ - MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const char ** retVar); + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const char ** retVar, size_t * retVarLength); /*! * int Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, const char * value, const size_t valueLength) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c index 7d08e75..aa4e152 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.c @@ -97,6 +97,7 @@ extern "C" { lib->bIsLoaded = false; lib->bLastCallEncounteredAnError = false; lib->osSpecificPointerData = NULL; + lib->pathToLibraryLength = 0; /* Check for set pathToLibrary. */ if (lib->pathToLibrary != NULL) @@ -248,17 +249,20 @@ extern "C" { return ret; } - int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const char ** retVar) + int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const char ** retVar, size_t * retVarLength) { /* Init vars. */ int ret = COMMON_ERROR_UNKNOWN_ERROR; /* Check for valid pointer. */ - if ((lib != NULL) && (retVar != NULL)) + if ((lib != NULL) && (retVar != NULL) && (retVarLength != NULL)) { /* Get the pathToLibrary pointer and copy it to retVar. */ (*retVar) = lib->pathToLibrary; + /* Get the length of pathToLibrary and copy it to retVarLength. */ + (*retVarLength) = lib->pathToLibraryLength; + /* Success. */ ret = COMMON_ERROR_SUCCESS; } @@ -310,14 +314,21 @@ extern "C" { { /* Deallocate the path string. */ DataProcess_Deallocate_CString(&(lib->pathToLibrary)); + + /* Reset the path string's length. */ + lib->pathToLibraryLength = 0; } - /* Check and see if we need to copy the tempPath pointer. */ + /* Check and see if we need to copy the tempPath pointer and length. */ if (tempPath != NULL) { /* Copy the tempPath pointer to the structure. */ lib->pathToLibrary = tempPath; } + if (valueLength > 0) + { + lib->pathToLibraryLength = valueLength; + } /* Success. */ ret = COMMON_ERROR_SUCCESS; diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h index fcb8977..21c5a7d 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Data_Structures_Private_API.h @@ -54,6 +54,7 @@ extern "C" { which is needed to unload the library or to search it.) */ char * pathToLibrary; /* Path to the dynamic library file on disk. */ + size_t pathToLibraryLength; /* Length of the pathToLibrary c-string in bytes. */ }; /* Create user defined data type for Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private structure. */ @@ -194,11 +195,11 @@ extern "C" { int Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, void * value); /*! - * int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const char ** retVar) + * int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const char ** retVar, size_t * retVarLength) * * ACCESSOR FUNCTION. * - * Returns the pointer for the pathToLibrary member variable. + * Returns the pointer for the pathToLibrary member variable, and the value of the pathToLibraryLength member variable. * * WARNING: This function will NOT deallocate a preexisting structure, and will overwrite the given pointer if it is * non-NULL if this function succeeds. Therefore if you need to keep the pointer, copy it elsewhere before calling this @@ -210,7 +211,7 @@ extern "C" { * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. * Otherwise returns the appropriate error code. */ - int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const char ** retVar); + int Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library_Private(const Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const char ** retVar, size_t * retVarLength); /*! * int Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library_Private(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library_Private * lib, const char * value, const size_t valueLength) From 6f2bfa698561ba2e18baa59d52b060058bd38888 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sat, 23 Jan 2016 03:40:06 -0500 Subject: [PATCH 48/50] Create Dynamic_Library_Subsystem.c & Add Common_Dynamic_Library_Subsystem_Reload_Library() This commit creates the Dynamic_Library_Subsystem.c file, which will contain most of common code from both Dynamic_Library_Subsystem_POSIX.c and Dynamic_Library_Subsystem_Windows.c. This commit also adds the Common_Dynamic_Library_Subsystem_Reload_Library() function to the Dynamic_Library_Subsystem. --- .../Dynamic_Library_Subsystem/CMakeLists.txt | 2 + .../Dynamic_Library_Subsystem.c | 617 ++++++++++++++++++ .../Dynamic_Library_Subsystem.h | 26 +- 3 files changed, 640 insertions(+), 5 deletions(-) create mode 100644 src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.c diff --git a/src/Common/Src/Dynamic_Library_Subsystem/CMakeLists.txt b/src/Common/Src/Dynamic_Library_Subsystem/CMakeLists.txt index 7f1299c..3391a8b 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/CMakeLists.txt +++ b/src/Common/Src/Dynamic_Library_Subsystem/CMakeLists.txt @@ -8,6 +8,7 @@ IF(${CMAKE_SYSTEM_NAME} MATCHES "Windows") # Building on windows. set(DYNAMIC_LIBRARY_SUBSYSTEM_INCLUDES Dynamic_Library_Subsystem_Data_Structures.c Dynamic_Library_Subsystem_Data_Structures_Private_API.c + Dynamic_Library_Subsystem.c Dynamic_Library_Subsystem_Windows.c) unset(DYNAMIC_LIBRARY_SUBSYSTEM_LINK_LIBS) @@ -15,6 +16,7 @@ IF(${CMAKE_SYSTEM_NAME} MATCHES "Windows") # Building on Linux. set(DYNAMIC_LIBRARY_SUBSYSTEM_INCLUDES Dynamic_Library_Subsystem_Data_Structures.c Dynamic_Library_Subsystem_Data_Structures_Private_API.c + Dynamic_Library_Subsystem.c Dynamic_Library_Subsystem_POSIX.c) set(DYNAMIC_LIBRARY_SUBSYSTEM_LINK_LIBS dl) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.c new file mode 100644 index 0000000..dc04527 --- /dev/null +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.c @@ -0,0 +1,617 @@ +/*! + Multiverse Engine Project 23/1/2016 Dynamic_Library_Subsystem Dynamic_Library_Subsystem.c + + Copyright (C) 2016 Multiverse Engine Project + + This program is free software; + you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; + either version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with this program; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Official source repository and project information can be found at + https://github.com/codebase7/mengine +*/ + +/* Internal includes. */ +#include "Dynamic_Library_Subsystem.h" +#include "Dynamic_Library_Subsystem_Syscall.h" +#include "../Error_Handler/Common_Error_Handler_Structures.h" +#include "../../../Core/Src/DataProcess.h" + +/* Check for C++ Compiler. */ +#ifdef __cplusplus +/* Define extern C. */ +extern "C" { +#endif /* __cplusplus */ + + int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const size_t pathToLibraryLength, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ + char * tempPath = NULL; /* Used to verifiy that the path was stored in the management structure. */ + size_t tempPathLength = 0; /* Used to store the length of the tempPath string in bytes. */ + char * copiedPathFromStruct = NULL; /* Used to copy the path from the management structure during a reload. */ + size_t copiedPathLengthFromStruct = 0; /* Used to store the length of the tempPath string in bytes. */ + void * callResult = NULL; /* The result of the call the engine's syscall function. */ + + /* Check to see if the pointer to the management structure is valid. */ + if (lib != NULL) + { + /* Check to see if pathToLibrary is NULL. */ + if ((pathToLibrary != NULL) && (pathToLibraryLength > 0)) + { + /* Check to see if the library is loaded. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(lib); + if ((retFromCall == COMMON_ERROR_FALSE) || (reloadLibrary)) + { + /* Check and see if the library is loaded. */ + if (retFromCall == COMMON_ERROR_TRUE) + { + /* Get the pathToLibrary pointer from the management structure. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &tempPath, &tempPathLength); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + /* Check and see if the given pathToLibrary is from the lib structure. */ + if (tempPath == pathToLibrary) + { + /* Copy the length data. */ + copiedPathLengthFromStruct = tempPathLength; + + /* We need to copy the pathToLibrary data. */ + retFromCall = DataProcess_Reallocate_C_String(&copiedPathFromStruct, 0, copiedPathLengthFromStruct); + if ((retFromCall == COMMON_ERROR_SUCCESS) && (copiedPathFromStruct != NULL)) + { + /* Copy the path string. */ + memcpy(copiedPathFromStruct, tempPath, copiedPathLengthFromStruct); + } + else + { + /* Could not allocate memory for tempPath. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_DEBUG("Common_Dynamic_Library_Subsystem_Load_Library(): Memory allocation function returned: "); + COMMON_LOG_DEBUG(Common_Get_Error_Message(retFromCall)); + } + + /* Reset tempPath and tempPathLength. */ + tempPath = NULL; + tempPathLength = 0; + } + + /* Call Unload_Library. */ + retFromCall = Common_Dynamic_Library_Subsystem_Unload_Library(lib); + } + else + { + /* Could not get the pathToLibrary pointer from the data structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Unable to get the pathToLibrary pointer from the data structure."); + COMMON_LOG_VERBOSE("Engine call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + /* Log additional error. */ + COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* Reset retFromCall. */ + retFromCall = COMMON_ERROR_UNKNOWN_ERROR; + } + + /* Only continue if the library was unloaded, or if we did not need to unload the library. */ + if ((retFromCall == COMMON_ERROR_UNKNOWN_ERROR) || (retFromCall == COMMON_ERROR_SUCCESS)) + { + /* Blank the values in lib. */ + Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library(lib); + + /* Copy the path string into lib. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library(lib, ((copiedPathFromStruct == NULL) ? (pathToLibrary) : (copiedPathFromStruct)), ((copiedPathLengthFromStruct == 0) ? (pathToLibraryLength) : (copiedPathLengthFromStruct))); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + /* Get the path back. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &tempPath, &tempPathLength); + if ((retFromCall == COMMON_ERROR_SUCCESS) && (tempPath != NULL) && (tempPathLength > 0)) + { + /* Call Syscall. */ + retFromCall = Common_Dynamic_Library_Subsystem_Load_Library_Syscall(tempPath, tempPathLength, &callResult); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + /* Copy returned osSpecificPointerData to the management structure. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(lib, callResult); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + /* Set bIsLoaded. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library(lib, true); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): <"); + COMMON_LOG_VERBOSE(tempPath); + COMMON_LOG_VERBOSE("> loaded."); + } + else + { + /* Could not set is loaded flag. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + } + } + else + { + /* Could not set os specific pointer data. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + } + } + else + { + /* Could not get osSpecificPointerData. Syscall failed. Set bLastCallEncounteredAnError flag in management structure. */ + ret = retFromCall; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library_Syscall(): Could not load <"); + COMMON_LOG_VERBOSE(pathToLibrary); + COMMON_LOG_VERBOSE("> Host function returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + /* Log additional error. */ + COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* Could not retrive path from lib structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + } + } + else + { + /* Could not copy path data. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + } + } + else + { + /* Encountered an error during the unload. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Unable to reload library."); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + /* Log additional error. */ + COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* Check and see if the library was already loaded. */ + if (retFromCall == COMMON_ERROR_TRUE) + { + /* Library is already loaded. */ + ret = DYNLIB_ERROR_LIBRARY_ALREADY_LOADED; + } + else + { + /* An error occured. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Could not get loaded library flag from management structure. Engine call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* pathToLibrary is NULL. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): No path to the library was given. Unable to load a library without the path to it."); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + /* Log additional error. */ + COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* Management structure is invalid. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): The engine's library structure for the given library is invalid. Unable to load a library without a valid library structure."); + } + + /* Check for an allocated copiedPathFromStruct and deallocate it if needed. */ + if (copiedPathFromStruct != NULL) + { + DataProcess_Deallocate_CString(&copiedPathFromStruct); + copiedPathLengthFromStruct = 0; + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ + void * osData = NULL; /* The osSpecificPointerData from the management structure. */ + char * pSym = NULL; /* Used to log the library path to the error log. */ + size_t pSymLength = 0; /* The length of pSym in bytes. */ + + /* Check to see if the pointer to the management structure is valid. */ + if (lib != NULL) + { + /* Check for a loaded library. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(lib); + if (retFromCall == COMMON_ERROR_TRUE) + { + /* Reset bLastCallEncounteredAnError. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, false); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + /* Get the osSpecificPointerData from the management structure. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library(lib, &osData); + if ((retFromCall == COMMON_ERROR_SUCCESS) && (osData != NULL)) + { + /* Call the syscall. */ + retFromCall = Common_Dynamic_Library_Subsystem_Unload_Library_Syscall(osData); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + /* The library was unloaded successfully. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library(lib, false); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + retFromCall = Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(lib, NULL); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): <"); + + /* Attempt to get the library path for the error log. */ + if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &pSym, &pSymLength) == COMMON_ERROR_SUCCESS) && + (pSym != NULL)) + { + COMMON_LOG_VERBOSE(pSym); + } + else + { + COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); + } + pSym = NULL; /* Clear abused pSym. */ + + COMMON_LOG_VERBOSE("> unloaded."); + } + else + { + /* Could not clear os specific pointer data in management structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not clear os specific pointer data in management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* Could not clear is loaded flag in management structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not clear is loaded flag in management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* Could not unload the library. */ + ret = retFromCall; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not unload <"); + + /* Attempt to get the library path for the error log. */ + if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &pSym, &pSymLength) == COMMON_ERROR_SUCCESS) && + (pSym != NULL)) + { + COMMON_LOG_VERBOSE(pSym); + } + else + { + COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); + } + pSym = NULL; /* Clear abused pSym. */ + + COMMON_LOG_VERBOSE("> Host function returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* Could not get osSpecificPointerData from management structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not get os specific data pointer from management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + else + { + /* Could not reset last call incountered an error flag. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not reset management structure's error flag. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + else + { + /* Library is not loaded. */ + ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The given library <"); + + /* Attempt to get the library path for the error log. */ + if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &pSym, &pSymLength) == COMMON_ERROR_SUCCESS) && + (pSym != NULL)) + { + COMMON_LOG_VERBOSE(pSym); + } + else + { + COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); + } + pSym = NULL; /* Clear abused pSym. */ + + COMMON_LOG_VERBOSE("> is not loaded."); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* Management structure is invalid. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The engine's library structure for the given library is invalid. Unable to unload a library without a valid library structure."); + } + + /* Return result. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Reload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ + char * tempPath = NULL; /* Used to verifiy that the path was stored in the management structure. */ + size_t tempPathLength = 0; /* Used to store the length of the tempPath string in bytes. */ + + /* Check for invalid arguments. */ + if (lib != NULL) + { + /* Get the pathToLibrary pointer from the management structure. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &tempPath, &tempPathLength); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + /* Check for valid path data */ + if ((tempPath != NULL) && (tempPathLength > 0)) + { + /* Call Common_Dynamic_Library_Subsystem_Load_Library(). */ + ret = Common_Dynamic_Library_Subsystem_Load_Library(tempPath, tempPathLength, true, lib); + } + else + { + /* The given library structure does not define a path. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Reload_Library(): The given library structure does not define a path."); + } + } + else + { + /* Could not get the pathToLibrary pointer from the data structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Reload_Library(): Unable to get the pathToLibrary pointer from the data structure."); + COMMON_LOG_VERBOSE("Engine call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + /* Log additional error. */ + COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* Library structure is invalid. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Reload_Library(): The engine's library structure for the given library is invalid."); + } + + /* Exit function. */ + return ret; + } + + int Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName, const size_t symbolNameLength, void ** retSym) + { + /* Init vars. */ + int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ + int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ + void * pSym = NULL; /* The returned symbol pointer. */ + size_t pSymLength = 0; /* The length of pSym (When being abused as a path string) in bytes. */ + void * osData = NULL; /* The osSpecificPointerData from the management structure. */ + + /* Check to see if the pointer to the management structure is valid. */ + if (lib != NULL) + { + /* Check and see if retSym is valid. */ + if (retSym != NULL) + { + /* Check to see if symbolName is NULL, or symbolNameLength is less than or equal to zero. */ + if ((symbolName != NULL) && (symbolNameLength > 0)) + { + /* Check for a loaded library. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(lib); + if (retFromCall == COMMON_ERROR_TRUE) + { + /* Reset bLastCallEncounteredAnError. */ + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, false); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + /* Get the osSpecificPointerData from the management structure. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library(lib, &osData); + if ((retFromCall == COMMON_ERROR_SUCCESS) && (osData != NULL)) + { + /* Call syscall. */ + retFromCall = Common_Dynamic_Library_Subsystem_Get_Symbol_Syscall(osData, symbolName, symbolNameLength, &pSym); + if (retFromCall == COMMON_ERROR_SUCCESS) + { + /* Copy pSym to retSym. */ + (*retSym) = pSym; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; + } + else + { + /* An error occured fetching the symbol. */ + ret = retFromCall; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not fetch symbol in <"); + + /* Attempt to get the library path for the error log. */ + if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, (char**)&pSym, &pSymLength) == COMMON_ERROR_SUCCESS) && + (pSym != NULL)) + { + COMMON_LOG_VERBOSE((char*)pSym); + } + else + { + COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); + } + pSym = NULL; /* Clear abused pSym. */ + + COMMON_LOG_VERBOSE("> Host function returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* Could not get osSpecificPointerData from management structure. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not get os specific data pointer from management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + else + { + /* Could not reset last call incountered an error flag. */ + ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not reset management structure's error flag. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + else + { + /* Library is not loaded. */ + ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The given library <"); + + /* Attempt to get the library path for the error log. */ + if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, (char**)&pSym, &pSymLength) == COMMON_ERROR_SUCCESS) && + (pSym != NULL)) + { + COMMON_LOG_VERBOSE((char*)pSym); + } + else + { + COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); + } + pSym = NULL; /* Clear abused pSym. */ + + COMMON_LOG_VERBOSE("> is not loaded."); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* symbolName is NULL. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): No symbol name was given, cannot load a symbol without a name to identifiy it."); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* retSym is NULL. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); + if (retFromCall != COMMON_ERROR_SUCCESS) + { + COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); + COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); + } + } + } + else + { + /* Library structure is invalid. */ + ret = COMMON_ERROR_INVALID_ARGUMENT; + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The engine's library structure for the given library is invalid. Unable to lookup function without a valid library structure."); + } + + /* Return result. */ + return ret; + } + +#ifdef __cplusplus +} /* End of extern C. */ +#endif /* __cplusplus */ diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h index 15be2bf..16931cd 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem.h @@ -101,20 +101,36 @@ extern "C" { */ MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib); + /*! + * int Common_Dynamic_Library_Subsystem_Reload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + * + * This function reloads the given library. + * + * (Effectively this function is a wrapper around Common_Dynamic_Library_Subsystem_Load_Library(), setting it's + * reloadLibrary argument to true, and using the given management structure's path.) + * + * Returns COMMON_ERROR_SUCCESS if the library is reloaded successfully. + * + * Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL, or if the given management structure + * does not define a path. (Such as when it's first created or if it was cleared by a call to + * Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library().) + * + * Otherwise returns any error given by Common_Dynamic_Library_Subsystem_Load_Library(). + */ + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Reload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib); + /*! * int Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, - * const char * symbolName, void ** retSym) + * const char * symbolName, const size_t symbolNameLength, void ** retSym) * * This function calls the system specific dynamic library handler and attempts to fetch a pointer to the first byte * of the given symbol. * - * Note: To check for an error result from this function, check the value of lib.bLastCallEncounteredAnError. - * If lib.bLastCallEncounteredAnError is false, no error occured during this function. Otherwise an error occured. - * * Pram: Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library * lib, A pointer to a properly constructed * management data structure used internally. * Pram: const char * symbolName, A pointer to a c-string that contains the name of the requested symbol to * search for. + * Pram: const size_t symbolNameLength, the length of symbolName in bytes. * Pram: void ** retSym, A double pointer that will hold the address to the first byte of the requested symbol if the function succeeds. * (retSym will NOT be modified if this function returns an error.) * @@ -125,7 +141,7 @@ extern "C" { * Returns COMMON_ERROR_INVALID_ARGUMENT if the given Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library, symbolName, or retSym pointer(s) was / were set to NULL. * Returns DYNLIB_ERROR_LIBRARY_NOT_LOADED if the library was not loaded according to the given management data structure. */ - MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName, void ** retSym); + MSYS_DLL_EXPORT int Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName, const size_t symbolNameLength, void ** retSym); #ifdef __cplusplus } /* End of extern C. */ #endif /* __cplusplus */ From 5a48cd5abd07d9ad4b184391e9c1fcf716b92605 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sat, 23 Jan 2016 03:41:57 -0500 Subject: [PATCH 49/50] Add Dynamic_Library_Subsystem_Syscall.h. This commit adds the Dynamic_Library_Subsystem_Syscall.h header file which defines the function signatures for the needed syscalls used by the Dynamic Library Subsystem. --- .../Dynamic_Library_Subsystem_Syscall.h | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Syscall.h diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Syscall.h b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Syscall.h new file mode 100644 index 0000000..da9f930 --- /dev/null +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Syscall.h @@ -0,0 +1,121 @@ +/*! + Multiverse Engine Project 23/1/2016 Dynamic_Library_Subsystem Dynamic_Library_Subsystem_Syscall.h + + Copyright (C) 2016 Multiverse Engine Project + + This program is free software; + you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; + either version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with this program; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Official source repository and project information can be found at + https://github.com/codebase7/mengine +*/ + +/* Include guard. */ +#ifndef MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_SYSCALL_H +#define MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_SYSCALL_H + +/*! + int Common_Dynamic_Library_Subsystem_Load_Library_Syscall( const char * pathToLibrary, + const size_t pathToLibraryLength, + void ** osSpecificPointerData) + + Issues the system call that performs the actual loading of a library. + + NOTE: This function should NOT be called directly by anything except + Common_Dynamic_Library_Subsystem_Load_Library(). Use that function + if you want to load a given library. + + Any reference / data structure returned by the host system call that is required to + reference the loaded library in the future will be pointed to by osSpecificPointerData. + (Memory allocation may be performed here, but any allocation must be deallocated by the + Unload Library syscall function if an allocation is performed.) + + This function also translates any error code given by the host system into a + Common Namespace Error Code before returning it. + + ---General result code list--- + Note: Due to translation of host error codes, ANY Common Namespace Error Code + is possible here: + Returns COMMON_ERROR_SUCCESS if the library was loaded successfully. + + Returns COMMON_ERROR_INVALID_ARGUMENT if one of the given pointers is NULL, + or the length argument is less than or equal to zero. + + Otherwise returns the appropriate error code. + + No-alteration clause: + - In case of error, this function will not modify it's arguments. + */ +int Common_Dynamic_Library_Subsystem_Load_Library_Syscall(const char * pathToLibrary, const size_t pathToLibraryLength, void ** osSpecificPointerData); + +/*! + int Common_Dynamic_Library_Subsystem_Unload_Library_Syscall(void * osData) + + Issues the system call that performs the actual unloading of a library. + + NOTE: This function should NOT be called directly by anything except + Common_Dynamic_Library_Subsystem_Unload_Library(). Use that function + if you want to unload a given library. + + Any allocation made by the Load Library syscall must be deallocated by this function, + if an allocation was performed. + + This function also translates any error code given by the host system into a + Common Namespace Error Code before returning it. + + ---General result code list--- + Note: Due to translation of host error codes, ANY Common Namespace Error Code + is possible here: + Returns COMMON_ERROR_SUCCESS if the library was unloaded successfully. + + Returns COMMON_ERROR_INVALID_ARGUMENT if the given pointer is NULL. + + Otherwise returns the appropriate error code. + + No-alteration clause: + - In case of error, this function will not modify it's arguments. + */ +int Common_Dynamic_Library_Subsystem_Unload_Library_Syscall(void * osData); + +/*! + int Common_Dynamic_Library_Subsystem_Get_Symbol_Syscall(void * osData, + const char * symbolName, + const size_t symbolNameLength, + void ** retSym) + + Issues the system call that performs the actual lookup of a given symbol in a given library. + + NOTE: This function should NOT be called directly by anything except + Common_Dynamic_Library_Subsystem_Get_Symbol(). Use that function + if you want to lookup a given symbol. + + This function also translates any error code given by the host system into a + Common Namespace Error Code before returning it. + + ---General result code list--- + Note: Due to translation of host error codes, ANY Common Namespace Error Code + is possible here: + Returns COMMON_ERROR_SUCCESS if the given symbol was found successfully. + (The given symbol will be pointed to by retSym.) + + Returns COMMON_ERROR_INVALID_ARGUMENT if one of the given pointers is NULL, + or the length argument is less than or equal to zero. + + Otherwise returns the appropriate error code. + + No-alteration clause: + - In case of error, this function will not modify it's arguments. + */ +int Common_Dynamic_Library_Subsystem_Get_Symbol_Syscall(void * osData, const char * symbolName, const size_t symbolNameLength, void ** retSym); + +#endif MSYS_DYNAMIC_LIBRARY_SUBSYSTEM_SYSCALL_H + +/* End of Dynamic_Library_Subsystem_Syscall.h. */ From d90d806e426ee0a4d75f6bffd40759186dcffda4 Mon Sep 17 00:00:00 2001 From: codebase7 Date: Sat, 23 Jan 2016 03:43:25 -0500 Subject: [PATCH 50/50] Convert Dynamic_Library_Subsystem_POSIX.c and Dynamic_Library_Subsystem_Windows.c to syscalls. This commit converts the functions in both Dynamic_Library_Subsystem_POSIX.c and Dynamic_Library_Subsystem_Windows.c to syscalls used by Dynamic_Library_Subsystem.c. --- .../Dynamic_Library_Subsystem_POSIX.c | 508 +++--------------- .../Dynamic_Library_Subsystem_Windows.c | 471 ++-------------- 2 files changed, 137 insertions(+), 842 deletions(-) diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c index f94d0c5..d94e773 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_POSIX.c @@ -22,7 +22,7 @@ #ifdef __linux__ /* Internal includes */ -#include "Dynamic_Library_Subsystem.h" +#include "Dynamic_Library_Subsystem_Syscall.h" #include "../Error_Handler/Common_Error_Handler_Structures.h" /* External includes. */ @@ -33,493 +33,149 @@ /* Define extern C. */ extern "C" { #endif /* __cplusplus */ - int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const size_t pathToLibraryLength, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + int Common_Dynamic_Library_Subsystem_Load_Library_Syscall(const char * pathToLibrary, const size_t pathToLibraryLength, void ** osSpecificPointerData) { /* Init vars. */ int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ void * callResult = NULL; /* The result of the call to dlopen(). */ char * hostErr = NULL; /* The result returned from dlerror(). */ - char * tempPath = NULL; /* Used to load the library. */ - /* Check to see if the pointer to the management structure is valid. */ - if (lib != NULL) + /* Check for invalid arguments. */ + if ((pathToLibrary != NULL) && (pathToLibraryLength > 0) && (osSpecificPointerData != NULL)) { - /* Check to see if pathToLibrary is NULL. */ - if (pathToLibrary != NULL) - { - /* Check to see if the library is loaded. */ - retFromCall = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(lib); - if ((retFromCall == COMMON_ERROR_FALSE) || (retFromCall == COMMON_ERROR_INVALID_ARGUMENT) || (reloadLibrary)) - { - /* Check and see if the library is loaded. */ - if (retFromCall == COMMON_ERROR_TRUE) - { - /* Call Unload_Library. */ - retFromCall = Common_Dynamic_Library_Subsystem_Unload_Library(lib); - } - else - { - /* Reset retFromCall. */ - retFromCall = COMMON_ERROR_UNKNOWN_ERROR; - } - - /* Only continue if the library was unloaded, or if we did not need to unload the library. */ - if ((retFromCall == COMMON_ERROR_UNKNOWN_ERROR) || (retFromCall == COMMON_ERROR_SUCCESS)) - { - /* Blank the values in lib. */ - Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library(lib); - - /* Copy the path string into lib. */ - retFromCall = Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library(lib, pathToLibrary, pathToLibraryLength); - if (retFromCall == COMMON_ERROR_SUCCESS) - { - /* Get the path back. */ - retFromCall = Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &tempPath); - if ((retFromCall == COMMON_ERROR_SUCCESS) && (tempPath != NULL)) - { - /* Call dlopen(). */ - callResult = dlopen(tempPath, RTLD_LAZY | RTLD_LOCAL); - - /* Check the callResult. */ - if (callResult == NULL) - { - /* An error occured. - There is no clean way to check the error given here, as dlerror() returns a human-readable string. - In addition, dlopen() does not have any defined error codes in the POSIX standard. - As such we have no way of returning the specific error encountered to the caller, - so we must return COMMON_ERROR_SYSTEM_SPECIFIC. - */ - ret = COMMON_ERROR_SYSTEM_SPECIFIC; + /* Call dlopen(). */ + callResult = dlopen(pathToLibrary, RTLD_LAZY | RTLD_LOCAL); - /* Could not load the library. */ - hostErr = dlerror(); - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Could not load <"); - COMMON_LOG_VERBOSE(tempPath); - COMMON_LOG_VERBOSE("> Host function returned: "); - COMMON_LOG_VERBOSE(hostErr); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - /* Log additional error. */ - COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - else - { - /* Cast the OS specific data structure pointer to void*. */ - retFromCall = Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(lib, ((void*)callResult)); - if (retFromCall == COMMON_ERROR_SUCCESS) - { - /* Set bIsLoaded. */ - retFromCall = Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library(lib, true); - if (retFromCall == COMMON_ERROR_SUCCESS) - { - /* Success. */ - ret = COMMON_ERROR_SUCCESS; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): <"); - COMMON_LOG_VERBOSE(tempPath); - COMMON_LOG_VERBOSE("> loaded."); - } - else - { - /* Could not set is loaded flag. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - } - } - else - { - /* Could not set os specific pointer data. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - } - } - } - else - { - /* Could not retrive path from lib structure. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - } - } - else - { - /* Could not copy path data. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - } - } - else - { - /* Encountered an error during the unload. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Unable to reload library."); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - /* Log additional error. */ - COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - } - else - { - /* Library is already loaded. */ - ret = DYNLIB_ERROR_LIBRARY_ALREADY_LOADED; - } + /* Check the callResult. */ + if (callResult == NULL) + { + /* An error occured. + There is no clean way to check the error given here, as dlerror() returns a human-readable string. + In addition, dlopen() does not have any defined error codes in the POSIX standard. + As such we have no way of returning the specific error encountered to the caller, + so we must return COMMON_ERROR_SYSTEM_SPECIFIC. + */ + ret = COMMON_ERROR_SYSTEM_SPECIFIC; + + /* Could not load the library. */ + hostErr = dlerror(); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library_Syscall(): Could not load <"); + COMMON_LOG_VERBOSE(pathToLibrary); + COMMON_LOG_VERBOSE("> Host function returned: "); + COMMON_LOG_VERBOSE(hostErr); } else { - /* pathToLibrary is NULL. */ - ret = COMMON_ERROR_INVALID_ARGUMENT; - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): No path to the library was given. Unable to load a library without the path to it."); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - /* Log additional error. */ - COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } + /* Cast the OS specific data structure pointer to void*. */ + (*osSpecificPointerData) = (void*)callResult; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; } } else { - /* Management structure is invalid. */ + /* Invalid argument. */ ret = COMMON_ERROR_INVALID_ARGUMENT; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): The engine's library structure for the given library is invalid. Unable to load a library without a valid library structure."); } /* Exit function. */ return ret; } - int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + int Common_Dynamic_Library_Subsystem_Unload_Library_Syscall(void * osData) { /* Init vars. */ int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ char * hostErr = NULL; /* The result returned from dlerror(). */ - void * osData = NULL; /* The osSpecificPointerData from the management structure. */ - char * pSym = NULL; /* Used to log the library path to the error log. */ - /* Check to see if the pointer to the management structure is valid. */ - if (lib != NULL) + /* Check for invalid arguments. */ + if (osData != NULL) { - /* Check for a loaded library. */ - retFromCall = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(lib); - if (retFromCall == COMMON_ERROR_TRUE) + /* Call dlclose(). */ + retFromCall = dlclose(osData); + if (retFromCall == 0) { - /* Reset bLastCallEncounteredAnError. */ - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, false); - if (retFromCall == COMMON_ERROR_SUCCESS) - { - /* Get the osSpecificPointerData from the management structure. */ - retFromCall = Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library(lib, &osData); - if ((retFromCall == COMMON_ERROR_SUCCESS) && (osData != NULL)) - { - /* Call dlclose(). */ - retFromCall = dlclose(lib->osSpecificPointerData); - if (retFromCall == 0) - { - /* The library was unloaded successfully. */ - retFromCall = Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library(lib, false); - if (retFromCall == COMMON_ERROR_SUCCESS) - { - retFromCall = Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(lib, NULL); - if (retFromCall == COMMON_ERROR_SUCCESS) - { - /* Success. */ - ret = COMMON_ERROR_SUCCESS; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): <"); - - /* Attempt to get the library path for the error log. */ - if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &pSym) == COMMON_ERROR_SUCCESS) && - (pSym != NULL)) - { - COMMON_LOG_VERBOSE(pSym); - } - else - { - COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); - } - pSym = NULL; /* Clear abused pSym. */ - - COMMON_LOG_VERBOSE("> unloaded."); - } - else - { - /* Could not clear os specific pointer data in management structure. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not clear os specific pointer data in management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - } - else - { - /* Could not clear is loaded flag in management structure. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not clear is loaded flag in management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - } - else - { - /* An error occured. - There is no clean way to check the error given here, as dlerror() returns a human-readable string. - In addition, dlclose() does not have any defined error codes in the POSIX standard. - As such we have no way of returning the specific error encountered to the caller, - so we must return COMMON_ERROR_SYSTEM_SPECIFIC. - */ - ret = COMMON_ERROR_SYSTEM_SPECIFIC; - - /* Could not load the library. */ - hostErr = dlerror(); - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not unload <"); - - /* Attempt to get the library path for the error log. */ - if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &pSym) == COMMON_ERROR_SUCCESS) && - (pSym != NULL)) - { - COMMON_LOG_VERBOSE(pSym); - } - else - { - COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); - } - pSym = NULL; /* Clear abused pSym. */ - - COMMON_LOG_VERBOSE("> Host function returned: "); - COMMON_LOG_VERBOSE(hostErr); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - } - else - { - /* Could not get osSpecificPointerData from management structure. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not get os specific data pointer from management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - else - { - /* Could not reset last call incountered an error flag. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not reset management structure's error flag. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } + /* The library was unloaded successfully. */ + ret = COMMON_ERROR_SUCCESS; } else { - /* Library is not loaded. */ - ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The given library <"); - - /* Attempt to get the library path for the error log. */ - if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &pSym) == COMMON_ERROR_SUCCESS) && - (pSym != NULL)) - { - COMMON_LOG_VERBOSE(pSym); - } - else - { - COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); - } - pSym = NULL; /* Clear abused pSym. */ - - COMMON_LOG_VERBOSE("> is not loaded."); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } + /* An error occured. + There is no clean way to check the error given here, as dlerror() returns a human-readable string. + In addition, dlclose() does not have any defined error codes in the POSIX standard. + As such we have no way of returning the specific error encountered to the caller, + so we must return COMMON_ERROR_SYSTEM_SPECIFIC. + */ + ret = COMMON_ERROR_SYSTEM_SPECIFIC; + + /* Could not load the library. */ + hostErr = dlerror(); + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library_Syscall(): Could not unload given library."); + COMMON_LOG_VERBOSE(" Host function returned: "); + COMMON_LOG_VERBOSE(hostErr); } } else { - /* Management structure is invalid. */ + /* Invalid argument. */ ret = COMMON_ERROR_INVALID_ARGUMENT; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The engine's library structure for the given library is invalid. Unable to unload a library without a valid library structure."); } /* Return result. */ return ret; } - int Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName, void ** retSym) + int Common_Dynamic_Library_Subsystem_Get_Symbol_Syscall(void * osData, const char * symbolName, const size_t symbolNameLength, void ** retSym) { /* Init vars. */ int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ void * pSym = NULL; /* The returned symbol pointer. */ - void * osData = NULL; /* The osSpecificPointerData from the management structure. */ char * hostErr = NULL; /* The result returned from dlerror(). */ - /* Check to see if the pointer to the management structure is valid. */ - if (lib != NULL) + /* Check for invalid arguments. */ + if ((osData != NULL) && (symbolName != NULL) && (retSym != NULL)) { - /* Check and see if retSym is valid. */ - if (retSym != NULL) + /* Call dlerror to clear the error state. */ + dlerror(); + + /* Call dlsym. */ + pSym = dlsym(osData, symbolName); + + /* Call dlerror again to check for an error. */ + hostErr = dlerror(); + if (hostErr != NULL) { - /* Check to see if symbolName is NULL. */ - if (symbolName != NULL) - { - /* Check for a loaded library. */ - retFromCall = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(lib); - if (retFromCall == COMMON_ERROR_TRUE) - { - /* Reset bLastCallEncounteredAnError. */ - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, false); - if (retFromCall == COMMON_ERROR_SUCCESS) - { - /* Get the osSpecificPointerData from the management structure. */ - retFromCall = Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library(lib, &osData); - if ((retFromCall == COMMON_ERROR_SUCCESS) && (osData != NULL)) - { - /* Call dlerror to clear the error state. */ - dlerror(); - - /* Call dlsym. */ - pSym = dlsym(osData, symbolName); - - /* Call dlerror again to check for an error. */ - hostErr = dlerror(); - if (hostErr != NULL) - { - /* An error occured. - There is no clean way to check the error given here, as dlerror() returns a human-readable string. - In addition, dlsym() does not have any defined error codes in the POSIX standard. - As such we have no way of returning the specific error encountered to the caller, - so we must return COMMON_ERROR_SYSTEM_SPECIFIC. - */ - ret = COMMON_ERROR_SYSTEM_SPECIFIC; - - /* An error occured fetching the symbol. */ - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not fetch symbol in <"); - - /* Attempt to get the library path for the error log. */ - if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, (char**)&pSym) == COMMON_ERROR_SUCCESS) && - (pSym != NULL)) - { - COMMON_LOG_VERBOSE((char*)pSym); - } - else - { - COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); - } - pSym = NULL; /* Clear abused pSym. */ - - COMMON_LOG_VERBOSE("> Host function returned: "); - COMMON_LOG_VERBOSE(hostErr); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - else - { - /* Copy pSym to retSym. */ - (*retSym) = pSym; - - /* Success. */ - ret = COMMON_ERROR_SUCCESS; - } - } - else - { - /* Could not get osSpecificPointerData from management structure. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not get os specific data pointer from management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - else - { - /* Could not reset last call incountered an error flag. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not reset management structure's error flag. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - else - { - /* Library is not loaded. */ - ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The given library <"); - - /* Attempt to get the library path for the error log. */ - if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, (char**)&pSym) == COMMON_ERROR_SUCCESS) && - (pSym != NULL)) - { - COMMON_LOG_VERBOSE((char*)pSym); - } - else - { - COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); - } - pSym = NULL; /* Clear abused pSym. */ - - COMMON_LOG_VERBOSE("> is not loaded."); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - } - else - { - /* symbolName is NULL. */ - ret = COMMON_ERROR_INVALID_ARGUMENT; - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): No symbol name was given, cannot load a symbol without a name to identifiy it."); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } + /* An error occured. + There is no clean way to check the error given here, as dlerror() returns a human-readable string. + In addition, dlsym() does not have any defined error codes in the POSIX standard. + As such we have no way of returning the specific error encountered to the caller, + so we must return COMMON_ERROR_SYSTEM_SPECIFIC. + */ + ret = COMMON_ERROR_SYSTEM_SPECIFIC; + + /* An error occured fetching the symbol. */ + COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol_Syscall(): Could not fetch symbol."); + COMMON_LOG_VERBOSE(" Host function returned: "); + COMMON_LOG_VERBOSE(hostErr); } else { - /* retSym is NULL. */ - ret = COMMON_ERROR_INVALID_ARGUMENT; - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } + /* Copy pSym to retSym. */ + (*retSym) = pSym; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; } } else { - /* Library structure is invalid. */ + /* Invalid argument. */ ret = COMMON_ERROR_INVALID_ARGUMENT; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The engine's library structure for the given library is invalid. Unable to lookup function without a valid library structure."); - } + } /* Return result. */ return ret; diff --git a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c index 314807f..c3dfe02 100644 --- a/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c +++ b/src/Common/Src/Dynamic_Library_Subsystem/Dynamic_Library_Subsystem_Windows.c @@ -19,7 +19,7 @@ */ /* Internal includes. */ -#include "Dynamic_Library_Subsystem.h" +#include "Dynamic_Library_Subsystem_Syscall.h" #include "../Error_Handler/Common_Error_Handler_Structures.h" #include "../Error_Handler/Windows_Error_Translation_Table.h" @@ -31,479 +31,118 @@ /* Define extern C. */ extern "C" { #endif /* __cplusplus */ - int Common_Dynamic_Library_Subsystem_Load_Library(const char * pathToLibrary, const size_t pathToLibraryLength, const bool reloadLibrary, Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + + int Common_Dynamic_Library_Subsystem_Load_Library_Syscall(const char * pathToLibrary, const size_t pathToLibraryLength, void ** osSpecificPointerData) { /* Init vars. */ int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ HMODULE callResult = NULL; /* The result of the call to LoadLibraryEx(). */ DWORD retLLEX = 0; /* Error code from LoadLibraryEx(). */ - char * tempPath = NULL; /* Used to load the library. */ - /* Check to see if the pointer to the management structure is valid. */ - if (lib != NULL) + /* Check for invalid arguments. */ + if ((pathToLibrary != NULL) && (pathToLibraryLength > 0) && (osSpecificPointerData != NULL)) { - /* Check to see if pathToLibrary is NULL. */ - if (pathToLibrary != NULL) - { - /* Check to see if the library is loaded. */ - retFromCall = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(lib); - if ((retFromCall == COMMON_ERROR_FALSE) || (retFromCall == COMMON_ERROR_INVALID_ARGUMENT) || (reloadLibrary)) - { - /* Check and see if the library is loaded. */ - if (retFromCall == COMMON_ERROR_TRUE) - { - /* Call Unload_Library. */ - retFromCall = Common_Dynamic_Library_Subsystem_Unload_Library(lib); - } - else - { - /* Reset retFromCall. */ - retFromCall = COMMON_ERROR_UNKNOWN_ERROR; - } - - /* Only continue if the library was unloaded, or if we did not need to unload the library. */ - if ((retFromCall == COMMON_ERROR_UNKNOWN_ERROR) || (retFromCall == COMMON_ERROR_SUCCESS)) - { - /* Blank the values in lib. */ - Common_Dynamic_Library_Subsystem_Blank_Loaded_Dynamic_Library(lib); - - /* Copy the path string into lib. */ - retFromCall = Common_Dynamic_Library_Subsystem_Set_PathToLibrary_Loaded_Dynamic_Library(lib, pathToLibrary, pathToLibraryLength); - if (retFromCall == COMMON_ERROR_SUCCESS) - { - /* Get the path back. */ - retFromCall = Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &tempPath); - if ((retFromCall == COMMON_ERROR_SUCCESS) && (tempPath != NULL)) - { - /* Call LoadLibraryEx(). */ - callResult = LoadLibraryEx(tempPath, NULL, 0); - - /* Check the callResult. */ - if (callResult == NULL) - { - /* Get the last error. */ - retLLEX = GetLastError(); - retFromCall = Common_Translate_Windows_Error_Code_To_Common_Error_Code(retLLEX); + /* Call LoadLibraryEx(). */ + callResult = LoadLibraryEx(pathToLibrary, NULL, 0); - /* Could not load the library. */ - ret = retFromCall; - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Could not load <"); - COMMON_LOG_VERBOSE(tempPath); - COMMON_LOG_VERBOSE("> Host function returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - /* Log additional error. */ - COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - else - { - /* Cast the OS specific data structure pointer to void*. */ - retFromCall = Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(lib, ((void*)callResult)); - if (retFromCall == COMMON_ERROR_SUCCESS) - { - /* Set bIsLoaded. */ - retFromCall = Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library(lib, true); - if (retFromCall == COMMON_ERROR_SUCCESS) - { - /* Success. */ - ret = COMMON_ERROR_SUCCESS; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): <"); - COMMON_LOG_VERBOSE(tempPath); - COMMON_LOG_VERBOSE("> loaded."); - } - else - { - /* Could not set is loaded flag. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - } - } - else - { - /* Could not set os specific pointer data. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - } - } - } - else - { - /* Could not retrive path from lib structure. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - } - } - else - { - /* Could not copy path data. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - } - } - else - { - /* Encountered an error during the unload. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): Unable to reload library."); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - /* Log additional error. */ - COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - } - else - { - /* Library is already loaded. */ - ret = DYNLIB_ERROR_LIBRARY_ALREADY_LOADED; - } + /* Check the callResult. */ + if (callResult == NULL) + { + /* Get the last error. */ + retLLEX = GetLastError(); + ret = Common_Translate_Windows_Error_Code_To_Common_Error_Code(retLLEX); } else { - /* pathToLibrary is NULL. */ - ret = COMMON_ERROR_INVALID_ARGUMENT; - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): No path to the library was given. Unable to load a library without the path to it."); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - /* Log additional error. */ - COMMON_LOG_VERBOSE(" Additionally, we could not set the error flag for the internal library structure. Engine Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } + /* Cast the OS specific data structure pointer to void*. */ + (*osSpecificPointerData) = ((void*)callResult); + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; } } else { - /* Management structure is invalid. */ + /* Invalid argument. */ ret = COMMON_ERROR_INVALID_ARGUMENT; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Load_Library(): The engine's library structure for the given library is invalid. Unable to load a library without a valid library structure."); } - + /* Exit function. */ return ret; } - - int Common_Dynamic_Library_Subsystem_Unload_Library(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib) + + int Common_Dynamic_Library_Subsystem_Unload_Library_Syscall(void * osData) { /* Init vars. */ int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ DWORD retFL = 0; /* Error code from FreeLibrary(). */ - void * osData = NULL; /* The osSpecificPointerData from the management structure. */ - char * pSym = NULL; /* Used to log the library path to the error log. */ - - /* Check to see if the pointer to the management structure is valid. */ - if (lib != NULL) + + /* Check for invalid arguments. */ + if (osData != NULL) { - /* Check for a loaded library. */ - retFromCall = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(lib); - if (retFromCall == COMMON_ERROR_TRUE) + /* Call FreeLibrary. */ + if (FreeLibrary((HMODULE)osData)) { - /* Reset bLastCallEncounteredAnError. */ - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, false); - if (retFromCall == COMMON_ERROR_SUCCESS) - { - /* Get the osSpecificPointerData from the management structure. */ - retFromCall = Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library(lib, &osData); - if ((retFromCall == COMMON_ERROR_SUCCESS) && (osData != NULL)) - { - /* Call FreeLibrary. */ - if (FreeLibrary((HMODULE)osData)) - { - /* The library was unloaded successfully. */ - retFromCall = Common_Dynamic_Library_Subsystem_Set_IsLoaded_Loaded_Dynamic_Library(lib, false); - if (retFromCall == COMMON_ERROR_SUCCESS) - { - retFromCall = Common_Dynamic_Library_Subsystem_Set_OsSpecificPointerData_Loaded_Dynamic_Library(lib, NULL); - if (retFromCall == COMMON_ERROR_SUCCESS) - { - /* Success. */ - ret = COMMON_ERROR_SUCCESS; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): <"); - - /* Attempt to get the library path for the error log. */ - if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &pSym) == COMMON_ERROR_SUCCESS) && - (pSym != NULL)) - { - COMMON_LOG_VERBOSE(pSym); - } - else - { - COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); - } - pSym = NULL; /* Clear abused pSym. */ - - COMMON_LOG_VERBOSE("> unloaded."); - } - else - { - /* Could not clear os specific pointer data in management structure. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not clear os specific pointer data in management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - } - else - { - /* Could not clear is loaded flag in management structure. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not clear is loaded flag in management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - } - else - { - /* Get the last error. */ - retFL = GetLastError(); - retFromCall = Common_Translate_Windows_Error_Code_To_Common_Error_Code(retFL); - - /* Could not unload the library. */ - ret = retFromCall; - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not unload <"); - - /* Attempt to get the library path for the error log. */ - if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &pSym) == COMMON_ERROR_SUCCESS) && - (pSym != NULL)) - { - COMMON_LOG_VERBOSE(pSym); - } - else - { - COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); - } - pSym = NULL; /* Clear abused pSym. */ - - COMMON_LOG_VERBOSE("> Host function returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - } - else - { - /* Could not get osSpecificPointerData from management structure. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not get os specific data pointer from management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - else - { - /* Could not reset last call incountered an error flag. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): Could not reset management structure's error flag. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } + /* The library was unloaded successfully. */ + ret = COMMON_ERROR_SUCCESS; } else { - /* Library is not loaded. */ - ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The given library <"); - - /* Attempt to get the library path for the error log. */ - if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, &pSym) == COMMON_ERROR_SUCCESS) && - (pSym != NULL)) - { - COMMON_LOG_VERBOSE(pSym); - } - else - { - COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); - } - pSym = NULL; /* Clear abused pSym. */ - - COMMON_LOG_VERBOSE("> is not loaded."); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } + /* Get the last error. */ + retFL = GetLastError(); + ret = Common_Translate_Windows_Error_Code_To_Common_Error_Code(retFL); } } else { - /* Management structure is invalid. */ + /* Invalid argument. */ ret = COMMON_ERROR_INVALID_ARGUMENT; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Unload_Library(): The engine's library structure for the given library is invalid. Unable to unload a library without a valid library structure."); } - - /* Return result. */ + + /* Exit function. */ return ret; } - int Common_Dynamic_Library_Subsystem_Get_Symbol(Common_Dynamic_Library_Subsystem_Loaded_Dynamic_Library *const lib, const char * symbolName, void ** retSym) + int Common_Dynamic_Library_Subsystem_Get_Symbol_Syscall(void * osData, const char * symbolName, const size_t symbolNameLength, void ** retSym) { /* Init vars. */ int ret = COMMON_ERROR_UNKNOWN_ERROR; /* The result of this function. */ int retFromCall = COMMON_ERROR_UNKNOWN_ERROR; /* The result of calls to other engine functions. */ - void * pSym = NULL; /* The returned symbol pointer. */ - void * osData = NULL; /* The osSpecificPointerData from the management structure. */ DWORD retGPA = 0; /* Error code from GetProcAddress(). */ - - /* Check to see if the pointer to the management structure is valid. */ - if (lib != NULL) + void * pSym = NULL; /* The returned symbol pointer. */ + + /* Check for invalid arguments. */ + if ((osData != NULL) && (symbolName != NULL) && (retSym != NULL)) { - /* Check and see if retSym is valid. */ - if (retSym != NULL) + /* Get the address. */ + pSym = (void*)GetProcAddress((HMODULE)osData, symbolName); + if (pSym == NULL) { - /* Check to see if symbolName is NULL. */ - if (symbolName != NULL) - { - /* Check for a loaded library. */ - retFromCall = Common_Dynamic_Library_Subsystem_Get_IsLoaded_Loaded_Dynamic_Library(lib); - if (retFromCall == COMMON_ERROR_TRUE) - { - /* Reset bLastCallEncounteredAnError. */ - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, false); - if (retFromCall == COMMON_ERROR_SUCCESS) - { - /* Get the osSpecificPointerData from the management structure. */ - retFromCall = Common_Dynamic_Library_Subsystem_Get_OsSpecificPointerData_Loaded_Dynamic_Library(lib, &osData); - if ((retFromCall == COMMON_ERROR_SUCCESS) && (osData != NULL)) - { - /* Get the address. */ - pSym = (void*)GetProcAddress((HMODULE)osData, symbolName); - if (pSym == NULL) - { - /* Get the last error. */ - retGPA = GetLastError(); - retFromCall = Common_Translate_Windows_Error_Code_To_Common_Error_Code(retGPA); - - /* An error occured fetching the symbol. */ - ret = retFromCall; - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not fetch symbol in <"); - - /* Attempt to get the library path for the error log. */ - if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, (char**)&pSym) == COMMON_ERROR_SUCCESS) && - (pSym != NULL)) - { - COMMON_LOG_VERBOSE((char*)pSym); - } - else - { - COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); - } - pSym = NULL; /* Clear abused pSym. */ - - COMMON_LOG_VERBOSE("> Host function returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(ret)); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - else - { - /* Copy pSym to retSym. */ - (*retSym) = pSym; - - /* Success. */ - ret = COMMON_ERROR_SUCCESS; - } - } - else - { - /* Could not get osSpecificPointerData from management structure. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not get os specific data pointer from management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - else - { - /* Could not reset last call incountered an error flag. */ - ret = ((retFromCall != COMMON_ERROR_SUCCESS) ? (retFromCall) : (COMMON_ERROR_INTERNAL_ERROR)); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): Could not reset management structure's error flag. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - else - { - /* Library is not loaded. */ - ret = DYNLIB_ERROR_LIBRARY_NOT_LOADED; - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The given library <"); - - /* Attempt to get the library path for the error log. */ - if ((Common_Dynamic_Library_Subsystem_Get_PathToLibrary_Loaded_Dynamic_Library(lib, (char**)&pSym) == COMMON_ERROR_SUCCESS) && - (pSym != NULL)) - { - COMMON_LOG_VERBOSE((char*)pSym); - } - else - { - COMMON_LOG_VERBOSE("[ERROR: COULD NOT FETCH PATH TO LIBRARY FROM MANAGEMENT STRUCTURE.]"); - } - pSym = NULL; /* Clear abused pSym. */ - - COMMON_LOG_VERBOSE("> is not loaded."); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } - } - else - { - /* symbolName is NULL. */ - ret = COMMON_ERROR_INVALID_ARGUMENT; - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): No symbol name was given, cannot load a symbol without a name to identifiy it."); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } - } + /* Get the last error. */ + retGPA = GetLastError(); + ret = Common_Translate_Windows_Error_Code_To_Common_Error_Code(retGPA); } else { - /* retSym is NULL. */ - ret = COMMON_ERROR_INVALID_ARGUMENT; - retFromCall = Common_Dynamic_Library_Subsystem_Set_LastCallEncounteredError_Loaded_Dynamic_Library(lib, true); - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(COMMON_ERROR_INVALID_ARGUMENT)); - if (retFromCall != COMMON_ERROR_SUCCESS) - { - COMMON_LOG_VERBOSE(" Additionally, could not set the error flag in the management structure. Call returned: "); - COMMON_LOG_VERBOSE(Common_Get_Error_Message(retFromCall)); - } + /* Copy pSym to retSym. */ + (*retSym) = pSym; + + /* Success. */ + ret = COMMON_ERROR_SUCCESS; } } else { - /* Library structure is invalid. */ + /* Invalid argument. */ ret = COMMON_ERROR_INVALID_ARGUMENT; - COMMON_LOG_VERBOSE("Common_Dynamic_Library_Subsystem_Get_Symbol(): The engine's library structure for the given library is invalid. Unable to lookup function without a valid library structure."); } - - /* Return result. */ + + /* Exit function. */ return ret; } + #ifdef __cplusplus } /* End of extern C. */ #endif /* __cplusplus */