-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryManager.py
155 lines (126 loc) · 6.31 KB
/
MemoryManager.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# ----------------------------------------------------------- #
# ____ _____ _ #
# / __ \ | __ \ (_) #
# | | | |_ __ ___ _ __ | |__) |___ ___ _ __ _ ___ #
# | | | | '_ \ / _ \ '_ \ | _ // _ \ / _ \| '_ \| |/ _ \ #
# | |__| | |_) | __/ | | | | | \ \ (_) | (_) | | | | | __/ #
# \____/| .__/ \___|_| |_| |_| \_\___/ \___/|_| |_|_|\___| #
# | | #
# |_| #
# #
# Antonio Carlos Vargas Torres A00813182 #
# Rubén Eugenio Cantu Vota A00814298 #
# ----------------------------------------------------------- #
import math
import pprint
import Enums as Enums
class MemoryManagerClass:
def __init__(self):
self.Enums = Enums.Enums().Instance
# Nombres de tipos de datos validos
self.DataTypes = self.Enums.DataTypes
# Nombres de los diferentes scopes que existen en el mapa de memoria
self.MemoryScopes = self.Enums.MemoryScopes
self.resetMemory()
def resetMemory(self):
# Tamaño del buffer por cada tipo de variable, para cada contexto
self.MaxVarsPerType = 1000
# Crear / Borrar contenido del diccionario de memoria.
self.Dictionary = {}
# Crear / Borrar contenido del array de los contadores.
self.Counters = []
# Asignar los valores iniciales a los contadores de memoria.
for i in range(1, ((len(self.DataTypes)) * len(self.MemoryScopes)) + 1):
self.Counters.append(self.MaxVarsPerType * i)
return True
def showMemory(self):
print("\nMemory: ")
pprint.pprint(self.Dictionary)
return True
def resetLocalMemory(self):
for i in range(0, len(self.DataTypes)):
indexInicial = self.getInitialIndexType(i)
indexFinal = self.translateToCounterIndex(2, list(self.DataTypes.keys())[list(self.DataTypes.values()).index(i)])
self.Counters[len(self.DataTypes) * 2 + i] = indexInicial
for j in range(0, indexFinal - indexInicial + 1):
self.deleteEntry(indexInicial + j)
return True
def translateToCounterIndex(self, scope, tipo):
try:
IndexType = self.DataTypes[tipo]
except ValueError:
print("\nERROR DATA TYPE. No existe el tipo de dato: ", tipo)
return None
IndexScope = 2 if scope > 1 else scope
return len(self.DataTypes) * IndexScope + IndexType
def addEntry(self, scope, tipo, valor):
# Obtengo el indice del arreglo de contadores de vars/consts, segun el tipo de dato y su scope
Index = self.translateToCounterIndex(scope, tipo)
if Index == None:
print("\nMEMORY ERROR. No se pudo agregar una variable a memoria")
return None
VirtualMemoryIndex = self.Counters[Index]
self.Dictionary[VirtualMemoryIndex] = valor
self.Counters[Index] = VirtualMemoryIndex + 1
self.checkMemoryOverflow()
return VirtualMemoryIndex
def getEntryType (self, virDir) :
if virDir in self.Dictionary.keys() :
TypeIndex = ((math.floor(virDir / self.MaxVarsPerType)) % len(self.DataTypes)) - 1
return list(self.DataTypes.keys())[list(self.DataTypes.values()).index(TypeIndex)]
else :
print("\nERROR MEMORIA. Direccion de memoria invalida. Direccion: ", virDir)
return None
def getEntryTypeId (self, virDir) :
if virDir in self.Dictionary.keys() :
TypeIndex = ((math.floor(virDir / self.MaxVarsPerType)) % len(self.DataTypes)) - 1
return TypeIndex
else :
print("\nERROR MEMORIA. Direccion de memoria invalida. Direccion: ", virDir)
return None
def getInitialIndexType(self, typeId):
indexInicial = (len(self.DataTypes) * 2 * self.MaxVarsPerType) + (typeId * self.MaxVarsPerType) + self.MaxVarsPerType
return indexInicial
def getLocalStart(self):
return (len(self.DataTypes) * 2 * self.MaxVarsPerType) + self.MaxVarsPerType
def getLocalIndexList(self):
indexInicialList = []
for i in range(0, len(self.DataTypes)):
indexInicialList.append(self.Counters[(len(self.DataTypes) * 2) + i])
return indexInicialList
def getEntryValue(self, virDir):
if virDir in self.Dictionary.keys() :
return self.Dictionary[virDir];
else :
print("\nERROR MEMORIA. Direccion de memoria invalida. Direccion: ", virDir)
return None
def setEntryValue(self, virDir, valor):
if virDir in self.Dictionary.keys() :
self.Dictionary[virDir] = valor
return True
else :
print("\nERROR MEMORIA. Direccion de memoria invalida. Direccion: ", virDir)
return None
def checkMemoryOverflow(self):
for i in range(0, len(self.Counters)) :
if self.Counters[i] < ((self.MaxVarsPerType * i) + self.MaxVarsPerType) :
print("\nERROR MEMORIA. Memoria saturada, ya no se puede cargar mas variables")
return None
return True
def deleteVarsByBounds(self, oldCounters, newCounters):
for i in range(0, len(self.DataTypes)):
for j in range(0, newCounters[i] - oldCounters[i]):
self.deleteEntry(oldCounters[i]+j)
self.Counters[len(self.DataTypes) * 2 + i] = oldCounters[i]
return True
def deleteEntry(self, virDir):
if virDir in self.Dictionary.keys() :
del self.Dictionary[virDir]
return True
else :
print("\nERROR MEMORIA. Direccion de memoria invalida. Direccion: ", virDir)
return None
class MemoryManager:
Instance = MemoryManagerClass()
def __init__(self):
pass