vma.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "mm.h"
  2. #include "slab.h"
  3. #include "internal.h"
  4. /**
  5. * @brief 获取一块新的vma结构体,并将其与指定的mm进行绑定
  6. *
  7. * @param mm 与VMA绑定的内存空间分布结构体
  8. * @return struct vm_area_struct* 新的VMA
  9. */
  10. struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)
  11. {
  12. struct vm_area_struct *vma = (struct vm_area_struct *)kmalloc(sizeof(struct vm_area_struct), 0);
  13. if (vma)
  14. vma_init(vma, mm);
  15. return vma;
  16. }
  17. /**
  18. * @brief 释放vma结构体
  19. *
  20. * @param vma 待释放的vma结构体
  21. */
  22. void vm_area_free(struct vm_area_struct *vma)
  23. {
  24. if (vma->vm_prev == NULL && vma->vm_next == NULL) // 如果当前是剩余的最后一个vma
  25. vma->vm_mm->vmas = NULL;
  26. kfree(vma);
  27. }
  28. /**
  29. * @brief 将vma结构体插入mm_struct的链表之中
  30. *
  31. * @param mm 内存空间分布结构体
  32. * @param vma 待插入的VMA结构体
  33. * @param prev 链表的前一个结点
  34. */
  35. void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma, struct vm_area_struct *prev)
  36. {
  37. struct vm_area_struct *next = NULL;
  38. vma->vm_prev = prev;
  39. if (prev) // 若指定了前一个结点,则直接连接
  40. {
  41. next = prev->vm_next;
  42. prev->vm_next = vma;
  43. }
  44. else // 否则将vma直接插入到给定的mm的vma链表之中
  45. {
  46. next = mm->vmas;
  47. mm->vmas = vma;
  48. }
  49. vma->vm_next = next;
  50. if (next != NULL)
  51. next->vm_prev = vma;
  52. }
  53. /**
  54. * @brief 将vma给定结构体从vma链表的结点之中删除
  55. *
  56. * @param mm 内存空间分布结构体
  57. * @param vma 待插入的VMA结构体
  58. */
  59. void __vma_unlink_list(struct mm_struct *mm, struct vm_area_struct *vma)
  60. {
  61. struct vm_area_struct *prev, *next;
  62. next = vma->vm_next;
  63. prev = vma->vm_prev;
  64. if (prev)
  65. prev->vm_next = next;
  66. else // 当前vma是链表中的第一个vma
  67. mm->vmas = next;
  68. if (next)
  69. next->vm_prev = prev;
  70. }