Assignment 9, due Apr. 3
Always, on every assignment, please write your name legibly as it appears on your University ID and on the class list! All assignments will be due at the start of class on the day indicated (usually a Friday). The only exceptions to this rule will be by advance arrangement unless there is what insurance companies call "an act of God" - something outside your control. Homework must be turned in on paper and in class! Late work may be turned in to the teaching assistant's mailbox, but see the late work policy. Never push late work under someone's door!
Consider the following design for an object file for use in a Unix or Linux system (note: This is fiction, not the real object format): An object file consists of a header, a code segment, and a static segment. The header contains 4 words: code, the offset of the first byte of the code segment within the file, codelen, the length of the code segment, stat, the offset of the static segment, and statlen, the length of the static segment. Offsets and lengths are guaranteed to be divisible by the disk sector size.
Consider implementing execve() with the equivalent of two consecutive calls to mmap, one to map the code segment and one to map the static segment of the new program. (It can continue to use the old stack segment.)
a) Work out the correct values of the parameters to the two mmap calls, assuming that the header of the object file has already been read. (0.5 points)
b) This design poses problems for the machine architecture and object code. What information must the new implementation of execve pass to the executed program for this to work. Hint: think about the addressing modes that will work under this scheme to access local variables, static variables, and other parts of the program. (0.5 points)
Problem: Write an appropriate C statement to allocate (but not initialize) a new object in your class, pointed to by the variable new which is a pointer to struct myclass. This statement would be an appropriate first executable statement in the body of myclass_init() -- the rest of the body would consist of code to initialize the newly allocated object. (0.5 points)
char * heap[VERYBIGNUMBER]; char * freepointer = &heap;
Recall that, in C, casting can convert between pointer types, so (int *)freepointer is a pointer to an integer stored in memory. Furthermore, in C, &freepointer is a pointer to the variable freepointer with type char * * (which means a pointer to a pointer to a character).
a) Write the simplest version of malloc() you can, assuming that it allocates memory from the variable called heap using freepointer to keep track of the not-yet-allocated memory locations. Your implementation does not need to support any deallocation mechanism. Once allocated, memory stays allocated forever. (0.5 points)
b) To maintain a list of free blocks, the first word of each free block would contain a pointer to the next free block. Given that a and b are of type char * (pointer to character), write C code that makes the block of memory pointed to by a begin with a pointer to b. (0.5 points)