Guest User

Untitled

a guest
Mar 18th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.88 KB | None | 0 0
  1. void* malloc(memory_t context, size_t size, bool Ring0)
  2. {
  3.     /* TODO: REMOVE CONTEXT!*/
  4.  
  5.     malloc_t* current = &context.malloc_list; /* On UserSpace Layer, this can be easily done, somehow.?*/
  6.     void* vmm_page;
  7.     malloc_t* newblock;
  8.  
  9.     /* HERE: LABEL*/
  10.     #define BlockSearch
  11.  
  12.     while ((current->size < (size + sizeof(malloc_t))) and (current->next != 0x0))
  13.     {
  14.         current = current->next;
  15.     }
  16.  
  17.     /* Found a block, let me do a quick check;*/
  18.     if (current->size == size)
  19.     {
  20.         current->previous->next = current->next; /* Now, i'm out :( */
  21.         current->next->previous = current->previous; /* You'll never find me back :[ */
  22.         return (void*)(current + sizeof(malloc_t));
  23.     }
  24.  
  25.     else if (current->size > (size + sizeof(malloc_t)))
  26.     {
  27.         current->size = current->size - (size + sizeof(malloc_t)); /* Der Block davor bleibt frei */
  28.  
  29.         newblock = current + current->size; /* TODO: checken */
  30.         newblock->size = size; /* Wir bauen uns einen Header des neuen Blocks */
  31.  
  32.         /* Warning: Check if Pages mapped */
  33.         return (void*)(newblock + sizeof(malloc_t));
  34.     }
  35.     else if (current->size < size)
  36.     {
  37.         size_t reqpagesize = size / PAGE_SIZE;
  38.         if (size % PAGE_SIZE > 0)
  39.         {
  40.             reqpagesize++;
  41.         }
  42.  
  43.         vmm_page = vmm_alloc(reqpagesize, Ring0);
  44.  
  45.         if (vmm_page == (void*)0xDEADBEEF)
  46.         {
  47.             return (void*) 0xDEADBEEF; /* Not enough free memory */
  48.         }
  49.  
  50.         else
  51.         {
  52.             if (current->next == 0x0)
  53.             {
  54.                 malloc_t* newbigblock;
  55.  
  56.                 newbigblock = (malloc_t*)vmm_page;
  57.                 newbigblock->size = (reqpagesize * PAGE_SIZE) - sizeof(malloc_t);
  58.                 newbigblock->previous = current;
  59.                 newbigblock->next = 0x0;
  60.  
  61.                 /* goto 'label';*/
  62.                 return malloc(context, size, Ring0);
  63.                 /* Suche jetzt erneut nach freien Blöcken
  64.                  * TODO: Anti-Deadlock Protection!
  65.                  */
  66.             }
  67.  
  68.             else
  69.             {
  70.                 /* Da machsch e dumms Gsicht */
  71.                 return (void*)0xDEADBEEF;
  72.             }
  73.  
  74.         }
  75.     }
  76.  
  77.     return (void*)0xDEADBEEF;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment