design.txt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. Allocate.
  2. =========
  3. We start with our initial segment.
  4. Address space
  5. I---------------------------------I
  6. B
  7. l
  8. k
  9. s
  10. We then split it at the aligner, which is used for making sure that the pointer is aligned properly.
  11. Address space
  12. I------I
  13. B ^ I--------------------------I
  14. l al
  15. k
  16. s
  17. We then use the remaining block, but leave the excessive space.
  18. Address space
  19. I------I
  20. B I--------I
  21. l \_________________/
  22. k our allocated block.
  23. s
  24. The pointer to the marked area is then returned.
  25. Deallocate
  26. ==========
  27. Address space
  28. I------I
  29. B I--------I
  30. l \_________________/
  31. k the used block we want to deallocate.
  32. s
  33. We start by inserting the block, while keeping the list sorted. See `insertion` for details.
  34. Address space
  35. I------I
  36. B I-----------------I
  37. l I--------I
  38. k
  39. s
  40. Now the merging phase starts. We first observe that the first and the second block shares the end and the start respectively, in other words, we can merge these by adding the size together:
  41. Address space
  42. I------------------------I
  43. B I--------I
  44. l
  45. k
  46. s
  47. Insertion
  48. =========
  49. We want to insert the block denoted by the tildes into our list. Perform a binary search to find where insertion is appropriate.
  50. Address space
  51. I------I
  52. B < here I--------I
  53. l I------------I
  54. k
  55. s I---I
  56. I~~~~~~~~~~I
  57. If the entry is not empty, we check if the block can be merged to the left (i.e., the previous block). If not, check if it is possible to the right. If both of these fails, we keep pushing the blocks to the right to the next entry until a empty entry is reached:
  58. Address space
  59. I------I
  60. B < here I--------I <~ this one cannot move down, due to being blocked.
  61. l
  62. k I------------I <~ thus we have moved this one down.
  63. s I---I
  64. I~~~~~~~~~~I
  65. Repeating yields:
  66. Address space
  67. I------I
  68. B < here
  69. l I--------I <~ this one cannot move down, due to being blocked.
  70. k I------------I <~ thus we have moved this one down.
  71. s I---I
  72. I~~~~~~~~~~I
  73. Now an empty space is left out, meaning that we can insert the block:
  74. Address space
  75. I------I
  76. B I----------I
  77. l I--------I
  78. k I------------I
  79. s I---I
  80. The insertion is now completed.
  81. Reallocation.
  82. =============
  83. We will first try to perform an in-place reallocation, and if that fails, we will use memmove.
  84. Address space
  85. I------I
  86. B \~~~~~~~~~~~~~~~~~~~~~/
  87. l needed
  88. k
  89. s
  90. We simply find the block next to our initial block. If this block is free and have sufficient size, we will simply merge it into our initial block. If these conditions are not met, we have to deallocate our list, and then allocate a new one, after which we use memmove to copy the data over to the newly allocated list.
  91. Guarantees made.
  92. ================
  93. 1. The list is always sorted.
  94. 2. No two free blocks overlap.
  95. 3. No two free blocks are adjacent.