Function to mangle/demangle functions

后端 未结 3 1288
野性不改
野性不改 2020-12-14 10:06

I have previously, here, been shown that C++ functions aren\'t easily represented in assembly. Now I am interested in reading them one way or another because Callgrind, part

3条回答
  •  爱一瞬间的悲伤
    2020-12-14 10:24

    This is a slight variation on Dave's version above. This is a unique_ptr version with a little bit of checking on the return type, though it looks like you could just ignore that, but somehow that just seems unclean.

    auto cppDemangle (const char *abiName)
    {
        //
        // This function allocates and returns storage in ret
        //
        int status;
        char *ret = abi::__cxa_demangle(abiName, 0 /* output buffer */, 0 /* length */, &status);
    
        auto deallocator = ( [](char *mem) { if (mem) free((void*)mem); } );
    
        if (status) {
            // 0: The demangling operation succeeded.
            // -1: A memory allocation failure occurred.
            // -2: mangled_name is not a valid name under the C++ ABI mangling rules.
            // -3: One of the arguments is invalid.
            std::unique_ptr retval(nullptr, deallocator);
        }
    
        //
        // Create a unique pointer to take ownership of the returned string so it
        // is freed when that pointers goes out of scope
        //
        std::unique_ptr retval(ret, deallocator);
        return retval;
    }
    

提交回复
热议问题