c - 如何在linux内核中将一个结构体存储在两个不同的内存页中?

标签 c gcc memory-management struct linux-kernel

我英语不好,我会尽力把问题说清楚。

假设我有一个结构:

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

我们知道A的成员会在内存中连续存储。但是,我想让 A 的前半部分和后半部分存储在两个不同的内存页中,这意味着该结构体在内存中进行了分区。我怎样才能实现它?
假设struct A是linux内核中的一个结构体,所以我在内核空间中编程。内核版本为3.10。

更新:为了明确我的目标,我画了下图,这是我想要的内存布局,这样可以避免内存空间的浪费:
enter image description here

最佳答案

如果目标是使结构内存不连续,请使用指针并执行 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/

相关文章:

c - 不知道如何在 C 中加载和使用 dll 文件

c - 带位域​​的结构偏移

C++程序启动并挂起而没有错误

android - Imageview 和 Imagebutton 在操作系统内存占用方面的区别

c - 我写入文件但更改未保存

c - C 中 *ptr 和 ptr* 有什么区别?

在MinGW(使用MSYS)中为Windows 10编译GTK+-2.0程序

c++ - vector 重新分配使用复制而不是 move 构造函数

ios - 调用 ReleaseDesignerOutlets 对 MonoTouch GC 有什么影响吗?

C++。如何在 Linux 中跟踪 .so 模块的内存分配