![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
| Display Modes | Rate This Thread |
|
|