ObjFHandle.st
author Claus Gittinger <cg@exept.de>
Tue, 12 Nov 1996 13:18:57 +0100
changeset 441 fa5637faa969
parent 388 067d7de615f5
child 474 bf2345df4093
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1995 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

Object subclass:#ObjectFileHandle
	instanceVariableNames:'sysHandle1 sysHandle2 pathName moduleID handleType weakMethodRef
		weakClassRefs weakFunctionRefs'
	classVariableNames:''
	poolDictionaries:''
	category:'System-Compiler'
!

!ObjectFileHandle class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1995 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    not for public use - used by ObjectFileLoader to keep track of loaded modules,
    associating objectFile names and moduleIDs to classes/methods.
    This is required to be able to fixup classes, methods and function references
    when an image is restarted and the dynamic objects are reloaded.

    [see also:]
        ExternalFunction
        ObjectFileLoader

    [author:]
        Claus Gittinger
"
! !

!ObjectFileHandle methodsFor:'accessing'!

classes
    "return the classes"

    weakClassRefs isNil ifTrue:[^nil].
    ^ weakClassRefs asArray 

    "Created: 14.9.1995 / 21:13:13 / claus"
!

classes:aCollectionOfClasses 
    "set the classes collection"

    weakClassRefs := WeakArray withAll:aCollectionOfClasses.
    weakClassRefs addDependent:self.
    handleType := #classLibraryObject 

    "Created: 14.9.1995 / 21:13:13 / claus"
!

functions
    "return the functions which were already extracted from the module."

    weakFunctionRefs isNil ifTrue:[^nil].
    ^ weakFunctionRefs asArray select:[:f | f notNil]

    "Created: 12.7.1996 / 15:33:09 / cg"
    "Modified: 12.7.1996 / 15:38:00 / cg"
!

method
    "return the method"

    weakMethodRef isNil ifTrue:[^ nil].
    ^ weakMethodRef at:1 

    "Created: 14.9.1995 / 21:13:13 / claus"
!

method:something
    "set the method"

    weakMethodRef := WeakArray with:something.
    weakMethodRef addDependent:self.
    handleType := #methodObject.

    "Created: 14.9.1995 / 21:13:13 / claus"
!

moduleID
    "return moduleID"

    ^ moduleID

    "Created: 14.9.1995 / 21:13:12 / claus"
!

moduleID:something
    "set moduleID"

    moduleID := something.

    "Created: 14.9.1995 / 21:13:12 / claus"
!

pathName
    "return pathName"

    ^ pathName

    "Created: 14.9.1995 / 21:13:12 / claus"
!

pathName:something
    "set pathName"

    pathName := something.

    "Created: 14.9.1995 / 21:13:12 / claus"
!

sysHandle1
    "return sysHandle1"

    ^ sysHandle1

    "Created: 14.9.1995 / 21:13:12 / claus"
!

sysHandle1:something
    "set sysHandle1"

    sysHandle1 := something.

    "Created: 14.9.1995 / 21:13:12 / claus"
!

sysHandle2
    "return sysHandle2"

    ^ sysHandle2

    "Created: 14.9.1995 / 21:13:12 / claus"
!

sysHandle2:something
    "set sysHandle2"

    sysHandle2 := something.

    "Created: 14.9.1995 / 21:13:12 / claus"
! !

!ObjectFileHandle methodsFor:'change & update'!

update:something with:aParameter from:changedObject
    "my method/class object was collected - unload the underlying objectfile"

    something == weakMethodRef ifTrue:[
	weakMethodRef nilAllCorpsesAndDo:[:idx |].
    ].

    something == weakClassRefs ifTrue:[
	weakClassRefs nilAllCorpsesAndDo:[:idx |].
    ].

    self isObsolete ifTrue:[
	('OBJFLOADER: unloading ' , pathName , '  (method/classes were garbageCollected)') infoPrintNL.
	ObjectFileLoader unloadObjectFile:pathName
    ].

    "Created: 5.12.1995 / 18:05:08 / cg"
! !

!ObjectFileHandle methodsFor:'copying'!

postCopy
    "flush module handles of the copy"

    sysHandle1 := sysHandle2 := moduleID := nil

    "Created: 5.12.1995 / 21:10:49 / cg"
    "Modified: 25.4.1996 / 09:43:53 / cg"
! !

!ObjectFileHandle methodsFor:'printing'!

printOn:aStream
    "append a printed representation of the receiver to aStream"

    aStream nextPutAll:self class name; nextPutAll:'(handle=<'. 
    sysHandle1 printOn:aStream. 
    aStream nextPutAll:' '. 
    sysHandle2 printOn:aStream. 
    aStream nextPutAll:'> path='''. 
    pathName printOn:aStream. 
    aStream nextPutAll:''' id='. 
    moduleID printOn:aStream. 
    aStream nextPutAll:' type='. 
    handleType printOn:aStream.
    aStream nextPutAll:' method=<'.
    self method printOn:aStream.
    aStream nextPutAll:'> classes=<'.
    self classes printOn:aStream.
    aStream nextPutAll:'>'.

    aStream nextPutAll:')'.

    "Modified: 25.4.1996 / 09:44:08 / cg"
! !

!ObjectFileHandle methodsFor:'queries'!

getFunction:aString
    "return a function object for an entry in the module represented by
     the receiver."

    |address f slot sz nW|

    address := ObjectFileLoader getFunction:aString from:self.
    address isNil ifTrue:[
        ^ nil
    ].

    weakFunctionRefs isNil ifTrue:[
        handleType := #functionObject.
        f := ExternalFunction basicNew code:address.
        f setName:aString moduleHandle:self.
        weakFunctionRefs := WeakArray with:f.
        ^ f.
    ].

    weakFunctionRefs nonNilElementsDo:[:anExternalFunction |
        anExternalFunction code = address ifTrue:[
            ^ anExternalFunction
        ]
    ].

    f := ExternalFunction basicNew code:address.
    f setName:aString moduleHandle:self.

    slot := weakFunctionRefs identityIndexOf:nil.
    slot ~~ 0 ifTrue:[
        weakFunctionRefs at:slot put:f.
        ^ f.
    ].

    sz := weakFunctionRefs size.
    nW := WeakArray new:(sz + 2).
    nW replaceFrom:1 to:sz with:weakFunctionRefs startingAt:1.
    nW at:sz+1 put:f.

    weakFunctionRefs := nW.
    ^ f

    "Created: 12.7.1996 / 14:59:09 / cg"
    "Modified: 18.10.1996 / 20:59:54 / cg"
!

hasUndefinedSymbols
    "return true, if the module has any undefined symbols.
     The only system, which allows modules with undefined symbols to be
     loaded is (currently) the linux a.out version."

    ^ ObjectFileLoader hasUndefinedSymbolsIn:self

    "Created: 12.7.1996 / 14:59:09 / cg"
!

isClassLibHandle
    "return true, if I am a handle for a class library"

    ^ handleType == #classLibraryObject 
!

isForCollectedObject
    "return true, if my clases/method has already been garbage collected"

    |ref|

    handleType == #classLibraryObject ifTrue:[
        ^ (weakClassRefs findFirst:[:x | x notNil and:[x ~~ 0]]) == 0
    ].
    handleType == #methodObject ifTrue:[
	ref := weakMethodRef at:1.
        ^ ref isNil or:[ref == 0]
    ].
    handleType == #functionObject ifTrue:[
        ^ (weakFunctionRefs findFirst:[:x | x notNil and:[x ~~ 0]]) == 0
    ].

    ^ false

    "Created: 6.12.1995 / 17:46:58 / cg"
    "Modified: 12.7.1996 / 15:42:38 / cg"
!

isFunctionObjectHandle
    "return true, if I am a handle for a c-code (or other) function object file"

    ^ handleType == #functionObject

    "Created: 12.7.1996 / 15:35:33 / cg"
    "Modified: 12.7.1996 / 17:25:31 / cg"
!

isMethodHandle
    "return true, if I am a handle for a single compiled method"

    ^ handleType == #methodObject
!

isObsolete
    "return true, if my clases/method has already been removed from
     the image. I.e. if the object file can be unloaded without danger."

    |ref|

    moduleID notNil ifTrue:[
        handleType == #classLibraryObject ifTrue:[
            ^ (weakClassRefs findFirst:[:x | x notNil and:[x ~~ 0]]) == 0
        ].
        handleType == #methodObject ifTrue:[
	    ref := weakMethodRef at:1.
            ^ (ref isNil or:[ref == 0])
        ].
        handleType == #functionObject ifTrue:[
            ^ (weakFunctionRefs findFirst:[:x | x notNil and:[x ~~ 0]]) == 0
        ].
    ].
    ^ false

    "Modified: 12.7.1996 / 15:42:21 / cg"
! !

!ObjectFileHandle class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libcomp/Attic/ObjFHandle.st,v 1.20 1996-10-22 19:34:16 cg Exp $'
! !