我正在尝试理解一个库实现,但是某些方法动态链接的方式令我感到困惑。 我知道使用派生类的对象时的调度机制,但没有对象我不知道它是如何工作的。 我试图理解的库是来自gcc的libitm。
头文件libitm.h将所有ABI方法声明为顶层方法,没有围绕它们的类或结构。对于大多数人我发现了一个独特的定义,所以我对它们没有任何疑问,我在这里省略它们。但是下面的摘录显示了70种方法的声明,这些定义让我感到困惑。
typedef uint8_t _ITM_TYPE_U1;
typedef uint16_t _ITM_TYPE_U2;
typedef uint32_t _ITM_TYPE_U4;
typedef uint64_t _ITM_TYPE_U8;
typedef float _ITM_TYPE_F;
typedef double _ITM_TYPE_D;
typedef long double _ITM_TYPE_E;
typedef float _Complex _ITM_TYPE_CF;
typedef double _Complex _ITM_TYPE_CD;
typedef long double _Complex _ITM_TYPE_CE;
#define ITM_BARRIERS(T) \
extern _ITM_TYPE_##T _ITM_R##T(const _ITM_TYPE_##T *) ITM_REGPARM; \
extern _ITM_TYPE_##T _ITM_RaR##T(const _ITM_TYPE_##T *) ITM_REGPARM; \
extern _ITM_TYPE_##T _ITM_RaW##T(const _ITM_TYPE_##T *) ITM_REGPARM; \
extern _ITM_TYPE_##T _ITM_RfW##T(const _ITM_TYPE_##T *) ITM_REGPARM; \
extern void _ITM_W##T (_ITM_TYPE_##T *, _ITM_TYPE_##T) ITM_REGPARM; \
extern void _ITM_WaR##T (_ITM_TYPE_##T *, _ITM_TYPE_##T) ITM_REGPARM; \
extern void _ITM_WaW##T (_ITM_TYPE_##T *, _ITM_TYPE_##T) ITM_REGPARM;
ITM_BARRIERS(U1)
ITM_BARRIERS(U2)
ITM_BARRIERS(U4)
ITM_BARRIERS(U8)
ITM_BARRIERS(F)
ITM_BARRIERS(D)
ITM_BARRIERS(E)
ITM_BARRIERS(CF)
ITM_BARRIERS(CD)
ITM_BARRIERS(CE)
在文件dispatch.h中声明了一个struct abi_dispatch,它用作TM算法特定调度的基础。在此结构中,上述70种方法的声明都是纯虚方法。以下摘录显示了方法和结构的宏定义。
#define ITM_READ_M(T, LSMOD, M, M2) \
M _ITM_TYPE_##T ITM_REGPARM ITM_##LSMOD##T##M2 (const _ITM_TYPE_##T *ptr) \
{ \
return load(ptr, abi_dispatch::LSMOD); \
}
#define ITM_READ_M_PV(T, LSMOD, M, M2) \
M _ITM_TYPE_##T ITM_REGPARM ITM_##LSMOD##T##M2 (const _ITM_TYPE_##T *ptr) \
= 0;
#define ITM_WRITE_M(T, LSMOD, M, M2) \
M void ITM_REGPARM ITM_##LSMOD##T##M2 (_ITM_TYPE_##T *ptr, \
_ITM_TYPE_##T val) \
{ \
store(ptr, val, abi_dispatch::LSMOD); \
}
#define ITM_WRITE_M_PV(T, LSMOD, M, M2) \
M void ITM_REGPARM ITM_##LSMOD##T##M2 (_ITM_TYPE_##T *ptr, \
_ITM_TYPE_##T val) \
= 0;
#define CREATE_DISPATCH_METHODS_T(T, M, M2) \
ITM_READ_M(T, R, M, M2) \
ITM_READ_M(T, RaR, M, M2) \
ITM_READ_M(T, RaW, M, M2) \
ITM_READ_M(T, RfW, M, M2) \
ITM_WRITE_M(T, W, M, M2) \
ITM_WRITE_M(T, WaR, M, M2) \
ITM_WRITE_M(T, WaW, M, M2)
#define CREATE_DISPATCH_METHODS_T_PV(T, M, M2) \
ITM_READ_M_PV(T, R, M, M2) \
ITM_READ_M_PV(T, RaR, M, M2) \
ITM_READ_M_PV(T, RaW, M, M2) \
ITM_READ_M_PV(T, RfW, M, M2) \
ITM_WRITE_M_PV(T, W, M, M2) \
ITM_WRITE_M_PV(T, WaR, M, M2) \
ITM_WRITE_M_PV(T, WaW, M, M2)
#define CREATE_DISPATCH_METHODS(M, M2) \
CREATE_DISPATCH_METHODS_T (U1, M, M2) \
CREATE_DISPATCH_METHODS_T (U2, M, M2) \
CREATE_DISPATCH_METHODS_T (U4, M, M2) \
CREATE_DISPATCH_METHODS_T (U8, M, M2) \
CREATE_DISPATCH_METHODS_T (F, M, M2) \
CREATE_DISPATCH_METHODS_T (D, M, M2) \
CREATE_DISPATCH_METHODS_T (E, M, M2) \
CREATE_DISPATCH_METHODS_T (CF, M, M2) \
CREATE_DISPATCH_METHODS_T (CD, M, M2) \
CREATE_DISPATCH_METHODS_T (CE, M, M2)
#define CREATE_DISPATCH_METHODS_PV(M, M2) \
CREATE_DISPATCH_METHODS_T_PV (U1, M, M2) \
CREATE_DISPATCH_METHODS_T_PV (U2, M, M2) \
CREATE_DISPATCH_METHODS_T_PV (U4, M, M2) \
CREATE_DISPATCH_METHODS_T_PV (U8, M, M2) \
CREATE_DISPATCH_METHODS_T_PV (F, M, M2) \
CREATE_DISPATCH_METHODS_T_PV (D, M, M2) \
CREATE_DISPATCH_METHODS_T_PV (E, M, M2) \
CREATE_DISPATCH_METHODS_T_PV (CF, M, M2) \
CREATE_DISPATCH_METHODS_T_PV (CD, M, M2) \
CREATE_DISPATCH_METHODS_T_PV (CE, M, M2)
struct abi_dispatch
{
public:
enum ls_modifier { NONTXNAL, R, RaR, RaW, RfW, W, WaR, WaW };
public:
CREATE_DISPATCH_METHODS_PV(virtual, )
}
可以在method-ml或method-gl中找到abi_dispatch的派生结构。他们使用CREATE_DISPATCH_METHODS(virtual,)和上面的宏来生成70个读/写方法,并将它们映射到加载/存储模板函数。
我的一个大问题是:当调用其中一个库函数时,运行时系统如何知道应该使用哪个方法定义(method-gl或method-ml中的那个)?在读/写方法的定义中,没有提到abi_dispatch对象。线程本地存储中存在一个abi-dispatch对象,但我不知道它是如何适应的。
感谢您的帮助。
答案 0 :(得分:2)
找到缺失的链接。
还有另一个宏(CREATE_DISPATCH_FUNCTIONS),它定义了上面的70个函数声明,并在调度对象中调用了相应的方法。