ExternalAddress.st
author Claus Gittinger <cg@exept.de>
Thu, 20 Mar 1997 21:18:28 +0100
changeset 2472 00e7a1b3b07f
parent 1960 0425b66d2226
child 2869 6fcc9aafb7c4
permissions -rw-r--r--
better distribution of the hash-key (look for zero-bits)

"
 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:#ExternalAddress
	instanceVariableNames:'address*'
	classVariableNames:''
	poolDictionaries:''
	category:'System-Support'
!

!ExternalAddress 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
"
    Instances of this class represent external (non-Smalltalk) addresses.
    They are only useful to represent handles as returned by C functions
    as smalltalk objects. For example, Window- or WidgetIDs (which are actually
    32 bit pointers) should be represented this way.
    (you should not use SmallIntegers for this, since they can only represent 31
     bits; LargeIntegers could be used in theory, but it is not a very good style
     to do so, since it makes things a bit cryptic - having ExternalAddresses
     around makes things pretty clear in inspectors etc.).

    There is not much you can do with ExternalAddresses on the smalltalk level;
    creation/use is mostly in primitive C-code via 
       __MKEXTERNALADDRESS(voidPtr) and __ExternalAddressVal(obj).

    ExternallAddresses are much like ExternalBytes - however, the latter
    allow you to access bytes via indexed at:/at:put: messages.
    ExternalAddresses do not allow such accesses (they are meant to remain
    anonymous).

    [author:]
        Claus Gittinger

    [see also:]
        ByteArray
        ExternalBytes ExternalFunction
        ( how to write primitive code :html: programming/primitive.html )
"
!

examples
"
    To pass some C-pointer from primitive code to smalltalk:

	...
	RETURN (__MKEXTERNALADDRESS(foo));

    pass it back to C and use it there:

	...
	if (__isExternalAddress(anExternalAddress)) {
	    ptr = __externalAddressVal(anExternalAddress));
	}
	...

    concrete example:

	test1
	%{
	    static char foo[] = {'h', 'e' ,'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '\n'};

	    RETURN (__MKEXTERNALADDRESS(foo));
	%}

	test2:anExternalAddress
	%{
	    if (__isExternalAddress(anExternalAddress)) {
		printf(__externalAddressVal(anExternalAddress));
		RETURN (self);
	    }
	%}
"
! !

!ExternalAddress class methodsFor:'queries'!

isBuiltInClass
    "return true if this class is known by the run-time-system.
     Here, true is returned."

    ^ true

    "Modified: 23.4.1996 / 15:57:53 / cg"
! !

!ExternalAddress methodsFor:'accessing'!

address
    "return the start address as an integer"

%{  /* NOCONTEXT */

    unsigned addr;

    addr = (unsigned)__INST(address_);
    RETURN ( __MKUINT(addr));
%}
! !

!ExternalAddress methodsFor:'comparing'!

= anExternalAddress
    "return true, if two externalAddress instance represent the same
     C pointer"

%{  /* NOCONTEXT */
    if (__isExternalAddress(anExternalAddress)
     && (__externalAddressVal(self) == __externalAddressVal(anExternalAddress))) {
	RETURN (true);
    }
%}.
    ^ false
!

hash
    "return a number useful for hashing"

%{  /* NOCONTEXT */
    unsigned addr = (unsigned) __externalAddressVal(self);

    if (addr > _MAX_INT) {
	if ((addr & 1) == 0) {
	    addr >>= 1;
	}
	if (addr > _MAX_INT) {
	    if ((addr & 1) == 0) {
		addr >>= 1;
	    }
	}
	if (addr > _MAX_INT) {
	    addr &= _MAX_INT;
	}
    } else {
	addr <<= 1;
	if (addr <= _MAX_INT) {
	    addr <<= 1;
	    if (addr <= _MAX_INT) {
		addr <<= 1;
	    }
	}
	addr >>= 1;
    }
    RETURN (__MKSMALLINT(addr));
%}
! !

!ExternalAddress methodsFor:'converting'!

asExternalBytes
    "return an ExternalBytes object pointing to where the receiver points to.
     Use of this is not recommended; primitives which return externalAddresses
     dont think that access to the memory is required/useful, while primitives
     which do think so should return an externalBytes instance right away."

    ^ ExternalBytes address:(self address)
! !

!ExternalAddress methodsFor:'printing & storing'!

displayString
    ^ 'ExternalAddress at:' , (self address printStringRadix:16)
! !

!ExternalAddress class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/ExternalAddress.st,v 1.7 1997-03-20 20:18:28 cg Exp $'
! !