在物理内存中动态创建数组

时间:2015-12-08 20:47:40

标签: c arrays operating-system kernel

在创建物理内存分配器来编写内核时,我试图创建一个train_y数组,其中数组的每个成员代表给定的物理4KB内存块。

Physical to Virtual Mapping and Area for new Array

目前,内核已加载到从地址y : array-like, shape = [n_samples] or [n_samples, n_outputs]struct page_frame

的物理内存中

0x000000000physfree的剩余块是空闲块。我想在physfree之后立即创建我的physEnd数组,并在数组中为struct page_framephysfree

内存中的每个块维护一个元素

此数组中的索引数应为0。问题是,physEnd直到运行时才知道,所以我不确定如何动态分配这样的数组。目前,以下内容未填充physEnd数组,因为只有第一个元素physEnd/4096显示为正确struct page_frame,其余元素未正确初始化。

memBlocks[1]

如何在内核顶部的地址动态创建这样的数组,以便跟踪我的免费物理块?我正在尝试利用指向名为struct page_frame的结构的指针,并将其设置为内存中struct page_frame* memBlocks; memBlocks = (struct page_frame*)kernbase + physfree; int count = 0; for(char* i = (char*) 0; i < (char*) ALIGNUP(physEnd)-PAGESIZE; i+=PAGESIZE) { //The new page frame to be placed in array struct page_frame insert; insert.used = 1; memBlocks[count] = insert; count++; } 之后的地址(因为当前启用了虚拟内存)。然后我希望数组的每个元素只占用memBlocks。我是否通过将kernbase + physfree索引为sizeof(struct page_frame)来正确访问这些元素?或者有更好的方法来创建这样的数组吗?

1 个答案:

答案 0 :(得分:1)

请记住,在C中,struct frame数组与指向struct frame的指针非常相似。简单的事情,至少如果你理解C的指针算法,就是简单地使用一个指向'数组'开头的指针。

鉴于struct frame *memBlocks,您可以memBlocks + i

访问第i个块