--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/JavaNativeMemory.st Fri Apr 08 12:02:36 2011 +0000
@@ -0,0 +1,126 @@
+"{ Package: 'stx:libjava' }"
+
+Object subclass:#JavaNativeMemory
+ instanceVariableNames:'addresses chunks'
+ classVariableNames:''
+ poolDictionaries:''
+ category:'Languages-Java-Support'
+!
+
+
+!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$'
+! !