Re: [program-l] memory management

  • From: "tribble" <lauraeaves@xxxxxxxxx>
  • To: <program-l@xxxxxxxxxxxxx>, "bprogramming" <programmingblind@xxxxxxxxxxxxx>
  • Date: Sun, 12 Apr 2009 11:43:43 -0500

Hi Ty --
There are several ways you could handle this -- I tend to prefer keeping the MM 
information in a separate data structure that points into your allocatable 
memory, rather than putting used or not-used flags in the blocks of memory 
themselves -- the reason being that if the programmers happen to overwrite the 
system info -- for example, writing out of bounds on an array, you may never 
know what hit you, so to speak.  Also, if you need several of your blocks to be 
contiguous, what will you do with the system information cookie at the start or 
end of your allocated blocks?
No, there is no simple way to handle memory management.  But the code isn't all 
that difficult to understand.
I wrote a little OO library in C++ that managed memory for a linker, and it was 
quite manageable. It contained a linked list of pointers into consecutive 
intervals of memory -- that is, the pointers in the list were of increasing 
value, and each record contained the number of bytes in that interval.  Then if 
I needed an interval of memory, I would search through the available intervals 
and find a best fit. And when I freed memory, I would insert the freed interval 
back into the list, and if it was adjacent to the free interval before it or 
after it, I would merge them into one big interval.
Searching the fragments sounds like it would be time consuming, but it is a 
relatively brief operation that doesn't get invoked that often.
Let me know if you have a question. I don't have the code for that old linker, 
but writing it or a better one yourself would be a good exercise.
Happy hacking.

  ----- Original Message ----- 
  From: Tyler Littlefield 
  To: programmingblind@xxxxxxxxxxxxx 
  Sent: Sunday, April 12, 2009 10:29 AM
  Subject: [program-l] memory management

  Hello list,
  I'm working on building a kernel from scratch (just for something to do, I'm 
learning quite a lot out of it), and have a question.
  I was thinking about how I'm going to implament the MM (memory manager).
  I had an idea of making blocks of memory available, putting a used or unused 
flag on them for now. each block would be of set size, and I could have a 
lookup table of sorts.
  My other idea was to make them all hold pointers to the next block of data.
  Now, I'm seeing issues with both problems.
  If I keep a list of reserved memory, and add a structure to the end of the 
list when a new chunk of memory is requested, that works great. But then we 
have the issue of memory getting destroyed in the middle.
  If someone requests a 1KB block and someone else requests 512 and then frees 
it, I can delete that and mark that memory as unused; my issue is this.
  Eventually my memory is going to be split up enough that it will require I 
"compact" it to fill in those gaps, which would break any code referencing that 
  Is there an easier way to do this?

  Tyler Littlefield
  email: tyler@xxxxxxxxxxxxx
  My programs don't have bugs, they're called randomly added features.

Other related posts: