malloc.c 11 KB

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