在VC6中如何知道内联函数是否展开了?

解决方案 »

  1.   

    The __inline keyword is equivalent to inline.Even with __forceinline, the compiler cannot inline code in all circumstances. The compiler cannot inline a function if: The function or its caller is compiled with /Ob0 (the default option for debug builds). The function and the caller use different types of exception handling (C++ exception handling in one, structured exception handling in the other). The function has a variable argument list.The function uses inline assembly, unless compiled with /Og, /Ox, /O1, or /O2.The function is recursive and not accompanied by #pragma inline_recursion(on). With the pragma, recursive functions are inlined to a default depth of 16 calls. To reduce the inlining depth, use inline_depth pragma.The function is virtual and is called virtually. Direct calls to virtual functions can be inlined.The program takes the address of the function and the call is made via the pointer to the function. Direct calls to functions that have had their address taken can be inlined.The function is also ed with the naked __declspec modifier.
      

  2.   


    __forceinline
    DWORD test1( void )
    {
    CONTEXT c;
    RtlCaptureContext( &c );
    return c.Eip;
    }DWORD test2( void )
    {
    CONTEXT c;
    RtlCaptureContext( &c );
    return c.Eip;
    }int _tmain(int argc, _TCHAR* argv[])
    {
    CONTEXT c1, c2;
    DWORD dwEip; RtlCaptureContext( &c1 );
    dwEip = test1();
    RtlCaptureContext( &c2 );
    if ( dwEip >= c1.Eip && dwEip <= c2.Eip )
    {
    cout << "test1 is a inline function." << endl;
    }
    else
    {
    cout << "test1 is a non-inline function." << endl;
    } RtlCaptureContext( &c1 );
    dwEip = test2();
    RtlCaptureContext( &c2 );
    if ( dwEip >= c1.Eip && dwEip <= c2.Eip )
    {
    cout << "test2 is a inline function." << endl;
    }
    else
    {
    cout << "test2 is a non-inline function." << endl;
    }
    system( "pause" );
    return 0;
    }
      

  3.   

    以上代码通过对eip寄存器的检验,判定函数是否内联。
    函数内部的eip是通过返回值传出的。如果你不想改动函数的返回值,可以将函数内部的CONTEXT放到全局域里,结果是一样的。
    CONTEXT g_c;
    __forceinline
    int test1( void )
    {
    cout << "test1 called" << endl;
    RtlCaptureContext( &g_c );
    return 0;
    }
    int test2( void )
    {
    cout << "test2 called" << endl;
    RtlCaptureContext( &g_c );
    return 0;
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
    CONTEXT c1, c2;
    RtlCaptureContext( &c1 );
    test1(); // 调用测试函数1
    RtlCaptureContext( &c2 );
    if ( g_c.Eip >= c1.Eip && g_c.Eip <= c2.Eip )
    {
    cout << "test1 is a inline function." << endl;
    }
    else
    {
    cout << "test1 is a non-inline function." << endl;
    }
    RtlCaptureContext( &c1 );
    test2(); // 调用测试函数2
    RtlCaptureContext( &c2 );
    if ( g_c.Eip >= c1.Eip && g_c.Eip <= c2.Eip )
    {
    cout << "test2 is a inline function." << endl;
    }
    else
    {
    cout << "test2 is a non-inline function." << endl;
    }
    system( "pause" );
    return 0;
    }
    输出结果:test1 called
    test1 is a inline function.
    test2 called
    test2 is a non-inline function.
    请按任意键继续. . .