如何使用kmalloc的执行数据结构比对

如何使用kmalloc的执行数据结构比对

本文介绍了如何使用kmalloc的执行数据结构比对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读数据结构调整的文章,但我一事无成。也许事情太复杂,我明白了。我也碰到数据结构的填充这也是必要的调整数据。如何添加一个数据结构填充到结构体usb_ep?另外我如何确保每当我执行kmalloc的读取应在内存偏移,这是4若干倍的数据?

I have been reading data structure alignment articles but I'm getting nowhere. Perhaps things are just too complicated for me to understand. I also came across data structure padding which is also necessary to align data. How do I add a data structure padding to struct usb_ep? Also how do I make sure that whenever I perform kmalloc the data to be read should be at a memory offset which is some multiple of 4?

推荐答案

对于对齐,用kmalloc将调整结构正常。如果你有一个4字节变量,将4字节对齐,如果你有一个8字节vaiable,这将是8个字节对齐。了解比对就是为什么填充需要的原因。

Regarding alignment, kmalloc will align the structures properly. If you have an 4byte variable, it will be 4bytes aligned, if you have an 8byte vaiable, it will be 8bytes aligned. Understanding alignment is the reason why padding is needed.

什么您不想要得到的是在你的结构变量之间garbade填充。你可以做到这一点与杂注包指令(可能是最简单的),或者通过手动添加填充。

What you dont want to get is garbade padding between the variables in your struct. You can do that with the pragma pack directive (probably easiest) or by adding the padding manually.

示例

struct usb_ep
{
 short a;  /* 2 bytes*/
 int b;    /* 4 bytes*/
 short c;  /* 2 bytes*/
};

的所有元素的大小是8个字节,但是由于对准要求,大小将12bytes。内存布局将是这样的:

The size of all the elements is 8bytes, but due to alignment requirements, the size will be 12bytes. Memory layout would be like this:

short a        - 2 bytes
char pad[2]    - 2 bytes of padding
int b          - 4 bytes
short c        - 2 bytes
char pad[2]    - 2 bytes of padding

为了不得到任何填充,或增加结构的大小,可以以满足对齐要求重新安排元素。

In order to not get any padding, or increasing the size of the struct, you can rearrange elements in order to satisfy the alignment requirements.

这是有一个结构:

struct usb_ep
{
 short a;  /* 2 bytes*/
 short c;  /* 2 bytes*/
 int b;    /* 4 bytes*/
};

将有8个字节的大小,并用于添加填充没有要求。

Will have the size of 8bytes, and no requirement for adding padding.

这篇关于如何使用kmalloc的执行数据结构比对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 04:56