lecture 19 -- intro to memory management what does malloc do? it allocates memory from a heap, anywhere from 1 byte to a huge chunk of contiguous memory. what uses malloc? well, malloc itself, "new" in c++, objects in general, string allocation in languages like PHP, etc. what kind of memory alloc does NOT use malloc? stack! eg local variables. old mac OS (pre-version 10) handle system: apps have fixed mem partition size (bad!) but programmers alloc "handles", which are ptrs to a ptr to actual mem. the ptrs to mem are in a list that the OS maintains. OS is free to move mem blocks (& their contents!) around to make things work, and update the ptr in the list. when you need access to your memory, you lock the handle, then deref it 2x to get to the actual mem. you should unlock when you're done accesses it so that the OS can move it around as necessary. what kind of data structures could be used to keep track of memory allocation? -- bit array -- linked lists linked lists would work better for most apps. bit array might be a good choice if you knew all the alloc's would be the same size, and you were building a specialized system. what kind of linked list data structures? -- keep track of used/allocated memory -- keep track of free memory ("holes") -- both what strategies are possible? -- best fit (search thru all holes, alloc new block in one that makes tightest fit [i.e., hole is just bigger or same as alloc request] -- first fit (search thru holes and take the 1st one that's big enough) -- next fit (start first fit search where previous alloc left off) -- worst fit (take the biggest hole; idea is that best fit tends to make lots of little holes. worst fit doesn't work well though) -- quick fit (lists of avail mem in std sizes) issues -- if you keep track of free mem/holes, you need to update this data struct when mem is deallocated. in general, data structs need updating both on alloc and dealloc. -- keep lists of mem (free or used) sorted by size (tree struct). costs more when updating data struct, but makes searches faster. then, first fit can == best fit (if data struct is already sorted by size).