From d1a03d19398960d6e34232f29a30f1d569fba2a7 Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Sat, 6 Nov 2010 06:26:56 +0100
Subject: Fix buffer size: 64bit GLBufferSizeTracker, hash over addr+size, save
 fail-fast hash cache, ..

Relates to GlueGen 6b6b9b3b81cdc85b7260664ebec547756a6be5d7, branch sgothel_wip_fixes01.

Memory object size is ptrdiff_t, hence long (64bit).
The hash value must include size as well, otherwise boundaries cannot be verified. (security)
Double check hash collisions while adding a new MemoryObject.
---
 make/config/jogl/gl-impl-CustomJavaCode-gl4bc.java | 92 +++++++++++-----------
 1 file changed, 46 insertions(+), 46 deletions(-)

(limited to 'make/config/jogl/gl-impl-CustomJavaCode-gl4bc.java')

diff --git a/make/config/jogl/gl-impl-CustomJavaCode-gl4bc.java b/make/config/jogl/gl-impl-CustomJavaCode-gl4bc.java
index ff177ec94..6776feeb7 100644
--- a/make/config/jogl/gl-impl-CustomJavaCode-gl4bc.java
+++ b/make/config/jogl/gl-impl-CustomJavaCode-gl4bc.java
@@ -274,32 +274,7 @@ public boolean glIsPBOUnpackEnabled() {
     return checkUnpackPBOEnabled(false);
 }
 
-// Attempt to return the same ByteBuffer object from glMapBuffer if
-// the vertex buffer object's base address and size haven't changed
-private static class ARBVBOKey {
-  private long addr;
-  private int  capacity;
-
-  ARBVBOKey(long addr, int capacity) {
-    this.addr = addr;
-    this.capacity = capacity;
-  }
-
-  public int hashCode() {
-    return (int) addr;
-  }
-
-  public boolean equals(Object o) {
-    if ((o == null) || (!(o instanceof ARBVBOKey))) {
-      return false;
-    }
-
-    ARBVBOKey other = (ARBVBOKey) o;
-    return ((addr == other.addr) && (capacity == other.capacity));
-  }
-}
-
-private Map/*<ARBVBOKey, ByteBuffer>*/ arbVBOCache = new HashMap();
+private HashMap/*<MemoryObject>*/ arbMemCache = new HashMap();
 
 /** Entry point to C language function: <br> <code> LPVOID glMapBuffer(GLenum target, GLenum access); </code>    */
 public java.nio.ByteBuffer glMapBuffer(int target, int access) {
@@ -307,53 +282,78 @@ public java.nio.ByteBuffer glMapBuffer(int target, int access) {
   if (__addr_ == 0) {
     throw new GLException("Method \"glMapBuffer\" not available");
   }
-  int sz = bufferSizeTracker.getBufferSize(bufferStateTracker, target, this);
+  final long sz = bufferSizeTracker.getBufferSize(bufferStateTracker, target, this);
   if (0 == sz) {
     return null;
   }
-  long addr;
-  addr = dispatch_glMapBuffer(target, access, __addr_);
+  final long addr = dispatch_glMapBuffer(target, access, __addr_);
   if (0 == addr) {
     return null;
   }
-  ARBVBOKey key = new ARBVBOKey(addr, sz);
-  ByteBuffer _res = (ByteBuffer) arbVBOCache.get(key);
-  if (_res == null) {
-    _res = newDirectByteBuffer(addr, sz);
-    Buffers.nativeOrder(_res);
-    arbVBOCache.put(key, _res);
+  ByteBuffer buffer;
+  MemoryObject memObj0 = new MemoryObject(addr, sz); // object and key
+  MemoryObject memObj1 = MemoryObject.getOrAddSafe(arbMemCache, memObj0);
+  if(memObj0 == memObj1) {
+    // just added ..
+    if(null != memObj0.getBuffer()) {
+        throw new InternalError();
+    }
+    buffer = newDirectByteBuffer(addr, sz);
+    Buffers.nativeOrder(buffer);
+    memObj0.setBuffer(buffer);
+  } else {
+    // already mapped
+    buffer = memObj1.getBuffer();
+    if(null == buffer) {
+        throw new InternalError();
+    }
   }
-  _res.position(0);
-  return _res;
+  buffer.position(0);
+  return buffer;
 }
 
 /** Encapsulates function pointer for OpenGL function <br>: <code> LPVOID glMapBuffer(GLenum target, GLenum access); </code>    */
 native private long dispatch_glMapBuffer(int target, int access, long glProcAddress);
 
 /** Entry point to C language function: <code> GLvoid *  {@native glMapNamedBufferEXT}(GLuint buffer, GLenum access); </code> <br>Part of <code>GL_EXT_direct_state_access</code>   */
-public java.nio.ByteBuffer glMapNamedBufferEXT(int buffer, int access)  {
+public java.nio.ByteBuffer glMapNamedBufferEXT(int bufferName, int access)  {
   final long __addr_ = ((GL4bcProcAddressTable)_context.getGLProcAddressTable())._addressof_glMapNamedBufferEXT;
   if (__addr_ == 0) {
     throw new GLException("Method \"glMapNamedBufferEXT\" not available");
   }
-  int sz = bufferSizeTracker.getDirectStateBufferSize(buffer, this);
+  final long sz = bufferSizeTracker.getDirectStateBufferSize(bufferName, this);
   if (0 == sz) {
     return null;
   }
-  long addr;
-  addr = dispatch_glMapNamedBufferEXT(buffer, access, __addr_);
+  final long addr = dispatch_glMapNamedBufferEXT(bufferName, access, __addr_);
   if (0 == addr) {
     return null;
   }
-  ByteBuffer _res = newDirectByteBuffer(addr, sz);
-  Buffers.nativeOrder(_res);
-  _res.position(0);
-  return _res;
+  ByteBuffer buffer;
+  MemoryObject memObj0 = new MemoryObject(addr, sz); // object and key
+  MemoryObject memObj1 = MemoryObject.getOrAddSafe(arbMemCache, memObj0);
+  if(memObj0 == memObj1) {
+    // just added ..
+    if(null != memObj0.getBuffer()) {
+        throw new InternalError();
+    }
+    buffer = newDirectByteBuffer(addr, sz);
+    Buffers.nativeOrder(buffer);
+    memObj0.setBuffer(buffer);
+  } else {
+    // already mapped
+    buffer = memObj1.getBuffer();
+    if(null == buffer) {
+        throw new InternalError();
+    }
+  }
+  buffer.position(0);
+  return buffer;
 }
 
 private native long dispatch_glMapNamedBufferEXT(int buffer, int access, long procAddress);
 
-native private ByteBuffer newDirectByteBuffer(long addr, int capacity);
+native private ByteBuffer newDirectByteBuffer(long addr, long capacity);
 
     public void glVertexPointer(GLArrayData array) {
       if(array.getComponentNumber()==0) return;
-- 
cgit v1.2.3