Title: You should base your assignment on the example from the following book: "The C programming Language" By Brian W. Kernighan and Dennis M. Ritchie: the example and its code you can find here.
1You should base your assignment on the example
from the following book"The C programming
Language" By Brian W. Kernighan and Dennis M.
Ritchiethe example and its code you can find
here. Copy it to C file and run to see how to
work with it (it is easy enough). In order to
add memory compaction to this example, you need
to make some changes in it 1. you should keep a
table (hash is recommended) to keep there all
pointers to allocated memory 2. each
pointer to allocated memory should be registered
in the table above registerVariable
function should add it into the table 3. when
assign two pointers, use assignPointer function
and not you also need to use
registerVariable function inside, in order to
register the new pointer 4. use
unregisterVariable function to erase pointer from
the table above, when free a memory block
it points on (i.e. in free function) Now you have
all the needed information to do memory
compaction the process is similar to one we
studied in the class - just move down blocks of
memory that have free memory space below them.
2Some details about mcompaction function 1. void
mcompaction( unsigned dt , unsigned minSegment ,
unsigned maxSegment )unsigned dt since memory
compaction is expansive we dont let it to run as
much time as it needs, but limit it by this
parameter (use COMPACTION_TIMEOUT constant that
is defined in mm.h). You should take start time
(using gettimeofday function that would be
explained later) and take time after each block
movement. If you still have time, move another
block. If your time is over, stop memory
compaction process.unsigned minSegment you
should ignore block that its size is less than
this parameter.unsigned maxSegment you should
ignore block that its size is bigger than this
parameter. 2. when move memory block, you must
update your hash table (that contains all
pointers as I explained above), so that next
reference to memory blocks would be right. Some
details about malloc functionbase it on the
function from the example above. But you need to
make some changes in it 1. you should have two
global variables totalFreeSize totalBusySize.
When add new memory space (using morecore
function from the example), free function is
called in order to add the new space to the free
blocks list. There you need to update
totalFreeSize variable. When memory block is
allocated, you need to update
3totalBusySize variable. You need this in order
to check the ratio between used and unused memory
space. 2. first try to allocate memory from you
free list if you dont have free block with
appropriate size, check if you can run memory
compaction if used space doesnt over load
factor (LOAD_FACTOR constant from mm.h) then you
can run it and after that try to allocate block
again if not, just use morecore function as
malloc in the example does. How to pass a memory
pointer as function parameterNow, when you pass
a memory pointer as function parameter, you need
to register it at the beginning of the function,
and unregister it at the end of the function.
User (that uses your library) should take care of
this. For you it is important when you debug your
program with some test file. sbrk function
change dynamically the amount of space allocated
for the calling process's data segment. You
dont really need to understand how this function
works for this assignment. The only place it is
used is in morecore function that is in the
example above. You dont need more than this.
4gettimeofday function returns current time
(since 0000 GMT, January 1, 1970). Example
static struct timeval tm_start, tm_now static
struct timezone tz long difftime
gettimeofday( tm_start , tz ) (here you
compact one memory block in memory compaction
process) gettimeofday( tm_now , tz )
difftime ( tm_now.tv_sec - tm_start.tv_sec )
1000000 tm_now.tv_usec - tm_start.tv_usec
After you get the difference between start time (
of your memory compaction process) and current
time, you can compare between it and
COMPACTION_TIMEOUT (from mm.h) difftime lt
COMPACTION_TIMEOUT Notein order to compile
your test.c file with your (or our) library
(libmm.a) you need to run gtgcc test.c
libmm.a