mirror of
https://github.com/matrix-construct/construct
synced 2024-11-25 16:22:35 +01:00
ircd: Consolidate demangle related into interface / unit.
This commit is contained in:
parent
e60370de75
commit
8ad264f72d
7 changed files with 130 additions and 48 deletions
39
include/ircd/demangle.h
Normal file
39
include/ircd/demangle.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
// Matrix Construct
|
||||
//
|
||||
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
||||
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice is present in all copies. The
|
||||
// full license for this software is available in the LICENSE file.
|
||||
|
||||
#pragma once
|
||||
#define HAVE_IRCD_DEMANGLE_H
|
||||
|
||||
namespace ircd::util
|
||||
{
|
||||
IRCD_EXCEPTION(ircd::error, demangle_error)
|
||||
IRCD_EXCEPTION(demangle_error, not_mangled)
|
||||
|
||||
string_view demangle(const mutable_buffer &out, const char *const &symbol);
|
||||
string_view demangle(const mutable_buffer &out, const string_view &symbol);
|
||||
std::string demangle(const string_view &symbol);
|
||||
std::string demangle(const char *const &symbol);
|
||||
template<class T> string_view demangle(const mutable_buffer &out);
|
||||
template<class T> std::string demangle();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
std::string
|
||||
ircd::util::demangle()
|
||||
{
|
||||
return demangle(typeid(T).name());
|
||||
}
|
||||
|
||||
template<class T>
|
||||
ircd::string_view
|
||||
ircd::util::demangle(const mutable_buffer &out)
|
||||
{
|
||||
return demangle(out, typeid(T).name());
|
||||
}
|
|
@ -59,10 +59,3 @@ enum class ircd::runlevel
|
|||
QUIT = 4, ///< [trans] Clean shutdown in progress
|
||||
FAULT = -1, ///< [trans] QUIT with exception (dirty shutdown)
|
||||
};
|
||||
|
||||
template<class T>
|
||||
std::string
|
||||
ircd::demangle()
|
||||
{
|
||||
return demangle(typeid(T).name());
|
||||
}
|
||||
|
|
|
@ -19,8 +19,6 @@ namespace ircd::mods
|
|||
IRCD_EXCEPTION(error, invalid_export)
|
||||
IRCD_EXCEPTION(error, expired_symbol)
|
||||
IRCD_EXCEPTION(error, undefined_symbol)
|
||||
IRCD_EXCEPTION(error, demangle_error)
|
||||
IRCD_EXCEPTION(demangle_error, not_mangled)
|
||||
|
||||
struct mod;
|
||||
struct module;
|
||||
|
|
|
@ -187,9 +187,6 @@ namespace ircd
|
|||
extern bool debugmode; ///< Toggle; available only ifdef RB_DEBUG
|
||||
extern bool nolisten; ///< Init option to not bind listener socks.
|
||||
extern bool noautomod; ///< Option to not load modules on init.
|
||||
|
||||
std::string demangle(const std::string &symbol);
|
||||
template<class T> std::string demangle();
|
||||
}
|
||||
|
||||
#include "string_view.h"
|
||||
|
@ -199,6 +196,7 @@ namespace ircd
|
|||
#include "allocator.h"
|
||||
#include "util/util.h"
|
||||
#include "exception.h"
|
||||
#include "demangle.h"
|
||||
#include "localee.h"
|
||||
#include "date.h"
|
||||
#include "logger.h"
|
||||
|
|
|
@ -91,6 +91,7 @@ libircd_la_SOURCES = \
|
|||
ctx.cc \
|
||||
rfc3986.cc \
|
||||
rfc1035.cc \
|
||||
demangle.cc \
|
||||
mods.cc \
|
||||
fmt.cc \
|
||||
db.cc \
|
||||
|
|
89
ircd/demangle.cc
Normal file
89
ircd/demangle.cc
Normal file
|
@ -0,0 +1,89 @@
|
|||
// Matrix Construct
|
||||
//
|
||||
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
||||
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice is present in all copies. The
|
||||
// full license for this software is available in the LICENSE file.
|
||||
|
||||
#include <cxxabi.h>
|
||||
|
||||
thread_local char outbuf[8192];
|
||||
|
||||
std::string
|
||||
ircd::util::demangle(const char *const &symbol)
|
||||
{
|
||||
const string_view demangled
|
||||
{
|
||||
demangle(outbuf, symbol)
|
||||
};
|
||||
|
||||
return std::string(demangled);
|
||||
}
|
||||
|
||||
std::string
|
||||
ircd::util::demangle(const string_view &symbol)
|
||||
{
|
||||
const string_view demangled
|
||||
{
|
||||
demangle(outbuf, symbol)
|
||||
};
|
||||
|
||||
return std::string(demangled);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::util::demangle(const mutable_buffer &out,
|
||||
const string_view &symbol_)
|
||||
{
|
||||
assert(size(symbol_) < 4096);
|
||||
thread_local char symbuf[8192];
|
||||
const string_view symbol
|
||||
{
|
||||
symbuf, strlcpy(symbuf, symbol_)
|
||||
};
|
||||
|
||||
return demangle(out, symbol.data());
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::util::demangle(const mutable_buffer &out,
|
||||
const char *const &symbol)
|
||||
{
|
||||
int status(0);
|
||||
size_t len(size(out));
|
||||
const char *const ret
|
||||
{
|
||||
abi::__cxa_demangle(symbol, data(out), &len, &status)
|
||||
};
|
||||
|
||||
switch(status)
|
||||
{
|
||||
case 0: return string_view
|
||||
{
|
||||
ret, strnlen(ret, len)
|
||||
};
|
||||
|
||||
case -1: throw demangle_error
|
||||
{
|
||||
"Demangle failed -1: memory allocation failure"
|
||||
};
|
||||
|
||||
case -2: throw not_mangled
|
||||
{
|
||||
"Demangle failed -2: mangled name '%s' is not valid", symbol
|
||||
};
|
||||
|
||||
case -3: throw demangle_error
|
||||
{
|
||||
"Demangle failed -3: invalid argument"
|
||||
};
|
||||
|
||||
default: throw demangle_error
|
||||
{
|
||||
"Demangle failed %d: unknown error", status
|
||||
};
|
||||
}
|
||||
}
|
36
ircd/mods.cc
36
ircd/mods.cc
|
@ -8,7 +8,6 @@
|
|||
// copyright notice and this permission notice is present in all copies. The
|
||||
// full license for this software is available in the LICENSE file.
|
||||
|
||||
#include <cxxabi.h>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/dll.hpp>
|
||||
|
||||
|
@ -810,38 +809,3 @@ ircd::mods::prefix_if_relative(const filesystem::path &path)
|
|||
{
|
||||
return path.is_relative()? (modroot / path) : path;
|
||||
}
|
||||
|
||||
std::string
|
||||
ircd::demangle(const std::string &symbol)
|
||||
{
|
||||
size_t len(0);
|
||||
int status(0);
|
||||
const custom_ptr<char> buf
|
||||
{
|
||||
abi::__cxa_demangle(symbol.c_str(), nullptr, &len, &status),
|
||||
std::free
|
||||
};
|
||||
|
||||
switch(status)
|
||||
{
|
||||
case 0:
|
||||
break;
|
||||
|
||||
case -1:
|
||||
throw mods::demangle_error("Demangle failed -1: memory allocation failure");
|
||||
|
||||
case -2:
|
||||
throw mods::not_mangled("Demangle failed -2: mangled name '%s' is not valid", symbol);
|
||||
|
||||
case -3:
|
||||
throw mods::demangle_error("Demangle failed -3: invalid argument");
|
||||
|
||||
default:
|
||||
throw mods::demangle_error("Demangle failed %d: unknown error", status);
|
||||
}
|
||||
|
||||
if(unlikely(!len))
|
||||
return {};
|
||||
|
||||
return std::string { buf.get(), strnlen(buf.get(), len) };
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue