Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // HEAP.h
- #pragma once
- #include <depthos/stdtypes.h>
- #define HEAP_MAGIC 0x7971D91F // 2037504287
- // heap_magic overflow - 0x2C7971D91F and 191016065311
- typedef struct __hmmc {
- uint32_t magic;
- size_t size;
- uint8_t used;
- struct __hmmc* next;
- }hmmc;
- //#define phmmc hmmc*
- typedef struct __mmh {
- uint32_t magic;
- hmmc* beg;
- hmmc* last;
- hmmc* cur;
- hmmc* end;
- size_t used_mm;
- }mmh;
- //#define pmmh mmh*
- #define mm2hmmc(mm) ((struct __hmmc*)(mm - sizeof(struct __hmmc)))
- #define hmmc2mm(hmmc) ((void*)(hmmc + sizeof(struct __hmmc)))
- void* sbrk(size_t size);
- void init_heap(mmh* heap,size_t size);
- mmh* create_heap(size_t size);
- void dumb_hmmc(hmmc* ch,uint8_t state);
- void dumb_mmh(mmh* h);
- void* malloc(size_t size,mmh* heap);
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // HEAP.C
- #include <depthos/string.h>
- #include <depthos/paging.h>
- #include <depthos/console.h>
- #include "depthos/heap.h"
- extern uint32_t end;
- extern pde_t *cur_pgd __align(4096);
- extern pde_t kernel_pgd[] __align(4096);
- extern int paging_enabled;
- uint32_t placeAddr = (uint32_t)&end;
- int heap_enabled = 0;
- int ts_enable = 0;
- /*
- #include <depthos/string.h>
- #include <depthos/paging.h>
- #include <depthos/console.h>
- #include "depthos/heap.h"
- extern uint32_t end;
- extern pde_t *cur_pgd __align(4096);
- extern pde_t kernel_pgd[] __align(4096);
- extern int paging_enabled;
- uint32_t placeAddr = (uint32_t)&end;
- int heap_enabled = 0;
- int ts_enable = 0;
- mmh* create_heap(size_t size) {
- mmh* heap;
- init_heap(heap,size);
- return heap;
- }
- void init_heap(mmh* heap,size_t size) {
- heap->beg = sbrk(size);
- heap->end = sbrk(0);
- heap->cur = heap->last = heap->beg;
- memset(hmmc2mm(heap->beg),hmmc2mm(heap->end) - hmmc2mm(heap->beg),0);
- }
- void* sbrk(size_t size) {
- if(ts_enable) {
- }
- else {
- uint32_t t = placeAddr;
- placeAddr += size;
- return (void*)t;
- }
- return NULL;
- }
- void* malloc(size_t size,mmh* heap) {
- hmmc* hcur = heap->cur;
- #define checkNew(ch) \
- if (ch->size == 0) { \
- ch += ch->size; \
- goto new_alloc; \
- }
- #define checkEnd(ch,h) \
- if(hmmc2mm(ch) + ch->size == h->end) { \
- ch->next = NULL; \
- h->cur = h->end; \
- } \
- if(heap->cur == heap->end) {
- return NULL;
- }
- checkNew(hcur);
- if(hcur->next != NULL){
- hcur = heap->beg;
- while(hcur != heap->cur) {
- checkNew(hcur);
- if(hcur->used) {
- hcur = hcur->next;
- continue;
- }
- if(hcur->size == size) {
- hmmc* nch = hmmc2mm(hcur) + hcur->size;
- hcur->next = nch;
- return (void*)hmmc2mm(hcur);
- }
- if(hcur->size > size) {
- hmmc* nch = hmmc2mm(hcur) + size;
- nch->next = NULL;
- hcur->next = nch;
- nch->next = mm2hmmc(hcur) + hcur->size;
- return (void*)hmmc2mm(hcur);
- }
- hcur = hcur->next;
- }
- }
- new_alloc:;
- hcur->next = hmmc2mm(hcur) + hcur->size;
- hcur->size = size;
- hcur->used = true;
- heap->cur = hmmc2mm(hcur) + hcur->size;
- heap->last = hcur;
- checkEnd(hcur,heap);
- return (void*)hmmc2mm(hcur);
- #undef checkNew
- #undef checkEnd
- }
- void free(void* p, mmh* heap) {
- hmmc* ch = mm2hmmc(p);
- if(ch->next == hmmc2mm(heap->cur)) {
- heap->cur = ch;
- heap->cur->next = NULL;
- }
- ch->used = false;
- }
- */
- /*
- #pragma once
- #include <depthos/stdtypes.h>
- typedef struct __hmmc {
- size_t size;
- bool used;
- struct __hmmc *next;
- }hmmc, *hmmc*;
- typedef struct __mmh {
- hmmc* beg;
- hmmc* last;
- hmmc* cur;
- hmmc* end;
- }mmh, *pmmh;
- #define mm2hmmc(mm) ((struct __hmmc*)(mm - sizeof(struct __hmmc)))
- #define hmmc2mm(hmmc) ((void*)(hmmc + sizeof(struct __hmmc)))
- */
- mmh* create_heap(size_t size) {
- mmh* heap;
- init_heap(heap,size);
- return heap;
- }
- void init_heap(mmh* heap,size_t size) {
- heap->beg = sbrk(size);
- heap->beg->magic = HEAP_MAGIC;
- heap->end = sbrk(0);
- heap->cur = heap->last = heap->beg;
- heap->magic = HEAP_MAGIC;
- memset(hmmc2mm(heap->beg),hmmc2mm(heap->end) - hmmc2mm(heap->beg),0);
- }
- void* sbrk(size_t size) {
- if(ts_enable) {
- }
- else {
- uint32_t t = placeAddr;
- placeAddr += size;
- return (void*)t;
- }
- return NULL;
- }
- void dumb_hmmc(hmmc* ch,uint8_t state) {
- printk("[HEAP] ");
- if(state == 1) {
- // console_write_color("ALLOC",BLACK_COLOR,PURPLE_COLOR);
- printk("ALLOC ");
- }
- else if(state == 2){
- printk("FREE ");
- // console_write_color("FREE",BLACK_COLOR,WWBLUE_COLOR);
- }
- printk("cur chunk - at 0x%x ( mem - 0x%x ) \n{.size = %d, .magic = %lu, .used = %d, .next = 0x%x}\n",ch,ch + sizeof(hmmc), ch->size,ch->magic,ch->used,ch->next + sizeof(hmmc));
- }
- void dumb_mmh(mmh* h) {
- printk("[HEAP] ");
- printk("heap info - \n{.beg = 0x%x, .magic = %lu, .cur = 0x%x, .last = 0x%x, .end = 0x%x}\n", h->beg + sizeof(hmmc),h->magic,h->cur + sizeof(hmmc),h->last + sizeof(hmmc),h->end + sizeof(hmmc));
- }
- void* malloc(size_t size,mmh* heap) {
- hmmc* hcur = heap->beg;
- hcur->next = NULL;
- if(heap->cur == heap->end) {
- print_mod("sorry, out of heap memory",MOD_ERR);
- }
- dumb_mmh(heap);
- while(hcur != heap->cur) {
- if(hcur->size == 0) {
- goto new_alloc;
- }
- if(hcur->used) {
- hcur = hcur->next;
- continue;
- }
- if(hcur->size == size) {
- void *mem = hmmc2mm(hcur);
- heap->used_mm += size;
- hcur->next = (struct __hmmc*)(mem + size + 1);
- hcur->used = true;
- return mem;
- }
- if(hcur->size > size) {
- void *mem = hmmc2mm(hcur);
- hcur->next = (struct __hmmc*)(mem + size + 1);
- hcur->used = true;
- return mem;
- }
- hcur = hcur->next;
- }
- new_alloc:;
- hcur = heap->cur;
- void *mem = hmmc2mm(hcur);
- hcur->size = size;
- hcur->magic = heap->magic;
- hcur->used = 1;
- heap->last = heap->cur;
- hcur = (hmmc*)(mem + size + 1);
- heap->cur = hcur;
- heap->last->next = heap->cur;
- return mem;
- }
- void free(void* p, mmh* heap) {
- hmmc* ch = mm2hmmc(p);
- ch->used = false;
- // dumb_hmmc(ch,2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement