From 4505cb7c93bae6a616b67421f9ed1f7dae4d66aa Mon Sep 17 00:00:00 2001
From: Yichao Yu <yyc1992@gmail.com>
Date: Thu, 19 Jun 2014 11:40:43 +0800
Subject: [PATCH] more improve trace output

---
 src/c_wrapper/clhelper.h | 22 ++++++++++------------
 src/c_wrapper/utils.h    | 26 +++++++++++++++++++++-----
 2 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/src/c_wrapper/clhelper.h b/src/c_wrapper/clhelper.h
index ccfddea0..f95391e1 100644
--- a/src/c_wrapper/clhelper.h
+++ b/src/c_wrapper/clhelper.h
@@ -80,9 +80,9 @@ get_vec_info(cl_int (*func)(ArgTypes...), const char *name,
              ArgTypes2&&... args)
 {
     size_t size = 0;
-    call_guarded(func, name, args..., 0, nullptr, &size);
+    call_guarded(func, name, args..., 0, nullptr, buf_arg(size));
     pyopencl_buf<T> buf(size / sizeof(T));
-    call_guarded(func, name, args..., size, buf.get(), &size);
+    call_guarded(func, name, args..., size_arg(buf), buf_arg(size));
     return buf;
 }
 #define pyopencl_get_vec_info(type, what, args...)                      \
@@ -135,8 +135,7 @@ get_opaque_info(cl_int (*func)(ArgTypes...), const char *name,
                 ArgTypes2&&... args)
 {
     typename CLObj::cl_type param_value;
-    call_guarded(func, name, args..., sizeof(param_value),
-                 buf_arg(param_value), nullptr);
+    call_guarded(func, name, args..., size_arg(param_value), nullptr);
     generic_info info;
     info.dontfree = 0;
     info.opaque_class = CLObj::class_id;
@@ -157,10 +156,10 @@ PYOPENCL_USE_RESULT static PYOPENCL_INLINE generic_info
 get_str_info(cl_int (*func)(ArgTypes...), const char *name,
              ArgTypes2&&... args)
 {
-    size_t param_value_size;
-    call_guarded(func, name, args..., 0, nullptr, &param_value_size);
-    pyopencl_buf<char> param_value(param_value_size);
-    call_guarded(func, name, args..., param_value, &param_value_size);
+    size_t size;
+    call_guarded(func, name, args..., 0, nullptr, buf_arg(size));
+    pyopencl_buf<char> param_value(size);
+    call_guarded(func, name, args..., param_value, buf_arg(size));
     generic_info info;
     info.dontfree = 0;
     info.opaque_class = CLASS_NONE;
@@ -176,14 +175,13 @@ PYOPENCL_USE_RESULT static PYOPENCL_INLINE generic_info
 get_int_info(cl_int (*func)(ArgTypes...), const char *name,
              const char *tpname, ArgTypes2&&... args)
 {
-    pyopencl_buf<T> param_value;
-    call_guarded(func, name, args..., sizeof(T),
-                 buf_arg(*param_value.get()), nullptr);
+    T value;
+    call_guarded(func, name, args..., size_arg(value), nullptr);
     generic_info info;
     info.dontfree = 0;
     info.opaque_class = CLASS_NONE;
     info.type = tpname;
-    info.value = (void*)param_value.release();
+    info.value = cl_memdup(&value);
     return info;
 }
 #define pyopencl_get_int_info(type, what, args...)                      \
diff --git a/src/c_wrapper/utils.h b/src/c_wrapper/utils.h
index 11a0e95e..70485b6f 100644
--- a/src/c_wrapper/utils.h
+++ b/src/c_wrapper/utils.h
@@ -29,6 +29,23 @@ tostring(const T& v)
 
 namespace pyopencl {
 
+PYOPENCL_USE_RESULT static PYOPENCL_INLINE void*
+cl_memdup(const void *p, size_t size)
+{
+    void *res = malloc(size);
+    memcpy(res, p, size);
+    return res;
+}
+
+template<typename T>
+PYOPENCL_USE_RESULT static PYOPENCL_INLINE T*
+cl_memdup(const T *p)
+{
+    // Not supported by libstdc++ yet...
+    // static_assert(std::is_trivially_copy_constructible<T>::value);
+    return static_cast<T*>(cl_memdup(static_cast<const void*>(p), sizeof(T)));
+}
+
 enum class ArgType {
     None,
     SizeOf,
@@ -206,8 +223,7 @@ struct _ToArgBuffer {
 
 template<ArgType AT=ArgType::None, typename T>
 static PYOPENCL_INLINE auto
-buf_arg(T &&buf)
-    -> decltype(_ToArgBuffer<AT, T>::convert(std::forward<T>(buf)))
+buf_arg(T &&buf) -> decltype(_ToArgBuffer<AT, T>::convert(std::forward<T>(buf)))
 {
     return _ToArgBuffer<AT, T>::convert(std::forward<T>(buf));
 }
@@ -220,10 +236,10 @@ buf_arg(T *buf, size_t l)
 }
 
 template<typename T>
-static PYOPENCL_INLINE ArgBuffer<T, ArgType::SizeOf>
-size_arg(T &buf)
+static PYOPENCL_INLINE auto
+size_arg(T &&buf) -> decltype(buf_arg<ArgType::SizeOf>(std::forward<T>(buf)))
 {
-    return ArgBuffer<T, ArgType::SizeOf>(&buf, 1);
+    return buf_arg<ArgType::SizeOf>(std::forward<T>(buf));
 }
 
 template<typename Buff, class = void>
-- 
GitLab