[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be | ||||
3 | // found in the LICENSE file. | ||||
4 | |||||
5 | #include "base/native_library.h" | ||||
6 | |||||
7 | #include <dlfcn.h> | ||||
8 | |||||
9 | #include "base/file_path.h" | ||||
10 | #include "base/logging.h" | ||||
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 11 | #include "base/string_util.h" |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 12 | |
13 | namespace base { | ||||
14 | |||||
15 | // static | ||||
16 | NativeLibrary LoadNativeLibrary(const FilePath& library_path) { | ||||
[email protected] | d0001a5d | 2009-05-18 15:54:50 | [diff] [blame] | 17 | void* dl = dlopen(library_path.value().c_str(), RTLD_LAZY|RTLD_DEEPBIND); |
[email protected] | 6dd6435 | 2009-06-29 23:36:22 | [diff] [blame^] | 18 | if (!dl) { |
19 | LOG(ERROR) << "dlopen failed when trying to open " << library_path.value() | ||||
20 | << ": " << dlerror(); | ||||
21 | } | ||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 22 | |
23 | return dl; | ||||
24 | } | ||||
25 | |||||
26 | // static | ||||
27 | void UnloadNativeLibrary(NativeLibrary library) { | ||||
28 | int ret = dlclose(library); | ||||
29 | if (ret < 0) | ||||
30 | NOTREACHED() << "dlclose failed: " << dlerror(); | ||||
31 | } | ||||
32 | |||||
33 | // static | ||||
34 | void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, | ||||
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 35 | const char* name) { |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 36 | return dlsym(library, name); |
37 | } | ||||
38 | |||||
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 39 | // static |
40 | string16 GetNativeLibraryName(const string16& name) { | ||||
41 | return ASCIIToUTF16("lib") + name + ASCIIToUTF16(".so"); | ||||
42 | } | ||||
43 | |||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 44 | } // namespace base |