Answered step by step
Verified Expert Solution
Question
1 Approved Answer
for this assignment, you will implement your own versions of the standard library functions malloc ( ) and free ( ) . Unlike the standard
for this assignment, you will implement your own versions of the standard library functions
malloc and free Unlike the standard implementations, your versions will detect common
usage errors and report them.
For this assignment, you will create
A library mymalloc.c with header mymalloc.h containing the functions and macros described
below.
A program memgrind.c that includes mymalloc.h
Additional test programs that include mymalloc.h along with the necessary Makefiles for
compiling these programs.
A README file containing the name and netID of both partners, your test plan, descriptions
of your test programs including any arguments they may take and any design notes you
think are worth pointing out. This should be a plain text file.
Submit all files to Canvas in a single Tar archive. Place all files in a directory called P
We should be able to run the following commands as written aside from the name of the archive:
$ tar xf ptar
$ cd P
$ make
$ memgrind
Background
The heap is a region of memory managed by the runtime system through two functions, malloc
and free which allocate and deallocate portions of the heap for use by client code.
We will model the heap as a sequence of variably sized chunks, where a chunk is a contiguous
sequence of bytes in the heap and all bytes in the heap belong to exactly one chunk. Each chunk has
a size, which is the number of bytes it contains, and may be either allocated inuse or deallocated
free
The number and sizes of the chunks is expected to vary over the runtime of a program. Large
chunks can be divided into smaller chunks, and small chunks can coalesce into larger chunks.
We can further model a chunk as having two parts. The header contains information the runtime
system needs to know about a chunk, such as its size and whether it is allocated. The payload
contains the actual object that will be used by client code. We say that the payload contains data,
and the header contains metadata.
Note that an object is itself a contiguous sequence of bytes, meaning that the runtime system
cannot intermix data and metadata.
To ensure smooth operation, the runtime system and the client code must operate by certain
rules:
On success, malloc returns a pointer to the payload of an allocated chunk containing at
least the requested number of bytes. The payload does not overlap any other allocated chunks.
The runtime system makes no assumptions about the data written to the payload of a chunk.
In particular, it cannot assume that certain special values are not written to the payload. In
other words, the runtime cannot extract any information by looking at the payload of an
allocated chunk. Conversely, clients may write to any byte received from malloc without
causing problems for the runtime system.
The runtime system never writes to the payload of an allocated chunk. Client code may
assume that data it writes to an object will remain, unchanged, until the object is explicitly
freed.
The runtime system never moves or resizes an allocated chunk.
The client never reads or writes outside the boundaries of the allocated payloads it receives.
The runtime system can assume that any data it writes to chunk headers or to the payloads
of unallocated chunks will be not be read or updated by client code.
malloc is called with a single integer, indicating the requested number of bytes. It searches
for a free chunk of memory containing at least that many bytes. If it finds a chunk that is large
enough, it may divide the chunk into two chunks, the first large enough for the request, and returns
a pointer to the first chunk. The second chunk remains free.
free is called with a single pointer, which must be a pointer to a chunk created by malloc
free will mark the chunk free, making it available for subsequent calls to malloc
Coalescing free chunks
Consider a case where we repeatedly call malloc and request, say, byte chunks until all of
memory has been used. We then free all these chunks and request a byte chunk. To fulfil this
request, we must coalesce two adjacent byte chunks into a single chunk that will have at least
bytes.
Coalescing can be done by malloc when it is unable to find space, but it is usually less
errorprone to have free automatically coalesce adjacent free chunks.
Functions like realloc that explicitly move or resize chunks are permitted, but malloc and free by
themselves must never change existing allocated chunks.
Alignment
C requires that pointers to data are properly aligned : pointers to byte data must be divisible by
pointers to byte data must be divisible by and so forth. We will assume that the largest data
type our programs will use has byte alignment. To ensure that any object allocated by malloc
has byte alignment, we
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