发新话题
打印

请问:怎样在内核中定义这样的结构体

请问:怎样在内核中定义这样的结构体

  它的起始地址后4位为0

struct test{
...
};

(test & 0x0000000f) ==0;

谢谢
想test............
通过宏定义:偏移地址、起始地址要对好,比较烦。
&((type *)0x----)
通过&((type *)0x----)->--引用成员。
不好意思没有表达准确

我们可以通过下面的方式来满足这样的一个要求(temp& 0x0000000f) ==0。
struct test *temp;

temp= __get_free_page(GFP_KERNEL);

因为获得地地址总是4k对齐的。

但是我们定义一个全局变量

struct test temp1;

那么它的地址会在编译的时候由编译器预设好了。
c 语言是否有限定词 或是其他的编译选项来达到这样的目的。
struct test temp1;
这样既使能实现也不易于维护。
我只见过# define --------&---,没发现c 语言是有限定词 或是其他的编译选项来达到这样的目的。
查一下----
调一下,相关信息贴出来研究
当分配方式不同时,4k对齐有时会很麻烦,比如SDL。。。
找到了一些文档。
http://docs.freebsd.org/info/gcc ... ble_Attributes.html

做如下定义。

struct test{
int num;
char c;
};

struct test temp1;
struct test temp2 __attribute__ ((aligned (1)));
struct test temp3 __attribute__ ((aligned (2)));
struct test temp4 __attribute__ ((aligned (4)));
struct test temp5 __attribute__ ((aligned (8)));
struct test temp6 __attribute__ ((aligned (16)));
struct test temp7 __attribute__ ((aligned (32)));

printk("&temp1=0x%x,&temp2=0x%x,&temp3=0x%x,&temp4=0x%x,&temp5=0x%x,&temp6=0x%x,&temp7=0x%x\n",&temp1,&temp2,&temp3,&temp4,&temp5,&temp6,&temp7);

比较一下这些地质就能看出差别来了。

TO 黄富强:
能否贴出用# define --------&---例子让我试验一下。谢谢!
对不起
具体的东西找不到了,这个文档很好。
这种东西具有不确定性,与编译器的构成机理、体系结构都有关,一定要约定实验条件。
Inquiring on Alignment of Types or Variables
============================================

  The keyword `__alignof__' allows you to inquire about how an object
is aligned, or the minimum alignment usually required by a type.  Its
syntax is just like `sizeof'.

  For example, if the target machine requires a `double' value to be
aligned on an 8-byte boundary, then `__alignof__ (double)' is 8.  This
is true on many RISC machines.  On more traditional machine designs,
`__alignof__ (double)' is 4 or even 2.

  Some machines never actually require alignment; they allow reference
to any data type even at an odd addresses.  For these machines,
`__alignof__' reports the *recommended* alignment of a type.

  When the operand of `__alignof__' is an lvalue rather than a type,
the value is the largest alignment that the lvalue is known to have.
It may have this alignment as a result of its data type, or because it
is part of a structure and inherits alignment from that structure.  For
example, after this declaration:

    struct foo { int x; char y; } foo1;

the value of `__alignof__ (foo1.y)' is probably 2 or 4, the same as
`__alignof__ (int)', even though the data type of `foo1.y' does not
itself demand any alignment.

  A related feature which lets you specify the alignment of an object
is `__attribute__ ((aligned (ALIGNMENT)))';
发新话题