You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dlopen_macro.h 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Copyright 2021 Huawei Technologies Co., Ltd
  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. #ifndef MINDSPORE_CCSRC_UTILS_DLOPEN_MACRO_H
  17. #define MINDSPORE_CCSRC_UTILS_DLOPEN_MACRO_H
  18. #ifndef _WIN32
  19. #include <dlfcn.h>
  20. #else
  21. #include <windows.h>
  22. #undef ERROR
  23. #undef SM_DEBUG
  24. #undef Yield
  25. #endif
  26. #include <string>
  27. #include <functional>
  28. #include "utils/log_adapter.h"
  29. #ifndef _WIN32
  30. #define PORTABLE_EXPORT __attribute__((visibility("default")))
  31. #else
  32. #define PORTABLE_EXPORT __declspec(dllexport)
  33. #endif
  34. #define PLUGIN_METHOD(name, return_type, params...) \
  35. extern "C" { \
  36. PORTABLE_EXPORT return_type Plugin##name(params); \
  37. } \
  38. constexpr const char *k##name##Name = "Plugin" #name; \
  39. using name##FunObj = std::function<return_type(params)>; \
  40. using name##FunPtr = return_type (*)(params);
  41. #define ORIGIN_METHOD(name, return_type, params...) \
  42. extern "C" { \
  43. return_type name(params); \
  44. } \
  45. constexpr const char *k##name##Name = #name; \
  46. using name##FunObj = std::function<return_type(params)>; \
  47. using name##FunPtr = return_type (*)(params);
  48. inline static std::string GetDlErrorMsg() {
  49. #ifndef _WIN32
  50. const char *result = dlerror();
  51. return (result == nullptr) ? "Unknown" : result;
  52. #else
  53. return std::to_string(GetLastError());
  54. #endif
  55. }
  56. template <class T>
  57. static T DlsymWithCast(void *handle, const char *symbol_name) {
  58. #ifndef _WIN32
  59. T symbol = reinterpret_cast<T>(reinterpret_cast<intptr_t>(dlsym(handle, symbol_name)));
  60. #else
  61. T symbol = reinterpret_cast<T>(GetProcAddress(reinterpret_cast<HINSTANCE__ *>(handle), symbol_name));
  62. #endif
  63. if (symbol == nullptr) {
  64. MS_LOG(EXCEPTION) << "Dynamically load symbol " << symbol_name << " failed, result = " << GetDlErrorMsg();
  65. }
  66. return symbol;
  67. }
  68. #define DlsymFuncObj(func_name, plugin_handle) DlsymWithCast<func_name##FunPtr>(plugin_handle, k##func_name##Name);
  69. #endif // MINDSPORE_CCSRC_UTILS_DLOPEN_MACRO_H