I want to design an algorithm for allocating and freeing memory pages and page tables. What data structures would allow best performance and simplest implementation?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Even though OS normally implement page tables, the simpler solution could be something like this.
Have a large contiguous memory as an array. When you allocate some memory, maintain that information in a linked list storing the index of the array and the length in the data part.
When you are building the linked list, make sure that it is sorted on the index.
When you want to allocate memory, scan the linked list and this will take O(N). where N is the allocations already done.
Deletion will be scanning the array for the particular index and removing the node in linked list.
Once the node is removed, have a separate linked list containing these free allocations.
Insertion will look like this.
1. Check in free list if there is an element in the list of size requested.
2. If not, allocate memory after the last element of linked list
Deletion will work like this,
1. Move the node to the free list.
Make sure free list and linked list are sorted on the index.
This approach doesn’t address the fragmentation issue in memory allocators.One easy approach is to use compaction. Regularly, scan the free node linked list and for each element move the elements in the array and update the index of the node in linked list appropriately.