ObjectFileHandle.st
author Stefan Vogel <sv@exept.de>
Tue, 21 Jun 2005 15:18:00 +0200
changeset 1603 4243be479e4f
parent 1554 8e923008be65
child 1614 894cf7de352c
permissions -rw-r--r--
more FFI (foreign function interface) support - still incomplete

"
 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.
"

"{ Package: 'stx:libcomp' }"

Object subclass:#ObjectFileHandle
	instanceVariableNames:'sysHandle1 sysHandle2 pathName moduleID handleType weakMethodRef
		weakClassRefs weakFunctionRefs snapshot'
	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 class methodsFor:'change & update'!

preSnapshot
    "about to write a snapshot.
     Mark all the current instances as beeing snapshotted"

    self allInstancesDo:[:i| i snapshot:true].
! !

!ObjectFileHandle methodsFor:'accessing'!

classes
    "return the classes"

    weakClassRefs isNil ifTrue:[^ #()].
    ^ 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:[^ #()].
    ^ weakFunctionRefs asArray select:[:f | f notNil and:[f ~~ 0]]

    "Created: 12.7.1996 / 15:33:09 / cg"
    "Modified: 30.1.1997 / 20:54:54 / cg"
!

makeClassLibHandle
    "make this a classLibHandle - must be done manually when init failed for proper de-registration
     later."

    handleType notNil ifTrue:[
	self error:'should not happen'.
    ].
    handleType := #classLibraryObject 
!

method
    "return the method (or nil, if I am not a method-file handle)"

    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"
!

methods
    "return the collection of methods (or an empty collection,
     if I am not a method-file handle)"

    |m|

    (m := self method) isNil ifTrue:[^ #()].
    "/ TODO: this will return a real collection, when method-only
    "/ binaries remember that relevant information ...
    ^ Array with:m
!

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"
!

snapshot
    "return the value of the instance variable 'snapshot' (automatically generated)"

    ^ snapshot
!

snapshot:something
    "set the value of the instance variable 'snapshot' (automatically generated)"

    snapshot := something.
!

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:'actions'!

unload
    "unload the object file represented by me"

    ObjectFileLoader unloadDynamicObject:self
! !

!ObjectFileHandle methodsFor:'change & update'!

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

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

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

    self isObsolete ifTrue:[
        weakFunctionRefs notNil ifTrue:[
            weakFunctionRefs removeDependent:self.
            weakFunctionRefs := nil.
        ].
        weakClassRefs notNil ifTrue:[
            weakClassRefs removeDependent:self.
            weakClassRefs := nil.
        ].
        weakMethodRef notNil ifTrue:[
            weakMethodRef removeDependent:self.
            weakMethodRef := nil.
        ].
        ('ObjectFileHandle [info]: unloading ' , pathName , '  (method/classes were garbageCollected)') infoPrintCR.
        ObjectFileLoader unloadObjectFile:pathName.
        snapshot == true ifFalse:[
            "This object file has never been saved in a snapshot,
             so it can be savely removed"

            pathName asFilename remove.
        ].
    ].

    "Created: 5.12.1995 / 18:05:08 / cg"
    "Modified: 30.1.1997 / 20:57:56 / 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 & storing'!

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"
!

getFunctionAddress:aString into:anExternalFunction
    "file the code address of the external function named aString into anExternalFunction."

    |address f slot sz nW|

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

    anExternalFunction code:address.
    weakFunctionRefs isNil ifTrue:[
        handleType := #functionObject.
        weakFunctionRefs := WeakArray with:f.
        ^ address.
    ].

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

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

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

    weakFunctionRefs := nW.
    ^ address
!

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:[
        weakMethodRef isNil ifTrue:[^ true].
        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"
!

isUnknownHandle
    "return true, if it is not (yet) known, what type of
     file is represented by the handle. (i.e. when loaded, but not yet
     registered/initialized)"

    ^ handleType isNil
!

package
    "retrieve the packageID of the class-library handle"

    |cls mthd|

    "/ sigh - for now, the package must be extracted from the first classes/methods
    "/ package.

    self isClassLibHandle ifTrue:[
        cls := self classes firstIfEmpty:nil.
        cls notNil ifTrue:[^ cls package]
    ].
    self isMethodHandle ifTrue:[
        cls := self methods firstIfEmpty:nil.
        mthd notNil ifTrue:[^ mthd package]
    ].
    ^ nil
! !

!ObjectFileHandle class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libcomp/ObjectFileHandle.st,v 1.31 2005-06-21 13:18:00 stefan Exp $'
! !