-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemList.c
252 lines (202 loc) · 7.16 KB
/
MemList.c
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "MemList.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
bool createMNode(MPos *p){
*p = malloc(sizeof(struct MNode));
return (*p != NULL);
}
/* Crear la lista */
void createMemList (MemList *L){
MPos p;
if(createMNode(&p)){
*L = p;
p->next = NULL;
}
}
/* Primero de la lista
MPos first(MemList L){
return L->next;
}
Ultimo de la lista
MPos last(MemList L){
MPos aux;
for (aux = L; aux -> next != NULL; aux = aux -> next);
return aux;
} */
/* Insertar en la lista de direcciones malloc */
bool InsertarNodoMalloc(MemList *L, struct MNode *block){
MPos aux;
if(*L == NULL) // No se puede insertar
return false;
else {
for (aux = *L; aux -> next != NULL; aux = aux -> next); // Insertar al final de la lista
aux -> next = block;
block -> next = NULL;
}
return true;
}
/* Insertar en la lista de direcciones shared */
bool InsertarNodoShared(MemList *L, MPos *p, unsigned long size, key_t clave){
MPos aux;
if(*L == NULL) // No se puede insertar
return false;
else {
struct MNode *block;
createMNode(&block);
time_t t = time(NULL);
char date[20];
struct tm *fecha = localtime(&t);
strftime(date, 100, "%d/%m/%Y %H:%M:%S\n", fecha);
strcpy(block->data.time, date);
block->data.size = size;
block->data.dir = p;
strcpy(block->data.type, "shared");
block->data.key = clave;
for (aux = *L; aux -> next != NULL; aux = aux -> next); // Insertar al final de la lista
aux -> next = block;
block -> next = NULL;
}
return true;
}
bool InsertarNodoMmap(MemList *MP, MPos *p, size_t size, int df, char *fichero){ // Insertar en la lista
MPos aux;
if(*MP == NULL) // No se puede insertar
return false;
else {
struct MNode *block;
createMNode(&block);
time_t t = time(NULL);
char date[20];
struct tm *fecha = localtime(&t);
strftime(date, 100, "%d/%m/%Y %H:%M:%S\n", fecha);
strcpy(block->data.time, date);
block->data.size = size;
block->data.dir = p;
strcpy(block->data.type, "mmap");
block->data.df = df;
strcpy(block->data.name, fichero);
for (aux = *MP; aux -> next != NULL; aux = aux -> next); // Insertar al final de la lista
aux -> next = block;
block -> next = NULL;
}
return true;
}
/* Elimina de la lista el elemento que ocupa la posición indicada */
void deleteAtPosition(MPos p, MemList *L){
MPos aux;
if (p->next == NULL){ // Borrar el ultimo elemento
for (aux = *L; aux->next != p; aux = aux->next);
aux->next = NULL;
} else { // Borrar por el medio
aux = p->next;
p->data = aux->data;
p->next = aux->next;
p = aux;
}
if(strcmp(p->data.type, "malloc") == 0)
free(p->data.dir);
free(p);
}
/* Borrar un bloque de memoria a partir de su direccion */
int deleteAddr(char *dir, MemList M, MemList S, MemList MP){
MPos aux;
char *ptr;
long addr = strtoul(dir,&ptr,16);
// Recorrer la lista malloc
for (aux = M; aux -> next != NULL && aux->data.dir != dir; aux = aux -> next); // Recorrer la lista
if(aux->data.dir == (long *) addr) {
deleteAtPosition(aux, &M);
return 0;
}
// Si no esta en la lista malloc, recorrer lista shared
for (aux = S; aux -> next != NULL && aux->data.dir != dir; aux = aux -> next); // Recorrer la lista
if(aux->data.dir == (long *) addr) {
deleteAtPosition(aux, &S);
return 0;
}
// Si no esta en la lista shared, recorrer la lista mmap
for (aux = MP; aux -> next != NULL && aux->data.dir != dir; aux = aux -> next); // Recorrer la lista
if(aux->data.dir == (long *) addr) {
deleteAtPosition(aux, &MP);
return 0;
}
return -1;
}
/* Borrar la lista */
void deleteMemList(MemList *L){
MPos p;
while(*L != NULL){ // Vamos borrando cada elemento uno a uno
p = *L;
*L = (*L)->next; // Vamos desplazando p
if(strcmp(p->data.type, "malloc") == 0)
free(p->data.dir);
free(p); // Liberamos memoria de p
}
}
/* Imprimir la lista */
void printMemList(MemList L, int op){ // -malloc = 0, -shared/-createshared = 1, -mmap = 2
MPos aux;
if(L != NULL) {
if (op == 0) { // Lista malloc
printf("----- Lista de bloques asignados malloc para el proceso %d -----\n", getpid());
for (aux = L->next; aux != NULL; aux = aux->next)
printf("\t %p \t %lu \t %s\n", aux->data.dir, aux->data.size, aux->data.time);
} else if (op == 1) { // Lista shared
printf("----- Lista de bloques asignados shared para el proceso %d -----\n", getpid());
for (aux = L->next; aux != NULL; aux = aux->next)
printf("\t %p \t %lu \t %s (key %d)\n", aux->data.dir, aux->data.size, aux->data.time, aux->data.key);
} else if (op == 2) { // Lista mmap
printf("----- Lista de bloques asignados mmap para el proceso %d -----\n", getpid());
for (aux = L->next; aux != NULL; aux = aux->next)
printf("\t %p \t %lu \t %s %s (descriptor %d)\n", aux->data.dir, aux->data.size, aux->data.time,
aux->data.name, aux->data.df);
}
}
}
void printMemList2(MemList L, MemList S, MemList MP){
MPos aux;
// Imprimir todos los bloques asigandos
printf("----- Lista de bloques asignados para el proceso %d -----\n",getpid());
if(L != NULL) {
for (aux = L->next; aux != NULL; aux = aux->next)
printf("\t %p \t %lu \t %s\n", aux->data.dir, aux->data.size, aux->data.time);
}
if(S != NULL) {
for (aux = S->next; aux != NULL; aux = aux->next)
printf("\t %p \t %lu \t %s (key %d)\n", aux->data.dir, aux->data.size, aux->data.time, aux->data.key);
}
if(MP != NULL) {
for (aux = MP->next; aux != NULL; aux = aux->next)
printf("\t %p \t %lu \t %s %s (descriptor %d)\n", aux->data.dir, aux->data.size, aux->data.time,
aux->data.name, aux->data.df);
}
}
/* Buscar un bloque de memoria a partir de su clave */
MPos findKeyBlock(int clave, MemList L){
MPos aux;
for (aux = L; aux -> next != NULL && aux->data.key != clave; aux = aux -> next); // Recorrer la lista
if(aux->data.key == clave)
return aux;
else
return NULL;
}
/* Buscar un bloque de memoria malloc por su tamaño */
MPos findBlock(int tam, MemList L){
MPos aux;
for (aux = L->next; aux -> next != NULL && aux->data.size != tam && strcmp(aux->data.type, "malloc") == 0; aux = aux -> next); // Recorrer la lista
if(aux->data.size == tam)
return aux;
else
return NULL;
}
/* Buscar un bloque de memoria mmap a partir de su nombre */
MPos findMmapBlock(char *name, MemList L){
MPos aux;
for (aux = L; aux -> next != NULL && strcmp(aux->data.name, name) != 0; aux = aux -> next); // Recorrer la lista
if(strcmp(aux->data.name, name) == 0)
return aux;
else
return NULL;
}