473,322 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

casting question in kernel source code


hello,
In kernel source code there is ip_fragment.c file my question is
regarding pointer function and casting for that look at required
snippet from that file
There is structure defined for queueing ip fragments as
struct ipq {
struct ipq *next; /* linked list pointers */
struct list_head lru_list; /* lru list member */
u32 user;
u32 saddr;
u32 daddr;
u16 id;
u8 protocol;
u8 last_in;
#define COMPLETE 4
#define FIRST_IN 2
#define LAST_IN 1

struct sk_buff *fragments; /* linked list of received fragments */
int len; /* total length of original datagram */
int meat;
spinlock_t lock;
atomic_t refcnt;
struct timer_list timer; /* when will this queue expire? */
struct ipq **pprev;
int iif;
struct timeval stamp;
};
static void ip_expire(unsigned long arg)
{
struct ipq *qp = (struct ipq *) arg;

spin_lock(&qp->lock);

if (qp->last_in & COMPLETE)
goto out;

ipq_kill(qp);
.....
.....
....
}

static struct ipq *ip_frag_create(unsigned hash, struct iphdr *iph, u32
user)
{
struct ipq *qp;

if ((qp = frag_alloc_queue()) == NULL)
goto out_nomem;

qp->protocol = iph->protocol;
qp->last_in = 0;
qp->id = iph->id;
qp->saddr = iph->saddr;
qp->daddr = iph->daddr;
qp->user = user;
qp->len = 0;
qp->meat = 0;
qp->fragments = NULL;
qp->iif = 0;

/* Initialize a timer for this entry. */
init_timer(&qp->timer);
qp->timer.data = (unsigned long) qp; /* pointer to queue */
qp->timer.function = ip_expire; /* expire function */
qp->lock = SPIN_LOCK_UNLOCKED;
atomic_set(&qp->refcnt, 1);

return ip_frag_intern(hash, qp);
}
My question is
1)how ip_expire is called in ip_frag_create? I mean where is its
argument. check that function above.
2) how following casting occurs in ip_expire(unsigned long arg)
struct ipq *qp = (struct ipq *) arg;
how a unsigned long be converted to structure?
regards,
rahul.

Nov 14 '05 #1
1 1857
ra*******@gmail.com wrote:
hello,
In kernel source code there is ip_fragment.c file my question is
regarding pointer function and casting for that look at required
snippet from that file
[... snipped even further; see up-thread ...]

static void ip_expire(unsigned long arg)
{
struct ipq *qp = (struct ipq *) arg;

[... in function ip_frag_create() ...]
/* Initialize a timer for this entry. */
init_timer(&qp->timer);
qp->timer.data = (unsigned long) qp; /* pointer to queue */
qp->timer.function = ip_expire; /* expire function */
[...]

My question is
1)how ip_expire is called in ip_frag_create? I mean where is its
argument. check that function above.
This is not a function call at all. It is an assignment:
`X = Y'. The `X' is a field in a struct or union (we can
deduce that much from the `.' syntax), probably a struct.
You didn't show the declaration of this struct or union type,
but we can also infer that its `function' field is a pointer.
Not a pointer to a data object, but a pointer to a function --
and the `Y' piece is the "target" of this pointer, the function
that the pointer points to.

At some later time, some piece of code probably makes a
call that looks something like

qp->timer.function(qp->timer.data);

.... and this will invoke the ip_expire() function.
2) how following casting occurs in ip_expire(unsigned long arg)
struct ipq *qp = (struct ipq *) arg;
how a unsigned long be converted to structure?


First, the `unsigned long' is not being converted to a
struct. It's being converted to a struct pointer, which is
a very different thing.

Second, the conversion is unsafe. Converting an integer
type to a pointer gives an implementation-defined result that
might not be useful at all. Some sloppy code (like the above)
assumes that these conversions will simply "preserve all the
bits" (if the integer has at least as many bits as a pointer),
but the C language does not guarantee this. The code relies
on the quirks of a particular compiler in a particular environment,
and is suspect in all other environments.

Note the corresponding conversion in the assignment to
`qp->timer.data' (which probably winds up being passed to
ip_expire() as its argument). This takes the pointer `qp' and
converts it to an `unsigned long' -- and again, the result is
implementation-defined and might not be useful.

Code like this has a nasty habit of working just fine for
a long time on many machines, and then comes the day when it's
ported to a machine of a different kind and things suddenly
go wrong. We went through this when implementations started
using 32 bits for `int' instead of 16, and we went through it
again when 64-bit quantities came along -- and it looks like
this chunk of code never learned those hard lessons.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ingo Nolden | last post by:
Hi there, I am writing a smart pointer that is similar to the boost intrusive ptr. I am trying to make it behave like a c++ pointer including the implicit and explicit casting behaviour. Below...
3
by: Steven T. Hatton | last post by:
http://netlab.ru.is/exception/LinuxCXX.shtml We have implemented a complete kernel level run-time support for C++ in the Linux kernel. In particular our run-time support enables the full use of...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
50
by: Romeo Colacitti | last post by:
Is the C library of most OSes (i.e, unix type OSes) implemented at the very low kernel or just outside kernel level? Looking through the source tree of Linux/BSDs it seems like the C library is...
7
by: I am Sam | last post by:
I have a DataGrid that is passing information to a stored procedure properly but the parameters aren't being casted properly. I was woundering if anyone can tell me how I should properly cast the...
4
by: tzuchien.chiu | last post by:
I've searched this new group with the keyword: "kernel library array list". I am looking for an open source C++ class library for kernel mode programming, because the C++ standard library and...
4
by: SiTTox | last post by:
Hi ppl. Pleace Help me to find information/source : how to encrypt file/ information in Linux/Unix only with Internal functions. without OpenSSL(and others) without writing own RSA,DES,PGP(and...
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.