-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.c
65 lines (54 loc) · 1.21 KB
/
memory.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
#include "inc/memory.h"
/*
* Allocation d'un objet
*/
Object* Object_allocate() {
return ( (Object*) calloc( 1, sizeof( Object ) ) );
}
/*
* Libération d'un objet
*/
void Object_free( Object* obj ) {
// On libère l'objet
free( obj );
}
/*
* Allocation d'un maillon objet
*/
ObjectM* ObjectM_allocate() {
ObjectM* maillon = calloc( 1, sizeof( ObjectM ) );
if( maillon != NULL ) {
maillon->prev = NULL;
maillon->obj = NULL;
maillon->next = NULL;
return maillon;
}
return NULL;
}
/*
* Libération d'un maillon objet
*/
void ObjectM_free( ObjectM* maillon, int free_object ) {
// Si le maillon est correct
if( maillon != NULL ) {
// S'il le faut, on libère l'objet du maillon
if( free_object )
Object_free( maillon->obj );
// On libère l'espace du maillon
free( maillon );
}
}
/*
* Libération de tous les maillons objet
*/
void ObjectM_freeAll( ObjectM* first, int free_object ) {
// Déclaration des variables utiles
ObjectM *maillon, *next;
// On parcours les maillons
for( maillon = first; maillon != NULL; maillon = next ) {
// On récupère le pointeur vers le prochain maillon
next = maillon->next;
// On libère le maillon
ObjectM_free( maillon, free_object );
}
}