cuda-api-wrappers
Thin C++-flavored wrappers for the CUDA Runtime API
demangle.hpp
Go to the documentation of this file.
1 
7 #ifndef CUDA_API_WRAPPERS_MANGLING_HPP_
8 #define CUDA_API_WRAPPERS_MANGLING_HPP_
9 
10 #if CUDA_VERSION >= 11040
11 #if !defined(_WIN32) && !defined(WIN32)
12 
13 #include "detail/span.hpp"
14 #include "detail/unique_span.hpp"
15 #include <nv_decode.h>
16 
17 namespace cuda {
18 
19 namespace detail_ {
20 
21 using mangle_status_t = int;
22 
23 inline void validate_mangling_status(int status)
24 {
25  switch (status) {
26  // 0 is fine
27  case -1: throw ::std::runtime_error("Memory allocation failure by __cu_demangle for a demangled CUDA identifier");
28  case -2: throw ::std::invalid_argument("Mangled identifier passed for demangling was invalid");
29  case -3: throw ::std::invalid_argument("Validation of one of the input arguments for a __cu_demangle() call failed");
30  }
31  return;
32 }
33 
34 // TODO: Assuming the length _does_ include the trailing '\0'
35 inline char* demangle(const char* mangled_identifier, char* buffer, size_t& allocated_size)
36 {
37  int status;
38  char* result = __cu_demangle(mangled_identifier, buffer, &allocated_size, &status);
39  validate_mangling_status(status);
40  return result;
41 }
42 
43 inline unique_span<char> demangle(const char* mangled_identifier)
44 {
45  size_t allocated_size { 0 };
46  auto demangled = demangle(mangled_identifier, nullptr, allocated_size);
47 #ifndef NDEBUG
48  if (allocated_size <= 1) {
49  throw ::std::logic_error("Invalid allocation size returned by __cu_demangle()");
50  }
51 #endif
52  return unique_span<char>{demangled, allocated_size - 1, c_free_deleter<char> };
53 }
54 
55 
56 } // namespace detail_
57 
58 inline unique_span<char> demangle(const char* mangled_identifier)
59 {
60  return detail_::demangle(mangled_identifier);
61 }
62 
63 template<typename T>
64 T demangle_as(const char* mangled_identifier)
65 {
66  auto demangled = detail_::demangle(mangled_identifier);
67  return { demangled.data(), demangled.data() + demangled.size() };
68 }
69 
70 template<>
71 inline ::std::string demangle_as<::std::string>(const char* mangled_identifier)
72 {
73  auto demangled = detail_::demangle(mangled_identifier);
74  return { demangled.data(), demangled.size() };
75 }
76 
77 } // namespace cuda
78 
79 #endif // !defined(_WIN32) && !defined(WIN32)
80 #endif // CUDA_VERSION >= 11040
81 #endif // CUDA_API_WRAPPERS_MANGLING_HPP_
Definitions and functionality wrapping CUDA APIs.
Definition: array.hpp:22