JavaNativeMemory.st
changeset 749 e898eaeff091
child 2152 1cbdfbcc685c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/JavaNativeMemory.st	Fri Aug 19 08:58:19 2011 +0000
@@ -0,0 +1,213 @@
+"
+ 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_SVN
+    ^ '$Id$'
+! !
\ No newline at end of file