malloc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #include <stdlib.h>
  2. #include <libsystem/syscall.h>
  3. #include <stddef.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <stdio.h>
  7. #define PAGE_4K_SHIFT 12
  8. #define PAGE_2M_SHIFT 21
  9. #define PAGE_1G_SHIFT 30
  10. #define PAGE_GDT_SHIFT 39
  11. // 不同大小的页的容量
  12. #define PAGE_4K_SIZE (1UL << PAGE_4K_SHIFT)
  13. #define PAGE_2M_SIZE (1UL << PAGE_2M_SHIFT)
  14. #define PAGE_1G_SIZE (1UL << PAGE_1G_SHIFT)
  15. // 屏蔽低于x的数值
  16. #define PAGE_4K_MASK (~(PAGE_4K_SIZE - 1))
  17. #define PAGE_2M_MASK (~(PAGE_2M_SIZE - 1))
  18. // 将addr按照x的上边界对齐
  19. #define PAGE_4K_ALIGN(addr) (((unsigned long)(addr) + PAGE_4K_SIZE - 1) & PAGE_4K_MASK)
  20. #define PAGE_2M_ALIGN(addr) (((unsigned long)(addr) + PAGE_2M_SIZE - 1) & PAGE_2M_MASK)
  21. /**
  22. * @brief 显式链表的结点
  23. *
  24. */
  25. typedef struct malloc_mem_chunk_t
  26. {
  27. uint64_t length; // 整个块所占用的内存区域的大小
  28. struct malloc_mem_chunk_t *prev; // 上一个结点的指针
  29. struct malloc_mem_chunk_t *next; // 下一个结点的指针
  30. } malloc_mem_chunk_t;
  31. static uint64_t brk_base_addr = 0; // 堆区域的内存基地址
  32. static uint64_t brk_max_addr = 0; // 堆区域的内存最大地址
  33. static uint64_t brk_managed_addr = 0; // 堆区域已经被管理的地址
  34. // 空闲链表
  35. // 按start_addr升序排序
  36. static malloc_mem_chunk_t *malloc_free_list = NULL;
  37. static malloc_mem_chunk_t *malloc_free_list_end = NULL; // 空闲链表的末尾结点
  38. static uint64_t count_last_free_size = 0; // 统计距离上一次回收内存,已经free了多少内存
  39. /**
  40. * @brief 将块插入空闲链表
  41. *
  42. * @param ck 待插入的块
  43. */
  44. static void malloc_insert_free_list(malloc_mem_chunk_t *ck);
  45. /**
  46. * @brief 当堆顶空闲空间大于2个页的空间的时候,释放1个页
  47. *
  48. */
  49. static void release_brk();
  50. /**
  51. * @brief 在链表中检索符合要求的空闲块(best fit)
  52. *
  53. * @param size 块的大小
  54. * @return malloc_mem_chunk_t*
  55. */
  56. static malloc_mem_chunk_t *malloc_query_free_chunk_bf(uint64_t size)
  57. {
  58. // 在满足best fit的前提下,尽可能的使分配的内存在低地址
  59. // 使得总的堆内存可以更快被释放
  60. if (malloc_free_list == NULL)
  61. {
  62. return NULL;
  63. }
  64. malloc_mem_chunk_t *ptr = malloc_free_list;
  65. malloc_mem_chunk_t *best = NULL;
  66. // printf("query size=%d", size);
  67. while (ptr != NULL)
  68. {
  69. // printf("ptr->length=%#010lx\n", ptr->length);
  70. if (ptr->length == size)
  71. {
  72. best = ptr;
  73. break;
  74. }
  75. if (ptr->length > size)
  76. {
  77. if (best == NULL)
  78. best = ptr;
  79. else if (best->length > ptr->length)
  80. best = ptr;
  81. }
  82. ptr = ptr->next;
  83. }
  84. return best;
  85. }
  86. /**
  87. * @brief 在链表中检索符合要求的空闲块(first fit)
  88. *
  89. * @param size
  90. * @return malloc_mem_chunk_t*
  91. */
  92. static malloc_mem_chunk_t *malloc_query_free_chunk_ff(uint64_t size)
  93. {
  94. if (malloc_free_list == NULL)
  95. return NULL;
  96. malloc_mem_chunk_t *ptr = malloc_free_list;
  97. while (ptr)
  98. {
  99. if (ptr->length >= size)
  100. {
  101. return ptr;
  102. }
  103. ptr = ptr->next;
  104. }
  105. return NULL;
  106. }
  107. /**
  108. * @brief 扩容malloc管理的内存区域
  109. *
  110. * @param size 扩大的内存大小
  111. */
  112. static int malloc_enlarge(int64_t size)
  113. {
  114. if (brk_base_addr == 0) // 第一次调用,需要初始化
  115. {
  116. brk_base_addr = sbrk(0);
  117. // printf("brk_base_addr=%#018lx\n", brk_base_addr);
  118. brk_managed_addr = brk_base_addr;
  119. brk_max_addr = brk_base_addr;
  120. }
  121. int64_t free_space = brk_max_addr - brk_managed_addr;
  122. // printf("size=%ld\tfree_space=%ld\n", size, free_space);
  123. if (free_space < size) // 现有堆空间不足
  124. {
  125. if (sbrk(size - free_space) != (void *)(-1))
  126. brk_max_addr = sbrk((0));
  127. else
  128. {
  129. put_string("malloc_enlarge(): no_mem\n", COLOR_YELLOW, COLOR_BLACK);
  130. return -ENOMEM;
  131. }
  132. // printf("brk max addr = %#018lx\n", brk_max_addr);
  133. }
  134. // 扩展管理的堆空间
  135. // 在新分配的内存的底部放置header
  136. // printf("managed addr = %#018lx\n", brk_managed_addr);
  137. malloc_mem_chunk_t *new_ck = (malloc_mem_chunk_t *)brk_managed_addr;
  138. new_ck->length = brk_max_addr - brk_managed_addr;
  139. // printf("new_ck->start_addr=%#018lx\tbrk_max_addr=%#018lx\tbrk_managed_addr=%#018lx\n", (uint64_t)new_ck, brk_max_addr, brk_managed_addr);
  140. new_ck->prev = NULL;
  141. new_ck->next = NULL;
  142. brk_managed_addr = brk_max_addr;
  143. malloc_insert_free_list(new_ck);
  144. return 0;
  145. }
  146. /**
  147. * @brief 合并空闲块
  148. *
  149. */
  150. static void malloc_merge_free_chunk()
  151. {
  152. if (malloc_free_list == NULL)
  153. return;
  154. malloc_mem_chunk_t *ptr = malloc_free_list->next;
  155. while (ptr != NULL)
  156. {
  157. // 内存块连续
  158. if (((uint64_t)(ptr->prev) + ptr->prev->length == (uint64_t)ptr))
  159. {
  160. // printf("merged %#018lx and %#018lx\n", (uint64_t)ptr, (uint64_t)(ptr->prev));
  161. // 将ptr与前面的空闲块合并
  162. ptr->prev->length += ptr->length;
  163. ptr->prev->next = ptr->next;
  164. if (ptr->next == NULL)
  165. malloc_free_list_end = ptr->prev;
  166. else
  167. ptr->next->prev = ptr->prev;
  168. // 由于内存组成结构的原因,不需要free掉header
  169. ptr = ptr->prev;
  170. }
  171. ptr = ptr->next;
  172. }
  173. }
  174. /**
  175. * @brief 将块插入空闲链表
  176. *
  177. * @param ck 待插入的块
  178. */
  179. static void malloc_insert_free_list(malloc_mem_chunk_t *ck)
  180. {
  181. if (malloc_free_list == NULL) // 空闲链表为空
  182. {
  183. malloc_free_list = ck;
  184. malloc_free_list_end = ck;
  185. ck->prev = ck->next = NULL;
  186. return;
  187. }
  188. else
  189. {
  190. malloc_mem_chunk_t *ptr = malloc_free_list;
  191. while (ptr != NULL)
  192. {
  193. if ((uint64_t)ptr < (uint64_t)ck)
  194. {
  195. if (ptr->next == NULL) // 当前是最后一个项
  196. {
  197. ptr->next = ck;
  198. ck->next = NULL;
  199. ck->prev = ptr;
  200. malloc_free_list_end = ck;
  201. break;
  202. }
  203. else if ((uint64_t)(ptr->next) > (uint64_t)ck)
  204. {
  205. ck->prev = ptr;
  206. ck->next = ptr->next;
  207. ptr->next = ck;
  208. ck->next->prev = ck;
  209. break;
  210. }
  211. }
  212. else // 在ptr之前插入
  213. {
  214. if (ptr->prev == NULL) // 是第一个项
  215. {
  216. malloc_free_list = ck;
  217. ck->prev = NULL;
  218. ck->next = ptr;
  219. ptr->prev = ck;
  220. break;
  221. }
  222. else
  223. {
  224. ck->prev = ptr->prev;
  225. ck->next = ptr;
  226. ck->prev->next = ck;
  227. ptr->prev = ck;
  228. break;
  229. }
  230. }
  231. ptr = ptr->next;
  232. }
  233. }
  234. }
  235. /**
  236. * @brief 获取一块堆内存
  237. *
  238. * @param size 内存大小
  239. * @return void* 内存空间的指针
  240. *
  241. * 分配内存的时候,结点的prev next指针所占用的空间被当做空闲空间分配出去
  242. */
  243. void *malloc(ssize_t size)
  244. {
  245. // printf("malloc\n");
  246. // 计算需要分配的块的大小
  247. if (size + sizeof(uint64_t) <= sizeof(malloc_mem_chunk_t))
  248. size = sizeof(malloc_mem_chunk_t);
  249. else
  250. size += sizeof(uint64_t);
  251. // 采用best fit
  252. malloc_mem_chunk_t *ck = malloc_query_free_chunk_bf(size);
  253. if (ck == NULL) // 没有空闲块
  254. {
  255. // printf("no free blocks\n");
  256. // 尝试合并空闲块
  257. malloc_merge_free_chunk();
  258. ck = malloc_query_free_chunk_bf(size);
  259. // 找到了合适的块
  260. if (ck)
  261. goto found;
  262. // printf("before enlarge\n");
  263. // 找不到合适的块,扩容堆区域
  264. if (malloc_enlarge(size) == -ENOMEM)
  265. return (void *)-ENOMEM; // 内存不足
  266. malloc_merge_free_chunk(); // 扩容后运行合并,否则会导致碎片
  267. // 扩容后再次尝试获取
  268. ck = malloc_query_free_chunk_bf(size);
  269. }
  270. found:;
  271. // printf("ck = %#018lx\n", (uint64_t)ck);
  272. if (ck == NULL)
  273. return (void *)-ENOMEM;
  274. // printf("ck->prev=%#018lx ck->next=%#018lx\n", ck->prev, ck->next);
  275. // 分配空闲块
  276. // 从空闲链表取出
  277. if (ck->prev == NULL) // 当前是链表的第一个块
  278. {
  279. malloc_free_list = ck->next;
  280. }
  281. else
  282. ck->prev->next = ck->next;
  283. if (ck->next != NULL) // 当前不是最后一个块
  284. ck->next->prev = ck->prev;
  285. else
  286. malloc_free_list_end = ck->prev;
  287. // 当前块剩余的空间还能容纳多一个结点的空间,则分裂当前块
  288. if ((int64_t)(ck->length) - size > sizeof(malloc_mem_chunk_t))
  289. {
  290. // printf("seperate\n");
  291. malloc_mem_chunk_t *new_ck = (malloc_mem_chunk_t *)(((uint64_t)ck) + size);
  292. new_ck->length = ck->length - size;
  293. new_ck->prev = new_ck->next = NULL;
  294. // printf("new_ck=%#018lx, new_ck->length=%#010lx\n", (uint64_t)new_ck, new_ck->length);
  295. ck->length = size;
  296. malloc_insert_free_list(new_ck);
  297. }
  298. // printf("malloc done: %#018lx, length=%#018lx\n", ((uint64_t)ck + sizeof(uint64_t)), ck->length);
  299. // 此时链表结点的指针的空间被分配出去
  300. return (void *)((uint64_t)ck + sizeof(uint64_t));
  301. }
  302. /**
  303. * @brief 当堆顶空闲空间大于2个页的空间的时候,释放1个页
  304. *
  305. */
  306. static void release_brk()
  307. {
  308. // 先检测最顶上的块
  309. // 由于块按照开始地址排列,因此找最后一个块
  310. if (malloc_free_list_end == NULL)
  311. {
  312. printf("release(): free list end is null. \n");
  313. return;
  314. }
  315. if ((uint64_t)malloc_free_list_end + malloc_free_list_end->length == brk_max_addr && (uint64_t)malloc_free_list_end <= brk_max_addr - (PAGE_2M_SIZE << 1))
  316. {
  317. int64_t delta = ((brk_max_addr - (uint64_t)malloc_free_list_end) & PAGE_2M_MASK) - PAGE_2M_SIZE;
  318. // printf("(brk_max_addr - (uint64_t)malloc_free_list_end) & PAGE_2M_MASK=%#018lx\n ", (brk_max_addr - (uint64_t)malloc_free_list_end) & PAGE_2M_MASK);
  319. // printf("PAGE_2M_SIZE=%#018lx\n", PAGE_2M_SIZE);
  320. // printf("tdfghgbdfggkmfn=%#018lx\n ", (brk_max_addr - (uint64_t)malloc_free_list_end) & PAGE_2M_MASK - PAGE_2M_SIZE);
  321. // printf("delta=%#018lx\n ", delta);
  322. if (delta <= 0) // 不用释放内存
  323. return;
  324. sbrk(-delta);
  325. brk_max_addr = sbrk(0);
  326. brk_managed_addr = brk_max_addr;
  327. malloc_free_list_end->length = brk_max_addr - (uint64_t)malloc_free_list_end;
  328. }
  329. }
  330. /**
  331. * @brief 释放一块堆内存
  332. *
  333. * @param ptr 堆内存的指针
  334. */
  335. void free(void *ptr)
  336. {
  337. // 找到结点(此时prev和next都处于未初始化的状态)
  338. malloc_mem_chunk_t *ck = (malloc_mem_chunk_t *)((uint64_t)ptr - sizeof(uint64_t));
  339. // printf("free(): addr = %#018lx\t len=%#018lx\n", (uint64_t)ck, ck->length);
  340. count_last_free_size += ck->length;
  341. malloc_insert_free_list(ck);
  342. if (count_last_free_size > PAGE_2M_SIZE)
  343. {
  344. count_last_free_size = 0;
  345. malloc_merge_free_chunk();
  346. release_brk();
  347. }
  348. }