Answered step by step
Verified Expert Solution
Link Copied!

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 list_node * 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 listnode_allocator {
struct list_node * free_list; // pointer to head of free list
// and any other fields you need
3
Your memory allocator should support the following functions:
// create a new node allocator with an empty free list
listnode_allocator * allocator_new(int size);
// allocate a new list node from the free list, or with malloc
// if free list empty
listnode * allocator_alloc(listnode_allocator * this);
// return a list node to allocator and store in the free list
void allocator_free(listnode_allocator * this, listnode* item);
// destructor; free all memory.used by the listnode allocator
// and the free list
void allocator_delete(listnode_allocator * this);
// free groups of nodes that are adjacent in memory
void allocator_free_groups(listnode_allocator * this);
The allocator_free_groups() 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 + NODE_SIZE == q)||(q + 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

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

a sin(2x) x Let f(x)=2x+1 In(be)

Answered: 1 week ago