Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. #define uheap_frames 131072
  2. struct Heap_page
  3. {
  4.     uint32 add;
  5.     uint32 Phy_add;
  6.     unsigned int size;
  7.     unsigned int count;
  8. };
  9. struct Heap_page Pages[uheap_frames];
  10.  
  11. void intilize(int c)
  12. {
  13.     if (c==0)
  14.     {
  15.         uint32 index = USER_HEAP_START;
  16.         int i;
  17.         for (i=0;i<uheap_frames;i++)
  18.         {
  19.             Pages[i].add = index;
  20.             Pages[i].size=0;
  21.             Pages[i].count=0;
  22.             Pages[i].Phy_add=0;
  23.             index += PAGE_SIZE;
  24.         }
  25.     }
  26.     else
  27.         return;
  28. }
  29. // Helper global variables
  30. int ind=-1;
  31. int mark=0;
  32.  
  33. void* malloc(uint32 size)
  34. {
  35.     //TODO: [PROJECT 2018 - MS2 - [4] User Heap] malloc() [User Side]
  36.     // Write your code here, remove the panic and write your code
  37.     // Steps:
  38.     //  1) Implement NEXT FIT strategy to search the heap for suitable space
  39.     //      to the required allocation size (space should be on 4 KB BOUNDARY)
  40.     //  2) if no suitable space found, return NULL
  41.     //   Else,
  42.     //  3) Call sys_allocateMem to invoke the Kernel for allocation
  43.     //  4) Return pointer containing the virtual address of allocated space,
  44.     //This function should find the space of the required range
  45.     // ******** ON 4KB BOUNDARY ******************* //
  46.  
  47.     //Use sys_isUHeapPlacementStrategyNEXTFIT()
  48.     //to check the current strategy
  49.  
  50.     intilize(mark);
  51.     mark++;
  52.     size = ROUNDUP(size, PAGE_SIZE);
  53.     int check=0;
  54.     int num = size/PAGE_SIZE;
  55.     int i;
  56.     int test = 0;
  57.     ind=(ind+1) % uheap_frames;
  58.  
  59.     int start = 0;
  60.     int update = 0;
  61.     for (i = (ind)%uheap_frames; test != 2; i = (i+1)%uheap_frames)
  62.     {
  63.         if (i==(ind%uheap_frames))
  64.             test++;
  65.         if(check == num)
  66.         {
  67.             ind = start;
  68.             break;
  69.         }
  70.         if (Pages[i].size == 0)
  71.         {
  72.             check++;
  73.             if (update == 0)
  74.             {
  75.                 start = i;
  76.                 update = 1;
  77.             }
  78.         }
  79.         else
  80.         {
  81.             check = 0;
  82.             update = 0;
  83.         }
  84.     }
  85.     if (check!=num)
  86.         return NULL;
  87.     uint32* temp = (uint32*) Pages[ind].add;
  88.     sys_allocateMem(temp,size)
  89.  
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement