Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define uheap_frames 131072
- struct Heap_page
- {
- uint32 add;
- uint32 Phy_add;
- unsigned int size;
- unsigned int count;
- };
- struct Heap_page Pages[uheap_frames];
- void intilize(int c)
- {
- if (c==0)
- {
- uint32 index = USER_HEAP_START;
- int i;
- for (i=0;i<uheap_frames;i++)
- {
- Pages[i].add = index;
- Pages[i].size=0;
- Pages[i].count=0;
- Pages[i].Phy_add=0;
- index += PAGE_SIZE;
- }
- }
- else
- return;
- }
- // Helper global variables
- int ind=-1;
- int mark=0;
- void* malloc(uint32 size)
- {
- //TODO: [PROJECT 2018 - MS2 - [4] User Heap] malloc() [User Side]
- // Write your code here, remove the panic and write your code
- // Steps:
- // 1) Implement NEXT FIT strategy to search the heap for suitable space
- // to the required allocation size (space should be on 4 KB BOUNDARY)
- // 2) if no suitable space found, return NULL
- // Else,
- // 3) Call sys_allocateMem to invoke the Kernel for allocation
- // 4) Return pointer containing the virtual address of allocated space,
- //This function should find the space of the required range
- // ******** ON 4KB BOUNDARY ******************* //
- //Use sys_isUHeapPlacementStrategyNEXTFIT()
- //to check the current strategy
- intilize(mark);
- mark++;
- size = ROUNDUP(size, PAGE_SIZE);
- int check=0;
- int num = size/PAGE_SIZE;
- int i;
- int test = 0;
- ind=(ind+1) % uheap_frames;
- int start = 0;
- int update = 0;
- for (i = (ind)%uheap_frames; test != 2; i = (i+1)%uheap_frames)
- {
- if (i==(ind%uheap_frames))
- test++;
- if(check == num)
- {
- ind = start;
- break;
- }
- if (Pages[i].size == 0)
- {
- check++;
- if (update == 0)
- {
- start = i;
- update = 1;
- }
- }
- else
- {
- check = 0;
- update = 0;
- }
- }
- if (check!=num)
- return NULL;
- uint32* temp = (uint32*) Pages[ind].add;
- sys_allocateMem(temp,size)
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement