Reputation: 111
I am working on a project in my computer organization class involving dynamic memory allocation and deallocation in MIPS assembly. We use the SPIM or MARS simulation environments.
As I was beginning to set up my code, I began to realize there are certain things in MIPs that I do not know how to do.
First, I understand how to allocate memory from the heap using a syscall code of 9 (sbrk). However, I do not understand how it is possible to deallocate memory back to the heap. There is one post on stack overflow of somebody asking this question years ago with no good answer. Most people say to use a negative number in the argument register, but this does not work in SPIM or MARS.
Second, I am unsure how to save dynamic variables by the user. For example, if the user requests 32 bytes from the heap, they are prompted to save a name for the variable. How do we dynamically create a new variable and assign it this new memory we have given? Essentially, I know we can create variables in the .data section of the code, but I am not sure how to do this dynamically. There isn't much good documentation I can find online and I am not comfortable with MIPS yet.
Overall, this semester has been quite a blur and we were rushed into this assignment (we have only had one MIPS assignment before this), so any help from you all is greatly appreciated. Assembly is quite a beast.
Here is the project prompt for the curious:
In this assignment, you are required to design and implement in MIPS a project for dynamic allocation and deallocation of memory on user demand. However, you can make the following simplifying assumptions:
- The memory pool is limited to 4096 bytes
- The memory pool divided into equal chunks of 32 bytes (chosen due to MIPS architecture). Thus, the 4096 bytes memory will be divided into 128 chunks.
- Despite allocation requests coming in any size, the actual allocations should be performed in integer multiples of chunks. Therefore, the byte size request will be rounded up to the closest multiple of 32.
- To succeed in this task, the code must allocate and deallocate memory upon request. If some of the requests cannot be handled, exception handling must activate to resolve the issue.
Upvotes: 0
Views: 1582
Reputation: 26786
However, I do not understand how it is possible to deallocate memory back to the heap.
MARS/SPIM do not allow for returning memory back to the heap, AFAIK. Even if they did allow the negative value sbrk like unix does, that only supports returning the most recently allocated (by sbrk) item — and this is different from client expectations that any heap item can be freed.
So, the only thing you can do is provide an intermediate allocator, that keeps track of free'd space, and prefers freed space (e.g. over increasing the heap space with sbrk) for new allocations when possible.
Second, I am unsure how to save dynamic variables by the user.
This is program responsibility — and by that I mean the test program or user program, not the library that implements malloc and free. I presume there will be some test program. That program will invoke your malloc & free implementations and probably will only pass the test by allocating & freeing lots of memory, but staying within the 4k limit of maximum memory.
To succeed in this task, the code must allocate and deallocate memory upon request. If some of the requests cannot be handled, exception handling must activate to resolve the issue.
I have trouble imagining what this means. Obviously, we can terminate the program when a request for memory is made that cannot be satisfied (i.e. out of memory), but this text suggests that exception handling of some sort can resolve the issue.
Upvotes: 4