Home > Windows API > How to make some crt functions like memcpy, memcmp, strcmp etc inline?

How to make some crt functions like memcpy, memcmp, strcmp etc inline?

In this blog post I’ll be talking about following #pragma compiler directives…

  • #pragma intrinsic
  • #pragma function

Ever wondered how to make function calls for memset, memcpy etc inline. It’s quite easy, you can use #pragma intrinsic directive to tell the compiler to inline calls to above functions. Check out this link for a list of native functions which can be made inline with this compiler directive.

An eg:

// These three functions are now inline
#pragma intrinsic( memset, memcpy, memcmp )

void Test()
{
   // memset function is now inlined
   memset(...);
}

Now how to undo intrinsic pragma effect? Use #pragma function directive. #pragma function directive does the opposite of #pragma intrinsic. Sometimes you may want that calls to functions specified in the pragma’s argument list be generated instead of being inlined. So in our above example if we want to prevent memset from being inlined just specify this using #pragma function directive. An example follows…

// These functions are no more inline
#pragma function( memset, memcpy )

void Test()
{
   // memset is no more inlined
   memset(...)
}

A bit more comprehensive example on #pragma intrinsic and function can be found in MSDN. #pragma function and intrinsic is particularly useful if we have a global compiler directive turned on/off which tells it to inline/not inline function calls. Also note that making a function call inline will make code generated bit big. Another caveat with inline functions is when fixing bugs and sending patches using dll’s, since the function is inlined, calls to this function cannot be updated, just in case if the bug is in this function. So use with care and have fun programming.

Advertisements
  1. May 18, 2009 at 8:44 pm

    Oh! I have never heard it before. Thanks Nibu Thomas!

  2. January 5, 2009 at 6:06 am

    Thanks Sarath!

    Bumped into this directive sometime back while browsing through MFC source code for CMemFile. So thought of posting this here, just for information.

  3. Sarath
    January 2, 2009 at 6:33 pm

    That’s make a great post. It’s totally new info for me.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: