752
|
1 |
"{ Package: 'stx:libjava' }"
|
|
2 |
|
|
3 |
Object subclass:#JavaNativeMemory
|
|
4 |
instanceVariableNames:'addresses chunks'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Languages-Java-Support'
|
|
8 |
!
|
|
9 |
|
|
10 |
|
|
11 |
!JavaNativeMemory class methodsFor:'instance creation'!
|
|
12 |
|
|
13 |
new
|
|
14 |
"return an initialized instance"
|
|
15 |
|
|
16 |
^ self basicNew initialize.
|
|
17 |
! !
|
|
18 |
|
|
19 |
!JavaNativeMemory methodsFor:'accessing'!
|
|
20 |
|
|
21 |
byteAt:address
|
|
22 |
|i chunk offset|
|
|
23 |
|
|
24 |
addresses size == 0 ifTrue:[
|
|
25 |
self errorAccessingUnallocatedMemory:address
|
|
26 |
].
|
|
27 |
i := 1.
|
|
28 |
[
|
|
29 |
i <= (addresses size - 1) and:[ (addresses at:i + 1) <= address ]
|
|
30 |
] whileTrue:[ i := i + 1 ].
|
|
31 |
offset := address - (addresses at:i).
|
|
32 |
chunk := chunks at:i.
|
|
33 |
(offset > chunk size) ifTrue:[
|
|
34 |
self errorAccessingUnallocatedMemory:address
|
|
35 |
].
|
|
36 |
^chunk at: offset + 1
|
|
37 |
|
|
38 |
"Created: / 09-12-2010 / 17:32:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
39 |
!
|
|
40 |
|
|
41 |
longAt:address put:value
|
|
42 |
|i chunk offset|
|
|
43 |
|
|
44 |
addresses size == 0 ifTrue:[
|
|
45 |
self errorAccessingUnallocatedMemory:address
|
|
46 |
].
|
|
47 |
i := 1.
|
|
48 |
[
|
|
49 |
i <= (addresses size - 1) and:[ (addresses at:i + 1) <= address ]
|
|
50 |
] whileTrue:[ i := i + 1 ].
|
|
51 |
offset := address - (addresses at:i).
|
|
52 |
chunk := chunks at:i.
|
|
53 |
(offset > chunk size) ifTrue:[
|
|
54 |
self errorAccessingUnallocatedMemory:address
|
|
55 |
].
|
|
56 |
chunk longLongAt: offset + 1 put: value
|
|
57 |
|
|
58 |
"Created: / 09-12-2010 / 17:32:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
59 |
! !
|
|
60 |
|
|
61 |
!JavaNativeMemory methodsFor:'allocation/deallocation'!
|
|
62 |
|
|
63 |
free: address
|
|
64 |
|
|
65 |
"Frees the memory"
|
|
66 |
|
|
67 |
| index |
|
|
68 |
address = 0 ifTrue:[^self].
|
|
69 |
|
|
70 |
index := addresses
|
|
71 |
indexOf: address
|
|
72 |
ifAbsent:[self error:'Never allocated'].
|
|
73 |
|
|
74 |
(chunks at: index)
|
|
75 |
ifNil:[self error: 'Freed twice'].
|
|
76 |
|
|
77 |
chunks at: index put: nil.
|
|
78 |
|
|
79 |
"Created: / 07-12-2010 / 23:33:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
80 |
!
|
|
81 |
|
|
82 |
malloc: size
|
|
83 |
|
|
84 |
"Allocate new chunk of memory, size bytes long"
|
|
85 |
|
|
86 |
| address |
|
|
87 |
self assert: addresses size == chunks size.
|
|
88 |
addresses size > 0
|
|
89 |
ifTrue: [address := addresses last + chunks last size ]
|
|
90 |
ifFalse:[address := 0].
|
|
91 |
address := address + (Random nextIntegerBetween: 128 and: 256).
|
|
92 |
|
|
93 |
addresses add: address.
|
|
94 |
chunks add: (ByteArray new: size).
|
|
95 |
|
|
96 |
^address
|
|
97 |
|
|
98 |
"Created: / 07-12-2010 / 23:25:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
99 |
! !
|
|
100 |
|
|
101 |
!JavaNativeMemory methodsFor:'error reporting'!
|
|
102 |
|
|
103 |
errorAccessingUnallocatedMemory:arg
|
|
104 |
self shouldImplement
|
|
105 |
|
|
106 |
"Created: / 07-12-2010 / 23:40:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
107 |
! !
|
|
108 |
|
|
109 |
!JavaNativeMemory methodsFor:'initialization'!
|
|
110 |
|
|
111 |
initialize
|
|
112 |
"Invoked when a new instance is created."
|
|
113 |
|
|
114 |
addresses := OrderedCollection new.
|
|
115 |
chunks := OrderedCollection new.
|
|
116 |
|
|
117 |
"/ super initialize. -- commented since inherited method does nothing
|
|
118 |
|
|
119 |
"Modified: / 07-12-2010 / 23:09:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
120 |
! !
|
|
121 |
|
|
122 |
!JavaNativeMemory class methodsFor:'documentation'!
|
|
123 |
|
|
124 |
version_SVN
|
|
125 |
^ '$Id$'
|
|
126 |
! !
|