CSc 520 - Principles of Programming Languages
7 : Memory Management -- Malloc

Christian Collberg

Department of Computer Science

University of Arizona

1 Dynamic Memory Allocation

2 Static allocation

3 Stack allocation

4 Dynamic Heap Allocation

5 Run-time Meory Organization

6 Run-time Meory Organization...

7 Heap Organization

8 Java Example


\begin{program}
XX\=XX\=\kill
class Employee \{String name; int salary;\} \\
cl...
...
\xx Employee cat = new Employee(''Catbert'',200000); \\
\x \} \}
\end{program}

9 Java Example...

dilbert2

10 Pascal/C/...Example


\begin{program}
XX\=XX\=\kill
program P; \\
\x record Employee String name; int...
...; \\
\x Employee cat = new Employee(''Catbert'',200000); \\
end.
\end{program}

11 Pascal/C/...Example...

dilbert1

12 Heap Organization...

heap1

13 Heap Organization...

regions

14 Heap Organization...

heap2

15 Malloc

16 Malloc...

malloc1

17 Malloc...

malloc2

18 Malloc...

malloc3

19 Malloc...

malloc4

20 Malloc...

malloc5

21 Malloc...

malloc6

22 Malloc...

malloc7

23 Fragmentation

heap3

24 Fragmentation...

heap3
$\Downarrow$
heap4

25 Fragmentation...

fragmentation1

26 Fragmentation...

27 Memory Allocation Schemes--Best-fit

bestfit1

28 Memory Allocation Schemes--Worst-fit

worstfit1

29 Example I

example1

30 Example I -- Best-Fit

example1bestfit

31 Example I -- Worst-Fit

example1worstfit

32 Example II

example2a

33 Example II/b

example2b

34 Memory Allocation Schemes--First-fit

firstfit1

35 Memory Allocation Schemes--Next-fit

nextfit1

36 Example III

example3a

37 Free

38 Free...

39 Free...

40 Free...

free1

41 Free...

42 Free...

43 Free...

  1. How are two regions coalesced? The one with the higher address is merged into the one with the lower address.
  2. Increment the size of the lower region by the size of the higher region (including the size of the higher region's header).
  3. Remove the higher region from the free list.

44 Putting it all together

45 Malloc(size)

  1. Add size of hidden header to size.
  2. Round size up to a whole number of words.
  3. Use Next-fit to search free list for first free region of at least size bytes.
  4. Start search using pointer kept in global variable.
  5. Check magic numbers as you go.
  6. If there isn't a free region big enough, return 0 (NULL).
  7. If free region isn't exactly size bytes, compute how much is left- over (excess).

46 Malloc(size)

  1. If excess is at least the size of a header plus one word: else
  2. Put size of allocated region into hidden header at the start of the region.
  3. Return address of first byte after hidden header.
  4. Update starting search pointer to next region on free list.

47 Free(address)

  1. Subtract size of hidden header from address to get hidden header's address.
  2. Check magic number in hidden header.
  3. Use size in hidden header to fill in free-list node header at start of region.
  4. Insert free region into sorted free list. The pointer to the start of the free list is stored in a global variable.

48 Free(address)

  1. If previous region on free list is adjacent to new free region:
    1. Coalesce by adding size of higher region (including header) to size of lower region.
    2. Remove higher region from free list.
    3. Also coalesce subsequent region on free list if it is adjacent.

49 More Algorithms

binning

50 More Algorithms...

Bit map:
if regions are of the same size (blocks), keep a bit map instead of a free list. One bit per block: 1 means allocated, 0 means free. One word of the bit map represents 32 blocks. Index of bit in bit map is the index of the region in the "array" of regions.
Garbage collection:
if a program does not have a pointer to a particular region of memory, that memory cannot be accessed by the program and can be automatically added to the free list. The programmer allocates memory, but never calls free. Examples: Lisp, Java

51 Readings and References



Christian Collberg 2008-01-30