You are on page 1of 21

Module R5:

Memory Management
Seth Theeke and Mason Greathouse
Key Concepts
Heap Allocation
and Management
Dynamic Memory Allocation
Dynamic Memory Allocation Strategies
Memory Control Blocks
Boundary Tag Method
Introduction
Memory will be kept track of through the
use of complete memory control
blocks(CMCB) AND limited memory
control blocks(LMCB)
These blocks will be stored in one of two
lists, which you must implement, free
memory and allocated memory
Memory Control Blocks
Each block of memory contains a Complete Memory Control
Block(CMCB), a Limited Memory Control Block(LMCB), and either
the amount of free space or the amount of space required to hold the
specified item(Queue, PCB, char[], etc.)
These control blocks act as a boundary to chunks of memory, either
free or allocated
CMCBs contain:
◦ Type - indicating free or allocated
◦ Beginning Address
◦ Size – indicating size of block in bytes
◦ Name – name of process(PCB) that is housed in the block
◦ Links to the next and previous block of the same type
LMCBs contain:
◦ Type - indicating free or allocated
◦ Size – indicating size in bytes of the block
Introduction
R5 should be done in two steps
◦ Part 1: Create a memory manager in a testing
environment inside MPX
◦ Part 2: Implement your memory manager into
MPX
Part 1
You will add six commands that you or
the user could use inside the test
environment
◦ Initialize Heap - Temporary
◦ Allocate Memory - Temporary
◦ Free Memory – Temporary
◦ IsEmpty - Temporary
◦ Show Free Memory
◦ Show Allocated Memory
Initialize Heap
This function will be used to allocate all the memory
available for your MPX
It will take an integer parameter that will indicate the size of
the heap in bytes, during testing, you shouldn’t need more
than 5000 bytes
Using kmalloc, allocate:
◦ The amount of bytes given + sizeof(CMBC) + sizeof(LMCB)
Store that location in a global variable labeling the start of
memory
You will then put a CMCB at the top of the heap and a
LMCB at the bottom of the heap, both of type free
This is also where you will want to initialize your free and
allocated lists
Allocate Memory
This function will be used to allocate memory
from the heap
Use a similar prototype as sys_alloc_mem
It will take an integer parameter indicating the
amount of bytes to be allocated from the heap
You will then implement the first-fit method of
memory allocation
You will need to find a block big enough to hold:
◦ parameter size + sizeof(CMCB) + sizeof(LMCB)
Free Memory
This procedure will be used to free a
particular block of memory that was
previously allocated
Use a similar prototype as sys_free_mem
It will use an address or a pointer to an
address in memory as its parameter and
will then search your allocated list for the
block
Free Memory Cont…
After you find the block to be freed, you
must remove it from the allocated list and
place it into the free list
Once in the free list, you must check to
see if there are any free blocks adjacent to
the one you just freed
If there are, you must merge the adjacent
blocks
Show Allocated and Free Memory
This is fairly simple, you will create a
function to traverse your list that will
show the address of the block as well as
the size of the block
These should be shown in order of
address
You will need to create one for both the
allocated and free list
IsEmpty
This will be a function that will return
true or false based on whether the heap is
empty, or put more simply, contains
ONLY free memory
Part 2
Implement your memory manager into MPX
Initialize your heap in kmain.c to around 50,000 bytes
Two Ways to implement your manager
◦ 1. Everywhere you used sys_alloc_mem or sys_free_mem,
replace those with your functions
◦ 2. Inside mpx_supt, you’ll see that the function calls for
sys_alloc and sys_free point to a student function if the
module is >=MODULE_R5
Remove the user’s ability to initialize the heap,
allocate or free memory, and check if the heap is
empty
Tips
This module relies on pointers more than any other so make
sure you brush up on all the rules and abilities that come
with pointers
Try to make it easier on yourself, you don’t have to follow
my methods word for word so if you find an easier way, go
for it as long as all the functionality is there and it doesn’t
involve cheating…
Draw, diagram, and write it out. It helps to try to draw out
what you are creating and changing to see what’s going
wrong similarly to the examples above
Error check like crazy, since there are a lot of pointers
involved, that means there are a lot of places that things can
get tangled up. Try to break it before we do in the demo
Questions?

You might also like