Home | History | Annotate | Download | only in platform
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/core/platform/abi.h"
     17 
     18 #if defined(PLATFORM_WINDOWS)
     19 #include <windows.h>
     20 #include <cstring>
     21 #else
     22 #include <cxxabi.h>
     23 #include <cstdlib>
     24 #endif
     25 
     26 #include <memory>
     27 #include <string>
     28 
     29 #if defined(PLATFORM_WINDOWS)
     30 
     31 extern "C" char* __unDName(char* output_string, const char* name,
     32                            int max_string_length, void* (*p_alloc)(std::size_t),
     33                            void (*p_free)(void*), unsigned short disable_flags);
     34 
     35 #endif  // defined(PLATFORM_WINDOWS)
     36 
     37 namespace tensorflow {
     38 namespace port {
     39 
     40 std::string MaybeAbiDemangle(const char* name) {
     41 #if defined(PLATFORM_WINDOWS)
     42   std::unique_ptr<char> demangled{__unDName(nullptr, name, 0, std::malloc,
     43                                             std::free,
     44                                             static_cast<unsigned short>(0))};
     45 
     46   return std::string(demangled.get() != nullptr ? demangled.get() : name);
     47 #else
     48   int status = 0;
     49   std::unique_ptr<char, void (*)(void*)> res{
     50       abi::__cxa_demangle(name, nullptr, nullptr, &status), std::free};
     51   return (status == 0) ? res.get() : name;
     52 #endif
     53 }
     54 
     55 }  // namespace port
     56 }  // namespace tensorflow
     57