Home > other >  How to free the heap memory in MIPS
How to free the heap memory in MIPS

Time:01-03

for learning pourposes I'm trying to implement a stack in the heap memory, When I push somthing I just need to do the systemcall sbrk, and that's fine. But when I proceed with a pop i can retrive my value, but I can't free the allocated space, there's any way to do this?

The sbrk syscall doesn't accept negative numbers, I've already tried.

Thanks in advance

CodePudding user response:

Unlike the real sbrk in UNIX, QtSpim/MARS syscall #9 doesn't support returning memory from the heap back to the system.

But, you can implement sbrk functionality yourself, as it is fairly simple.  (malloc/free would be more complex involving free lists and such, but this is much simpler.)

You need a subroutine that takes the adjustment number just like the real sbrk, of course, and maintains a small amount of persistent/global state — maybe two words: the UNIX-style sbrk address and the MARS-style syscall #9 address, or, one of those or the other and a free count.

Releasing memory (negative sbrk parameter) simply means moving the UNIX-style sbrk address back and/or increasing the free count, and otherwise doing nothing.

Later allocations (positive sbrk parameter) consider the gap between markers, or free count, in allocating new heap space, and only increase underlying MARS heap if free count goes to 0 and there's still more bytes in the allocation request.

  • Related