Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Many applications allocate very large numbers of blocks of memory of the same size. For example, a program that uses a lot of linked lists
Many applications allocate very large numbers of blocks of memory of the same size.
For example, a program that uses a lot of linked lists may aflocate large numbers of list
nodes, such as:
struct listnode
double data;
struct listnode next;
b
Where a program allocates and frees large numbers of list nodes using the C malloc
and free functions, the execution time overheads can be large. One common solution
is to avoid calling the free function to refease the memory for a list node and to instead
add the list node to a linked list known as the free list. When new list nodes should be
allocated, the program will first check whether there are any nodes on the free list. If
there is a free node, the program will take that node from the free list. Otherwise, the
program will allocate a new list node using the malloc function as normal.
Write an abstract datatype that represents a memory allocator for list nodes that
implements this memory allocation and freeing strategy. Your allocator should have
the following structure:
struct listnodeallocator
struct listnode freelist; pointer to head of free list
and any other fields you need
Your memory allocator should support the following functions:
create a new node allocator with an empty free list
listnodeallocator allocatornewint size;
allocate a new list node from the free list, or with malloc
if free list empty
listnode allocatoralloclistnodeallocator this;
return a list node to allocator and store in the free list
void allocatorfreelistnodeallocator this, listnode item;
destructor; free all memory.used by the listnode allocator
and the free list
void allocatordeletelistnodeallocator this;
free groups of nodes that are adjacent in memory
void allocatorfreegroupslistnodeallocator this;
The allocatorfreegroups function frees groups of listnodes that are adjacent in
memory. Given two pointers, p and g that point to listnodes, the two listnodes are
adjacent if p NODESIZE qq NODESIZE p where NODESIZE is a
constant that you can use in your code. Wherever a sequence of two or more adjacent
nodes appears in memory, your routine should free each of these nodes. Note that
the elements of the free list may be in any order, and you need to be able to find
nodes that are adjacent in memory despite the arbitrary ordering.
Please comment on the time complexity of your solution.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started