comparison deps/Platinum/ThirdParty/Neptune/Source/System/Posix/NptPosixDynamicLibraries.cpp @ 0:3425707ddbf6

Initial import (hopefully this mercurial stuff works...)
author fraserofthenight
date Mon, 06 Jul 2009 08:06:28 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3425707ddbf6
1 /*****************************************************************
2 |
3 | Neptune - Dynamic Libraries :: Posix Implementation
4 |
5 | (c) 2001-2008 Gilles Boccon-Gibod
6 | Author: Gilles Boccon-Gibod (bok@bok.net)
7 |
8 ****************************************************************/
9
10 /*----------------------------------------------------------------------
11 | includes
12 +---------------------------------------------------------------------*/
13 #include "NptLogging.h"
14 #include "NptDynamicLibraries.h"
15
16 #include <dlfcn.h>
17
18 /*----------------------------------------------------------------------
19 | logging
20 +---------------------------------------------------------------------*/
21 NPT_SET_LOCAL_LOGGER("neptune.posix.dynamic-libraries")
22
23 /*----------------------------------------------------------------------
24 | NPT_PosixDynamicLibrary
25 +---------------------------------------------------------------------*/
26 class NPT_PosixDynamicLibrary : public NPT_DynamicLibraryInterface
27 {
28 public:
29 // constructor and destructor
30 NPT_PosixDynamicLibrary(void* library, const char* name) :
31 m_Library(library), m_Name(name) {}
32
33 // NPT_DynamicLibraryInterface methods
34 virtual NPT_Result FindSymbol(const char* name, void*& symbol);
35 virtual NPT_Result Unload();
36
37 private:
38 // members
39 void* m_Library;
40 NPT_String m_Name;
41 };
42
43 /*----------------------------------------------------------------------
44 | NPT_DynamicLibrary::Load
45 +---------------------------------------------------------------------*/
46 NPT_Result
47 NPT_DynamicLibrary::Load(const char* name, NPT_Flags flags, NPT_DynamicLibrary*& library)
48 {
49 if (name == NULL) return NPT_ERROR_INVALID_PARAMETERS;
50
51 // default return value
52 library = NULL;
53
54 // compute the mode
55 int mode = 0;
56 if (flags & NPT_DYANMIC_LIBRARY_LOAD_FLAG_NOW) {
57 mode &= RTLD_NOW;
58 } else {
59 mode &= RTLD_LAZY;
60 }
61
62 // load the lib
63 NPT_LOG_FINE_2("loading library %s, flags=%x", name, flags);
64 void* handle = dlopen(name, mode);
65 if (handle == NULL) {
66 NPT_LOG_FINE("library not found");
67 return NPT_FAILURE;
68 }
69
70 // instantiate the object
71 NPT_LOG_FINE_1("library %s loaded", name);
72 library = new NPT_DynamicLibrary(new NPT_PosixDynamicLibrary(handle, name));
73
74 return NPT_SUCCESS;
75 }
76
77 /*----------------------------------------------------------------------
78 | NPT_PosixDynamicLibrary::FindSymbol
79 +---------------------------------------------------------------------*/
80 NPT_Result
81 NPT_PosixDynamicLibrary::FindSymbol(const char* name, void*& symbol)
82 {
83 if (name == NULL) return NPT_ERROR_INVALID_PARAMETERS;
84 symbol = NULL;
85 if (m_Library == NULL) return NPT_ERROR_NO_SUCH_ITEM;
86
87 NPT_LOG_FINE_1("finding symbol %s", name);
88 symbol = dlsym(m_Library, name);
89 return symbol?NPT_SUCCESS:NPT_ERROR_NO_SUCH_ITEM;
90 }
91
92 /*----------------------------------------------------------------------
93 | NPT_PosixDynamicLibrary::Unload
94 +---------------------------------------------------------------------*/
95 NPT_Result
96 NPT_PosixDynamicLibrary::Unload()
97 {
98 NPT_LOG_FINE_1("unloading library %s", (const char*)m_Name);
99 int result = dlclose(m_Library);
100 if (result == 0) {
101 return NPT_SUCCESS;
102 } else {
103 return NPT_FAILURE;
104 }
105 }