1 Dispense scritte da M. Ancona CAPITOLO 2 - HEAP (v 1.0) Dispense di proprietà del DISI



Similar documents
MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Assembly Language: Function Calls" Jennifer Rexford!

The stack and the stack pointer

Lecture 27 C and Assembly

Pro3 1 : listes chaînées

Instruction Set Architecture

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

Instruction Set Architecture

Novas Software, Inc. Introduction. Goal. PART I: Original Technology Donation 1. Data Dump Reader

1 The Java Virtual Machine

The V8 JavaScript Engine

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Algorithms and Data Structures

Lecture 1: Data Storage & Index

7.1 Our Current Model

Machine-Level Programming II: Arithmetic & Control

Persistent Binary Search Trees

& Data Processing 2. Exercise 3: Memory Management. Dipl.-Ing. Bogdan Marin. Universität Duisburg-Essen

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

Microcontroller Basics A microcontroller is a small, low-cost computer-on-a-chip which usually includes:

Symbol Tables. Introduction

Machine Programming II: Instruc8ons

PES Institute of Technology-BSC QUESTION BANK

10CS35: Data Structures Using C

University of Toronto Faculty of Applied Science and Engineering

CS:APP Chapter 4 Computer Architecture Instruction Set Architecture. CS:APP2e

Typy danych. Data types: Literals:

System Software Prof. Dr. H. Mössenböck

Charts with Names, More Controls, and Data Validation. Fall 2014 Assist. Prof. Zaixin Lu

Data Tool Platform SQL Development Tools

THUMB Instruction Set

Positional Numbering System

U:\montages\dominique\servo\moteur_AV_AR\program\moteur_AV_AR.asm jeudi 28 avril :32

Memory Management in Mac OS

Scheduling for QoS Management

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

Data Structures and Data Manipulation

CS61: Systems Programing and Machine Organization

Raima Database Manager Version 14.0 In-memory Database Engine

AN2183. Using FLASH as EEPROM on the MC68HC908GP32. Introduction. Semiconductor Products Sector Application Note

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Call Subroutine (PC<15:0>) TOS, (W15)+2 W15 (PC<23:16>) TOS, Process data. Write to PC NOP NOP NOP NOP

2) Write in detail the issues in the design of code generator.

Common Data Structures

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Unix Scripts and Job Scheduling

Reduced Instruction Set Computer (RISC)

TREE BASIC TERMINOLOGIES

Principles of Database Management Systems. Overview. Principles of Data Layout. Topic for today. "Executive Summary": here.

1.- L a m e j o r o p c ió n e s c l o na r e l d i s co ( s e e x p li c a r á d es p u é s ).

HOW TO USE MINITAB: DESIGN OF EXPERIMENTS. Noelle M. Richard 08/27/14

How To Protect Your Computer From Being Copied On A Microsoft X86 Microsoft Microsoft System (X86) On A Linux System (Amd) On An X (Amd2) (X64) (Amd

M6800. Assembly Language Programming

Chapter 2 Topics. 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

Recovery Principles in MySQL Cluster 5.1

Rotation Operation for Binary Search Trees Idea:

Introduction to Microcontrollers


Introduction to MIPS Assembly Programming

CSCI 3136 Principles of Programming Languages

Binary Heaps. CSE 373 Data Structures

Storage in Database Systems. CMPSCI 445 Fall 2010

1 Classical Universal Computer 3

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

Module 2 Stacks and Queues: Abstract Data Types

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

System Calls and Standard I/O

picojava TM : A Hardware Implementation of the Java Virtual Machine

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Review of Hashing: Integer Keys

Exercise 4 Learning Python language fundamentals

Physical Data Organization

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

An Overview of Stack Architecture and the PSC 1000 Microprocessor

Simple C Programs. Goals for this Lecture. Help you learn about:

USB Human Interface Joystick Demonstration Create a HID USB Device (sample Joystick) By Jared St.Clare, October 2009 Version 1

風 水. Heap Feng Shui in JavaScript. Alexander Sotirov.

Intel Assembler. Project administration. Non-standard project. Project administration: Repository

Network Flow I. Lecture Overview The Network Flow Problem

Using Heap Allocation in Intel Assembly Language

BCD (ASCII) Arithmetic. Where and Why is BCD used? Packed BCD, ASCII, Unpacked BCD. BCD Adjustment Instructions AAA. Example

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Writing a Logical Decoding Plug-In. Christophe Pettus PostgreSQL Experts, Inc. FOSDEM 2015

DATA STRUCTURE - STACK

M A S S A C H U S E T T S I N S T I T U T E O F T E C H N O L O G Y DEPARTMENT OF ELECTRICAL ENGINEERING AND COMPUTER SCIENCE

An Introduction to the ARM 7 Architecture

The Design of the Inferno Virtual Machine. Introduction

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

R-trees. R-Trees: A Dynamic Index Structure For Spatial Searching. R-Tree. Invariants

Analysis of Algorithms I: Binary Search Trees

University of Massachusetts Amherst Department of Computer Science Prof. Yanlei Diao

Data Structures Fibonacci Heaps, Amortized Analysis

Computer Architecture

Lecture 2 February 12, 2003

PART B QUESTIONS AND ANSWERS UNIT I

Web Programming Step by Step

Transcription:

1

! ""# $ %! && " $% $% " ' #" ( ) """ " $ % ) "&# $ % "" ' "*"* "(!" " "# $ % & ++,++,++,'! + +( &"# $-&,.,#'%'!"" &&*"( "!#$(*" #! %( " " " #' "&!/ """,0 &"#*'/ # "! / " ""# " 1 "" " #0 ""# " + +' &! ("#0#"+" #+!!"( " # & &( &! *"#0"&2! " # ( #0 "& $*% #0 #&*"" " $3%*$% '"!""!' 2

'#'%% &! ()*+(*!,$,-.&/ "" & #"" 4+, *!! "*""& 0 $"& %'!"516$56%& ( &! $" %,$%+,%!,! ' #$%7/ "" #'56 "," " ' / 0 " * " " +#$+ " " #"5,589,''',:5;9,:5' 5"""" 0"" *!'"" #0"&#&"!!1"'!1" """ & "" " # "" $/%' " " "( 3

056 "*0* "( &!/0"#"5:5 5 " & & & # " "0'! "0"""*", # ", "0' 0! /9<*""""0"( """""%*& #""** "" &' &( " #0 " 0""" #0"' " ( &!/"# &"!1" "" $%'! """' " "*". 10/' " ""*" &," "",& ' =" *"0>?@ &!1" AB@!1" ' " 0 & 0 & 3 /3" & 0 "#"* **' 51*"!" &" ""' )( /C'=6'-1"$/>>%"# 0 ' 0 " " 3 "*' 4

22'*.&/,, -1)3412,$1%51 #'4$1 65 %' * #' #61 " 71&89:. / ) 41 6%' %5'61 4 - " ""1;1<,=1&890.0/ 2,$1 6>#%' 61 4 41#7=1&89?! / 0 " 0""? 0 # &"9,"$DE%( mov QL[r0],r1 ; root della quicklist beq L1 ; salta se vuota movl (r1),ql[r0] ; restituisci il 1o blocco br done L1: cmpl r0,tailrest ; c'e' spazio? bgtr L2 ; no, usa schema complesso movl tailbagin,r1 ; inizio tail addl3 r0,r1,tailbegin ; incrementa subl2 r0,tailrest ; decr spazio restante br done L2: pushl r0 ; push requested size calls $1,hardalloc ; call compex strategy 5

( -- -- package specification -- with SYSTEM generic minql: integer:=o; -- min quick list item size maxql: integer :=32; -- max quick list item size OSchunk: integer :=4096; -- how much we get from the OS package QuickFit is function Alloc(n: natural) return ststem.address; procedure DeAlloc(s: system.address;n: natural); end QuickFit; -- -- package body of QuickFit -- with UNCHECKED_CONVERSION; package body QuickFit is -------------------- Types For Storage on a Free List type free_cell; -- a free block(see below) type freptr is access free_cell; -- pointer to a free block type free_cell is record next: freeptr; -- pointer to the next free block on -- a list size: natural; -- size of the current free block end record; -------------------- Constants Related to Allocation Size Chunk: constant integer := natural'size/system.storage_unit; MinChunk: constant integer := _cell'size/system.storage_unit; -------------------- The Free Space Pointer QL: array(minql..maxql) of freeptr; -- One list for each size MiscList: freeptr; -- A single list for all sizes TailBegin: system.address; -- Beginning of space left in the tail TailRemaining: natural :=0; -- Amount of space left in the tail -------------------- Unchecked Conversion Between Types function ConvertToFreeptr is new UNCHECKED_CONVERSION(natural,freePtr); function ConvertToFreeptr is new UNCHECKED_CONVERSION(system.address,freePtr); function ConvertToAddress is new 6

UNCHECKED_CONVERSION(freePtr,system.address); function ConvertToAddress is new UNCHECKED_CONVERSION(natural,system.address); function ConvertToNatural is new UNCHECKED_CONVERSION(system.address,natural); -------------------- Support Routines function "+"(s: system.address; i: natural) return system.address is -- addition between addresses and naturals begin return ConvertToAddress(ConvertToNatural(s)+i); end "+"; function ComputeSize(size natural) return natural is -- Round a size request up to -- (a) an integral numb. of chunks" and, -- (b) at least MinChunk (so that there is always -- space for the free space information) begin if size < minchunk then return minchunk; else return (size+(chunk-1))/chunk; end if; end ComputeSize; -------------------- Allocaion Strategy Routines function QuickAlloc(size: natural) return system.address is -- perform an allocation from one of the quick lists cell: freeptr; begin cell := QL(size); QL(size):= cell.next; return ConvertToAddress(cell); end QuickAlloc; function TailAlloc(size: natural) return system.address is -- perform an allocation from the tail locn: system.address; begin locn := TailBegin; TailBegin := TailBegin + size; Tailremaining := Tailremaining - size; return locn; end TailAlloc; 7

function MiscAlloc(size: natural) return system.address is -- perform an allocation from the micellaneous list begin -- Uses whatever method pleases you. -- Return the null address if allocation is not possible. -- The next line is only for syntactic correction return ConverToAddress(null); end MiscAlloc; function ExtendTail is -- obtain new storage from the operating system begin if TailRemaining > 0 then then DeAlloc(TailBegin,TailRemaining) end if; -- -- Tailbegin := << space obtained from the operating system.>> -- <<raise storage_error if the OS fails>> -- TailRemaining := OSChunk; -- end ExtendTail; function HardAlloc(size: natural) return system.address is begin if size > OSchunk then raise storage_error; end if; locn := MiscAlloc(size); if licn /= ConvertToAddress(null) then return locn; end if; extendtail; return TailAlloc(size); end hardalloc; --------------------------- Entry points to the QuickFit Allocator function Alloc(n: natural) return system.address is -- allocate a chunk of size (at least) n storage units size: natural; begin size := ComputeSize(n); if size in minql..maxql then if QL(size) /= null then return QuicjAlloc(size); elsif size <= TailRemaining then return TailAlloc(size); else return HardAlloc(size); end if; elsif size <= TailRemaining then return TailAlloc(size); else return HardAlloc(size); end if; end Alloc; 8

procedure DeAlloc(s: system.address; n: natural) is -- deallocate the chunk at s of length n size: natural; cell: freeptr := ConvertToFreePtr(s); begin size := ComputeSize(n); cell.size := size; if not(size in minql..maxql) then cell.next := MiscList; Misc.List := cell; else cell.next := QL(size); QL(size) := cell; end if; end DeAlloc; end QuickFit; 9

%#@52 #%&;<0" " #F;' # 0 "" $"" & GG"3"% ' 1# 0# '""#.&/'#'*"0 & 516' & #0 " &","# 0" & " " 0"' 5!1"& 0*" " ++ "3#' A: ARRAY[1..N] OF ADDRESS (* freelist *) &( size(a[i]=i*b) per i=1,..n-1; HI""#0"&#' #""""& "H1I&( (s<=k*b)and(forall i<k: ((s> B*i) or (A[i]=NIL))) "1J")"""#&, 1K")#&&" " " &" J1L;"' & " ", "& " H)I' #"" ", #0 " ",0* ' 3HIM&" #0 "$ 9<> #3"%HI#0"&# "*$8F%L,FJ9,<,''''"" #";* # & 0"' " ";( 10

PROCEDURE Allocate(VAR a: ADDRESS; size LONGINT); VAR i: INTEGER;l,r:ADDRESS; BEGIN i:= MIN(size/B,N)) (* indice lista forzato <=N *) WHILE (i<n)&(a[i]=nil) DO i:=i+1 OD; (*find min non empty list*) l:=adr(a[i]); a:=l^;(*addr. and value of pnt to 1th free block*) WHILE (a<>nil)&(a.size<size) DO (*if i=n then first fit*) l:=adr(a.next); a:=l^ OD;(*end while*) IF a#nil THEN l^:=a.next; (* remove block from freelist*); IF a.size>size THEN (* block must be split *) i:=min((a.size-size)/b,n); r:=a+size; r.size:=a.size-size; (* adj. size of residual blk*) r.next:=a[i];(* insert residual blk in free list *) A[i]:=r (* insert residual blk in free list *) FI FI END Allocate; '2' /"& *" ""0 1;;/G ' # &# "! ""1!"* " " " /1'!" 1;;"G "# HBI, #""" 0 ' *"" *, "" &" #0 0! '*"1, **,"( PROCEDURE Mark(q: ADDRESS); VAR off:address; BEGIN IF (q<>nil)&unmarked(q) THEN SetMark(q);off:=FirstPointerOffset(q); WHILE off>=0 DO Mark(MEM[q+off]); off:= NextPointerOffset(q,off) OD; FI END Mark; 11

/" "( " 0 & 2 #" &*" """& '=**"&"+"+ ' %; " & * #" & * """&, &M? *"$% $" * 6"**" :**" 0 ** **" '5"**"""" &#""" '!&*+3 " +0 & **" ' +=+"" "" /$"0" ""%" *( 12

" " #( #& ;**"0"&&&# 0 & #"' =" 3 " "( &"""G&*""" 6"**":**"3 " ( PROCEDURE Mark(q: ADDRESS); VAR off:address; BEGIN IF (q#nil)&unmarked(q) THEN SetMark(q);INC(Tag(q)); WHILE MEM[Tag(q)]>=0 DO Mark(MEM[q+MEM[Tag(q)]]); INC(Tag(q)); OD; (*end while*) RestoreTag(q) FI END Mark; 13

&3 #*"1 " **" ""! # +*!' "**"!"**"&' 5"&#0" " Tag(q):= Tag(q)+mem[Tag(q)] 14

1""*,&$!.%=1$!% * 1&""( PROCEDURE Mark(q: ADDRESS); VAR r: ADDRESS; BEGIN (* (q#nil)&justmarked(q) *) DO (* endless loop*) Increment(Tag(q)); IF mem[tag(q)] >=0 THEN r:=mem[q+mem[tag(q)]]; IF (r#nil)&unmarked(r) THEN SetMark(r); Mark(r) FI ELSE RestoreTag(q); RETURN (* exit from endless loop*) FI OD (* closes endless loop*) END Mark; *$"*0,"""N' "#"1" '0"& """0$!%2!(J2 $!%' C=1$!(CC//%2 D(CC//2 $L$!.%-"1$!%L% /1(J 32 C $ $!%%2 6H $!%IKJ? (JH!8H $!%II2 6$.%=1$% /1$%2 "0$!%2!(J 6 /" $!%2 6/1J 3 E / $!% 6 C C12 15

*!" " "1 0 0 "" "' C"0 /0 7 HOI, "1& "# 0&&& &"'5"&# "1, "", "& ( "! "' &0 " &# " "" "1'5""*0( Stack=Empty p=nil Push(q) mem[q+mem[tag(q)]] := p; p := q; Pop(q) a := p+mem[tag(p)]; r:= mem[a];mem[a]:=q;q:=p;p:=r; PROCEDURE Mark(q: ADDRESS); VAR p,r,a: ADDRESS; BEGIN (* (q#nil)&justmarked(q) *) p :=NIL; DO (*endless loop *) Increment(Tag(q)); IF mem[tag(q)] >=0 THEN r:=mem[q+mem[tag(q)]]; IF (r#nil)&unmarked(r) THEN SetMark(r); mem[q+mem[tag(q)]] := p; p :=q; q := r; FI ELSE RestoreTag(q); IF p=nil THEN EXIT ELSE a := p + mem[tag(p)]; r := mem[a]; mem[a] := q; q :=p; p:=r FI FI OD (*closing endless loop *) END Mark; &" " ""# B>???' "0,& # & 0 & "" 3 " " #' 3 " & " (! &&3 " 0**"" "' ""&3 " """" 3 " '! &!""" $" "#%' 16

" ""# B>??? *" 1 ' * 68000 mark phase for garbage collector * A0: pointer to father * A1: pointer to node * A2: temporary, for pointer rotation * A3: tag or pointer to current pointer offset * pointer offsets are usually accessed via A3 with offset of Offset(ptable)-4-1. The pointer is incremented before it is accessed, thus the subtraction of PtrSize. The subtraction of 1 comes from the set mark bit (bit # 0). * D0: offset * D1: temporary PtrSize EQU 4 size of pointers and offsets Tag EQU -4 offset of type tag Tail EQU Tag+3 low byte of type tag Mark EQU 0 mark bit (in TagL) PTab EQU 36 ptable offset Offset EQU PTab-PtrSize-1 Start MOVE.L A1,D1 NIL test BEQ End NIL BSET.B #Mark,TagL(A1) test and set mark bit BNE End marked MOVE.L #0,A0 father:=nil MOVE.L Tag(A1),A3 lad first tag BRA Loop Up ADD.L D0,A3 adjust tag MOVE.L A3,Tag(A1) save tag MOVE.L A0,D1 NIL test BEQ End father=nil (sentinel) MOVE.L Tag(A0),A3 load father.tag MOVE.L Qffset(A3),D0 load offset MOVE.L (A0,D0),A2 rotate pointers, step1 MOVE.L A1,(A0,D0) rotate pointers, step2 MOVE.L A0,A1 rotate pointers, step3 MOVE.L A2,A0 rotate pointers, step4 Loop ADDQ.L #PtrSize,A3 address of next offset MOVE.L OffSet(A3),D0 load next offset BMI Up negative sentinel reached,eol MOVE.L (A1,D0),A2 load son(& rotate pts, step1) MOVE.L A2,D1 NIL test BEQ Loop NIL BSET.B #Mark,TagL(A2) test and set mark bit BNE Loop marked 17

Down MOVE.L A3,Tag(A1) save tag MOVE.L A0,(A1,D0) rotate pointers, step1 MOVE.L A1,A0 rotate pointers, step2 MOVE.L A2,A1 rotate pointers, step3 MOVE.L Tag(A1),A3 load new tag BRA Loop End!!"*"" &" 0 "! # #' *!"""""#'" " # " *" # """&'C3 #"*3 " 0"'#0#" ",**0"## 3 " '*"""' *"&!"*"'"G "" & "#0 "&" ##0 "* " ' &' 18

PROCEDURE Scan; VAR p: ADDRESS; BEGIN a[1..n]:=nil; q:=heapstart; DO WHILE (q#memsize)&marked(q) DO ResetMark(q); q:= q+mem[mem[q]] OD; IF q # MemSize THEN p:=q; DO q := q + mem[mem[q]] UNTIL (q=memsize) OR Marked(q); Insert(p,q-p) FI UNTIL q = MemSize END Scan; "$,"%""## * """'&!";"G 0#0# &"0""","#0"'/ #&" ##0! """&'5"& "** ;"G #0#' 19

22'*.&/251-16'5A'2' 61 4-1 5 4 10B<08. /,4165'##'''261%- C1&D+9!1??&<?9B.0/,43' $41652%>%#61&8D8.E/ 5 1 6;# >%% *,)% #61441DE0&1&8D95 4.:/ ) 41 64 - " ""61 <,=1 &890.?/ 3 #4=16%; *% * %>#2 F%%% 5 # 2>#561 #1 01 &8?B1&DE<&8:.9/1*3$16'%*'< 2% %' #61 %GDD 1 &8DD1 &B9<&&D.D/ 5 %41,,1 6 ** #5< *'2';%%%61 #1&B+D!1&8?91:B&<:B:./ '44&0&37; #41 4@AAH7 "AA4AAC4" 20