Advertisement
Guest User

Untitled

a guest
Sep 9th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. heap.h
  2. #ifndef HEAP_H
  3. #define HEAP_H
  4.  
  5. #include <types.h>
  6. #include <kernel_vma.h>
  7.  
  8. #define HEAP1_VMEM_ADDR KERNEL_VMA + 0x01000000
  9. #define HEAP1_PMEM_ADDR 0x01000000
  10. #define HEAP1_SIZE 0x01000000
  11. #define HEAP1_BLOCK_SIZE 0x00000010
  12.  
  13. #define HEAP2_VMEM_ADDR KERNEL_VMA + 0x02000000
  14. #define HEAP2_PMEM_ADDR 0x02000000
  15. #define HEAP2_SIZE 0x01000000
  16. #define HEAP2_BLOCK_SIZE 0x00001000
  17.  
  18.  
  19. heap.c:
  20. #include <types.h>
  21. #include <heap.h>
  22.  
  23. #include <libk/math.h>
  24. #include <paging.h>
  25. #include <libk/stdio.h>
  26.  
  27. kheap_t main_kheap;
  28.  
  29. void kheap_init(kheap_t* kheap)
  30. {
  31. kheap->fblock = NULL;
  32. }
  33.  
  34. void kheap_add_block(kheap_t* kheap, uint64_t addr, uint32_t size, uint32_t bsize)
  35. {
  36. kheapblock_t *kheapblock;
  37.  
  38. // store size & bsize into kheapblock
  39. kheapblock = (kheapblock_t*)addr;
  40. kheapblock->size = size - (uint32_t)sizeof(kheapblock_t);
  41. kheapblock->bsize = bsize;
  42.  
  43. // add kheapblock to kheap
  44. kheapblock->next = kheap->fblock;
  45. kheap->fblock = kheapblock;
  46.  
  47. // block count & bitmap
  48. uint32_t bcnt = kheapblock->size / kheapblock->bsize;
  49. uint8_t* bm = (uint8_t*)&kheapblock[1];
  50.  
  51. // clear bitmap
  52. for (size_t i = 0; i < bcnt; i++) {
  53. bm[i] = 0;
  54. }
  55.  
  56. uint32_t bcnt_used = upper_div((bcnt + (uint32_t)sizeof(kheapblock_t)), bsize);
  57. for (size_t i = 0; i < bcnt_used; i++) {
  58. bm[i] = 5;
  59. }
  60.  
  61. kheapblock->used = bcnt_used;
  62. }
  63.  
  64. void* kheap_alloc(kheap_t* kheap, uint32_t size)
  65. {
  66. kheapblock_t* kheapblock;
  67.  
  68. // find kheapblock that has enough space
  69. for (kheapblock = kheap->fblock; kheapblock; kheapblock = kheapblock->next) {
  70. if (kheapblock->size - (kheapblock->used * kheapblock->bsize) < size) {
  71. continue;
  72. }
  73.  
  74. // use heap with bsize 4096 just for that block size
  75. bool ind1 = ((size % 4096) == 0);
  76. bool ind2 = (kheapblock->bsize == 4096);
  77. if (ind1 + ind2 == 1) {
  78. continue;
  79. }
  80.  
  81. uint32_t bcnt = kheapblock->size / kheapblock->bsize;
  82. uint32_t bneed = upper_div(size, kheapblock->bsize);
  83. uint8_t* bm = (uint8_t*)&kheapblock[1];
  84.  
  85. // find empty block
  86. for (size_t i = 0; i < bcnt; i++) {
  87. if (bm[i] != 0) {
  88. continue;
  89. }
  90.  
  91. // find bneed consecutive empty blocks
  92. size_t j;
  93. for (j = 0; bm[i + j] == 0 && j < bneed && i + j < bcnt; j++);
  94. if (j != bneed) {
  95. i += j - 1;
  96. continue;
  97. }
  98.  
  99. // using id for the block that is different from previous or next block
  100. uint8_t idp = bm[i - 1], idn = bm[i + j], id;
  101. for (id = idp + 1; id == idn || id == 0; id++);
  102.  
  103. // mark blocks as used
  104. for (j = 0; j < bneed; j++) {
  105. bm[i + j] = id;
  106. }
  107.  
  108. kheapblock->used += bneed;
  109.  
  110. return (void*)(i * kheapblock->bsize + (uintptr_t)&kheapblock[0]);
  111. }
  112. }
  113. printf("Error: there is no remaining memory in kheap\n");
  114. return NULL;
  115. }
  116.  
  117. void kheap_free(kheap_t* kheap, void* pointer)
  118. {
  119. kheapblock_t* kheapblock;
  120. for (kheapblock = kheap->fblock; kheapblock; kheapblock = kheapblock->next) {
  121. if ((uintptr_t)(pointer) > (uintptr_t)kheapblock && (uintptr_t)kheapblock + sizeof(kheapblock_t) + kheapblock->size) {
  122. // found block
  123.  
  124. // get index of bitmap entry
  125. uintptr_t pointer_offset = (uintptr_t)pointer - (uintptr_t)&kheapblock[1];
  126. uint32_t bi = (uint32_t)pointer_offset / kheapblock->bsize;
  127. uint8_t* bm = (uint8_t*)&kheapblock[1];
  128. uint8_t id = bm[bi];
  129. uint32_t max = kheapblock->size / kheapblock->bsize;
  130.  
  131. // set blocks as free
  132. size_t i;
  133. for (i = bi; bm[i] == id && i < max; i++) {
  134. bm[i] = 0;
  135. }
  136.  
  137. kheapblock->used -= (uint32_t)i - bi;
  138. return;
  139. }
  140. }
  141. printf("Error: %x not freed from kheap\n", pointer);
  142. }
  143.  
  144. void init_heap()
  145. {
  146. kheap_init(&main_kheap);
  147. kheap_add_block(&main_kheap, HEAP1_VMEM_ADDR, HEAP1_SIZE, HEAP1_BLOCK_SIZE);
  148. kheap_add_block(&main_kheap, HEAP2_VMEM_ADDR, HEAP2_SIZE, HEAP2_BLOCK_SIZE);
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement