Tuesday, March 25, 2008

Forcing Cpp Functions To Be Called Non-inlined

Can you think of a way to force a function (usually a very small one) from not being inlined by a compiler for optimization purpose.
Often such little functions are auto-inlined by the optimizer, when compiling in release/optimized mode. Although there is a way to prevent function from inlining by prototyping it with the _cdecl keyword, sometimes all you need just specific calls to be non-inlined.
Here is the macro:
#define NON_INLINED_CALL(a) (((int)(a)+1)?(a):(0))

Now, by forcing some fake calculation (int(a)+1) we stop the compiler from optimizing given line, also by using v ? (a) : (0), we are forcing the compiler to use the _cdecl way of calling the function.
Example:

static int test(int a, int b, int c)
{
return a*b*c;
}
#define NON_INLINED_CALL(a) (((int)(a)+1)?(a):(0))
extern int results[2];

extern int a, b, c;
extern int d, e, f;
void test2( void )

{
results[0] = test(a,b,c); // Eventually with good optimizations this will be inlined
results[1] = NON_INLINED_CALL(test)(d,e,f); // No inlining here
}

Under MSVC we can construct simple and faster macro
#define NON_INLINED_CALL_MSVC(a) ((a) ? (a) : 0)

but the latter will not stop GCC optimizer, and we'll not get the results we wanted to.

P.S. I didn't test any other compilers - only GCC 2.99(ps2-ee one), and MSVC6.00

Monday, March 24, 2008

Preventing object creation on stack

In C++, sometimes you don't want the user of your class to create the object of it on stack, because the stack resource is rare. Is there any way to do this?
Yes!!!
What you should do is to make your destructor private, rather than public or protected.
You can do like this:
class CTest {
private:
~CTest ();
}
Why only private, because if you make it protected, yes the object this class can't not be created on stack, but it's derived class can!

Then, if you want to create the object on stack, like
CTest test;

The compiler is gona complain, the error message is most probably like 'CTest::~CTest' : cannot access private member declared in class CTest'.

That's because the compiler will actually check if it can delete the object if it's created on stack, because it's compiler's business to take care of this. If you do like:
CTest* test
or
CTest* test = new CTest
It's gona be fine, because the only thing you put on the stack is just the pointer, compiler don't even have to call the destructor of the pointer.
Then, how do you delete the object whose destructor is private?? Just use delete operator is not going to work, because delete operator is not a part of the CTest class. So you have to provide a function which will delete the object itself.
It's like:
void CTest::Release()
{
delete this;
}
Since "delete this" is not a "good manner"(it works, but it's not recommended), you have to pay more attention when you use this. You have to make sure that the object is not created by "new[]". You have to make sure Release is the last function you call on this object, etc.