Operating
Systems Lab: Assignment
6 (Marks 10)
Date of assignment out:
12.10.2006
Last date of report: 26.10.2006
- You
have to create a static library
that supports calls similar to malloc
and free (on SUN SPARC). Your function
prototypes
and data structure for memory management are the following:
struct
mMemoryArea {
void *firstFree ; // Address of the first free
area
unsigned totA ; // Total area
unsigned freeA; // Free area
unsigned largestFreeA ; // Size of the largest free
area
unsigned numFreeA; // Number of free areas
}
void strategy(unsigned fit, unsigned list) ;
void *mMalloc(unsigned n);
void mFree(void *p);
void mArea(struct mMemoryArea *p) ;
- The
library will use sbrk()
or brk() call to get
memory (multiple of pages) from the OS and will manage the
space on its own. It will not use malloc() or free() of C library.
- void
strategy(unsigned fit, unsigned list): this function is used to
select strategies. The value of fit
= 0 indicates first-fit,
fit = 1 indicates best-fit and fit = 2 indicates worst-fit for allocation of a
block. The value of the second parameter indicates
the nature of the free block list. list = 0 indicates
that the free
block list is unordered, list = 1 indicates that it is
arranged in ascending order
of the size of free blocks and list = 2 indicates descending order list. The
default
strategy is first-fit of
allocation and a free block list maintained in ascending order. The strategy
can be modified by calling this function.
- void
*mMalloc(unsigned n): on request for a block of n bytes of space, a free block
of at
least m+8 bytes will be
allocated, where m is the
smallest multiple of 8 that
is greater than or equal to n.
Naturally the size of the smallest
block
is 16 bytes. The first 4 bytes (at the
lower address side)
holds the actual size (m+8)
of the block. The function
returns the address of the byte after the first 8 bytes.
If the size of a
selected block is more than 16 bytes of the requested size, it is
split and the remaining portion of the block is inserted in
the free
block list with proper initialization and following the selected
strategy. The first 4 bytes of a free block holds its
size, the remaining portion of a free block (4 bytes aligned to 8 byte
boundary) is used to store the address of the
next free block in the list. If there are two adjacent free blocks,
they are to be merged to form a larger free block. The
head of the free block list
will be in a global variable in the library.
- void mFree(void *p): an allocated block
pointed by p is inserted
in the free list by proper initialization and following the selected
strategy as discussed earlier.
- void mArea(struct mMemoryArea *p):
fills the structure pointed by the parameter p with the current data of the
memory area.
- Essential files are mmalloc.h, mmalloc.c The
following
commands show how to create a static library and how to link it with a
user program.
$ gcc -Wall -c
mmalloc.c
$ ar -qv libmmalloc.a mmalloc.o
$ gcc -Wall testMyMalloc.c -L. -lmmalloc
- Prepare a Makefile that will take the user
program testMyMalloc.c as
a parameter and will
create the a.out file.
Finally prepare the tar
file <rollNo>_06.tar
with mmalloc.h, mmalloc.c
and the Makefile. Keep it
in the proper
directory. Do not change names or types of any of the
fields of struct, function or files.