Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <errno.h>
  3. #include <string.h>
  4. #include "./include/my_malloc.h"
  5.  
  6. metadata_t *head = NULL;
  7.  
  8. struct chunk {
  9. struct chunk *next, *prev;
  10. size_t size;
  11. int free;
  12. void *data;
  13. };
  14. void my_putchar(char c)
  15. {
  16. write(1, &c, 1);
  17. }
  18. typedef struct chunk *Chunk;
  19.  
  20.  
  21.  
  22. void my_putnbr(int nb)
  23. {
  24. int debut;
  25. int fin;
  26.  
  27. if (nb < 0) {
  28. my_putchar('-');
  29. my_putnbr(-nb);
  30. } else {
  31. fin = nb % 10;
  32. debut = nb / 10;
  33. if (debut != 0)
  34. my_putnbr(debut);
  35. my_putchar(fin + '0');
  36. }
  37. }
  38.  
  39.  
  40. int put_adress(unsigned long nb)
  41. {
  42. int to_print;
  43. int base;
  44. char *str = "0123456789";
  45.  
  46. base = strlen(str);
  47. if (nb >= base) {
  48. to_print = nb % base;
  49. nb = (nb - to_print) / base;
  50. put_adress(nb);
  51. my_putchar(str[to_print]);
  52. } else
  53. my_putchar(str[nb]);
  54. return (0);
  55. }
  56.  
  57. static void *malloc_base() {
  58. /*static Chunk b = NULL;
  59. if (!b) {
  60. b = sbrk(word_align(sizeof(struct chunk)));
  61. if (b == (void*) -1) {
  62. _exit(127);
  63. }
  64. b->next = NULL;
  65. b->prev = NULL;
  66. b->size = 0;
  67. b->free = 0;
  68. b->data = NULL;
  69. }
  70. return b;*/
  71. }
  72.  
  73. metadata_t *find_block(size_t size, metadata_t **old)
  74. {
  75. metadata_t *tmp = head;
  76.  
  77. while (tmp) {
  78.  
  79. void *tmppp = sbrk(0);
  80. put_adress((long int)tmppp);
  81. write(1, "\n", 1);
  82. put_adress((long int)tmp);
  83. write(1, "\n", 1);
  84.  
  85. my_putnbr(tmp->size);
  86.  
  87. my_putnbr(tmp->free);
  88.  
  89. write(1, "step01\n", 7);
  90.  
  91. if (tmp->free && tmp->size > size) {
  92. write(1, "step04\n", 7);
  93. return tmp;
  94. }
  95. write(1, "step02\n", 7);
  96. *old = tmp;
  97. write(1, "step03\n", 7);
  98. tmp = tmp->next;
  99. }
  100. return NULL;
  101. }
  102.  
  103. void malloc_merge_next(Chunk c) {
  104. c->size = c->size + c->next->size + sizeof(struct chunk);
  105. c->next = c->next->next;
  106. if (c->next) {
  107. c->next->prev = c;
  108. }
  109. }
  110.  
  111. size_t get_size_to_alloc(size_t raw_size)
  112. {
  113. size_t amount = 0;
  114. while (amount <= raw_size) {
  115. amount += getpagesize();
  116. }
  117. amount += (getpagesize() * 10);
  118. return amount;
  119. }
  120.  
  121. void split_block(metadata_t *first, size_t size)
  122. {
  123. metadata_t *second = (metadata_t*)((char*) first
  124. + size + sizeof(metadata_t));
  125. second->next = first->next;
  126. first->next = second;
  127. if (second->next)
  128. second->next->prev = second;
  129. second->size = first->size - (size + sizeof(metadata_t));
  130. first->size = size;
  131. second->free = 1;
  132. second->prev = first;
  133. second->data = second + 1;
  134. }
  135.  
  136. void *alloc_first(size_t size)
  137. {
  138. size_t raw_size = size + sizeof(metadata_t);
  139. size_t to_alloc = get_size_to_alloc(raw_size);
  140.  
  141. head = sbrk(0);
  142. if (sbrk(to_alloc) == (void*) -1)
  143. return NULL;
  144. head->size = to_alloc - sizeof(metadata_t);
  145. head->next = NULL;
  146. head->data = head + 1;
  147. head->prev = NULL;
  148. head->free = 0;
  149. split_block(head, size);
  150. put_adress((long int)head);
  151. write(1, "===\n", 4);
  152. put_adress((long int)head->next);
  153. write(1, "\n===\n", 5);
  154. return head;
  155. }
  156.  
  157. void *append_new_blocks(metadata_t *last, size_t size)
  158. {
  159. size_t raw_size = size + sizeof(metadata_t);
  160. size_t to_alloc = get_size_to_alloc(raw_size);
  161. metadata_t *new;
  162.  
  163. new = sbrk(0);
  164. if (sbrk(to_alloc) == (void*) -1)
  165. return NULL;
  166. new->data = new + 1;
  167. new->prev = last;
  168. new->free = 0;
  169. new->next = NULL;
  170. new->size = to_alloc - sizeof(metadata_t);
  171. last->next = new;
  172. split_block(new, size);
  173. return new;
  174. }
  175.  
  176. void *fill_block(metadata_t *to_fill, size_t size)
  177. {
  178. write(1, "fill\n", 5);
  179. to_fill->free = 0;
  180. if (to_fill->size > size + sizeof(metadata_t))
  181. split_block(to_fill, size);
  182. return to_fill;
  183. }
  184.  
  185. void *malloc(size_t size)
  186. {
  187. metadata_t *prev;
  188. metadata_t *tmp;
  189.  
  190. my_putnbr(size);
  191. write(1, "\n", 1);
  192.  
  193. if (!size)
  194. return NULL;
  195. if (!head)
  196. return alloc_first(size);
  197. put_adress((long int)head);
  198. write(1, "===\n", 4);
  199. put_adress((long int)head->next);
  200. write(1, "\n===\n", 5);
  201. write(1, "step1\n", 6);
  202. tmp = find_block(size, &prev);
  203. write(1, "step2\n", 6);
  204. if (!tmp) {
  205. write(1, "step3\n", 6);
  206. return append_new_blocks(prev, size);
  207. } else {
  208. write(1, "step4\n", 6);
  209. return fill_block(tmp, size);
  210. }
  211. }
  212.  
  213. void free(void *ptr) {
  214. return;
  215. if (!ptr || ptr < malloc_base() || ptr > sbrk(0)) return;
  216. Chunk c = (Chunk) ptr - 1;
  217. if (c->data != ptr) return;
  218. c->free = 1;
  219.  
  220. if (c->next && c->next->free) {
  221. malloc_merge_next(c);
  222. }
  223. if (c->prev->free) {
  224. malloc_merge_next(c = c->prev);
  225. }
  226. if (!c->next) {
  227. c->prev->next = NULL;
  228. sbrk(- c->size - sizeof(struct chunk));
  229. }
  230. }
  231. /*
  232. void *calloc(size_t nmemb, size_t size) {
  233. return NULL;
  234. size_t length = nmemb * size;
  235. void *ptr = malloc(length);
  236. if (ptr) {
  237. char *dst = ptr;
  238. for (size_t i = 0; i < length; *dst = 0, ++dst, ++i);
  239. }
  240. return ptr;
  241. }
  242.  
  243. void *realloc(void *ptr, size_t size) {
  244. return NULL;
  245. void *newptr = malloc(size);
  246. if (newptr && ptr && ptr >= malloc_base() && ptr <= sbrk(0)) {
  247. Chunk c = (Chunk) ptr - 1;
  248. if (c->data == ptr) {
  249. size_t length = c->size > size ? size : c->size;
  250. char *dst = newptr, *src = ptr;
  251. for (size_t i = 0; i < length; *dst = *src, ++src, ++dst, ++i);
  252. free(ptr);
  253. }
  254. }
  255. return newptr;
  256. }
  257. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement