MCPcopy Create free account
hub / github.com/YatSenOS/YatSenOS-Tutorial-Volume-1 / allocatePages

Method allocatePages

lab7/src/5/src/kernel/memory.cpp:166–209  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

164}
165
166int MemoryManager::allocatePages(enum AddressPoolType type, const int count)
167{
168 // 第一步:从虚拟地址池中分配若干虚拟页
169 int virtualAddress = allocateVirtualPages(type, count);
170 if (!virtualAddress)
171 {
172 return 0;
173 }
174
175 bool flag;
176 int physicalPageAddress;
177 int vaddress = virtualAddress;
178
179 // 依次为每一个虚拟页指定物理页
180 for (int i = 0; i < count; ++i, vaddress += PAGE_SIZE)
181 {
182 flag = false;
183 // 第二步:从物理地址池中分配一个物理页
184 physicalPageAddress = allocatePhysicalPages(type, 1);
185 if (physicalPageAddress)
186 {
187 //printf("allocate physical page 0x%x\n", physicalPageAddress);
188
189 // 第三步:为虚拟页建立页目录项和页表项,使虚拟页内的地址经过分页机制变换到物理页内。
190 flag = connectPhysicalVirtualPage(vaddress, physicalPageAddress);
191 }
192 else
193 {
194 flag = false;
195 }
196
197 // 分配失败,释放前面已经分配的虚拟页和物理页表
198 if (!flag)
199 {
200 // 前i个页表已经指定了物理页
201 releasePages(type, virtualAddress, i);
202 // 剩余的页表未指定物理页
203 releaseVirtualPages(type, virtualAddress + i * PAGE_SIZE, count - i);
204 return 0;
205 }
206 }
207
208 return virtualAddress;
209}
210
211int MemoryManager::allocateVirtualPages(enum AddressPoolType type, const int count)
212{

Callers 1

first_threadFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected