From 4bb95c2fbda8c2e3a539ee85d94bccb8e1240086 Mon Sep 17 00:00:00 2001
From: Marko Bencun <mbencun@gmail.com>
Date: Sun, 1 Sep 2013 16:22:02 +0200
Subject: [PATCH] simplified get_info conversion

---
 pyopencl/cffi_cl.py          | 25 +++++++++----------------
 src/c_wrapper/wrap_cl.cpp    | 31 +++++++++++++++++++------------
 src/c_wrapper/wrap_cl_core.h | 24 ++----------------------
 3 files changed, 30 insertions(+), 50 deletions(-)

diff --git a/pyopencl/cffi_cl.py b/pyopencl/cffi_cl.py
index 48659487..5a3e0b64 100644
--- a/pyopencl/cffi_cl.py
+++ b/pyopencl/cffi_cl.py
@@ -376,23 +376,16 @@ class Platform(_Common):
             r.__dict__["platform"] = self
         return result
 
-        
 def _generic_info_to_python(info):
-    if info.type == _lib.generic_info_type_chars:
-        return _ffi.string(info.value._chars)
-    if info.type == _lib.generic_info_type_array:
-        ar = _ffi.cast('%s[%s]' % (_ffi.string(info.array_element_type), info.value._array.size), info.value._array.array)
-        return list(ar)
-        
-    for type_ in ('cl_uint',
-                  'cl_mem_object_type',
-                  'cl_build_status',
-                  'cl_program_binary_type',
-                  'size_t',
-                  ):
-        if info.type == getattr(_lib, 'generic_info_type_%s' % type_):
-            return getattr(info.value, '_%s' % type_)
-    raise NotImplementedError(info.type)
+    type_ = _ffi.string(info.type)
+    if type_ == 'char*':
+        ret = _ffi.string(_ffi.cast(type_, info.value))
+    elif type_.endswith(']'):
+        ret = list(_ffi.cast(type_, info.value))
+    else:
+        ret = _ffi.cast('%s*' % type_, info.value)[0]
+    _lib._free(info.value)
+    return ret
 
 class Kernel(object):
     _id = 'kernel'
diff --git a/src/c_wrapper/wrap_cl.cpp b/src/c_wrapper/wrap_cl.cpp
index ab511fd9..75d1781b 100644
--- a/src/c_wrapper/wrap_cl.cpp
+++ b/src/c_wrapper/wrap_cl.cpp
@@ -5,6 +5,7 @@
 #include <stdexcept>
 #include <string.h>
 #include <memory>
+#include <sstream>
 
 #define MALLOC(TYPE, VAR, N) TYPE *VAR = reinterpret_cast<TYPE*>(malloc(sizeof(TYPE)*(N)));
 
@@ -80,20 +81,20 @@
 			  (FIRST_ARG, SECOND_ARG, param_value_size,	\
 			   param_value, &param_value_size));		\
     generic_info info;							\
-    info.type = generic_info_type_chars;				\
-    info.value._chars = param_value;					\
+    info.type = "char*";				\
+    info.value = (void*)param_value;					\
     return info;							\
   }
 
 #define PYOPENCL_GET_INTEGRAL_INFO(WHAT, FIRST_ARG, SECOND_ARG, TYPE)	\
   {									\
-    TYPE param_value;							\
+    MALLOC(TYPE, param_value, 1);					\
     PYOPENCL_CALL_GUARDED(clGet##WHAT##Info,				\
-			  (FIRST_ARG, SECOND_ARG, sizeof(param_value), &param_value, 0)); \
+			  (FIRST_ARG, SECOND_ARG, sizeof(param_value), param_value, 0)); \
     generic_info info;							\
-    info.type = generic_info_type_##TYPE;				\
-      info.value._##TYPE = param_value;					\
-	return info;							\
+    info.type = #TYPE;							\
+      info.value = (void*)param_value;					\
+      return info;							\
   }
 
 #define PYOPENCL_GET_ARRAY_INFO(TYPE, VEC)	\
@@ -103,11 +104,9 @@
       ar[i] = VEC[i];				\
     }						\
     generic_info info;				\
-    info.type = generic_info_type_array;	\
-    info.value._array.array = ar;		\
-    info.value._array.size = VEC.size();	\
-    info.array_element_type = #TYPE;		\
-      return info;				\
+    info.type = _copy_str(std::string(#TYPE"[") + tostring(VEC.size()) + "]"); \
+    info.value = (void*)ar;						\
+    return info;							\
   }
   
 #define PYOPENCL_RETURN_NEW_EVENT(evt)		\
@@ -229,6 +228,12 @@ run_python_gc(); \
     return PYOPENCL_CL_VERSION;
   }
 
+template<class T>
+std::string tostring(const T& v) {
+  std::ostringstream ostr;
+  ostr << v;
+  return ostr.str();
+}
 
 extern "C"
 namespace pyopencl
@@ -238,6 +243,8 @@ namespace pyopencl
     strcpy(cstr, str.c_str());
     return cstr;
   }
+
+  
   
   // {{{ error
   class error : public std::runtime_error
diff --git a/src/c_wrapper/wrap_cl_core.h b/src/c_wrapper/wrap_cl_core.h
index b51b9537..adf90029 100644
--- a/src/c_wrapper/wrap_cl_core.h
+++ b/src/c_wrapper/wrap_cl_core.h
@@ -1,28 +1,8 @@
 typedef enum { KND_UNKNOWN, KND_SOURCE, KND_BINARY } program_kind_type;
 
-typedef enum {
-  generic_info_type_cl_uint,
-  generic_info_type_cl_mem_object_type,
-  generic_info_type_cl_build_status,
-  generic_info_type_cl_program_binary_type,
-  generic_info_type_size_t,
-  generic_info_type_chars,
-  generic_info_type_array,
-} generic_info_type_t;
-
 typedef struct {
-  generic_info_type_t type;
-  const char *array_element_type;
-  union value_t {
-    cl_uint _cl_uint;
-    cl_mem_object_type _cl_mem_object_type;
-    cl_build_status _cl_build_status;
-    cl_program_binary_type _cl_program_binary_type;
-    size_t _size_t;
-    char *_chars;
-    
-    struct { void *array; uint32_t size; } _array;
-  } value;
+  const char *type;
+  void *value;
 } generic_info;
 
 typedef struct {
-- 
GitLab