CS 321 Spring 2013 > Assignment 4 |
Assignment 4 is due at 5 p.m. Tuesday, April 4. It is worth 25 points.
E-mail
answers to the exercises below to
ggchappell@alaska.edu
,
using the subject
“SA4
”.
mapalloc.h
and mapalloc.cpp
, from Exercise A,
and randacc.cpp
, from Exercise B.
These files (or a single archive file containing them)
should be attached to your e-mail message.
In this exercise, you will write simple memory allocation &
deallocation functions, using mmap
to get the memory.
Write a C++ package to do memory allocation and freeing,
much like the standard library functions malloc
and free
.
mapAlloc
and mapFree
.
Prototype them in the header mapalloc.h
,
and implement them in the source file mapalloc.cpp
.mapAlloc
takes a std::size_t
,
indicating a number of bytes to be allocated.
It returns a (void *)
.
The function attempts to allocated a block of (at least) the given size.
The return value is a pointer to the allocated block,
if the allocation was successful;
otherwise, it is NULL
.mapFree
takes a (void *)
,
which is a pointer to a block previously allocated with mapAlloc
.
It returns nothing.
The function deallocates the given block.mmap
;
deallocation must be done using munmap
.
and the result is notchar * p = (char *)mapAlloc(n);
NULL
,
then the bytes with addresses p
through p+n-1
must be allocated memory with both read and write access.
Furthermore, successive calls to mapAlloc
must return pointers to distinct, nonoverlapping blocks.mapFree
may assume that it is given a
pointer that was previously returned by mapAlloc
,
and that mapFree
will never be called twice with the
same pointer.
So you do not need to test for these conditions.main
function.munmap
takes the size of the block to
deallocate, as a parameter.
However, mapFree
is not given the size of the block.
So you will need to store this information somewhere.
How you store it, is up to you, but be intelligent about it.
In this exercise, you will write a program
that does random-access file I/O
using mmap
.
Write a complete C++ program to do random-access I/O on a file. The file will contain 100 records, each holding an integer. The user is repeated allowed the choice of reading and printing the value in a specified record, or writing a user-provided value to a specified record. Data written by the program should be available for reading the next time the program is executed.
randacc.cpp
.mmap
.
It must be random-access,
meaning that the program computes the location of a record
and reads/writes there, without reading previous records.
mmap
should be available under Cygwin.
Alternatively, you can write your code using NetRun
;
however, NetRun
will not allow the file access
required in Exercise B.
ggchappell@alaska.edu