Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.24 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 テ trouver une zone mテゥmoire adテゥquate
  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 immテediatemment a droite est libre
  63. else{
  64. struct fb* fb_new = (void *)fb_current + sizeof(int) + size;
  65. fb_new->size = fb_current->size - (size + sizeof(int));
  66. fb_new->next = (void *)fb_current->next;
  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. }
  74. *(int*)((void*)fb_current) = size;
  75. //printf("Prochain bloc libre en %p",*(struct fb **)mem);
  76. return (void *) fb_current + sizeof(int);
  77.  
  78. /*
  79. //Si le bloc immテゥdiatemment a droite est occupテゥ
  80. if(size == fb_current->size-sizeof(int)){
  81. if(voisin_gauche != NULL){
  82. voisin_gauche->next = (void *)fb_current->next;
  83. }
  84. else{
  85. *(struct fb**)mem = (void *)fb_current->next;
  86. }
  87. }
  88. //Si le bloc immテediatemment a droite est libre
  89. else{
  90. struct fb* fb_new = (void *)fb_current + sizeof(int) + size;
  91. fb_new->size = fb_current->size - (size + sizeof(int));
  92. fb_new->next = (void *)fb_current->next;
  93. if(voisin_gauche != NULL){
  94. voisin_gauche->next = (void *)fb_new;
  95. }
  96. else{
  97. *(struct fb**)mem = (void *)fb_new;
  98. }
  99. }
  100. *(int*)((void*)fb_current) = size;
  101. //printf("Prochain bloc libre en %p",*(struct fb **)mem);
  102. return (void *) fb_current + sizeof(int);*/
  103. }
  104. else{
  105. return NULL;
  106. }
  107. /* Code de Antoine : ne marche pas dans tout les cas
  108. if (fb_current != NULL){
  109.  
  110.  
  111.  
  112. struct fb* fb_next = fb_current->next;
  113. struct fb* fb_before = *(struct fb**) mem;
  114. if (fb_before != fb_current){
  115. while (fb_before->next != fb_current){
  116. fb_before = fb_before->next;
  117. }
  118. }
  119.  
  120. //Calcul de la taille restante du bloc テ allouer
  121. size_t rest_size = fb_current->size - size + sizeof(int);
  122. //Cas 1 = Plus de place pour allouer une zone libre
  123. if (rest_size < sizeof(struct fb)){
  124. if (fb_before == fb_current){
  125. *(struct fb**) mem = fb_next;
  126. } else {
  127. fb_before->next = fb_next;
  128. }
  129. *(int*)fb_current = fb_current->size - sizeof(int);
  130. ret = (void *) fb_current + sizeof(int);
  131. //Cas 2 = Crテゥation d'une nouvelle zone libre
  132. }else {
  133. struct fb* fb_new = (void *)fb_current + sizeof(int) + size;
  134. fb_new->size = fb_current->size - (size + sizeof(int));
  135. fb_new->next = fb_current->next;
  136. if (fb_before == fb_current){
  137. printf("On devrait passer la\n");
  138. *(struct fb**) mem = fb_new;
  139. } else {
  140. fb_before->next = fb_new;
  141. }
  142. *(int*) fb_current = size;
  143. ret = (void *)fb_current + sizeof(int);
  144. printf("On passe + %p + %d\n",ret,sizeof(int));
  145. }
  146. }else{
  147. return NULL;
  148. }
  149. printf("Valeur de current = %d\n",*(int*)fb_current);
  150. return ret;
  151. */}
  152. void affiche_chainage(){
  153. struct fb* tete = *(struct fb**)get_memory_adr();
  154. while(tete != NULL){
  155. printf(" %p => ",(void*)tete);
  156. tete = tete->next;
  157. }
  158. printf("\n");
  159. }
  160.  
  161. void mem_free(void* zone){
  162.  
  163. if(!est_liberable(zone)){
  164. printf("L'adresse %p n'est pas une adresse de debut de zone allouee\n",zone);
  165. return;
  166. }
  167. void* mem = get_memory_adr();
  168. //Adresse oテケ est stockee la valeur de la zone a liberer
  169. void * ptr_taille_zone = zone - sizeof(int);
  170. size_t taille_zone = *(int*) ptr_taille_zone;
  171. size_t nouvelle_taille = taille_zone + sizeof(int);
  172.  
  173. struct fb * voisin_gauche = trouver_voisin_gauche(zone);
  174. struct fb * voisin_droit = trouver_voisin_droite(zone);
  175. printf("Mon voisin de droite est : %p \n", (void *) voisin_droit);
  176. printf("Mon voisin de gauche est : %p \n", (void *) voisin_gauche);
  177.  
  178. //Si il y a un voisin a gauche qui n'est pas le premier
  179. if(voisin_gauche != NULL && (void*)voisin_gauche+voisin_gauche->size == ptr_taille_zone){
  180. //Si il y a un voisin a droite, fusionner avec lui
  181. nouvelle_taille += voisin_gauche->size;
  182. //Si la zone droite adjacente est libre : fusionner
  183. if((void *) voisin_droit == zone + taille_zone){
  184. nouvelle_taille+= voisin_droit->size;
  185. voisin_gauche->next = (void*)voisin_droit->next;
  186. }
  187. else{
  188. voisin_gauche->next = (void*)voisin_droit;
  189. }
  190. voisin_gauche->size = nouvelle_taille;
  191. }
  192.  
  193. //Si on doit libテゥrer le premier bloc disponible
  194. else if(voisin_gauche == voisin_droit){
  195. struct fb* fb_new = ptr_taille_zone;
  196. //Si la zone adjacente droite est libre : fusionner
  197. if((void *) voisin_droit == zone + taille_zone){
  198. nouvelle_taille+= voisin_droit->size;
  199. fb_new->next = voisin_droit->next;
  200. }
  201. else{
  202. fb_new->next = voisin_droit;
  203. }
  204. fb_new->size = nouvelle_taille;
  205. *(struct fb**) get_memory_adr() = (void *) fb_new;
  206. }
  207. //si il n'y a pas de voisin a gauche
  208. else{
  209. struct fb* fb_new = ptr_taille_zone;
  210. //si la zone adjacente droite est libre : fusionner
  211. if((void *) voisin_droit == zone + taille_zone){
  212. nouvelle_taille+= voisin_droit->size;
  213. fb_new->next = voisin_droit->next;
  214. }
  215. else{
  216. fb_new->next = voisin_droit;
  217. }
  218. fb_new->size = nouvelle_taille;
  219. voisin_gauche->next = fb_new;
  220. }
  221.  
  222. /* Tentative foirテゥe de free, ca part dans tout les sens : on met a plat et on recommence !
  223. //Si le bloc a liberer doit devenir le premier bloc disponible
  224. if((void *)voisin_gauche == NULL){
  225. printf("Passe lalalla\n");
  226. //Si il n'y a plus aucun bloc libre a droite
  227. if((void *)voisin_droit == NULL){
  228. printf("PPPPPPPP\n");
  229. voisin_gauche = *(struct fb**)mem;
  230. // Cas oテケ la zone a gauche est libre et qu'il n'y a plus de voisin a droite
  231. if(voisin_gauche != NULL &&((void*)voisin_gauche)+voisin_gauche->size == zone-sizeof(int)){
  232. voisin_gauche->size+= taille_zone+sizeof(int);
  233. voisin_gauche->next=voisin_gauche->next;
  234. printf("C'est gagnテゥ\n");
  235. }
  236. //Cas oテケ la zone a gauche est libre et qu'il n'y a plus de voisin a droite
  237. else if(voisin_gauche != NULL &&((void*)voisin_gauche)+voisin_gauche->size != zone-sizeof(int)&& (void*)(*(struct fb**)mem) != (void*)mem){
  238. struct fb* fb_new = ptr_taille_zone;
  239. fb_new->size = taille_zone + sizeof(int);
  240. fb_new->next = (void *)voisin_droit;
  241. voisin_gauche->next = (void*)fb_new;
  242. printf("fusion\n");
  243. }
  244. //Cas oテケ il faut crテゥer un nouveau descripteur car pas de zone a gauche
  245. else{
  246. printf("affectation\n");
  247. struct fb* fb_new = ptr_taille_zone;
  248. fb_new->size = taille_zone + sizeof(int);
  249. fb_new->next = (void *)voisin_droit;
  250. *(struct fb**) get_memory_adr() = (void *) fb_new;
  251. }
  252. }
  253. //Si la zone immediatemment a droite est libre : fusionner
  254. else if(zone+taille_zone == (void *)voisin_droit){
  255. struct fb* is_first = *(struct fb**)mem ;
  256. if((void*)is_first+is_first->size == ptr_taille_zone){
  257. is_first->size += taille_zone+sizeof(int)+voisin_droit->size;
  258. is_first->next = (void *)voisin_droit->next;
  259. }
  260. else{
  261. struct fb* fb_new = ptr_taille_zone;
  262. fb_new->size = taille_zone + voisin_droit->size + sizeof(int);
  263. fb_new->next = (void *)voisin_droit->next;
  264. *(struct fb**)mem = (void *) fb_new;
  265. }
  266. printf("Ou lテ ?\n");
  267. }
  268. //Si la zone immediatemment a droite est occupテゥe
  269. else{
  270. struct fb* is_first = *(struct fb**)mem ;
  271. if((void*)is_first+is_first->size == ptr_taille_zone){
  272. is_first->size += taille_zone+sizeof(int);
  273. is_first->next = (void *)voisin_droit->next;
  274. }
  275. else{
  276. struct fb* fb_new = ptr_taille_zone;
  277. fb_new->size = taille_zone + sizeof(int);
  278. fb_new->next = (void *)voisin_droit;
  279. printf("On passelalal\n");
  280. is_first->next = (void *) fb_new;
  281. }
  282. }
  283. }
  284. //Si le bloc que l'on souhaite libテゥrer a un predecesseur libre
  285. else{
  286. //Si plus de voisin a droite, impossible a declencher ????
  287. if((void*)voisin_droit == NULL){
  288.  
  289. struct fb* fb_new = ptr_taille_zone;
  290. fb_new->size = taille_zone + sizeof(int);
  291. fb_new->next = NULL;
  292. voisin_gauche->next = fb_new;
  293. }
  294. //Si le voisin a droite est libre, fusionner voisin gauche et droite
  295. else if(zone+taille_zone == (void *)voisin_droit && (void*)voisin_gauche+voisin_gauche->size==ptr_taille_zone){
  296. voisin_gauche->size += taille_zone + voisin_droit->size + sizeof(int);
  297. voisin_gauche->next = voisin_droit->next;
  298. }
  299. //Si le voisin テ droite est occupテゥ
  300. else{
  301. //SI le voisin de gauche est occupテゥ, crテゥer un fb et que celui テ droite est libre
  302. if((void *)voisin_gauche+voisin_gauche->size != ptr_taille_zone){
  303. struct fb* fb_new = ptr_taille_zone;
  304. fb_new->size = taille_zone + sizeof(int)+voisin_droit->size;
  305. fb_new->next = (void *)voisin_droit->next;
  306. voisin_gauche->next = (void*) fb_new;
  307. }
  308. //Si le voisin de gauche est libre, fusionner
  309. else{
  310. voisin_gauche->size += taille_zone + sizeof(int);
  311. }
  312. }
  313. }*/
  314. }
  315.  
  316. /*
  317. Code de free de Antoine : ne marche pas
  318. printf("Adresse de la zone テ libテゥrer : %p\n",zone);
  319. struct fb* fb = *(struct fb**) get_memory_adr();
  320. void* zone_begin = zone - sizeof(int);
  321. size_t size_zone = *(int*) zone_begin;
  322. printf("Taille de la zone テ libテゥrer : %d\n",size_zone);
  323.  
  324. while ((fb + fb->size) < (struct fb*)zone_begin){
  325. fb = fb->next;
  326. }
  327.  
  328. //Zone libre prテゥcテゥdente adjacente テ la zone テ libテゥrer
  329. if ((fb + fb->size) == zone_begin){
  330. //Zone suivante
  331. if (fb + fb->size + sizeof(int) + size_zone == fb->next){
  332. fb->size = fb->size + sizeof(int) + size_zone + fb->next->size;
  333. fb->next = fb->next->next;
  334. } else {
  335. fb->size = fb->size + size_zone + sizeof(int);
  336. }
  337. } else {
  338. //A faire crテゥer fb et vテゥrifier si zone libre suivante adjacente
  339. struct fb* fb_new = (struct fb*)zone_begin;
  340. if ((struct fb*)zone_begin + sizeof(int) + size_zone == fb){
  341. fb_new->size = sizeof(int) + size_zone + fb->size;
  342. fb_new->next = fb->next;
  343. } else {
  344. fb_new->size = sizeof(int) + size_zone;
  345. fb_new->next = fb;
  346. }
  347. }*/
  348.  
  349. size_t mem_get_size(void * zone){
  350. return *(int*) zone - sizeof(int);
  351. }
  352.  
  353. /* Itテゥrateur sur le contenu de l'allocateur */
  354. void mem_show(void (*print)(void * zone, size_t size, int free)){
  355. affiche_chainage();
  356. void* mem = get_memory_adr();
  357. struct fb* fb = *(struct fb**) mem;
  358. void* addr = mem + sizeof(struct fb*);
  359. size_t count = sizeof(struct fb**);
  360. while(count < get_memory_size()){
  361. if(addr == (void *)fb){
  362. print(addr, fb->size, 1);
  363. addr = addr + fb->size;
  364. count += fb->size;
  365. fb = fb->next;
  366. } else {
  367. print(addr, *(int*)addr, 0);
  368. count += *(int*)addr + sizeof(int);
  369. addr += *(int*)addr + sizeof(int);
  370. }
  371. }
  372. }
  373.  
  374. int est_liberable(void * zone){
  375. void* mem = get_memory_adr();
  376. struct fb* fb = *(struct fb**) mem;
  377. void* addr = mem + sizeof(struct fb*);
  378. size_t count = sizeof(struct fb**);
  379. int retour = 0;
  380. while(count < get_memory_size() && retour == 0){
  381. if(addr == (void *)fb){
  382. addr = addr + fb->size;
  383. count += fb->size;
  384. fb = fb->next;
  385. } else {
  386. if(addr+sizeof(int) == zone)
  387. retour =1;
  388. count += *(int*)addr + sizeof(int);
  389. addr += *(int*)addr + sizeof(int);
  390. }
  391. }
  392. return retour;
  393. }
  394.  
  395.  
  396. /* Choix de la strategie et strategies usuelles */
  397. typedef struct fb* (mem_fit_function_t)(struct fb *, size_t);
  398.  
  399. void mem_fit(mem_fit_function_t*);
  400.  
  401. struct fb* mem_fit_first(struct fb* list, size_t size){
  402. while(list != NULL && list->size < size+sizeof(int)){
  403. list = list->next;
  404. }
  405. printf("%p\n",list);
  406. return list == NULL ? NULL : list;
  407. }
  408.  
  409. /* Si vous avez le temps */
  410. struct fb* mem_fit_best(struct fb* list, size_t size);
  411. struct fb* mem_fit_worst(struct fb* list, size_t size);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement