aboutsummaryrefslogtreecommitdiff
path: root/fifo.py
blob: 7fa55a06d25c288987a4dfedd6d30b6b0d26eda0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python3

# UPEM / Programmation système / Projet : Simulation de gestion de mémoire virtuelle
# Pacien TRAN-GIRARD et Adam NAILI

from mem import Memory


class FifoPaginator:
  def __init__(self, mem):
    self.mem = mem
    self._main_cursor = 0
    self._virt_cursor = 0

  def _put_main(self, page):
    overwritten = self.mem.main[self._main_cursor]
    self.mem.main[self._main_cursor] = page
    self._main_cursor = (self._main_cursor + 1) % len(self.mem.main)
    return overwritten

  def _put_virt(self, page):
    self.mem.virt[self._virt_cursor] = page
    self._virt_cursor += 1

  def load(self, page):
    if not self.mem.has_page(page): raise IndexError
    if page in self.mem.main: return
    overwritten = self._put_main(page)
    if overwritten not in self.mem.virt: self._put_virt(overwritten)