Advertisement
lfed255-dev

Paste From Computer

Sep 6th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #include <depthos/heap.h>
  2. #include <depthos/paging.h>
  3.  
  4. extern uint32_t end;
  5. uint32_t _memory_start = (uint32_t)&end;
  6. extern page_t heap_blks_pgt1[1024] __align(4096);
  7.  
  8. typedef struct __heap_blockm_state {
  9. enum {
  10. CACHEB,
  11. RESB,
  12. }type;
  13. uint32_t begin;
  14. uint32_t end;
  15. union {
  16. struct {
  17. heap_cacheblock_t* cbtop;
  18. heap_cacheblock_t* cbroot;
  19. };
  20. struct {
  21. heap_resblock_t* rbtop;
  22. heap_resblock_t* rbroot;
  23. };
  24. };
  25. }heap_blockm_state;
  26.  
  27.  
  28.  
  29. static heap_blockm_state_t _cbstate = { .type = CACHEB };
  30. static heap_blockm_state_t _rbstate = { .type = RESB };
  31. bool _heap_autorescue_mode;
  32.  
  33.  
  34. static heap_cacheblock_t* _heap_alloc_cacheblock() {
  35. heap_cacheblock_t *newblk;
  36. if (_cbstate.cbtop == NULL) {
  37. _cbstate.cbtop = (heap_cacheblock_t*)_cbstate.begin;
  38. _cbstate.cbroot = (heap_cacheblock_t*)_cbstate.cbtop;
  39. return _cbstate.cbtop;
  40. } else {
  41. newblk = _find_cacheblock();
  42. if (newblk) {
  43. newblk->busy_mru = true;
  44. return newblk;
  45. }
  46. if (_cbstate.cbtop + sizeof(heap_cacheblock_t) >= _cbstate.end) {
  47. return NULL;
  48. }
  49. newblk = (heap_cacheblock_t*)_cbstate.cbtop + sizeof(heap_cacheblock_t);
  50. newblk->prev = _cbstate.cbtop;
  51. _cbstate.cbtop->next = newblk;
  52. _cbstate.cbtop = newblk;
  53. newblk->busy_mru = true;
  54. return newblk;
  55. }
  56.  
  57. }
  58. static heap_cacheblock_t* _heap_find_cacheblock() {
  59. heap_cacheblock_t *cur;
  60. cur = _cbstate.cbroot;
  61. while(cur != NULL && cur->busy_mru) {
  62. cur = cur->next;
  63. }
  64. return cur;
  65.  
  66. }
  67.  
  68. static void _heap_free_cacheblock(heap_cacheblock_t* cb) {
  69. cb->busy_mru = false;
  70. }
  71.  
  72. heap_resblock_t* _heap_alloc_resblock() {
  73. heap_resblock_t *newblk;
  74. if (_rbstate.rbtop == NULL) {
  75. _rbstate.rbtop = (heap_resblock_t*)_rbstate.begin;
  76. _rbstate.rbroot = (heap_resblock_t*)_rbstate.rbtop;
  77. return _rbstate.rbtop;
  78. } else {
  79. newblk = _find_resblock();
  80. if (newblk) {
  81. newblk->busy_mru = true;
  82. return newblk;
  83. }
  84. if (_rbstate.rbtop + sizeof(heap_resblock_t) >= _rbstate.end) {
  85. return NULL;
  86. }
  87. newblk = (heap_resblock_t*)_rbstate.rbtop + sizeof(heap_resblock_t);
  88. newblk->prev = _rbstate.rbtop;
  89. _rbstate.rbtop->next = newblk;
  90. _rbstate.rbtop = newblk;
  91. newblk->busy_mru = true;
  92. return newblk;
  93. }
  94.  
  95. }
  96. heap_resblock_t* _heap_find_resblock() {
  97. heap_resblock_t *cur;
  98. cur = _rbstate.rbroot;
  99. while(cur != NULL && cur->busy_mru) {
  100. cur = (heap_resblock_t*)cur + sizeof(heap_resblock_t);
  101. }
  102. return cur;
  103.  
  104. }
  105. void _heap_free_resblock(heap_resblock_t* rb) {
  106. rb->busy_mru = false;
  107. }
  108.  
  109.  
  110. void __kheap_init() {
  111. _rbstate.begin = parse_page(&heap_blks_pgt1[0]).frame;
  112. _rbstate.end = parse_page(&heap_blks_pgt1[11]).frame - 1;
  113.  
  114. _cbstate.begin = parse_page(&heap_blks_pgt1[11]).frame; // 10(11) - 2000 resources blocks
  115. _cbstate.end = parse_page(&heap_blks_pgt1[1023]).frame + 4096 - 1; // other - cache blocks
  116. _heap_autorescue_mode = true;
  117. }
  118.  
  119.  
  120. #define lbtomem(lb) ((void*)lb + sizeof(struct __heap_localblock))
  121. #define memtolb(mem) ((heap_localblock_t*)mem - sizeof(struct __heap_localblock))
  122.  
  123. void* __heap_alloc(heap_resblock_t* resb, size_t size) {
  124. heap_cacheblock_t** curp = &resb->root_cacheblock;
  125. heap_cacheblock_t* cur = *curp;
  126.  
  127. while(*curp != NULL) {
  128. if(cur->busy) goto nextel;
  129. if(cur->size == size) {
  130. cur->busy = true;
  131. return lbtomem(cur->localblock);
  132. }
  133. if(cur->size > size) {
  134. heap__heap_alloc_cacheblock()
  135. }
  136.  
  137. nextel:;
  138. cur = cur->next;
  139. curp = &cur->next;
  140. }
  141.  
  142. }
  143. void __heap_free(void* ptr) {
  144. // ...
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement