LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-24-2004, 09:14 PM   #1
frankli
LQ Newbie
 
Registered: Dec 2003
Posts: 25

Rep: Reputation: 15
question about alloc_netdev??


static struct net_device *alloc_netdev(int sizeof_priv, const char *mask,
void (*setup)(struct net_device *))
{
struct net_device *dev;
int alloc_size;

/* ensure 32-byte alignment of the private area */
alloc_size = sizeof (*dev) + sizeof_priv + 31;

dev = (struct net_device *) kmalloc (alloc_size, GFP_KERNEL);
if (dev == NULL)
{
printk(KERN_ERR "alloc_dev: Unable to allocate device memory.\n");
return NULL;
}

memset(dev, 0, alloc_size);

if (sizeof_priv)
dev->priv = (void *) (((long)(dev + 1) + 31) & ~31);

setup(dev);
strcpy(dev->name, mask);

return dev;
}

the function of alloc_netdev is above.I'm disturbed about the sentence:
alloc_size = sizeof (*dev) + sizeof_priv + 31;
dev->priv = (void *) (((long)(dev + 1) + 31) & ~31);
what about this mean?and how can it ensure 32-byte alignment?thanks!
 
Old 06-24-2004, 09:18 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
do out the bitwise math of this:

//round up x to next 4 byte boundary
x = 14;
x = (x + 3) & ~3

i could explain it, but the only way to 'get it' is to do out the bits.
 
Old 06-24-2004, 09:31 PM   #3
frankli
LQ Newbie
 
Registered: Dec 2003
Posts: 25

Original Poster
Rep: Reputation: 15
I still not fully understand,can you explain it exactly?
 
Old 06-24-2004, 11:56 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
01110 is 14
(01110 + 00011) is x + 3 = 10001 is 17
10001 & 11100 is 17 & ~3 = 10000 is 16

16 is the first multiple of 4 greater than 14. It's rounding 14 up to the next 4 byte boundary (16). But it works with any number obviously. Not just 14

Last edited by itsme86; 06-24-2004 at 11:59 PM.
 
Old 06-26-2004, 12:38 AM   #5
frankli
LQ Newbie
 
Registered: Dec 2003
Posts: 25

Original Poster
Rep: Reputation: 15
alloc_size = sizeof (*dev) + sizeof_priv + 31;
like this alloc_size,who can tell me why this need to add 31?? and
dev->priv = (void *) (((long)(dev + 1) + 31) & ~31);
I know what this mean, and anybody who can tell me this algorithm can be applied anywhere?
 
Old 06-26-2004, 01:21 AM   #6
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
you can only use that technique for powers of two.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 04:23 AM
Not your regular GRUB question - just a short question for a fried MBR!! ziphem Linux - General 3 01-31-2005 01:51 PM
Question 1 Firewall Log Question 2 Network Monitor Soulful93 Linux - Networking 4 08-04-2004 11:05 PM
login prompt question & kde scheme question JustinCoyan Slackware 2 06-09-2004 02:02 PM
Lilo/kernel question & font question phek Linux - General 9 09-18-2001 12:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration