Docsity

Allocation - Data Structures and Advanced Programming - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Allocation - Data Structures and Advanced Programming - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Lecture

Today: A little side excursion into nitty-gritty stuff: Storage man-

agement

Scope and Lifetime

• Scope of a declaration is portion of program text to which it applies

(is visible).

– Need not be contiguous.

– In Java, is static: independent of data.

• Lifetime or extent of storage is portion of program execution dur-

ing which it exists.

– Always contiguous

– Generally dynamic: depends on data

• Classes of extent:

– Static: entire duration of program

– Local or automatic: duration of call or block execution (local vari-

able)

– Dynamic: From time of allocation statement (new) to dealloca-

tion, if any.

Under the Hood: Allocation

• Java pointers (references) are represented as integer addresses.

• Corresponds to machine’s own practice.

• In Java, cannot convert integers ↔ pointers,

• But crucial parts of Java runtime implemented in C, or sometimes

machine code, where you can.

• Crude allocator in C:

char store[STORAGE_SIZE]; // Allocated array

size_t remainder = STORAGE_SIZE;

/** A pointer to a block of at least N bytes of storage */

void* simpleAlloc (size_t n) { // void*: pointer to anything

if (n > remainder) ERROR ();

remainder = (remainder - n) & ~0x7; // Make multiple of 8

return (void*) (store + remainder);

Example of Storage Layout: Unix

Stack (local)

Unallocated

Heap (new)

Static storage Executable Address 0 code

• OS gives way to turn chunks of unallocated region into heap.

• Happens automatically for stack.

Free Lists

• Explicit allocator grabs chunks of storage from OS and gives to

applications.

• Or gives recycled storage, when available.

• When storage is freed, added to free list data structure to be re-

cycled.

• Used both for explicit freeing and some kinds of automatic garbage

collection.

• Problem: free memory fragments.

Boundary Tag Methods

F/A

Admin word

A/F

A/A

F/A

A/F

F/A

Next free

A/F

Prev. free X Y Z

FREE

LIST

G1 = malloc(96);

X = malloc(115);

Y = malloc(156);

G2 = malloc(19);

Z = malloc(26);

G3 = malloc(155);

free(G1); free(G3); free(G2);

Size

Free?/Prev Free?

Admin word

Buddy System at Work

0

128

64 32 0

X

X = malloc (32);

192

R

160 128

Q

64

Z

32

Y

0

X

Y = malloc (32); Z = malloc (64); Q = malloc (32); R = malloc (64)

192 160 128

Q

64

Z

32 0

X

free (Y); free (R);

128

64

Z

0 free (X); free (Q);

Garbage Collection: Reference Counting

• Idea: Keep count of number of pointers to each object. Release

when count goes to 0.

X: 1 1 1

1 A 1 B 1 C

Y:

X: 1 2 1

1 A 1 B 1 C

Y:

Y = X.tail;

X: 0 3 1

1 A 1 B 1 C

Y:

X = Y;

X: 2 1

0 A 1 B 1 C etc.

Y:

Copying Garbage Collection

• Mark-and-sweep algorithms don’t move any exisiting objects—pointers

stay the same.

• The total amount of work depends on the amount of memory swept—

i.e., the total amount of active (non-garbage) storage + amount of

garbage. Not necessarily a big hit: the garbage had to be active at

one time, and hence there was always some “good” processing in the

past for each byte of garbage scanned.

• Another approach: copying garbage collection takes time propor-

tional to amount of active storage:

– Traverse the graph of active objects breadth first, copying them

into a large contiguous area (called “to-space”).

– As you copy each object, mark it and put a forwarding pointer

into it that points to where you copied it.

– The next time you have to copy a marked object, just use its

forwarding pointer instead.

– When done, the space you copied from (“from-space”) becomes

the next to-space; in effect, all its objects are freed in constant

time.

Copying Garbage Collection Illustrated

Roots B 5 E

from: 42

A

D

B

G F

C

A

D

7 G D

E F

C

G

E

to:

(a)

Roots B’ 5 E’

from: 42

A

B’

B*

G F

C

A

D

7 G E’

E* F

C

G

E

to: D

B’

G D

(b) (^) E’ B: Old object

B’: New object

Roots B’ 5 E’

from: 42

A

B’

B*

G F

C

A D’

D*

7 G E’

E* F

C G’

G*

E

to: D’

B’

G’ D

E’ D’

7 G

G’

E

(c)

Roots B’ 5 E’

from: 42

A

B’

B*

G F

C

A D’

D*

7 G E’

E* F

C G’

G*

E

to: D’

B’

G’ D’

E’ D’

7 G’

G’

E’

(d)