Memory Management: Static Allocation - FTP Directory Listing

clippersdogheartedSoftware and s/w Development

Dec 14, 2013 (3 years and 6 months ago)

83 views

© Bertrand Meyer and Yishai Feldman

Notice

Some

of

the

material

is

taken

from

Object
-
Oriented

Software

Construction
,

2
nd

edition,

by

Bertrand

Meyer

(Prentice

Hall)
.

Included

here

by

permission

of

ISE,

for

the

benefit

of

IDC

students
.

Other

uses,

duplication

or

distribution

require

permission

from

ISE

<info@eiffel
.
com>
.

For

more

material

see

http
:
//eiffel
.
com


© Bertrand Meyer and Yishai Feldman

Memory Management:

Static Allocation

© Bertrand Meyer and Yishai Feldman

Memory Management:

Stack
-
Based Allocation

© Bertrand Meyer and Yishai Feldman

Memory Management:

Free Allocation (Heap
-
Based)

© Bertrand Meyer and Yishai Feldman

Detachment

If

x

is

attached

to

O
1

and

y

is

attached

to

O
2
,

then
:

D
1



An

assignment

of

the

form

x

:
=

Void
,

or

x

:
=

v


where

v

is

void,

detaches

x

from

O
1
.

D
2



An

assignment

of

the

form

y

:
=

z
,

where

z

is

attached

to

an

object

other

than

O
2
,

detaches

y

from

O
2
.

D
3



Termination

of

a

routine

detaches

formal

arguments

from

any

attached

objects
.

D
4



A

creation

instruction

!!

x

,

attaches

x

to

a

newly

created

object,

and

hence

detaches

x

if

it

was

previously

attached

to

an

object

O
1
.

© Bertrand Meyer and Yishai Feldman

Reachable Objects

© Bertrand Meyer and Yishai Feldman

LINKED_LIST: put_right

class

LINKED_LIST

feature

put_right

(
v

:

ELEMENT
)

is


--

insert

an

element

of

value

v

to

the

right

of

cursor


local

new

:

LINKABLE


do



!!
new
.
make

(
v
)



new
.
put_linkable_right

(
active
.
linkable_right
)



active
.
put_linkable_right

(
new
)


end

© Bertrand Meyer and Yishai Feldman

LINKED_LIST: remove_right

remove_right

is


--

delete

element

to

the

right

of

cursor


local

next

:

LINKABLE


do



next

:
=

active
.
linkable_right



active
.
put_linkable_right

(
next
.
linkable_right
)


end

active

© Bertrand Meyer and Yishai Feldman

put_right

with Memory Management

class

LINKED_LIST

feature

put_right

(
v

:

ELEMENT
)

is


--

insert

an

element

of

value

v

to

the

right

of

cursor


local

new

:

LINKABLE


do



new

:
=

fresh

(
v
)


--

!!
new
.
make

(
v
)



new
.
put_linkable_right

(
active
.
linkable_right
)



active
.
put_linkable_right

(
new
)


end

© Bertrand Meyer and Yishai Feldman

remove_right

with Memory Management

remove_right

is


--

delete

element

to

the

right

of

cursor


local

next

:

LINKABLE


do



next

:
=

active
.
linkable_right



active
.
put_linkable_right

(
next
.
linkable_right
)



recycle

(
next
)


end

© Bertrand Meyer and Yishai Feldman

Recycling Objects (
1
)

feature

{
NONE
}


available
:

STACK_OF_LINKABLES


recycle

(
dead
:

LINKABLE
)

is



--

Return

dead

to

the

available

list
.



require




dead

/=

Void



do




available

.

put

(
dead
)



end

© Bertrand Meyer and Yishai Feldman

Recycling Objects (
2
)

fresh

(
v
:

ELEMENT_TYPE
)
:

LINKABLE

is


--

A

new

element

with

value

v
,

for

reuse

in

an

insertion


do



if

available

.

empty

then




!!

Result

.

make

(
v
)



else




Result

:
=

available

.

item




Result

.

put

(
v
)




available

.

remove



end


end

© Bertrand Meyer and Yishai Feldman

Free List (as Stack)

class

STACK_OF_LINKABLES

feature

{
LINKED_LIST
}


item
:

LINKABLE



--

Element

at

top


empty
:

BOOLEAN

is



--

Is

there

no

item?



do




Result

:
=

(
item

=

Void
)



end

© Bertrand Meyer and Yishai Feldman

Free List (
2
)

put

(
element
:

LINKABLE
)

is



--

Add

element

on

top
.


require

element

/=

Void


do



element

.

put_right

(
item
)
;

item

:
=

element


end

remove

is



--

Remove

last

item

added
.


require

not

empty


do



item

:
=

item

.

right


end

© Bertrand Meyer and Yishai Feldman

Garbage Collection Algorithms


Stop

and

copy


Mark

and

sweep


Generation

scavenging


On
-
the
-
fly

/

parallel

© Bertrand Meyer and Yishai Feldman

Reclaiming Other Resources

dispose

is



--

Action

to

be

taken

in

case

of

reclamation

by



--

garbage

collector
:



--

close

the

associated

file

if

open
.



--

Called

automatically

by

garbage

collector
.


do



if

opened

then




close



end


end

© Bertrand Meyer and Yishai Feldman

Dimensions of Generalization

© Bertrand Meyer and Yishai Feldman

Genericity in Weakly
-
Typed
Languages

Dynamically
-
Typed

("Smalltalk")

my_stack

.

put

(
my_circle
)

my_account

:
=

my_stack

.

item

my_account

.

withdraw

(
5000
)




Statically
-
Typed

(Java)

my_stack
.
put(my_circle)
;

my_account

=

(account)my_stack
.
item
;



my_account
.
withdraw(
5000
)
;

© Bertrand Meyer and Yishai Feldman

Declaring Generic Classes

indexing

description
:

"
Stacks

of

elements

of

an

arbitrary

type

G

"

class

STACK

[
G
]

feature


count
:

INTEGER



--

Number

of

elements

in

stack


empty
:

BOOLEAN

is


--
Are

there

no

items?



do



end


full
:

BOOLEAN

is



--

Is

representation

full?



do



end


item
:

G

is




--

Top

element



do



end


put

(
x
:

G
)

is




--

Add

x

on

top
.



do



end


remove

is




--

Remove

top

element
.



do



end

end

--

class

STACK

© Bertrand Meyer and Yishai Feldman

Using Generic Classes

Declarations

sc
:

STACK

[
CIRCLE
]
;

sa
:

STACK

[
ACCOUNT
]

c
:

CIRCLE
;

a
:

ACCOUNT

Valid

Instructions

sc

.

put

(
c
)


--

Push

a

circle

onto

a

stack

of

circles

sa

.

put

(
a
)


--

Push

an

account

onto

a

stack

of

accounts

c

:
=

sc

.

item


--

Assign

to

a

circle

entity

the

top

of

a

stack

of

circles

Invalid

Instructions

sc

.

put

(
a
)
;


--

Push

an

account

onto

a

stack

of

circles

sa

.

put

(
c
)
;


--

Push

a

circle

onto

a

stack

of

accounts

c

:
=

sa

.

item


--

Access

as

a

circle

the

top

of

a

stack

of

accounts

© Bertrand Meyer and Yishai Feldman

Operations on Entities

of Generic Type

The

valid

uses

for

an

entity

x

whose

type

G

is

a

formal

generic

parameter

are

the

following
:

G
1



Use

of

x

as

left
-
hand

side

in

an

assignment,

x

:
=

y
,

where

the

right
-
hand

side

expression

y

is

also

of

type

G
.

G
2



Use

of

x

as

right
-
hand

side

of

an

assignment

y

:
=

x
,

where

the

left
-
hand

side

entity

y

is

also

of

type

G
.

G
3



Use

of

x

in

a

boolean

expression

of

the

form

x

=

y

or

x

/=

y
,

where

y

is

also

of

type

G
.

G
4



Use

of

x

as

actual

argument

in

a

routine

call

corresponding

to

a

formal

argument

declared

of

type

G
,

or

of

type

ANY
.

G
5



Use

as

target

of

a

call

to

a

feature

of

ANY
.

© Bertrand Meyer and Yishai Feldman

ARRAY

(Outline)

class

ARRAY

[
G
]

creation

make

feature


make

(
minindex
,

maxindex
:

INTEGER
)

is




--

Allocate

array

with

bounds

minindex

and

maxindex




--

(empty

if

minindex

>

maxindex
)



do



end


lower
,

upper
,

count
:

INTEGER




--

Minimum

and

maximum

legal

index
;

array

size
.


put

(
v
:

G
;

i
:

INTEGER
)

is




--

Assign

v

to

the

entry

of

index

i



do



end


infix

"
@
",

item

(
i
:

INTEGER
)
:

G

is




--

Entry

of

index

i



do



end

end

--

class

ARRAY

© Bertrand Meyer and Yishai Feldman

Genericity in Java:
Object

public

class

Stack
1

{


public

Stack
1
()



{

...

}


public

boolean

empty()



{

...

}


public

Object

item()



{

...

}


public

void

put(
Object

element)



{

...

}


public

void

remove()



{

...

}

© Bertrand Meyer and Yishai Feldman

Casting for Retrieval


public

static

void

main(String

argv[])


{


String

value
;


Stack
1

stack

=

new

Stack
1
()
;


stack
.
put(new

String("First"))
;


stack
.
put(new

String("Second"))
;


value

=

(String)
stack
.
item()
;


System
.
out
.
println(value)
;


stack
.
remove()
;


value

=

(String)
stack
.
item()
;


System
.
out
.
println(value)
;


stack
.
remove()
;


}

© Bertrand Meyer and Yishai Feldman

Generic Vectors in Java (
1
)

import

java
.
util
.
*
;


public

class

Stack
1

{


protected

Vector

elements
;


protected

int

count
;



public

Stack
1
()


{


elements

=

new

Vector()
;


count

=

0
;


}

© Bertrand Meyer and Yishai Feldman

Generic Vectors in Java (
2
)

public

boolean

empty()

{


return

count

==

0
;

}



/**


*

@require

!empty()


*/

public

Object

item()

{


return

elements
.
elementAt(count

-

1
)
;

}

© Bertrand Meyer and Yishai Feldman

Generic Vectors in Java (3)

public

void

put(Object

element)

{


if

(elements
.
size()

>

count)


elements
.
setElementAt(element,

count)
;


else


elements
.
addElement(element)
;


count++
;

}


/**


*

@require

!empty()


*/

public

void

remove()

{


count
--
;

}

© Bertrand Meyer and Yishai Feldman

Genericity in Java: Interfaces

public

interface

G

{}


public

class

Stack
1

{


public

Stack
1
()

{

...

}


public

boolean

empty()

{

...

}


public

G

item()

{

...

}


public

void

put(
G

element)

{

...

}


public

void

remove()

{

...

}

}

© Bertrand Meyer and Yishai Feldman

Casting with Interfaces


public

static

void

main(String

argv[])


{


GString

value
;


Stack
1

stack

=

new

Stack
1
()
;


stack
.
put(new

GString("First"))
;


stack
.
put(new

GString("Second"))
;


value

=

(GString)
stack
.
item()
;


System
.
out
.
println(value)
;


stack
.
remove()
;


value

=

(GString)
stack
.
item()
;


System
.
out
.
println(value)
;


stack
.
remove()
;


}

© Bertrand Meyer and Yishai Feldman

Implementation with Vectors

/**


*

@require

!empty()


*/

public

G

item()

{


return

(G)
elements
.
elementAt(count

-

1
)
;

}