You are on page 1of 29

Computer Science 210

Computer Organization

Dynamic Storage and Heap Management


Arrays and Linked Structures
import java.util.*;

List<String> names = new ArrayList<String>();

Queue<Integer> numbers = new LinkedList<Integer>();

for (int i = 1; i <= 5; i++){


names.add("Name" + i);
numbers.add(i);
}

An array-based structure allocates storage for several elements

A linked structure allocates storage for each element as needed


Nodes in Linked Structures
import java.util.*;

public class LinkedList<E> extends AbstractSequentialList<E>{

private Node<E> head, tail;


private in size;
data next data next
public LinkedList(){
head = tail = null;
size = 0;
}

public boolean add(E element){


Node<E> newNode = new Node(element, null);
if (tail != null)
tail.next = newNode;
else
head = newNode;
tail = newNode;
size++;
return true;
}
Nodes in Linked Structures
public boolean add(E element){
Node<E> newNode = new Node(element, null);
if (tail != null)
tail.next = newNode;
else
head = newNode;
tail = newNode; data next data next
size++;
return true; In assembler, links (head,
}
tail, next) are addresses
private class Node<E>{

private E data;
data next
private Node<E> next;

private Node(E element, Node<E> next){


data = element;
this.next = next;
}
}
Dynamic Storage and the System Heap
x0000
Operating system
The system stack provides
x3000
dynamic storage for subroutine
calls
User program code

The system heap provides


dynamic storage for linked nodes
System heap and other dynamically allocated
objects

System stack
The stack pointer moves up, and
xFDFF
the heap pointer moves down
I/O device registers
xFFFF
Heap Management

• Languages like Python, Java, and Lisp allow


the programmer to request new dynamic
memory and recycle unused memory
automatically (garbage collection)

• Languages like C, C++, and Ada require


explicit allocation and deallocation of
dynamic memory
A Simple LC-3 Heap Manager

• Our heap manager allows programs to


allocate and deallocate nodes for linked
structures (no automatic garbage collection)

• Each node has just two fields, a data field


and a next field
The Heap Interface

• The heap structure consists of


– a heap pointer (in R5, which now can’t be used
for anything else)
– three new subroutines, INITHEAP, NEW, and
DISPOSE
Subroutine INITHEAP

• The subroutine INITHEAP is called at


program startup

• This routine initializes the heap pointer and


formats the heap memory into a sequence of
linked nodes (sometimes also called a free
list)
Subroutine NEW
• The subroutine NEW is called to obtain a pointer to a
new node

• This routine receives the data element in R1 and a


pointer to the next node in R2

• It returns a pointer to the new node in R3

• The empty pointer is x0000


Create a Linked List of Two Nodes
;; Author: Ken Lambert

;; This program uses the system heap to create and link together two nodes.

.ORIG x3000

;; Main program register usage:


; R1 = the datum for a new node
; R2 = the next link for a new node
; R3 = a pointer to the new node

; Main program code


JSR INITHEAP ; Initialize the heap
LD R1, X ; Set the parameter for the first datum
AND R2, R2, #0 ; Set the parameter for the next link
JSR NEW
ST R3, HEAD ; Reset HEAD to the new node
LD R1, Y ; Set the parameter for the second datum
LD R2, HEAD ; Set the parameter for the next link
JSR NEW
ST R3, HEAD ; Reset HEAD to the new node
HALT

; Main program data variables


X .FILL 6 ; A data element
Y .FILL 5 ; A data element
HEAD .BLKW 1 ; A pointer to the first node in the list
Create a Linked List with a Loop

;; Author: Ken Lambert

;; This program uses the system heap to create and


;; link together five nodes.

.ORIG x3000

;; Main program register usage:


; R1 = the datum for a new node
; R2 = the next link for a new node
; R3 = a pointer to the new node

; Main program code


JSR INITHEAP ; Initialize the heap
LD R1, MAXDATUM ; Set the parameter for the first datum
AND R2, R2, #0 ; Set the parameter for the next link
WHILE JSR NEW
ST R3, HEAD ; Set HEAD to the new node
ADD R2, R3, #0 ; Set the parameter for the next link
ADD R1, R1, #-1 ; Set the parameter for the next datum
BRp WHILE ; Return to top of loop
HALT

; Main program data variables


MAXDATUM .FILL 5
HEAD .BLKW 1
Process a Linked List: Output
; Output process
LD R2, RT ; Get return character
LD R3, DIGITOFFSET ; Get digit offset
LD R1, HEAD ; Get pointer to first node
OUTLOOP BRz ENDOUT ; Stop if it's empty
LDR R0, R1, #0 ; Access and output the datum
ADD R0, R0, R3
OUT
ADD R0, R2, #0 ; Output the newline
OUT
LDR R1, R1, #1 ; Move to the next node
BR OUTLOOP
ENDOUT HALT

; Main program data variables


MAXDATUM .FILL 5
HEAD .BLKW 1
RT .FILL #13
DIGITOFFSET .FILL #48
Disposing of a Linked List
List<String> names = new LinkedList<String>();

// Add a bunch of names to the list and process them

names = null; // The garbage collector automatically recycles nodes to the heap
Disposing of a Linked List
; Disposal process
LD R1, HEAD ; Get the pointer to the first node
DISLOOP BRz ENDDIS ; Stop if it's empty
LDR R2, R1, #1 ; Save the pointer to the next node
JSR DISPOSE
ADD R1, R2, #0 ; Move to the next node
BR DISLOOP
ENDDIS ST R1, HEAD ; HEAD should now be empty
HALT
Subroutine NEW
;; Subroutine NEW
; Returns the address of a new node in R3
; Input parameters: R1 - the datum for the node
; R2 - the next field of the node
; R5 - the heap pointer
; Output parameters: R3 - the address of the new node
; R5 - the heap pointer

NEW STR R1, R5, #0 ; Store the datum in the new node
ADD R5, R5, #1 ; Move heap pointer to the next field of the new node
ADD R3, R5, #0 ; Save a copy of the heap pointer
LDR R5, R5, #0 ; Reset the heap pointer to the address contained in the next field
STR R2, R3, #0 ; Set the next field of the new node
ADD R3, R3, #-1 ; Set the output parameter to the address of the new node
RET

R5

data next data next data next data next


Subroutine NEW
;; Subroutine NEW
; Returns the address of a new node in R3
; Input parameters: R1 - the datum for the node
; R2 - the next field of the node
; R5 - the heap pointer
; Output parameters: R3 - the address of the new node
; R5 - the heap pointer

NEW STR R1, R5, #0 ; Store the datum in the new node
ADD R5, R5, #1 ; Move heap pointer to the next field of the new node
ADD R3, R5, #0 ; Save a copy of the heap pointer
LDR R5, R5, #0 ; Reset the heap pointer to the address contained in the next field
STR R2, R3, #0 ; Set the next field of the new node
ADD R3, R3, #-1 ; Set the output parameter to the address of the new node
RET

R5

R1 next data next data next data next


Subroutine NEW
;; Subroutine NEW
; Returns the address of a new node in R3
; Input parameters: R1 - the datum for the node
; R2 - the next field of the node
; R5 - the heap pointer
; Output parameters: R3 - the address of the new node
; R5 - the heap pointer

NEW STR R1, R5, #0 ; Store the datum in the new node
ADD R5, R5, #1 ; Move heap pointer to the next field of the new node
ADD R3, R5, #0 ; Save a copy of the heap pointer
LDR R5, R5, #0 ; Reset the heap pointer to the address contained in the next field
STR R2, R3, #0 ; Set the next field of the new node
ADD R3, R3, #-1 ; Set the output parameter to the address of the new node
RET

R5

R1 next data next data next data next


Subroutine NEW
;; Subroutine NEW
; Returns the address of a new node in R3
; Input parameters: R1 - the datum for the node
; R2 - the next field of the node
; R5 - the heap pointer
; Output parameters: R3 - the address of the new node
; R5 - the heap pointer

NEW STR R1, R5, #0 ; Store the datum in the new node
ADD R5, R5, #1 ; Move heap pointer to the next field of the new node
ADD R3, R5, #0 ; Save a copy of the heap pointer
LDR R5, R5, #0 ; Reset the heap pointer to the address contained in the next field
STR R2, R3, #0 ; Set the next field of the new node
ADD R3, R3, #-1 ; Set the output parameter to the address of the new node
RET

R5

R1 next data next data next data next

R3
Subroutine NEW
;; Subroutine NEW
; Returns the address of a new node in R3
; Input parameters: R1 - the datum for the node
; R2 - the next field of the node
; R5 - the heap pointer
; Output parameters: R3 - the address of the new node
; R5 - the heap pointer

NEW STR R1, R5, #0 ; Store the datum in the new node
ADD R5, R5, #1 ; Move heap pointer to the next field of the new node
ADD R3, R5, #0 ; Save a copy of the heap pointer
LDR R5, R5, #0 ; Reset the heap pointer to the address contained in the next field
STR R2, R3, #0 ; Set the next field of the new node
ADD R3, R3, #-1 ; Set the output parameter to the address of the new node
RET

R5

R1 next data next data next data next

R3
Subroutine NEW
;; Subroutine NEW
; Returns the address of a new node in R3
; Input parameters: R1 - the datum for the node
; R2 - the next field of the node
; R5 - the heap pointer
; Output parameters: R3 - the address of the new node
; R5 - the heap pointer

NEW STR R1, R5, #0 ; Store the datum in the new node
ADD R5, R5, #1 ; Move heap pointer to the next field of the new node
ADD R3, R5, #0 ; Save a copy of the heap pointer
LDR R5, R5, #0 ; Reset the heap pointer to the address contained in the next field
STR R2, R3, #0 ; Set the next field of the new node
ADD R3, R3, #-1 ; Set the output parameter to the address of the new node
RET

R5

R1 R2 data next data next data next

R3
Subroutine NEW
;; Subroutine NEW
; Returns the address of a new node in R3
; Input parameters: R1 - the datum for the node
; R2 - the next field of the node
; R5 - the heap pointer
; Output parameters: R3 - the address of the new node
; R5 - the heap pointer

NEW STR R1, R5, #0 ; Store the datum in the new node
ADD R5, R5, #1 ; Move heap pointer to the next field of the new node
ADD R3, R5, #0 ; Save a copy of the heap pointer
LDR R5, R5, #0 ; Reset the heap pointer to the address contained in the next field
STR R2, R3, #0 ; Set the next field of the new node
ADD R3, R3, #-1 ; Set the output parameter to the address of the new node
RET

R5

R1 R2 data next data next data next

R3
Subroutine DISPOSE
;; Subroutine DISPOSE
; Returns an old node (addressed by R1) to the heap
; Input parameters: R1 - a pointer to the old node
; R5 - the heap pointer
; Output parameter: R5 - the heap pointer

DISPOSE ADD R1, R1, #1 ; Advance to the old node's next field
STR R5, R1, #0 ; Store the heap pointer there
ADD R5, R1, #-1 ; Aim the heap pointer at the old node
RET

R5

data next data next data next data next

R1
Subroutine DISPOSE
;; Subroutine DISPOSE
; Returns an old node (addressed by R1) to the heap
; Input parameters: R1 - a pointer to the old node
; R5 - the heap pointer
; Output parameter: R5 - the heap pointer

DISPOSE ADD R1, R1, #1 ; Advance to the old node's next field
STR R5, R1, #0 ; Store the heap pointer there
ADD R5, R1, #-1 ; Aim the heap pointer at the old node
RET

R5

data next data next data next data next

R1
Subroutine DISPOSE
;; Subroutine DISPOSE
; Returns an old node (addressed by R1) to the heap
; Input parameters: R1 - a pointer to the old node
; R5 - the heap pointer
; Output parameter: R5 - the heap pointer

DISPOSE ADD R1, R1, #1 ; Advance to the old node's next field
STR R5, R1, #0 ; Store the heap pointer there
ADD R5, R1, #-1 ; Aim the heap pointer at the old node
RET

R5

data next data next data next data next

R1
Subroutine DISPOSE
;; Subroutine DISPOSE
; Returns an old node (addressed by R1) to the heap
; Input parameters: R1 - a pointer to the old node
; R5 - the heap pointer
; Output parameter: R5 - the heap pointer

DISPOSE ADD R1, R1, #1 ; Advance to the old node's next field
STR R5, R1, #0 ; Store the heap pointer there
ADD R5, R1, #-1 ; Aim the heap pointer at the old node
RET

R5

data next data next data next data next

R1
Subroutine INITHEAP
;; Subroutine INITHEAP
; Initializes the heap pointer and formats the heap memory as a free list
; Input parameter: R5 - the heap pointer
; Output parameter: R5 - the heap pointer
; Other registers: R1 - address of the next field of current node
; R2 - address of the next node
; R3 - counter for the loop

INITHEAP LD R5, HEAPTOP ; Initialize the heap pointer


ADD R1, R5, #1 ; Pointer to the next field of the current node
ADD R2, R5, #2 ; Pointer to the next node
LD R3, HEAPSIZE ; Counter for the number of nodes

HEAPLOOP BRz ENDHEAP


STR R2, R1, #0 ; Store pointer to the next node in the next field of the
current node
ADD R1, R1, #2 ; Advance the pointers
ADD R2, R2, #2
ADD R3, R3, #-1 ; Decrement the counter
BR HEAPLOOP
ENDHEAP RET

; Data variables for subroutine INITHEAP


HEAPTOP .FILL x302F ; Address of the beginning of the heap
HEAPSIZE .FILL #10 ; Number of nodes in the initial heap
Extensions

• Add checks for heap underflow with error


handling

• Add capability for nodes of varying sizes

• Garbage collection (probably worth a


master’s thesis)

You might also like