Just use the ByteBuffer address to directly unmap the memory.
authorNot Zed <notzed@gmail.com>
Sun, 26 Jan 2020 07:53:59 +0000 (18:23 +1030)
committerNot Zed <notzed@gmail.com>
Sun, 26 Jan 2020 07:53:59 +0000 (18:23 +1030)
Thanks go to Maurizio Cimadamore <maurizio.cimadamore@oracle.com> for the
suggestion.

src/notzed.zcl/classes/au/notzed/zcl/CLCommandQueue.java
src/notzed.zcl/tests/au/notzed/zcl/CLBufferTest.java

index f878bb8..063b480 100644 (file)
@@ -1343,22 +1343,6 @@ public class CLCommandQueue extends CLExtendable {
                }
        }
 
-       private MemoryAddress getMap(ByteBuffer bb) {
-               synchronized (maps) {
-                       // Note this can't use a hashtable as ByteBuffer
-                       // hashes on state like position.
-                       for (int i=0;i<maps.size();i++) {
-                               MapData d = maps.get(i);
-
-                               if (d.buffer == bb) {
-                                       maps.remove(i);
-                                       return d.raw;
-                               }
-                       }
-               }
-               throw new IllegalArgumentException();
-       }
-
        public ByteBuffer enqueueMapBuffer(CLBuffer buffer, boolean blocking,
                                           long flags,
                                           long offset,
@@ -1379,14 +1363,7 @@ public class CLCommandQueue extends CLExtendable {
                        if (res != 0)
                                throw new CLException(res);
 
-                       // Need to map the segment and track it separately
-                       ByteBuffer bb = Memory.ofNative(cmap, size).asByteBuffer();
-
-                       addMap(cmap, bb);
-
-                       info.post(event);
-
-                       return bb;
+                       return Memory.ofNative(cmap, size).asByteBuffer();
                } catch (CLException | RuntimeException | Error t) {
                        throw t;
                } catch (Throwable t) {
@@ -1443,14 +1420,7 @@ public class CLCommandQueue extends CLExtendable {
                                    ?                     stride * region[1] + region[0] // 2D
                                    : slice * region[2] + stride * region[1] + region[0]; // 3D
 
-                       // Need to map the segment and track it separately
-                       ByteBuffer bb = Memory.ofNative(cmap, size).asByteBuffer();
-
-                       addMap(cmap, bb);
-
-                       info.post(event);
-
-                       return bb;
+                       return Memory.ofNative(cmap, size).asByteBuffer();
                } catch (CLException | RuntimeException | Error t) {
                        throw t;
                } catch (Throwable t) {
@@ -1461,7 +1431,7 @@ public class CLCommandQueue extends CLExtendable {
        public void enqueueUnmapMemObject(CLMemory mem, ByteBuffer mapped,
                                          CLEventList wait,
                                          CLEventList event) throws CLException {
-               MemoryAddress cmap = getMap(mapped);
+               MemoryAddress cmap = MemorySegment.ofByteBuffer(mapped).baseAddress();
 
                try (Allocator frame = Memory.stack()) {
                        EventInfo info = new EventInfo(frame, wait, event);
index 6cf19a7..2359ee0 100644 (file)
@@ -247,6 +247,8 @@ public class CLBufferTest {
                }
        }
 
+       // clcommandqueue no longer tracks the memory so double-free is at your own risk.
+       @Ignore
        @Test
        public void testMap2() throws Exception {
                System.out.println("mapBuffer double unmap");