JavaNativeMemory.st
author Claus Gittinger <cg@exept.de>
Thu, 24 Nov 2011 12:54:24 +0100
changeset 2290 cd61fd0b66ac
parent 2265 c3b1b1c03533
child 2353 fa7400d022a0
permissions -rw-r--r--
fixed: #version_SVN ($ to §)

"
 COPYRIGHT (c) 1996-2011 by Claus Gittinger
 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
                            SWING Research Group, Czech Technical University in Prague

 Parts of the code written by Claus Gittinger are under following
 license:

 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.

 Parts of the code written at SWING Reasearch Group [1] are MIT licensed:

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the 'Software'), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.

 [1] Code written at SWING Research Group contain a signature
     of one of the above copright owners.
"
"{ Package: 'stx:libjava' }"

Object subclass:#JavaNativeMemory
	instanceVariableNames:'addresses chunks'
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Java-Support'
!

!JavaNativeMemory class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1996-2011 by Claus Gittinger
 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
                            SWING Research Group, Czech Technical University in Prague

 Parts of the code written by Claus Gittinger are under following
 license:

 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.

 Parts of the code written at SWING Reasearch Group [1] are MIT licensed:

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the 'Software'), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.

 [1] Code written at SWING Research Group contain a signature
     of one of the above copright owners.

"
! !

!JavaNativeMemory class methodsFor:'instance creation'!

new
    "return an initialized instance"

    ^ self basicNew initialize.
! !

!JavaNativeMemory methodsFor:'accessing'!

byteAt:address
    |i chunk offset|

    addresses size == 0 ifTrue:[
        self errorAccessingUnallocatedMemory:address
    ].
    i := 1.
    [
        i <= (addresses size - 1) and:[ (addresses at:i + 1) <= address ]
    ] whileTrue:[ i := i + 1 ].
    offset := address - (addresses at:i).
    chunk := chunks at:i.
    (offset > chunk size) ifTrue:[
        self errorAccessingUnallocatedMemory:address
    ].
    ^chunk at: offset + 1

    "Created: / 09-12-2010 / 17:32:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

longAt:address put:value 
    |i chunk offset|

    addresses size == 0 ifTrue:[
        self errorAccessingUnallocatedMemory:address
    ].
    i := 1.
    [
        i <= (addresses size - 1) and:[ (addresses at:i + 1) <= address ]
    ] whileTrue:[ i := i + 1 ].
    offset := address - (addresses at:i).
    chunk := chunks at:i.
    (offset > chunk size) ifTrue:[
        self errorAccessingUnallocatedMemory:address
    ].
    chunk longLongAt: offset + 1 put: value

    "Created: / 09-12-2010 / 17:32:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaNativeMemory methodsFor:'allocation/deallocation'!

free: address

    "Frees the memory"

    | index |
    address = 0 ifTrue:[^self].

    index := addresses 
                indexOf: address 
                ifAbsent:[self error:'Never allocated'].

    (chunks at: index) 
                ifNil:[self error: 'Freed twice'].

    chunks at: index put: nil.

    "Created: / 07-12-2010 / 23:33:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

malloc: size

    "Allocate new chunk of memory, size bytes long"

    | address |
    self assert: addresses size == chunks size.
    addresses size > 0 
            ifTrue: [address := addresses last + chunks last size ]
            ifFalse:[address := 0].
    address := address + (Random nextIntegerBetween: 128 and: 256).

    addresses add: address.
    chunks add: (ByteArray new: size).

    ^address

    "Created: / 07-12-2010 / 23:25:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaNativeMemory methodsFor:'error reporting'!

errorAccessingUnallocatedMemory:arg
    self shouldImplement

    "Created: / 07-12-2010 / 23:40:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaNativeMemory methodsFor:'initialization'!

initialize
    "Invoked when a new instance is created."

    addresses := OrderedCollection new.
    chunks := OrderedCollection new.

    "/ super initialize.   -- commented since inherited method does nothing

    "Modified: / 07-12-2010 / 23:09:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaNativeMemory class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libjava/JavaNativeMemory.st,v 1.3 2011-11-24 11:53:30 cg Exp $'
!

version_CVS
    ^ '$Header: /cvs/stx/stx/libjava/JavaNativeMemory.st,v 1.3 2011-11-24 11:53:30 cg Exp $'
!

version_SVN
    ^ '§Id: JavaNativeMemory.st,v 1.1 2011/08/18 19:06:53 vrany Exp §'
! !