Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <depthos/heap.h>
- #include <depthos/paging.h>
- extern uint32_t end;
- uint32_t _memory_start = (uint32_t)&end;
- extern page_t heap_blks_pgt1[1024] __align(4096);
- typedef struct __heap_blockm_state {
- enum {
- CACHEB,
- RESB,
- }type;
- uint32_t begin;
- uint32_t end;
- union {
- struct {
- heap_cacheblock_t* cbtop;
- heap_cacheblock_t* cbroot;
- };
- struct {
- heap_resblock_t* rbtop;
- heap_resblock_t* rbroot;
- };
- };
- }heap_blockm_state;
- static heap_blockm_state_t _cbstate = { .type = CACHEB };
- static heap_blockm_state_t _rbstate = { .type = RESB };
- bool _heap_autorescue_mode;
- static heap_cacheblock_t* _heap_alloc_cacheblock() {
- heap_cacheblock_t *newblk;
- if (_cbstate.cbtop == NULL) {
- _cbstate.cbtop = (heap_cacheblock_t*)_cbstate.begin;
- _cbstate.cbroot = (heap_cacheblock_t*)_cbstate.cbtop;
- return _cbstate.cbtop;
- } else {
- newblk = _find_cacheblock();
- if (newblk) {
- newblk->busy_mru = true;
- return newblk;
- }
- if (_cbstate.cbtop + sizeof(heap_cacheblock_t) >= _cbstate.end) {
- return NULL;
- }
- newblk = (heap_cacheblock_t*)_cbstate.cbtop + sizeof(heap_cacheblock_t);
- newblk->prev = _cbstate.cbtop;
- _cbstate.cbtop->next = newblk;
- _cbstate.cbtop = newblk;
- newblk->busy_mru = true;
- return newblk;
- }
- }
- static heap_cacheblock_t* _heap_find_cacheblock() {
- heap_cacheblock_t *cur;
- cur = _cbstate.cbroot;
- while(cur != NULL && cur->busy_mru) {
- cur = cur->next;
- }
- return cur;
- }
- static void _heap_free_cacheblock(heap_cacheblock_t* cb) {
- cb->busy_mru = false;
- }
- heap_resblock_t* _heap_alloc_resblock() {
- heap_resblock_t *newblk;
- if (_rbstate.rbtop == NULL) {
- _rbstate.rbtop = (heap_resblock_t*)_rbstate.begin;
- _rbstate.rbroot = (heap_resblock_t*)_rbstate.rbtop;
- return _rbstate.rbtop;
- } else {
- newblk = _find_resblock();
- if (newblk) {
- newblk->busy_mru = true;
- return newblk;
- }
- if (_rbstate.rbtop + sizeof(heap_resblock_t) >= _rbstate.end) {
- return NULL;
- }
- newblk = (heap_resblock_t*)_rbstate.rbtop + sizeof(heap_resblock_t);
- newblk->prev = _rbstate.rbtop;
- _rbstate.rbtop->next = newblk;
- _rbstate.rbtop = newblk;
- newblk->busy_mru = true;
- return newblk;
- }
- }
- heap_resblock_t* _heap_find_resblock() {
- heap_resblock_t *cur;
- cur = _rbstate.rbroot;
- while(cur != NULL && cur->busy_mru) {
- cur = (heap_resblock_t*)cur + sizeof(heap_resblock_t);
- }
- return cur;
- }
- void _heap_free_resblock(heap_resblock_t* rb) {
- rb->busy_mru = false;
- }
- void __kheap_init() {
- _rbstate.begin = parse_page(&heap_blks_pgt1[0]).frame;
- _rbstate.end = parse_page(&heap_blks_pgt1[11]).frame - 1;
- _cbstate.begin = parse_page(&heap_blks_pgt1[11]).frame; // 10(11) - 2000 resources blocks
- _cbstate.end = parse_page(&heap_blks_pgt1[1023]).frame + 4096 - 1; // other - cache blocks
- _heap_autorescue_mode = true;
- }
- #define lbtomem(lb) ((void*)lb + sizeof(struct __heap_localblock))
- #define memtolb(mem) ((heap_localblock_t*)mem - sizeof(struct __heap_localblock))
- void* __heap_alloc(heap_resblock_t* resb, size_t size) {
- heap_cacheblock_t** curp = &resb->root_cacheblock;
- heap_cacheblock_t* cur = *curp;
- while(*curp != NULL) {
- if(cur->busy) goto nextel;
- if(cur->size == size) {
- cur->busy = true;
- return lbtomem(cur->localblock);
- }
- if(cur->size > size) {
- heap__heap_alloc_cacheblock()
- }
- nextel:;
- cur = cur->next;
- curp = &cur->next;
- }
- }
- void __heap_free(void* ptr) {
- // ...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement