The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Inline function inside Classes deepthi.s High Level Programming 1 08-22-2008 02:47 PM
Passing global variable to a function which is called by another function sars Shell Programming and Scripting 4 06-30-2008 12:39 PM
How to return void function pointer umen High Level Programming 1 03-22-2008 04:01 PM
Problem with function which reutrns pointer to a value jazz High Level Programming 5 10-28-2005 11:03 AM
Will exe file size decrease while using inline function vijaysabari High Level Programming 1 08-31-2005 11:45 AM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 09-23-2009
emitrax emitrax is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 38
Function pointer to inline function ?

Hi.

Problem: I have to parse the payload of a packet. The payload could be in Big Endian Format (network byte order) or little. That depends on a flag present in the header of the packet.

Solution: A horrible solution could be to check for that flag everytime I have to read a field in the payload, that is

Code:
if (header->most_significan_byte)
    var = ntohl(payload->field32);
else
    var = payload->field32;

and that would be done in all the function body that is responsible for parsing the payload. Quite awful indeed.

I was thinking to use a function pointer, actually two, and init them to ntohl and ntohs if the payload was in network order, or init them to an empty function otherwise. Something like

Code:
uint32_t (*_ntohl)(uint32_t hostlong);
uint16_t (*_ntohs)(uint16_t hostshort);
...

uint32_t empty_ntohl(uint32_t hostlong)
{
     return hostlong;
}

uint16_t empty_ntohs(uint16_t hostshort)
{
     return hostshort;
}

...
if (header->most_sifnigican_byte) {
   _ntohl = ntohl;
   _ntohs =  ntohs;
} else {
   _ntohl = empty_ntohl;
   _ntohs = empty_ntohs;
}
..

var = _ntohl(payload->field32);

Now. Before going ahead and mess up with my already-working-code-without-byte-ordering-support, I'd like to know if this solution is
1 - correct
2 - efficient

or if any of you guys have already faced this problem and have a better and more elegant solution.

In the subject, I wrote function pointer to inline function, because I was wondering if the empty function could be inline to improve performances and avoid the function call
overhead.

Would the pointer function call be replaced with actual inline function in case it points to the inline one?
The answer if of course not, because the last one is done at
compile time. Yeah.. I'm basically answering myself while writing. :-)

Anyway, any comment and/or suggestion?

Thanks in advance.
S.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0