简体   繁体   中英

How to free the heap memory in MIPS

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

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 a small amount of 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 that gap 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM