Created
June 11, 2019 10:00
-
-
Save FedorLap2006/44bb5b468561f066dbaa807e3c78896f to your computer and use it in GitHub Desktop.
DepthOS heap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
└─< (master|MERGING)* >──» qemu-system-i386 -M pc-i440fx-2.8 -kernel DepthOS-1.0 -s -serial stdio -monitor none -append 'console=ttyS0' | |
[HEAP] heap info - | |
{.beg = 0x11c100, .magic = 2037504287, .cur = 0x11c100, .last = 0x11c100, .end = 0x11c164} | |
[HEAP] ALLOC cur chunk - at 0x11c000 ( mem - 0x11c100 ) | |
{.size = 5, .magic = 2037504287, .used = 1, .next = 0x11c206} | |
[HEAP] heap info - | |
{.beg = 0x11c100, .magic = 2037504287, .cur = 0x11c206, .last = 0x11c100, .end = 0x11c164} | |
[HEAP] ALLOC cur chunk - at 0x11c106 ( mem - 0x11c206 ) | |
{.size = 0, .magic = 0, .used = 0, .next = 0x100} | |
0x11c100 - hello | |
[HEAP] FREE cur chunk - at 0x11c0f0 ( mem - 0x11c1f0 ) | |
{.size = 0, .magic = 0, .used = 0, .next = 0x100} | |
[HEAP] heap info - | |
{.beg = 0x11c100, .magic = 2037504287, .cur = 0x11c206, .last = 0x11c100, .end = 0x11c164} | |
[HEAP] cur chunk - at 0x11c000 ( mem - 0x11c100 ) | |
{.size = 5, .magic = 2037504287, .used = 1, .next = 0x11c206} | |
[HEAP] ALLOC cur chunk - at 0x11c106 ( mem - 0x11c206 ) | |
{.size = 5, .magic = 2037504287, .used = 1, .next = 0x11c30c} | |
[HEAP] heap info - | |
{.beg = 0x11c100, .magic = 2037504287, .cur = 0x11c30c, .last = 0x11c206, .end = 0x11c164} | |
[HEAP] ALLOC cur chunk - at 0x11c20c ( mem - 0x11c30c ) | |
{.size = 0, .magic = 0, .used = 0, .next = 0x100} | |
0x11c206 - new!! | |
0x11c100 - hello | |
[HEAP] FREE cur chunk - at 0x11c1f6 ( mem - 0x11c2f6 ) | |
┌─<Фёдор @ hp in /c/c/U/Ф/D/G/DepthOS> | |
└─< (master|MERGING)* >──» scp fed-lap@linux-fedlap06:~/code/github/DepthOS/DepthOS-1.0 DepthOS-1.0 130 < 12:40:33 | |
fed-lap@linux-fedlap06's password: | |
DepthOS-1.0 100% 83KB 554.7KB/s 00:00 | |
┌─<Фёдор @ hp in /c/c/U/Ф/D/G/DepthOS> | |
└─< (master|MERGING)* >──» qemu-system-i386 -M pc-i440fx-2.8 -kernel DepthOS-1.0 -s -serial stdio -monitor none -append 'console=ttyS0' | |
[HEAP] heap info - | |
{.beg = 0x11c100, .magic = 2037504287, .cur = 0x11c100, .last = 0x11c100, .end = 0x11c164} | |
0x11c100 - hello | |
[HEAP] heap info - | |
{.beg = 0x11c100, .magic = 2037504287, .cur = 0x11c206, .last = 0x11c100, .end = 0x11c164} | |
0x11c206 - new!! | |
0x11c100 - hello | |
┌─<Фёдор @ hp in /c/c/U/Ф/D/G/DepthOS> | |
└─< (master|MERGING)* >──» |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
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; | |
} | |
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
void free(void *p,mmh* heap); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment