5656import com .oracle .svm .core .feature .InternalFeature ;
5757import com .oracle .svm .core .nmt .NativeMemoryTracking ;
5858import com .oracle .svm .core .nmt .NmtCategory ;
59- import com .oracle .svm .core .nmt .NmtVirtualMemoryData ;
59+ import com .oracle .svm .core .nmt .NmtPreImageHeapData ;
60+ import com .oracle .svm .core .nmt .NmtPreImageHeapDataAccess ;
6061import com .oracle .svm .core .os .VirtualMemoryProvider ;
6162import com .oracle .svm .core .posix .headers .Unistd ;
6263import com .oracle .svm .core .util .PointerUtils ;
@@ -112,14 +113,8 @@ public UnsignedWord getGranularity() {
112113
113114 @ Override
114115 @ Uninterruptible (reason = "Called from uninterruptible code." , mayBeInlined = true )
115- public Pointer reserve (UnsignedWord nbytes , UnsignedWord alignment , boolean executable ) {
116- return reserve0 (nbytes , alignment , executable , WordFactory .nullPointer (), NmtCategory .Internal );
117- }
118-
119- @ Override
120- @ Uninterruptible (reason = "Called from uninterruptible code." , mayBeInlined = true )
121- public Pointer reserve (UnsignedWord nbytes , UnsignedWord alignment , boolean executable , NmtVirtualMemoryData nmtData ) {
122- return reserve0 (nbytes , alignment , executable , nmtData , NmtCategory .Internal );
116+ public Pointer reserve (UnsignedWord nbytes , UnsignedWord alignment , boolean executable , NmtPreImageHeapData nmtData ) {
117+ return reserve0 (nbytes , alignment , executable , nmtData , NmtCategory .ImageHeap );
123118 }
124119
125120 @ Override
@@ -129,7 +124,7 @@ public Pointer reserve(UnsignedWord nbytes, UnsignedWord alignment, boolean exec
129124 }
130125
131126 @ Uninterruptible (reason = "Called from uninterruptible code." , mayBeInlined = true )
132- private Pointer reserve0 (UnsignedWord nbytes , UnsignedWord alignment , boolean executable , NmtVirtualMemoryData nmtData , NmtCategory category ) {
127+ private Pointer reserve0 (UnsignedWord nbytes , UnsignedWord alignment , boolean executable , NmtPreImageHeapData nmtData , NmtCategory category ) {
133128 if (nbytes .equal (0 )) {
134129 return WordFactory .nullPointer ();
135130 }
@@ -151,8 +146,7 @@ private Pointer reserve0(UnsignedWord nbytes, UnsignedWord alignment, boolean ex
151146 if (nmtData .isNull ()) {
152147 NativeMemoryTracking .trackReserve (mappingBegin , mappingSize , category );
153148 } else {
154- nmtData .setReserved (nmtData .getReserved ().add (mappingSize ));
155- nmtData .setBaseAddr (mappingBegin );
149+ NmtPreImageHeapDataAccess .enqueueReserve (nmtData , mappingBegin , mappingSize , category );
156150 }
157151 return mappingBegin ;
158152 }
@@ -173,24 +167,16 @@ private Pointer reserve0(UnsignedWord nbytes, UnsignedWord alignment, boolean ex
173167 }
174168 if (nmtData .isNull ()) {
175169 NativeMemoryTracking .trackReserve (begin , mappingSize .subtract (unmappedSize ), category );
176- // NativeMemoryTracking.trackReserve(begin, nbytes, category); // *** didn't fix problem.
177170 } else {
178- nmtData .setReserved (nmtData .getReserved ().add (mappingSize .subtract (unmappedSize )));
179- nmtData .setBaseAddr (begin );
171+ NmtPreImageHeapDataAccess .enqueueReserve (nmtData , begin , mappingSize .subtract (unmappedSize ), category );
180172 }
181173 return begin ;
182174 }
183175
184176 @ Override
185177 @ Uninterruptible (reason = "Called from uninterruptible code." , mayBeInlined = true )
186- public Pointer mapFile (PointerBase start , UnsignedWord nbytes , WordBase fileHandle , UnsignedWord offset , int access ) {
187- return mapFile0 (start , nbytes , fileHandle , offset , access , WordFactory .nullPointer (), NmtCategory .Internal );
188- }
189-
190- @ Override
191- @ Uninterruptible (reason = "Called from uninterruptible code." , mayBeInlined = true )
192- public Pointer mapFile (PointerBase start , UnsignedWord nbytes , WordBase fileHandle , UnsignedWord offset , int access , NmtVirtualMemoryData nmtData ) {
193- return mapFile0 (start , nbytes , fileHandle , offset , access , nmtData , NmtCategory .Internal );
178+ public Pointer mapFile (PointerBase start , UnsignedWord nbytes , WordBase fileHandle , UnsignedWord offset , int access , NmtPreImageHeapData nmtData ) {
179+ return mapFile0 (start , nbytes , fileHandle , offset , access , nmtData , NmtCategory .ImageHeap );
194180 }
195181
196182 @ Override
@@ -200,7 +186,7 @@ public Pointer mapFile(PointerBase start, UnsignedWord nbytes, WordBase fileHand
200186 }
201187
202188 @ Uninterruptible (reason = "Called from uninterruptible code." , mayBeInlined = true )
203- private Pointer mapFile0 (PointerBase start , UnsignedWord nbytes , WordBase fileHandle , UnsignedWord offset , int access , NmtVirtualMemoryData nmtData , NmtCategory category ) {
189+ private Pointer mapFile0 (PointerBase start , UnsignedWord nbytes , WordBase fileHandle , UnsignedWord offset , int access , NmtPreImageHeapData nmtData , NmtCategory category ) {
204190 if ((start .isNonNull () && !isAligned (start )) || nbytes .equal (0 )) {
205191 return WordFactory .nullPointer ();
206192 }
@@ -212,22 +198,16 @@ private Pointer mapFile0(PointerBase start, UnsignedWord nbytes, WordBase fileHa
212198 int fd = (int ) fileHandle .rawValue ();
213199 Pointer result = mmap (start , nbytes , accessAsProt (access ), flags , fd , offset .rawValue ());
214200 if (result .notEqual (MAP_FAILED ())) {
215- commitAndMaybeReserve (result , start , nbytes , nmtData , category );
201+ trackCommitAndMaybeReserve (result , start , nbytes , nmtData , category );
216202 return result ;
217203 }
218204 return WordFactory .nullPointer ();
219205 }
220206
221207 @ Override
222208 @ Uninterruptible (reason = "Called from uninterruptible code." , mayBeInlined = true )
223- public Pointer commit (PointerBase start , UnsignedWord nbytes , int access ) {
224- return commit0 (start , nbytes , access , WordFactory .nullPointer (), NmtCategory .Internal );
225- }
226-
227- @ Override
228- @ Uninterruptible (reason = "Called from uninterruptible code." , mayBeInlined = true )
229- public Pointer commit (PointerBase start , UnsignedWord nbytes , int access , NmtVirtualMemoryData nmtData ) {
230- return commit0 (start , nbytes , access , nmtData , NmtCategory .Internal );
209+ public Pointer commit (PointerBase start , UnsignedWord nbytes , int access , NmtPreImageHeapData nmtData ) {
210+ return commit0 (start , nbytes , access , nmtData , NmtCategory .ImageHeap );
231211 }
232212
233213 @ Override
@@ -237,7 +217,7 @@ public Pointer commit(PointerBase start, UnsignedWord nbytes, int access, NmtCat
237217 }
238218
239219 @ Uninterruptible (reason = "Called from uninterruptible code." , mayBeInlined = true )
240- private Pointer commit0 (PointerBase start , UnsignedWord nbytes , int access , NmtVirtualMemoryData nmtData , NmtCategory category ) {
220+ private Pointer commit0 (PointerBase start , UnsignedWord nbytes , int access , NmtPreImageHeapData nmtData , NmtCategory category ) {
241221 if ((start .isNonNull () && !isAligned (start )) || nbytes .equal (0 )) {
242222 return WordFactory .nullPointer ();
243223 }
@@ -253,28 +233,26 @@ private Pointer commit0(PointerBase start, UnsignedWord nbytes, int access, NmtV
253233 /* The memory returned by mmap is guaranteed to be zeroed. */
254234 final Pointer result = mmap (start , nbytes , accessAsProt (access ), flags , NO_FD , NO_FD_OFFSET );
255235 if (result .notEqual (MAP_FAILED ())) {
256- commitAndMaybeReserve (result , start , nbytes , nmtData , category );
236+ trackCommitAndMaybeReserve (result , start , nbytes , nmtData , category );
257237 return result ;
258238 }
259239 return nullPointer ();
260240 }
261241
262242 @ Uninterruptible (reason = "Called from uninterruptible code." , mayBeInlined = true )
263- private void commitAndMaybeReserve (PointerBase baseAddr , PointerBase start , UnsignedWord nbytes , NmtVirtualMemoryData nmtData , NmtCategory category ) {
243+ private static void trackCommitAndMaybeReserve (PointerBase baseAddr , PointerBase start , UnsignedWord nbytes , NmtPreImageHeapData nmtData , NmtCategory category ) {
264244 // Account for possible reserve before commit
265245 if (start .isNull ()) {
266246 if (nmtData .isNull ()) {
267247 NativeMemoryTracking .trackReserve (baseAddr , nbytes , category );
268248 } else {
269- nmtData .setReserved (nmtData .getReserved ().add (nbytes ));
270- nmtData .setBaseAddr (baseAddr );
249+ NmtPreImageHeapDataAccess .enqueueReserve (nmtData , baseAddr , nbytes , category );
271250 }
272251 }
273252 if (nmtData .isNull ()) {
274253 NativeMemoryTracking .trackCommit (baseAddr , nbytes , category );
275254 } else {
276- nmtData .setCommitted (nmtData .getCommitted ().add (nbytes ));
277- nmtData .setBaseAddr (baseAddr );
255+ NmtPreImageHeapDataAccess .enqueueCommit (nmtData , baseAddr , nbytes , category );
278256 }
279257 }
280258
@@ -305,6 +283,12 @@ public int uncommit(PointerBase start, UnsignedWord nbytes) {
305283 @ Override
306284 @ Uninterruptible (reason = "Called from uninterruptible code." , mayBeInlined = true )
307285 public int free (PointerBase start , UnsignedWord nbytes ) {
286+ return free (start , nbytes , WordFactory .nullPointer ());
287+ }
288+
289+ @ Override
290+ @ Uninterruptible (reason = "Called from uninterruptible code." , mayBeInlined = true )
291+ public int free (PointerBase start , UnsignedWord nbytes , NmtPreImageHeapData nmtData ) {
308292 if (start .isNull () || !isAligned (start ) || nbytes .equal (0 )) {
309293 return -1 ;
310294 }
@@ -313,9 +297,8 @@ public int free(PointerBase start, UnsignedWord nbytes) {
313297 Pointer mappingBegin = PointerUtils .roundDown (start , granularity );
314298 UnsignedWord mappingSize = UnsignedUtils .roundUp (nbytes , granularity );
315299 int ret = munmap (mappingBegin , mappingSize );
316- if (ret == 0 ) {
317- NativeMemoryTracking .trackFree (start , mappingSize ); // *** use start here, not
318- // mappingBegin
300+ if (ret == 0 && nmtData .isNull ()) {
301+ NativeMemoryTracking .trackFree (start );
319302 }
320303 return ret ;
321304 }
0 commit comments