| freeList | ---> |
|
---> |
|
---> | . . . |
cons and other functions obtain new cells from the free list.
| Cell Replacement | ||
| Explicit | Automatic | |
| Reference counts | Garbage collection | |
E.g.,
| - | A bother for the user. |
| - | Not secure.
|
| bList | ---> |
|
freeList | ---> |
|
---> | . . . | |||||
| aList | ---> | |||||||||||
| | v |
||||||||||||
| bList | ---> |
|
---> |
|
---> | . . . | ||||
| freeList | ---> |
Reference counts
| aList ----> |
|
---> |
|
---> |
|
|||||||||
| | ^ v | a | |
| v b |
| ^ v | c | |
||||||||||||
| | | | | |||||||||||||
| bList ----> |
|
cList | ---> |
|
||||||||||
| | v d |
| v e |
|||||||||||||
When a reference is created, the reference count is incremented.
When a reference is destroyed, the reference count is decremented.
When the reference count becomes zero, its two references (car and cdr) are destroyed and the cell is placed on the free list.
PROBLEM: cyclic structures may not be reclaimed.
| |------- | ------------- | ---| | ||||||||
| v | | | |||||||||
| aList ----> |
|
---> |
|
---| | ||||||
| | v a |
| v b |
|||||||||
The reference counts of both cells in this circular list become 1 if the binding to aList is broken, but both are inaccessible.
| |------- | ------------- | ---| | ||||||||
| v | | | |||||||||
| aList --X--> |
|
---> |
|
---| | ||||||
| | v a |
| v b |
|||||||||
This situation can occur when rplaca and rplacd are used.
Garbage collection
Two phases:
Mark accessible cells.
for each root r, mark( r )
mark( r )
if r is not marked then
set mark bit of r
mark( left( r ))
mark( right( r ))
end if
Sweep unmarked cells onto the free list.
for each cell
if cell is marked then
reset mark bit of cell
else
link cell onto free list
Usually implemented using a table containing one bit for each cons cell available to the interpreter, rather than using a special bit in each cell.
PROBLEM: long unexpected delays.
SOLUTIONS: incremental garbage collection, generational scavenging, . . .