--- GIMPACT/include/GIMPACT/gim_math.h	2007/08/10 08:36:24	1.1
+++ GIMPACT/include/GIMPACT/gim_math.h	2007/08/10 12:39:47
@@ -45,8 +45,14 @@
 //! @{
 /*! Types */
 #define GREAL float
+#if defined(__amd64__)
+#include <stdint.h>
+#define GINT int32_t
+#define GUINT uint32_t
+#else  /* not defined(__amd64__) */
 #define GINT long
 #define GUINT unsigned long
+#endif /* not defined(__amd64__) */
 /*! Constants for integers*/
 #define GUINT_BIT_COUNT 32
 #define GUINT_EXPONENT 5
--- GIMPACT/src/gim_memory.cpp	2007/08/10 08:37:37	1.1
+++ GIMPACT/src/gim_memory.cpp	2007/08/12 10:11:49
@@ -29,7 +29,9 @@
 
 #include "GIMPACT/gim_memory.h"
 #include "stdlib.h"
+#if !defined(__FreeBSD__)
 #include "malloc.h"
+#endif // !defined(__FreeBSD__)
 //#include "mm_malloc.h"
 
 static gim_alloc_function *g_allocfn = 0;
@@ -164,34 +166,126 @@
 
 //!** Basic buffer prototyoe functions
 
+// XXX: modified for 64-bit environment
+// by KONNO Akihisa <konno@researchers.jp> 2007-08-10
+
+#if defined(__amd64__)
+struct ptrhash_t {
+  GUINT handle;
+  void *ptr;
+};
+
+ptrhash_t *ptrhash;
+size_t ptrhash_size = 0;
+size_t current_hash_num = 0;
+#define INIT_PTRHASH_SIZE 128
+
+static void init_hashtable()
+{
+  if (ptrhash_size == 0) {
+    if ((ptrhash = (ptrhash_t *)gim_alloc(INIT_PTRHASH_SIZE * sizeof(ptrhash_t))) == 0)
+      return;
+    ptrhash_size = INIT_PTRHASH_SIZE;
+  }
+  for (int i = 0; i < ptrhash_size; ++i) {
+    ptrhash[i].handle = i + 1;
+    ptrhash[i].ptr = 0;
+  }
+}
+
+static void double_hashtable()
+{
+  if ((ptrhash = (ptrhash_t *)gim_realloc(ptrhash, ptrhash_size * sizeof(ptrhash_t), 2 * ptrhash_size * sizeof(ptrhash_t))) == 0)
+    return;
+  for (int i = ptrhash_size; i < 2*ptrhash_size; ++i) {
+    ptrhash[i].handle = i + 1;
+    ptrhash[i].ptr = 0;
+  }
+  ptrhash_size *= 2;
+}
+
+static GUINT hash(void *const _ptr)
+{
+  if (_ptr == 0)		// irregal pointer
+    return 0;
+
+  if (ptrhash_size == 0)
+    init_hashtable();
+  else if (ptrhash_size <= current_hash_num)
+    double_hashtable();
+
+  if (ptrhash_size == 0 || ptrhash_size <= current_hash_num)
+    // failed to allocate or expand the hash table
+    return 0;
+
+  for (int i = 0; i < ptrhash_size; ++i)
+    if (ptrhash[i].ptr == 0) {	// vacant entry
+      ptrhash[i].ptr = _ptr;
+      return ptrhash[i].handle;
+      ++current_hash_num;
+    }
+
+  // If reached here, no vacant entry is found.
+  return 0;
+}
+
+static void *handle2ptr(GUINT _handle)
+{
+  if (_handle == 0 || _handle >= ptrhash_size)
+    // inappropriate handle
+    return 0;
+
+  return ptrhash[_handle - 1].ptr;
+}
+
+static void *remove_hash(GUINT _handle)
+{
+  void *ret_ptr = 0;
+
+  if (_handle == 0 || _handle >= ptrhash_size)
+    // inappropriate handle
+    return 0;
+
+  ret_ptr = ptrhash[_handle - 1].ptr;
+  ptrhash[_handle - 1].ptr = 0;
+  --current_hash_num;
+
+  return ret_ptr;
+}
+#else  // not defined(__amd64__)
+#define hash(ptr) ((GUINT)(ptr))
+#define handle2ptr(handle) (handle)
+#define remove_hash(handle) ((void *)(handle))
+#endif // not defined(__amd64__)
+
 GUINT _system_buffer_alloc_function(GUINT size,int usage)
 {
     void * newdata = gim_alloc(size);
     memset(newdata,0,size);
-    return (GUINT)(newdata);
+    return hash(newdata);
 }
 
 GUINT _system_buffer_alloc_data_function(const void * pdata,GUINT size,int usage)
 {
     void * newdata = gim_alloc(size);
     memcpy(newdata,pdata,size);
-    return (GUINT)(newdata);
+    return hash(newdata);
 }
 
 GUINT _system_buffer_realloc_function(GUINT buffer_handle,GUINT oldsize,int old_usage,GUINT newsize,int new_usage)
 {
-    void * newdata = gim_realloc((void *)buffer_handle,oldsize,newsize);
-    return (GUINT)(newdata);
+    void * newdata = gim_realloc(remove_hash(buffer_handle),oldsize,newsize);
+    return hash(newdata);
 }
 
 void _system_buffer_free_function(GUINT buffer_handle,GUINT size)
 {
-    gim_free((void*)buffer_handle,size);
+    gim_free(remove_hash(buffer_handle),size);
 }
 
 char * _system_lock_buffer_function(GUINT buffer_handle,int access)
 {
-    return (char * )(buffer_handle);
+    return (char * )handle2ptr(buffer_handle);
 }
 
 
@@ -206,7 +300,7 @@
 		GUINT copysize)
 {
     char * pdata;
-	pdata = (char *)source_buffer_handle;
+	pdata = (char *)handle2ptr(source_buffer_handle);
 	memcpy(destdata,pdata+source_pos,copysize);
 }
 
@@ -217,7 +311,7 @@
 		GUINT copysize)
 {
     char * pdata;
-	pdata = (char * )dest_buffer_handle;
+	pdata = (char * )handle2ptr(dest_buffer_handle);
 	memcpy(pdata+dest_pos,sourcedata,copysize);
 }
 
@@ -229,8 +323,8 @@
 		GUINT copysize)
 {
     char * pdata1,*pdata2;
-	pdata1 = (char *)source_buffer_handle;
-	pdata2 = (char *)dest_buffer_handle;
+	pdata1 = (char *)handle2ptr(source_buffer_handle);
+	pdata2 = (char *)handle2ptr(dest_buffer_handle);
 	memcpy(pdata2+dest_pos,pdata1+source_pos,copysize);
 }
 
@@ -241,7 +335,7 @@
 
 GUINT _shared_buffer_alloc_data_function(const void * pdata,GUINT size,int usage)
 {
-    return (GUINT)pdata;
+    return hash((void *)pdata);
 }
 
 GUINT _shared_buffer_realloc_function(GUINT buffer_handle,GUINT oldsize,int old_usage,GUINT newsize,int new_usage)
