簡體   English   中英

如何在Linux內核中將結構存儲在兩個不同的內存頁面中?

[英]How to make a struct stored in two different memory pages in 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。

更新 :為使目標明確,我繪制了以下圖片,這是我想要的內存布局,這可以避免浪費內存空間:
在此處輸入圖片說明

如果目標是使結構內存不連續,請使用指針並執行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();

您可以通過在結構成員之間添加填充並將其分配在頁面邊界上來實現,例如:

struct A {
    int a, b;
    char padding[PAGE_SIZE - 2 * sizeof(int)];
    int c, d;
};

但是,它只能處理一個大小的頁面(例如4KB),而存在巨大的不同大小的頁面(x86_64上為2MB和1GB)。

或者,如果不需要填充,則以這種方式為該結構分配內存,以使對象跨越頁面邊界。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM