Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void* malloc(memory_t context, size_t size, bool Ring0)
- {
- /* TODO: REMOVE CONTEXT!*/
- malloc_t* current = &context.malloc_list; /* On UserSpace Layer, this can be easily done, somehow.?*/
- void* vmm_page;
- malloc_t* newblock;
- /* HERE: LABEL*/
- #define BlockSearch
- while ((current->size < (size + sizeof(malloc_t))) and (current->next != 0x0))
- {
- current = current->next;
- }
- /* Found a block, let me do a quick check;*/
- if (current->size == size)
- {
- current->previous->next = current->next; /* Now, i'm out :( */
- current->next->previous = current->previous; /* You'll never find me back :[ */
- return (void*)(current + sizeof(malloc_t));
- }
- else if (current->size > (size + sizeof(malloc_t)))
- {
- current->size = current->size - (size + sizeof(malloc_t)); /* Der Block davor bleibt frei */
- newblock = current + current->size; /* TODO: checken */
- newblock->size = size; /* Wir bauen uns einen Header des neuen Blocks */
- /* Warning: Check if Pages mapped */
- return (void*)(newblock + sizeof(malloc_t));
- }
- else if (current->size < size)
- {
- size_t reqpagesize = size / PAGE_SIZE;
- if (size % PAGE_SIZE > 0)
- {
- reqpagesize++;
- }
- vmm_page = vmm_alloc(reqpagesize, Ring0);
- if (vmm_page == (void*)0xDEADBEEF)
- {
- return (void*) 0xDEADBEEF; /* Not enough free memory */
- }
- else
- {
- if (current->next == 0x0)
- {
- malloc_t* newbigblock;
- newbigblock = (malloc_t*)vmm_page;
- newbigblock->size = (reqpagesize * PAGE_SIZE) - sizeof(malloc_t);
- newbigblock->previous = current;
- newbigblock->next = 0x0;
- /* goto 'label';*/
- return malloc(context, size, Ring0);
- /* Suche jetzt erneut nach freien Blöcken
- * TODO: Anti-Deadlock Protection!
- */
- }
- else
- {
- /* Da machsch e dumms Gsicht */
- return (void*)0xDEADBEEF;
- }
- }
- }
- return (void*)0xDEADBEEF;
- }
Advertisement
Add Comment
Please, Sign In to add comment