1 /*
2 * analyzer_loader.cpp - analyzer loader
3 *
4 * Copyright (c) 2015 Intel Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Wind Yuan <feng.yuan@intel.com>
19 */
20
21 #include "analyzer_loader.h"
22 #include <dlfcn.h>
23
24 namespace XCam {
25
AnalyzerLoader(const char * lib_path,const char * symbol)26 AnalyzerLoader::AnalyzerLoader (const char *lib_path, const char *symbol)
27 : _handle (NULL)
28 {
29 XCAM_ASSERT (lib_path);
30 _path = strndup (lib_path, XCAM_MAX_STR_SIZE);
31 XCAM_ASSERT (symbol);
32 _symbol = strndup (symbol, XCAM_MAX_STR_SIZE);
33 }
34
~AnalyzerLoader()35 AnalyzerLoader::~AnalyzerLoader ()
36 {
37 close_handle ();
38 if (_path)
39 xcam_free (_path);
40 if (_symbol)
41 xcam_free (_symbol);
42 }
43
44 void *
load_library(const char * lib_path)45 AnalyzerLoader::load_library (const char *lib_path)
46 {
47 void *desc = NULL;
48
49 void *handle = open_handle (lib_path);
50 //XCAM_ASSERT (handle);
51 if (!handle) {
52 XCAM_LOG_WARNING ("open dynamic lib:%s failed", XCAM_STR (lib_path));
53 return NULL;
54 }
55 desc = load_symbol (handle);
56 if (!desc) {
57 XCAM_LOG_WARNING ("get symbol(%s) from lib:%s failed", _symbol, XCAM_STR (lib_path));
58 close_handle ();
59 return NULL;
60 }
61
62 XCAM_LOG_DEBUG ("got symbols(%s) from lib(%s)", _symbol, XCAM_STR (lib_path));
63 return desc;
64 }
65
66 void*
open_handle(const char * lib_path)67 AnalyzerLoader::open_handle (const char *lib_path)
68 {
69 void *handle = NULL;
70
71 if (_handle != NULL)
72 return _handle;
73
74 handle = dlopen (lib_path, RTLD_LAZY);
75 if (!handle) {
76 XCAM_LOG_DEBUG (
77 "open user-defined lib(%s) failed, reason:%s",
78 XCAM_STR (lib_path), dlerror ());
79 return NULL;
80 }
81 _handle = handle;
82 return handle;
83 }
84
85 void *
get_symbol(void * handle)86 AnalyzerLoader::get_symbol (void* handle)
87 {
88 void *desc = NULL;
89
90 XCAM_ASSERT (handle);
91 XCAM_ASSERT (_symbol);
92 desc = (void *)dlsym (handle, _symbol);
93 if (!desc) {
94 XCAM_LOG_DEBUG ("get symbol(%s) failed from lib(%s), reason:%s", _symbol, XCAM_STR (_path), dlerror ());
95 return NULL;
96 }
97
98 return desc;
99 }
100
101 bool
close_handle()102 AnalyzerLoader::close_handle ()
103 {
104 if (!_handle)
105 return true;
106 dlclose (_handle);
107 _handle = NULL;
108 return true;
109 }
110
111 };
112