I understand the 8051 has limited RAM, but why can't we use dynamic memory allocation (like malloc/free) ? how does limited RAM specifically cause problems?We also NEVER did dynamic memory allocation and never used recursive subroutines.
I understand the 8051 has limited RAM, but why can't we use dynamic memory allocation (like malloc/free) ? how does limited RAM specifically cause problems?We also NEVER did dynamic memory allocation and never used recursive subroutines.
The overhead of managing a heap is just not worth the effort on such a limited processor. If you want to try it, you can KNOCK YOURSELF OUT!I understand the 8051 has limited RAM, but why can't we use dynamic memory allocation (like malloc/free) ? how does limited RAM specifically cause problems?
Heaps have overheads. They have a start header that describes remaining virgin space, free list(s), etc. every address you get back, actually has an unseen header, likely must be aligned on some 2^n boundary, perhaps 16 bytes maybe more. if you make many small allocations (say, 8, 16 or so bytes) then perhaps 50% of used memory is dedicated to all that housekeeping.I understand the 8051 has limited RAM, but why can't we use dynamic memory allocation (like malloc/free) ? how does limited RAM specifically cause problems?
That would certainly be an interesting problem to explore. In most heaps when we free an allocation, the heap logic does thisTo counter what @Futurist said, I could easily implement a heap using 1-byte headers and no alignment required on a system that has only 128B of heap available. (or 256B with 2 byte alignment.).
I don't give a damn what most memory allocators do. They are generally written for systems with WAY more than 256 bytes.That would certainly be an interesting problem to explore. In most heaps when we free an allocation, the heap logic does this
Exactamente!I don't give a damn what most memory allocators do. They are generally written for systems with WAY more than 256 bytes.
...
The reason for not using dynamic allocation on small memory systems has nothing to do with difficulty of implementing it.
That could work, but freeing necessitates examining the preceding block too, in this design you'd need to scan all blocks from the start (since you don't record prev_block_size in your block header) in order to identify the preceding block.I don't give a damn what most memory allocators do. They are generally written for systems with WAY more than 256 bytes.
Here are two ways to do it:
1. With a header and 2-byte alignment:
A one byte header is used. It is the size/2 of the block in upper 7 bits and the low bit indicates it is allocated.
malloc: Scan through the heap looking for a free block large enough. if found, mark it as allocated and set the new length. If there is more than 1 byte left over after the requested amount, make a new free block from the remaining bytes. Return the address of the header + 1. If none found, return NIL.
Free: simply back up one and mark the block as free by setting the high bit. If you want to get fancy, check for next block free and combine them.
2. Bitmap.
Allocate 16 bytes as a bitmap for the heap. 0 indicates a free block 1 an allocated block.
malloc: Look for a string of 1 bits large enough for the request + 1. Set the size in the first byte, mark it allocated, and return the address + 1;
free: back up by one and get the size (0-255). Mark all the corresponding bits in the bitmap as free.
The reason for not using dynamic allocation on small memory systems has nothing to do with difficulty of implementing it.