Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. #include "mem.h"
  2. #include "common.h"
  3. #include <stdio.h>
  4.  
  5. void mem_init(char* mem, size_t taille){
  6. *(struct fb**) mem = mem + sizeof(struct fb*);
  7. struct fb* first_free_zone = *(struct fb**) mem ;
  8. first_free_zone->size = taille-sizeof(struct fb*);
  9. first_free_zone->next = NULL;
  10. printf("Taille : %d , Adresse de la premiere zone libre: %p\n",first_free_zone->size,first_free_zone);
  11. }
  12.  
  13. //retourne l'adresse de la prochaine zone inoccupテee, retourne NULL si il n'y en a pas
  14. struct fb* trouver_voisin_droite(void * zone){
  15. struct fb * parcours = *((struct fb**) get_memory_adr());
  16. while(parcours != NULL && (void *) parcours + parcours->size < zone){
  17. parcours = parcours->next;
  18. }
  19. return parcours;
  20. };
  21.  
  22.  
  23. //Renvoi l'adresse de la zone libre la plus proche de la zone "zone" a liberer.
  24. struct fb* trouver_voisin_gauche(void * zone){
  25. struct fb * parcours = *((struct fb**) get_memory_adr());
  26. struct fb * prec = parcours;
  27. while(parcours != NULL && (void *) parcours + parcours->size <= zone-sizeof(int)){
  28. prec = parcours;
  29. parcours = parcours->next;
  30. }
  31. return prec;
  32. };
  33.  
  34.  
  35. void* mem_alloc(size_t size){
  36. void* mem = get_memory_adr();
  37.  
  38. struct fb* fb_current = mem_fit_first(*(struct fb**) mem, size);
  39. printf("On cherche a allouer a l'adresse %p\n", fb_current);
  40. //Si on a rテゥussi a trouver une zone memoire adequate
  41. if (fb_current != NULL){
  42.  
  43. struct fb * voisin_gauche = trouver_voisin_gauche((void*)fb_current);
  44. struct fb * voisin_droit = trouver_voisin_droite((void*)fb_current);
  45. //printf("Mon voisin de droite est : %p \n", (void *) voisin_droit);
  46. //printf("Mon voisin de gauche est : %p \n", (void *) voisin_gauche);
  47.  
  48. //Modification de la taille de la zone si nテゥcessaire : si on ne peut pas allouer de fb dans l'espace restant
  49. if(fb_current->size - size - sizeof(int) <= sizeof(struct fb)){
  50. size = fb_current->size - sizeof(int);
  51. }
  52. //Si le bloc à droite est occupé
  53. if(size == fb_current->size-sizeof(int)){
  54.  
  55. if(voisin_gauche != NULL && (void*)voisin_gauche->next == (void*)fb_current){
  56. voisin_gauche->next = (void *)fb_current->next;
  57. }
  58. else{
  59. *(struct fb**)mem = (void *)fb_current->next;
  60. }
  61. }
  62. //Si le bloc immediatemment a droite est libre
  63. else{
  64. struct fb* suivant = (void*)fb_current->next;
  65. struct fb* fb_new = (void *)fb_current + sizeof(int) + size;
  66.  
  67. if(voisin_gauche != NULL && (void*)voisin_gauche->next == (void*)fb_current){
  68. voisin_gauche->next = (void *)fb_new;
  69. }
  70. else{
  71. *(struct fb**)mem = (void *)fb_new;
  72. }
  73. fb_new->size = fb_current->size - (size + sizeof(int));
  74. fb_new->next = (void*)suivant;
  75. }
  76. *(int*)((void*)fb_current) = size;
  77. //printf("Prochain bloc libre en %p",*(struct fb **)mem);
  78. return (void *) fb_current + sizeof(int);
  79. }
  80. else{
  81. return NULL;
  82. }
  83. }
  84.  
  85. void affiche_chainage(){
  86. struct fb* tete = *(struct fb**)get_memory_adr();
  87. while(tete != NULL){
  88. printf(" %p => ",(void*)tete);
  89. tete = tete->next;
  90. }
  91. printf("\n");
  92. }
  93.  
  94. void mem_free(void* zone){
  95.  
  96. if(!est_liberable(zone)){
  97. printf("L'adresse %p n'est pas une adresse de debut de zone allouee\n",zone);
  98. return;
  99. }
  100. void* mem = get_memory_adr();
  101. //Adresse oテケ est stockee la valeur de la zone a liberer
  102. void * ptr_taille_zone = zone - sizeof(int);
  103. size_t taille_zone = *(int*) ptr_taille_zone;
  104. size_t nouvelle_taille = taille_zone + sizeof(int);
  105.  
  106. struct fb * voisin_gauche = trouver_voisin_gauche(zone);
  107. struct fb * voisin_droit = trouver_voisin_droite(zone);
  108. //printf("Mon voisin de droite est : %p \n", (void *) voisin_droit);
  109. //printf("Mon voisin de gauche est : %p \n", (void *) voisin_gauche);
  110.  
  111. //Si il y a un voisin a gauche qui n'est pas le premier
  112. if(voisin_gauche != NULL && (void*)voisin_gauche+voisin_gauche->size == ptr_taille_zone){
  113. //Si il y a un voisin a droite, fusionner avec lui
  114. nouvelle_taille += voisin_gauche->size;
  115. //Si la zone droite adjacente est libre : fusionner
  116. if((void *) voisin_droit == zone + taille_zone){
  117. nouvelle_taille+= voisin_droit->size;
  118. voisin_gauche->next = (void*)voisin_droit->next;
  119. }
  120. else{
  121. voisin_gauche->next = (void*)voisin_droit;
  122. }
  123. voisin_gauche->size = nouvelle_taille;
  124. }
  125.  
  126. //Si on doit liberer le premier bloc disponible
  127. else if(voisin_gauche == voisin_droit){
  128. struct fb* fb_new = ptr_taille_zone;
  129. //Si la zone adjacente droite est libre : fusionner
  130. if((void *) voisin_droit == zone + taille_zone){
  131. nouvelle_taille+= voisin_droit->size;
  132. fb_new->next = voisin_droit->next;
  133. }
  134. else{
  135. fb_new->next = voisin_droit;
  136. }
  137. fb_new->size = nouvelle_taille;
  138. *(struct fb**) get_memory_adr() = (void *) fb_new;
  139. }
  140. //si il n'y a pas de voisin a gauche
  141. else{
  142. struct fb* fb_new = ptr_taille_zone;
  143. //si la zone adjacente droite est libre : fusionner
  144. if((void *) voisin_droit == zone + taille_zone){
  145. nouvelle_taille+= voisin_droit->size;
  146. fb_new->next = voisin_droit->next;
  147. }
  148. else{
  149. fb_new->next = voisin_droit;
  150. }
  151. fb_new->size = nouvelle_taille;
  152. voisin_gauche->next = fb_new;
  153. }
  154. }
  155.  
  156. size_t mem_get_size(void * zone){
  157. return *(int*) zone - sizeof(int);
  158. }
  159.  
  160. /* Iterateur sur le contenu de l'allocateur */
  161. void mem_show(void (*print)(void * zone, size_t size, int free)){
  162. affiche_chainage();
  163. void* mem = get_memory_adr();
  164. struct fb* fb = *(struct fb**) mem;
  165. void* addr = mem + sizeof(struct fb*);
  166. size_t count = sizeof(struct fb**);
  167. while(count < get_memory_size()){
  168. if(addr == (void *)fb){
  169. print(addr, fb->size, 1);
  170. addr = addr + fb->size;
  171. count += fb->size;
  172. fb = fb->next;
  173. } else {
  174. print(addr, *(int*)addr, 0);
  175. count += *(int*)addr + sizeof(int);
  176. addr += *(int*)addr + sizeof(int);
  177. }
  178. }
  179. }
  180.  
  181. int est_liberable(void * zone){
  182. void* mem = get_memory_adr();
  183. struct fb* fb = *(struct fb**) mem;
  184. void* addr = mem + sizeof(struct fb*);
  185. size_t count = sizeof(struct fb**);
  186. int retour = 0;
  187. while(count < get_memory_size() && retour == 0){
  188. if(addr == (void *)fb){
  189. addr = addr + fb->size;
  190. count += fb->size;
  191. fb = fb->next;
  192. } else {
  193. if(addr+sizeof(int) == zone)
  194. retour =1;
  195. count += *(int*)addr + sizeof(int);
  196. addr += *(int*)addr + sizeof(int);
  197. }
  198. }
  199. return retour;
  200. }
  201.  
  202.  
  203. /* Choix de la strategie et strategies usuelles */
  204. typedef struct fb* (mem_fit_function_t)(struct fb *, size_t);
  205.  
  206. void mem_fit(mem_fit_function_t*);
  207.  
  208. struct fb* mem_fit_first(struct fb* list, size_t size){
  209. while(list != NULL && list->size < size+sizeof(int)){
  210. list = list->next;
  211. }
  212. printf("%p\n",list);
  213. return list == NULL ? NULL : list;
  214. }
  215.  
  216. /* Si vous avez le temps */
  217. struct fb* mem_fit_best(struct fb* list, size_t size);
  218. struct fb* mem_fit_worst(struct fb* list, size_t size);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement