From 4906642ac3c566bbf365fbe89bc619707b333f61 Mon Sep 17 00:00:00 2001 From: castano Date: Mon, 26 Sep 2011 05:33:53 +0000 Subject: [PATCH] Revert Library.*, needed for CUDA stuff. --- src/nvcore/CMakeLists.txt | 1 - src/nvcore/Debug.h | 8 +++--- src/nvcore/Library.cpp | 45 ++++++++++++++++++++++++++++++++++ src/nvcore/Library.h | 51 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 src/nvcore/Library.cpp create mode 100644 src/nvcore/Library.h diff --git a/src/nvcore/CMakeLists.txt b/src/nvcore/CMakeLists.txt index fa3fd5c..8cfe35c 100644 --- a/src/nvcore/CMakeLists.txt +++ b/src/nvcore/CMakeLists.txt @@ -17,7 +17,6 @@ SET(CORE_SRCS StrLib.h StrLib.cpp Stream.h StdStream.h - TextReader.h TextReader.cpp TextWriter.h TextWriter.cpp Timer.h Utils.h) diff --git a/src/nvcore/Debug.h b/src/nvcore/Debug.h index c2a7fc7..28c81d8 100644 --- a/src/nvcore/Debug.h +++ b/src/nvcore/Debug.h @@ -133,10 +133,10 @@ namespace nv if (reinterpret_cast(ptr) < 0x10000ULL) return false; if (reinterpret_cast(ptr) >= 0x000007FFFFFEFFFFULL) return false; #else - if (reinterpret_cast(ptr) == 0xcccccccc) return false; - if (reinterpret_cast(ptr) == 0xcdcdcdcd) return false; - if (reinterpret_cast(ptr) == 0xdddddddd) return false; - if (reinterpret_cast(ptr) == 0xffffffff) return false; + if (reinterpret_cast(ptr) == 0xcccccccc) return false; + if (reinterpret_cast(ptr) == 0xcdcdcdcd) return false; + if (reinterpret_cast(ptr) == 0xdddddddd) return false; + if (reinterpret_cast(ptr) == 0xffffffff) return false; #endif return true; } diff --git a/src/nvcore/Library.cpp b/src/nvcore/Library.cpp new file mode 100644 index 0000000..d757e8f --- /dev/null +++ b/src/nvcore/Library.cpp @@ -0,0 +1,45 @@ + +#include "Library.h" +#include "Debug.h" + +#if NV_OS_WIN32 +#define WIN32_LEAN_AND_MEAN +#define VC_EXTRALEAN +#include +#elif NV_OS_XBOX +#include +#else +#include +#endif + + + +void * nvLoadLibrary(const char * name) +{ +#if NV_OS_WIN32 + return (void *)LoadLibraryExA( name, NULL, 0 ); +#elif NV_OS_XBOX + return (void *)LoadLibraryA( name ); +#else + return dlopen(name, RTLD_LAZY); +#endif +} + +void nvUnloadLibrary(void * handle) +{ + nvDebugCheck(handle != NULL); +#if NV_OS_WIN32 || NV_OS_XBOX + FreeLibrary((HMODULE)handle); +#else + dlclose(handle); +#endif +} + +void * nvBindSymbol(void * handle, const char * symbol) +{ +#if NV_OS_WIN32 || NV_OS_XBOX + return (void *)GetProcAddress((HMODULE)handle, symbol); +#else + return (void *)dlsym(handle, symbol); +#endif +} diff --git a/src/nvcore/Library.h b/src/nvcore/Library.h new file mode 100644 index 0000000..999e662 --- /dev/null +++ b/src/nvcore/Library.h @@ -0,0 +1,51 @@ +// This code is in the public domain -- castano@gmail.com + +#pragma once +#ifndef NV_CORE_LIBRARY_H +#define NV_CORE_LIBRARY_H + +#include "nvcore.h" + +#if NV_OS_WIN32 +#define LIBRARY_NAME(name) #name ".dll" +#elif NV_OS_DARWIN +#define NV_LIBRARY_NAME(name) "lib" #name ".dylib" +#else +#define NV_LIBRARY_NAME(name) "lib" #name ".so" +#endif + +NVCORE_API void * nvLoadLibrary(const char * name); +NVCORE_API void nvUnloadLibrary(void * lib); +NVCORE_API void * nvBindSymbol(void * lib, const char * symbol); + +class NVCORE_CLASS Library +{ +public: + Library(const char * name) + { + handle = nvLoadLibrary(name); + } + ~Library() + { + if (isValid()) + { + nvUnloadLibrary(handle); + } + } + + bool isValid() const + { + return handle != NULL; + } + + void * bindSymbol(const char * symbol) + { + return nvBindSymbol(handle, symbol); + } + +private: + void * handle; +}; + + +#endif // NV_CORE_LIBRARY_H