我的英语不好,我会尽力将问题弄清楚。

假设我有一个结构是:

struct A {
   /* the first half */
   int a;
   int b;
   /* the second half */
   int c;
   int d;
} ;


我们知道A的成员将连续存储在内存中。但是,我想将A的前半部分和后半部分存储在两个不同的内存页面中,这意味着该结构在内存中进行了分区。我该如何实现?
假设struct A是Linux内核中的结构,因此我正在内核空间中进行编程。内核版本为3.10。

更新:为明确目标,我画了下面的图片,这是我想要的内存布局,这可以避免浪费内存空间:
c - 如何在Linux内核中将结构存储在两个不同的内存页面中?-LMLPHP

最佳答案

如果目标是使结构内存不连续,请使用指针并执行kmalloc。

struct first_half  {
 int a;
 int b;
};

struct second_half {
int c;
int d;
};


 struct A {
    /* the first half */
    struct first_half *fh;
    /* the second half */
    struct second_half *sh;
 } ;

 fh = (struct first_half *) kmalloc();
 sh = (struct second_half *) kmalloc();

关于c - 如何在Linux内核中将结构存储在两个不同的内存页面中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38588638/

10-16 19:03