• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SRC_ANDROID_INTERNAL_LAZY_LIBRARY_LOADER_H_
18 #define SRC_ANDROID_INTERNAL_LAZY_LIBRARY_LOADER_H_
19 
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 namespace perfetto {
24 namespace android_internal {
25 
26 // Dynamically loads (once) the libperfetto_android_internal.so and looks up the
27 // given symbol name. It never unloads the library once loaded. Doing so is very
28 // bug-prone (see b/137280403).
29 // |name| can be either a symbol name (e.g. DoFoo) or a (partially or fully)
30 // qualified name (e.g., android_internal::DoFoo). In the latter case the
31 // namespace is dropped only the symbol name is retained (in other words this
32 // function assumes all symbols are declared as extern "C").
33 void* LazyLoadFunction(const char* name);
34 
35 // Convenience wrapper to avoid the reinterpret_cast boilerplate. Boils down to:
36 // FunctionType name = reinterpret_cast<FunctionType>(LazyLoadFunction(...)).
37 // Can be used both for member fields and local variables.
38 #define PERFETTO_LAZY_LOAD(FUNCTION, VAR_NAME)                          \
39   decltype(&FUNCTION) VAR_NAME = reinterpret_cast<decltype(&FUNCTION)>( \
40       ::perfetto::android_internal::LazyLoadFunction(#FUNCTION))
41 
42 }  // namespace android_internal
43 }  // namespace perfetto
44 
45 #endif  // SRC_ANDROID_INTERNAL_LAZY_LIBRARY_LOADER_H_
46