-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemlist.c
More file actions
291 lines (260 loc) · 10.2 KB
/
memlist.c
File metadata and controls
291 lines (260 loc) · 10.2 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Pablo Portas López pablo.portas
// Pablo Míguez Muiño pablo.miguez.moino
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include "memlist.h"
#include "auxiliar.h"
#include "color.h"
// Variable global del memorial
tMemList memorial = {-1,NULL};
void *MList_add_malloc(int size) {
void *addr = malloc(size);
if (addr == NULL) return NULL;
tFNameL empty = "";
MList_aux_insertItem(addr, size, time(NULL), MALLOC, -1, empty, -1, MNULL, &memorial);
return addr;
}
void *MList_add_mmap(tFNameL dir, int protection) {
int df, map = MAP_PRIVATE, modo = O_RDONLY;
struct stat s;
void *p;
if (protection & PROT_WRITE)
modo = O_RDWR;
if (stat(dir, &s) == -1 || (df = open(dir, modo)) == -1)
return NULL;
if ((p = mmap(NULL, s.st_size, protection, map, df, 0)) == MAP_FAILED)
return NULL;
MList_aux_insertItem(p, s.st_size, time(NULL), MAPPED, -1, dir, df, MNULL, &memorial);
return p;
}
void *MList_add_shared(key_t clave, size_t tam) {
void *p;
int aux, id, flags = 0777;
struct shmid_ds s;
if (tam) /*tam distito de 0 indica crear */
flags = flags | IPC_CREAT | IPC_EXCL; /*cuando no es crear pasamos de tamano 0*/
if (clave == IPC_PRIVATE) /*no nos vale*/
{
errno = EINVAL;
return NULL;
}
if ((id = shmget(clave, tam, flags)) == -1)
return (NULL);
if ((p = shmat(id,NULL, 0)) == (void *) -1) {
aux = errno;
if (tam)
shmctl(id,IPC_RMID,NULL);
errno = aux;
return (NULL);
}
shmctl(id,IPC_STAT, &s); /* si no es crear, necesitamos el tamano, que es s.shm_segsz*/
tFNameL empty = "";
MList_aux_insertItem(p, s.shm_segsz, time(NULL), SHARED, clave, empty, -1, MNULL, &memorial);
return (p);
}
void MList_remove_malloc(int size) {
if (!MList_aux_isEmptyList(memorial)) {
for (tPosMemL posaux = MList_aux_first(memorial); posaux != NULL; posaux = MList_aux_next(posaux, memorial)) {
if (posaux->alloc == MALLOC && posaux->size == size) {
// Dirección encontrada
free(posaux->address); // Liberamos la memoria
MList_aux_deleteAtPosition(posaux, &memorial); // Eliminamos los registros
return;
}
}
Aux_general_Imprimir_Error("No hay bloque de ese tamaño asignado con malloc");
} else Aux_general_Imprimir_Error("Asigna memoria primero");
}
void MList_remove_mmap(tFNameL dir) {
if (!MList_aux_isEmptyList(memorial)) {
for (tPosMemL posaux = MList_aux_first(memorial); posaux != NULL; posaux = MList_aux_next(posaux, memorial)) {
if (posaux->alloc == MAPPED && !strcmp(posaux->file_name, dir)) {
// Archivo encontrado
close(posaux->file_desc); // Cerrar archivo
munmap(posaux->address, posaux->size); // Liberamos la memoria
MList_aux_deleteAtPosition(posaux, &memorial); // Eliminamos los registros
return;
}
}
Aux_general_Imprimir_Error("No hay archivos con ese nombre mapeados en memoria");
} else Aux_general_Imprimir_Error("Asigna memoria primero");
}
void MList_remove_shared(key_t key) {
if (!MList_aux_isEmptyList(memorial)) {
for (tPosMemL posaux = MList_aux_first(memorial); posaux != NULL; posaux = MList_aux_next(posaux, memorial)) {
if (posaux->alloc == SHARED && posaux->smb_key == key) {
// Dirección encontrada
shmdt(posaux->address); // Liberar memoria
MList_aux_deleteAtPosition(posaux, &memorial); // Eliminamos los registros
return;
}
}
Aux_general_Imprimir_Error("No hay bloques con esa key en memoria");
} else Aux_general_Imprimir_Error("Asigna memoria primero");
}
void MList_remove_addr(tAddressL address) {
if (!MList_aux_isEmptyList(memorial)) {
for (tPosMemL posaux = MList_aux_first(memorial); posaux != NULL; posaux = MList_aux_next(posaux, memorial)) {
if (posaux->address == address) {
if (posaux->alloc == MALLOC) free(posaux->address);
if (posaux->alloc == MAPPED) {
close(posaux->file_desc);
munmap(posaux->address, posaux->size);
}
if (posaux->alloc == SHARED) shmdt(posaux->address);
MList_aux_deleteAtPosition(posaux, &memorial); // Eliminamos los registros
return;
}
}
printf("%p", address);
Aux_general_Imprimir_Error("No se ha encontrado esta dirección de memoria "
"entre las asignadas con malloc, shared o mmap");
} else Aux_general_Imprimir_Error("Asigna memoria primero");
}
void MList_delete_all() {
if (MList_aux_isEmptyList(memorial)) return;
tPosMemL posaux;
while (!MList_aux_isEmptyList(memorial)) {
posaux = MList_aux_first(memorial);
if (posaux->alloc == MALLOC) free(posaux->address);
if (posaux->alloc == MAPPED) {
close(posaux->file_desc);
munmap(posaux->address, posaux->size);
}
if (posaux->alloc == SHARED) shmdt(posaux->address);
MList_aux_deleteAtPosition(posaux, &memorial);
}
}
void MList_print(enum tAllocL type_of_alloc) {
tPosMemL posaux;
if (MList_aux_isEmptyList(memorial)) {
fprintf(stderr,ANSI_COLOR_RED "No hay bloques de memoria asignados para el proceso %d" ANSI_COLOR_RESET "\n",
getpid());
return;
}
char *allocname = NULL;
switch (type_of_alloc) {
case MALLOC: // Imprime las asignaciones por malloc
allocname = "malloc";
case SHARED: // Imprime las asignaciones por memoria compartida
if (allocname == NULL) allocname = "shared";
case MAPPED: // Imprime las asignaciones por mapeo
if (allocname == NULL) allocname = "mmap";
printf(ANSI_COLOR_YELLOW "*** Lista de bloques asignados %s para el proceso %d" ANSI_COLOR_RESET "\n",
allocname, getpid());
for (posaux = MList_aux_first(memorial); posaux != NULL; posaux = MList_aux_next(posaux, memorial)) {
if (posaux->alloc == type_of_alloc) MList_aux_printNode(posaux);
}
break;
default: // Imprime todos los tipos de asignaciones
printf(ANSI_COLOR_YELLOW "*** Lista de bloques asignados para el proceso %d" ANSI_COLOR_RESET "\n",
getpid());
for (posaux = MList_aux_first(memorial); posaux != NULL; posaux = MList_aux_next(posaux, memorial))
MList_aux_printNode(posaux);
break;
}
}
// AUXILIARES / INTERNAS
void MList_aux_createEmptyList(tMemList *lista) {
lista->contador = -1;
lista->start = MNULL;
}
bool MList_aux_isEmptyList(tMemList lista) {
if (lista.contador == -1) return true;
return false;
}
tPosMemL MList_aux_first(tMemList lista) {
return lista.start;
}
tPosMemL MList_aux_last(tMemList lista) {
tPosMemL puntero;
for (puntero = lista.start; puntero->siguiente != MNULL; puntero = puntero->siguiente);
return puntero;
}
tPosMemL MList_aux_next(tPosMemL posicion, tMemList lista) {
return posicion->siguiente;
}
tPosMemL MList_aux_previous(tPosMemL posicion, tMemList lista) {
if (posicion == lista.start) return MNULL;
tPosMemL aux;
for (aux = lista.start; aux->siguiente != posicion; aux = aux->siguiente);
return aux;
}
bool MList_aux_insertItem(tAddressL direccion, int size, time_t alloct_time, enum tAllocL type_of_alloc, int smb_key,
tFNameL file_name, int file_descriptor, tPosMemL posicion, tMemList *lista) {
if (!MList_aux_isEmptyList(*lista) && lista->contador == MAX_MLIST_SIZE) return false;
tPosMemL aux1 = malloc(sizeof(struct tNodeMem));
if (aux1 == NULL) return false;
// Creación del nodo
aux1->address = direccion;
aux1->size = size;
aux1->time = alloct_time;
aux1->alloc = type_of_alloc;
aux1->smb_key = smb_key;
strcpy(aux1->file_name, file_name);
aux1->file_desc = file_descriptor;
aux1->siguiente = MNULL;
if (lista->start == MNULL)
lista->start = aux1;
else if (posicion == MNULL) {
tPosMemL aux2 = MList_aux_last(*lista);
aux2->siguiente = aux1;
} else if (posicion == MList_aux_first(*lista)) {
aux1->siguiente = posicion;
lista->start = aux1;
} else {
tPosMemL aux2 = MList_aux_previous(posicion, *lista);
aux2->siguiente = aux1;
aux1->siguiente = posicion;
}
lista->contador += 1;
return true;
}
void MList_aux_deleteAtPosition(tPosMemL posicion, tMemList *lista) {
tPosMemL aux, anterior;
if (MList_aux_next(posicion, *lista) == MNULL) {
if (posicion == lista->start) {
lista->start = MNULL;
} else {
aux = MList_aux_previous(posicion, *lista);
aux->siguiente = MNULL;
}
} else {
if (posicion == lista->start) {
lista->start = MList_aux_next(posicion, *lista);
} else {
anterior = MList_aux_previous(posicion, *lista);
anterior->siguiente = MList_aux_next(posicion, *lista);
}
}
lista->contador -= 1;
free(posicion);
}
void MList_aux_printNode(tPosMemL posicion) {
char datebuffer[80];
struct tm *tm_info = localtime(&posicion->time);
strftime(datebuffer, sizeof(datebuffer), "%d %b %y %H:%M", tm_info);
switch (posicion->alloc) {
case MALLOC:
printf("\t%p %15d %s %s\n", posicion->address, posicion->size, datebuffer, "malloc");
break;
case SHARED:
printf("\t%p %15d %s %s (key %d)\n", posicion->address, posicion->size, datebuffer, "shared",
posicion->smb_key);
break;
case MAPPED:
printf("\t%p %15d %s %s (descriptor %d)\n", posicion->address, posicion->size, datebuffer,
posicion->file_name, posicion->file_desc);
break;
}
}