Sponsored Content
Top Forums Programming Function pointer to inline function ? Post 302355575 by emitrax on Wednesday 23rd of September 2009 05:47:41 AM
Old 09-23-2009
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.
 

10 More Discussions You Might Find Interesting

1. Programming

Will exe file size decrease while using inline function

using namespace std; void g(); class A { public : A() { g();g();g(); cout << "Constructor of A"<< endl ;} }; inline void g(){ cout << "vijay" <<endl; } int main() { A a; } when i use inline i get size 303488 Aug 31 12:05 a.out* when not using inline i get size 303572 Aug 31... (1 Reply)
Discussion started by: vijaysabari
1 Replies

2. Programming

Problem with function which reutrns pointer to a value

i have a function: char *pcCityIdToCountryName(ADMIN_DB_DATA *pstHEader, unit uiCityID) this returns a pointer to CountryName if cityId is given. to retrieve countryname i give: char *CountryName; CountryName = pcCityIdToCountryName(..................); but when i compile it is giving :... (5 Replies)
Discussion started by: jazz
5 Replies

3. Programming

How to return void function pointer

Hello all im trying to build function that will return void function pointer what is mean is ( not working ) the main function void * myClass::getFunction(int type){ if(type==1) return &myClass::Test1; if(type==2) return &myClass::Test2; } void myClass::Test1(){... (1 Reply)
Discussion started by: umen
1 Replies

4. Programming

Inline function inside Classes

#include <iostream> using namespace std; class A { public: int Getvalue() { return i;} private: int i; }; int main() {} The above code compiles properly in g++ or in any other C++ compiler. BUT, the variable 'i' is used (in 'return i' statement) before it is... (1 Reply)
Discussion started by: deepthi.s
1 Replies

5. Programming

Function Returning Pointer

Hi guys. how a functions such fdopen, ... can return pointer? are these functions use static memory(variables)? (6 Replies)
Discussion started by: majid.merkava
6 Replies

6. Programming

structure pointer array as function parameters

if i create an array of pointers to a structure "struct node" as: struct node *r; and create "n" number of "linked lists" and assign it to the various struct pointers r using some function with a return type as structure pointer as: r=multiplty(.......) /*some parameters*/ is... (2 Replies)
Discussion started by: mscoder
2 Replies

7. Programming

Trivial doubt about C function pointer

Hi, In the below C code, #include <stdio.h> void print() { printf("Hello\n"); } int main() { void (*f)() = (void (*)()) print; f(); (*f)(); } I wonder, how the syntaxes "f()" and "(*f)()" are treated as same without any error? Is this an improvement or ANSI/ISO... (1 Reply)
Discussion started by: royalibrahim
1 Replies

8. Programming

Pure C function pointer on printing vowels twice

Have difficulty to understand this pure C code to only print vowels twice from input string. Questions are commented at the end of each place. #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <limits.h> /* *Demonstrate the use of dispatch tables */ /*Print a char... (11 Replies)
Discussion started by: yifangt
11 Replies

9. Programming

Segmentation fault when I pass a char pointer to a function in C.

I am passing a char* to the function "reverse" and when I execute it with gdb I get: Program received signal SIGSEGV, Segmentation fault. 0x000000000040083b in reverse (s=0x400b2b "hello") at pointersExample.c:72 72 *q = *p; Attached is the source code. I do not understand why... (9 Replies)
Discussion started by: jose_spain
9 Replies

10. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies
IPQ_MESSAGE_TYPE(3)					     Linux Programmer's Manual					       IPQ_MESSAGE_TYPE(3)

NAME
ipq_message_type, ipq_get_packet, ipq_getmsgerr -- query queue messages SYNOPSIS
#include <linux/netfilter.h> #include <libipq.h> int ipq_message_type(const unsigned char *buf); ipq_packet_msg_t *ipq_get_packet(const unsigned char *buf); int ipq_get_msgerr(const unsigned char *buf); DESCRIPTION
The ipq_message_type function returns the type of queue message returned to userspace via ipq_read. ipq_message_type should always be called following a successful call to ipq_read to determine whether the message is a packet message or an error message. The buf parameter should be the same data obtained from the previous call to ipq_read. ipq_message_type will return one of the following values: NLMSG_ERROR An error message generated by the Netlink transport. IPQM_PACKET A packet message containing packet metadata and optional packet payload data. The ipq_get_packet function should be called if ipq_message_type returns IPQM_PACKET. The buf parameter should point to the same data used for the call to ipq_message_type. The pointer returned by ipq_get_packet points to a packet message, which is declared as follows: typedef struct ipq_packet_msg { unsigned long packet_id; /* ID of queued packet */ unsigned long mark; /* Netfilter mark value */ long timestamp_sec; /* Packet arrival time (seconds) */ long timestamp_usec; /* Packet arrvial time (+useconds) */ unsigned int hook; /* Netfilter hook we rode in on */ char indev_name[IFNAMSIZ]; /* Name of incoming interface */ char outdev_name[IFNAMSIZ]; /* Name of outgoing interface */ unsigned short hw_protocol; /* Hardware protocol (network order) */ unsigned short hw_type; /* Hardware type */ unsigned char hw_addrlen; /* Hardware address length */ unsigned char hw_addr[8]; /* Hardware address */ size_t data_len; /* Length of packet data */ unsigned char payload[0]; /* Optional packet data */ } ipq_packet_msg_t; Each of these fields may be read by the application. If the queue mode is IPQ_COPY_PACKET and the data_len value is greater than zero, the packet payload contents may be accessed in the memory following the ipq_packet_msg_t structure to a range of data_len. The packet_id field contains a packet identifier to be used when calling ipq_set_verdict. The ipq_get_msgerr function should be called if ipq_message_type returns NLMSG_ERROR. The buf parameter should point to the same data used for the call to ipq_message_type. The value returned by ipq_get_msgerr is set by higher level kernel code and corresponds to standard errno values. BUGS
None known. AUTHOR
James Morris <jmorris@intercode.com.au> COPYRIGHT
Copyright (c) 2000-2001 Netfilter Core Team. Distributed under the GNU General Public License. SEE ALSO
iptables(8), libipq(3). Linux iptables 1.2 16 October 2001 IPQ_MESSAGE_TYPE(3)
All times are GMT -4. The time now is 07:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy