Advertisement
lfed255-dev

heap.c

Jun 15th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.88 KB | None | 0 0
  1.  
  2. // HEAP.h
  3.  
  4. #pragma once
  5.  
  6. #include <depthos/stdtypes.h>
  7.  
  8. #define HEAP_MAGIC 0x7971D91F // 2037504287
  9.  
  10. // heap_magic overflow - 0x2C7971D91F and 191016065311
  11.  
  12. typedef struct __hmmc {
  13.         uint32_t magic;
  14.         size_t size;
  15.         uint8_t used;
  16.         struct __hmmc* next;
  17. }hmmc;
  18.  
  19. //#define phmmc hmmc*
  20.  
  21.  
  22. typedef struct __mmh {
  23.         uint32_t magic;
  24.         hmmc* beg;
  25.         hmmc* last;
  26.         hmmc* cur;
  27.         hmmc* end;
  28.         size_t used_mm;
  29. }mmh;
  30.  
  31. //#define pmmh mmh*
  32.  
  33.  
  34. #define mm2hmmc(mm) ((struct __hmmc*)(mm - sizeof(struct __hmmc)))
  35. #define hmmc2mm(hmmc) ((void*)(hmmc + sizeof(struct __hmmc)))
  36.  
  37. void* sbrk(size_t size);
  38.  
  39. void init_heap(mmh* heap,size_t size);
  40.  
  41. mmh* create_heap(size_t size);
  42.  
  43. void dumb_hmmc(hmmc* ch,uint8_t state);
  44. void dumb_mmh(mmh* h);
  45.  
  46. void* malloc(size_t size,mmh* heap);
  47.  
  48.  
  49. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. // HEAP.C
  51.  
  52. #include <depthos/string.h>
  53. #include <depthos/paging.h>
  54. #include <depthos/console.h>
  55. #include "depthos/heap.h"
  56.  
  57.  
  58. extern uint32_t end;
  59. extern pde_t *cur_pgd __align(4096);
  60. extern pde_t kernel_pgd[] __align(4096);
  61. extern int paging_enabled;
  62.  
  63. uint32_t placeAddr = (uint32_t)&end;
  64.  
  65. int heap_enabled = 0;
  66.  
  67. int ts_enable = 0;
  68.  
  69. /*
  70. #include <depthos/string.h>
  71. #include <depthos/paging.h>
  72. #include <depthos/console.h>
  73. #include "depthos/heap.h"
  74.  
  75.  
  76. extern uint32_t end;
  77. extern pde_t *cur_pgd __align(4096);
  78. extern pde_t kernel_pgd[] __align(4096);
  79. extern int paging_enabled;
  80.  
  81. uint32_t placeAddr = (uint32_t)&end;
  82.  
  83. int heap_enabled = 0;
  84.  
  85. int ts_enable = 0;
  86.  
  87. mmh* create_heap(size_t size) {
  88.         mmh* heap;
  89.         init_heap(heap,size);
  90.         return heap;
  91. }
  92.  
  93. void init_heap(mmh* heap,size_t size) {
  94.         heap->beg = sbrk(size);
  95.         heap->end = sbrk(0);
  96.         heap->cur = heap->last = heap->beg;
  97.    
  98.         memset(hmmc2mm(heap->beg),hmmc2mm(heap->end) - hmmc2mm(heap->beg),0);
  99. }
  100.  
  101.  
  102. void* sbrk(size_t size) {
  103.         if(ts_enable) {
  104.  
  105.         }
  106.         else {
  107.                 uint32_t t = placeAddr;
  108.                 placeAddr += size;
  109.                 return (void*)t;
  110.         }
  111.         return NULL;
  112. }
  113.  
  114.  
  115. void* malloc(size_t size,mmh* heap) {
  116.         hmmc* hcur = heap->cur;
  117.  
  118. #define checkNew(ch)        \
  119.         if (ch->size == 0) { \
  120.                 ch += ch->size;         \
  121.                 goto new_alloc;     \
  122.         }
  123.  
  124. #define checkEnd(ch,h)                                          \
  125.         if(hmmc2mm(ch) + ch->size == h->end) {  \
  126.                 ch->next = NULL;                                        \
  127.                 h->cur = h->end;                                        \
  128.         }                                                                               \
  129.  
  130.         if(heap->cur == heap->end) {
  131.                 return NULL;
  132.         }
  133.  
  134.         checkNew(hcur);
  135.         if(hcur->next != NULL){
  136.                 hcur = heap->beg;
  137.                 while(hcur != heap->cur) {
  138.                         checkNew(hcur);
  139.  
  140.                         if(hcur->used) {
  141.                                 hcur = hcur->next;
  142.                                 continue;
  143.                         }
  144.  
  145.                         if(hcur->size == size) {
  146.                                 hmmc* nch = hmmc2mm(hcur) + hcur->size;
  147.                                 hcur->next = nch;
  148.  
  149.                                 return (void*)hmmc2mm(hcur);
  150.                         }
  151.                         if(hcur->size > size) {
  152.                                 hmmc* nch = hmmc2mm(hcur) + size;
  153.                                 nch->next = NULL;
  154.                                 hcur->next = nch;
  155.                                 nch->next = mm2hmmc(hcur) + hcur->size;
  156.  
  157.                                 return (void*)hmmc2mm(hcur);
  158.                         }
  159.                         hcur = hcur->next;
  160.                 }
  161.         }
  162.  
  163.  
  164. new_alloc:;
  165.         hcur->next = hmmc2mm(hcur) + hcur->size;
  166.         hcur->size = size;
  167.         hcur->used = true;
  168.  
  169.         heap->cur = hmmc2mm(hcur) + hcur->size;
  170.         heap->last = hcur;
  171.  
  172.         checkEnd(hcur,heap);
  173.  
  174.         return (void*)hmmc2mm(hcur);
  175.  
  176. #undef checkNew
  177. #undef checkEnd
  178.  
  179. }
  180.  
  181. void free(void* p, mmh* heap) {
  182.         hmmc* ch = mm2hmmc(p);
  183.         if(ch->next == hmmc2mm(heap->cur)) {
  184.                 heap->cur = ch;
  185.                 heap->cur->next = NULL;
  186.         }
  187.  
  188.         ch->used = false;
  189. }
  190. */
  191.  
  192. /*
  193. #pragma once
  194.  
  195. #include <depthos/stdtypes.h>
  196.  
  197.  
  198. typedef struct __hmmc {
  199.         size_t size;
  200.         bool used;
  201.         struct __hmmc *next;
  202. }hmmc, *hmmc*;
  203.  
  204.  
  205. typedef struct __mmh {
  206.         hmmc* beg;
  207.         hmmc* last;
  208.         hmmc* cur;
  209.         hmmc* end;
  210. }mmh, *pmmh;
  211.  
  212.  
  213. #define mm2hmmc(mm) ((struct __hmmc*)(mm - sizeof(struct __hmmc)))
  214. #define hmmc2mm(hmmc) ((void*)(hmmc + sizeof(struct __hmmc)))
  215. */
  216. mmh* create_heap(size_t size) {
  217.         mmh* heap;
  218.         init_heap(heap,size);
  219.         return heap;
  220. }
  221.  
  222. void init_heap(mmh* heap,size_t size) {
  223.         heap->beg = sbrk(size);
  224.  
  225.         heap->beg->magic = HEAP_MAGIC;
  226.         heap->end = sbrk(0);
  227.         heap->cur = heap->last = heap->beg;
  228.         heap->magic = HEAP_MAGIC;
  229.         memset(hmmc2mm(heap->beg),hmmc2mm(heap->end) - hmmc2mm(heap->beg),0);
  230. }
  231.  
  232.  
  233. void* sbrk(size_t size) {
  234.         if(ts_enable) {
  235.  
  236.         }
  237.         else {
  238.                 uint32_t t = placeAddr;
  239.                 placeAddr += size;
  240.                 return (void*)t;
  241.         }
  242.         return NULL;
  243. }
  244. void dumb_hmmc(hmmc* ch,uint8_t state) {
  245.         printk("[HEAP] ");
  246.         if(state == 1) {
  247.         //      console_write_color("ALLOC",BLACK_COLOR,PURPLE_COLOR);
  248.                 printk("ALLOC ");
  249.         }
  250.         else if(state == 2){
  251.                 printk("FREE ");
  252.         //      console_write_color("FREE",BLACK_COLOR,WWBLUE_COLOR);
  253.         }
  254.         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));
  255. }
  256.  
  257. void dumb_mmh(mmh* h) {
  258.         printk("[HEAP] ");
  259.         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));
  260. }
  261.  
  262. void* malloc(size_t size,mmh* heap) {
  263.         hmmc* hcur = heap->beg;
  264.         hcur->next = NULL;
  265.         if(heap->cur == heap->end) {
  266.                 print_mod("sorry, out of heap memory",MOD_ERR);
  267.         }
  268.  
  269.  
  270.         dumb_mmh(heap);
  271.  
  272.         while(hcur != heap->cur) {
  273.  
  274.                 if(hcur->size == 0) {
  275.                         goto new_alloc;
  276.                 }
  277.  
  278.                 if(hcur->used) {
  279.                         hcur = hcur->next;
  280.                         continue;
  281.                 }
  282.  
  283.                 if(hcur->size == size) {
  284.                         void *mem = hmmc2mm(hcur);
  285.                         heap->used_mm += size;
  286.  
  287.                         hcur->next = (struct __hmmc*)(mem + size + 1);
  288.  
  289.                         hcur->used = true;
  290.  
  291.                         return mem;
  292.  
  293.                 }
  294.                 if(hcur->size > size) {
  295.                         void *mem = hmmc2mm(hcur);
  296.  
  297.                         hcur->next = (struct __hmmc*)(mem + size + 1);
  298.  
  299.                         hcur->used = true;
  300.  
  301.                         return mem;
  302.                 }
  303.  
  304.                 hcur = hcur->next;
  305.         }
  306.  
  307. new_alloc:;
  308.         hcur = heap->cur;
  309.         void *mem = hmmc2mm(hcur);
  310.  
  311.  
  312.  
  313.         hcur->size = size;
  314.         hcur->magic = heap->magic;
  315.         hcur->used = 1;
  316.  
  317.         heap->last = heap->cur;
  318.  
  319.         hcur = (hmmc*)(mem + size + 1);
  320.         heap->cur = hcur;
  321.  
  322.         heap->last->next = heap->cur;
  323.  
  324.  
  325.  
  326.  
  327.         return mem;
  328.  
  329. }
  330.  
  331. void free(void* p, mmh* heap) {
  332.         hmmc* ch = mm2hmmc(p);
  333.         ch->used = false;
  334.  
  335. //      dumb_hmmc(ch,2);
  336.  
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement