| 128 | } |
| 129 | |
| 130 | int MemoryManager::allocatePages(enum AddressPoolType type, const int count) |
| 131 | { |
| 132 | // 第一步:从虚拟地址池中分配若干虚拟页 |
| 133 | int virtualAddress = allocateVirtualPages(type, count); |
| 134 | if (!virtualAddress) |
| 135 | { |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | bool flag; |
| 140 | int physicalPageAddress; |
| 141 | int vaddress = virtualAddress; |
| 142 | |
| 143 | // 依次为每一个虚拟页指定物理页 |
| 144 | for (int i = 0; i < count; ++i, vaddress += PAGE_SIZE) |
| 145 | { |
| 146 | flag = false; |
| 147 | // 第二步:从物理地址池中分配一个物理页 |
| 148 | physicalPageAddress = allocatePhysicalPages(type, 1); |
| 149 | if (physicalPageAddress) |
| 150 | { |
| 151 | //printf("allocate physical page 0x%x\n", physicalPageAddress); |
| 152 | |
| 153 | // 第三步:为虚拟页建立页目录项和页表项,使虚拟页内的地址经过分页机制变换到物理页内。 |
| 154 | flag = connectPhysicalVirtualPage(vaddress, physicalPageAddress); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | flag = false; |
| 159 | } |
| 160 | |
| 161 | // 分配失败,释放前面已经分配的虚拟页和物理页表 |
| 162 | if (!flag) |
| 163 | { |
| 164 | // 前i个页表已经指定了物理页 |
| 165 | releasePages(type, virtualAddress, i); |
| 166 | // 剩余的页表未指定物理页 |
| 167 | releaseVirtualPages(type, virtualAddress + i * PAGE_SIZE, count - i); |
| 168 | return 0; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return virtualAddress; |
| 173 | } |
| 174 | |
| 175 | int MemoryManager::allocateVirtualPages(enum AddressPoolType type, const int count) |
| 176 | { |
no outgoing calls
no test coverage detected