A4 programming
COMP 2160 Programming Practices
Assignment 4
Page 1 of 2
Due Date: April 5th at 11:59pm
NOTES Please read and follow instructions carefully; not doing so will result in a loss of marks. Your code must follow the programming standards (available in UMLearn). You can write your code on any machines; remember to verify your code on one of the lab machines. You must include a README file which describes how to compile and run your solution. You can
also explain your concerns or personal opinions regarding this assignment. Use submission tool (handin) in CS Unix system as described in Assignment Submission
Guidelines (available in UMLearn).
OBJECT MANAGER In this assignment, you have to implement a simple object manager. The object manager has two buffers for its memory pool (though only one is in use at any given time), uses a linked list to keep the recording of managed objects, and uses reference ID to refer each object. A managed object has their ID, reference counting, address location on the object manager’s buffer, and size in number of bytes.
To use this object manager module, users must initialize the module with a memory pool size, allocate memory for an object through the module, request a pointer of the object by reference ID, and indicate a lack of interest on the object. This also means that our module must be able to manage their heap (buffer) and collect any garbage in the memory when it is necessary.
In UMLearn, I have uploaded the interface (ObjectManager.h) and the partial implementation (ObjectManager.c). You are NOT allowed to modify the interface (i.e., function signature). I specifically added comment (TODO) for you to quickly search. For this assignment, complete the implementation, apply Design by Contact principle, and write unit tests to check the implementation. I have uploaded a simple test code (main.c) as an example. You can start building your test on top of the given example or your can create your own. It is up to you. Once completed, prepare makefile for markers to compile your code quickly.
The following functions are to be implemented by you. The quality of your implementation and testing of the object manager module will be evaluated. If you do not understand any comments or the implementation details, ask your instructor for clarification. Start early and submit often.
boolean initialize( const int size ); Initialize the object manager, allocate buffer to given size, initialize any variables as necessary, and return its initialization status. This can be called multiple times; first call (or call after calling destroy()) will be effective to change the pool size.
COMP 2160 Programming Practices
Assignment 4
Page 2 of 2
void destroy( void ); Release all resources used by the object manager. This can be called multiple times. That is, the function should not fire any assertion.
Ref insert( const int size ); Insert an object to the object manager. If there is not enough room to allocate the given size, the function must fire garbage collection (in our case, compact()). When there is no room even after garbage collection, the function should simply return NULL_REF. The linked list node has to be appended to the end of the list. Be sure to maintain valid links between the nodes and to correctly point the first and the last node (memBlockStart and memBlockEnd).
void removeRef( const Ref ref ); Remove an object’s reference count. Remove the indexing node if reference count reaches zero. When you remove the index node, do not forget to clear memory used by this object for the security reason.
static void compact( void ) Perform the memory defragment by moving data from one buffer to another. For example, if the buffer1 was in use, the data has to be moved to the buffer2 and current buffer pointer should be pointed accordingly. To evaluate our implementation, print the following statistics to stdout every time this function is called:
the number of objects that exist the current number of bytes in use the number of bytes collected
static void validate( void ) This function should check invariant. Complete this function by checking valid states of the object manager with assertion.