在创建物理内存分配器来编写内核时,我试图创建一个train_y
数组,其中数组的每个成员代表给定的物理4KB内存块。
目前,内核已加载到从地址y : array-like, shape = [n_samples] or [n_samples, n_outputs]
到struct page_frame
从0x000000000
到physfree
的剩余块是空闲块。我想在physfree
之后立即创建我的physEnd
数组,并在数组中为struct page_frame
到physfree
此数组中的索引数应为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)
来正确访问这些元素?或者有更好的方法来创建这样的数组吗?
答案 0 :(得分:1)
请记住,在C中,struct frame
数组与指向struct frame
的指针非常相似。简单的事情,至少如果你理解C的指针算法,就是简单地使用一个指向'数组'开头的指针。
鉴于struct frame *memBlocks
,您可以memBlocks + i