[Beignet] [PATCH 10/11] Runtime: Apply base_object to cl_kernel

junyan.he at inbox.com junyan.he at inbox.com
Tue Jul 19 11:25:56 UTC 2016


From: Junyan He <junyan.he at intel.com>

Signed-off-by: Junyan He <junyan.he at intel.com>
---
 src/cl_api.c    |    2 +-
 src/cl_kernel.c |   19 +++++++++----------
 src/cl_kernel.h |    8 +++++---
 src/cl_utils.h  |    2 +-
 4 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/src/cl_api.c b/src/cl_api.c
index 790e692..9a52ed3 100644
--- a/src/cl_api.c
+++ b/src/cl_api.c
@@ -1316,7 +1316,7 @@ clGetKernelInfo(cl_kernel        kernel,
     cl_uint n = kernel->arg_n;
     FILL_GETINFO_RET (cl_uint, 1, &n, CL_SUCCESS);
   } else if (param_name == CL_KERNEL_REFERENCE_COUNT) {
-    cl_int ref = kernel->ref_n;
+    cl_int ref = CL_OBJECT_GET_REF(kernel);
     FILL_GETINFO_RET (cl_int, 1, &ref, CL_SUCCESS);
   } else if (param_name == CL_KERNEL_FUNCTION_NAME) {
     const char * n = cl_kernel_get_name(kernel);
diff --git a/src/cl_kernel.c b/src/cl_kernel.c
index ccecd83..8fafe42 100644
--- a/src/cl_kernel.c
+++ b/src/cl_kernel.c
@@ -45,14 +45,16 @@ cl_kernel_delete(cl_kernel k)
 #ifdef HAS_CMRT
   if (k->cmrt_kernel != NULL) {
     cmrt_destroy_kernel(k);
-    k->magic = CL_MAGIC_DEAD_HEADER; /* For safety */
+    CL_OBJECT_DESTROY_BASE(k);
     cl_free(k);
     return;
   }
 #endif
 
   /* We are not done with the kernel */
-  if (atomic_dec(&k->ref_n) > 1) return;
+  if (CL_OBJECT_DEC_REF(k) > 1)
+    return;
+
   /* Release one reference on all bos we own */
   if (k->bo)       cl_buffer_unreference(k->bo);
   /* This will be true for kernels created by clCreateKernel */
@@ -68,7 +70,8 @@ cl_kernel_delete(cl_kernel k)
   }
   if (k->image_sz)
     cl_free(k->images);
-  k->magic = CL_MAGIC_DEAD_HEADER; /* For safety */
+
+  CL_OBJECT_DESTROY_BASE(k);
   cl_free(k);
 }
 
@@ -77,9 +80,7 @@ cl_kernel_new(cl_program p)
 {
   cl_kernel k = NULL;
   TRY_ALLOC_NO_ERR (k, CALLOC(struct _cl_kernel));
-  SET_ICD(k->dispatch)
-  k->ref_n = 1;
-  k->magic = CL_MAGIC_KERNEL_HEADER;
+  CL_OBJECT_INIT_BASE(k, CL_OBJECT_KERNEL_MAGIC);
   k->program = p;
   k->cmrt_kernel = NULL;
 
@@ -108,7 +109,7 @@ cl_kernel_get_attributes(cl_kernel k)
 LOCAL void
 cl_kernel_add_ref(cl_kernel k)
 {
-  atomic_inc(&k->ref_n);
+  CL_OBJECT_INC_REF(k);
 }
 
 LOCAL cl_int
@@ -409,12 +410,10 @@ cl_kernel_dup(cl_kernel from)
   if (UNLIKELY(from == NULL))
     return NULL;
   TRY_ALLOC_NO_ERR (to, CALLOC(struct _cl_kernel));
-  SET_ICD(to->dispatch)
+  CL_OBJECT_INIT_BASE(to, CL_OBJECT_KERNEL_MAGIC);
   to->bo = from->bo;
   to->opaque = from->opaque;
   to->vme = from->vme;
-  to->ref_n = 1;
-  to->magic = CL_MAGIC_KERNEL_HEADER;
   to->program = from->program;
   to->arg_n = from->arg_n;
   to->curbe_sz = from->curbe_sz;
diff --git a/src/cl_kernel.h b/src/cl_kernel.h
index 05a882e..dd5a842 100644
--- a/src/cl_kernel.h
+++ b/src/cl_kernel.h
@@ -21,6 +21,7 @@
 #define __CL_KERNEL_H__
 
 #include "cl_internals.h"
+#include "cl_base_object.h"
 #include "cl_driver.h"
 #include "cl_gbe_loader.h"
 #include "CL/cl.h"
@@ -46,9 +47,7 @@ typedef struct cl_argument {
 
 /* One OCL function */
 struct _cl_kernel {
-  DEFINE_ICD(dispatch)
-  uint64_t magic;             /* To identify it as a kernel */
-  volatile int ref_n;         /* We reference count this object */
+  _cl_base_object base;
   cl_buffer bo;               /* The code itself */
   cl_program program;         /* Owns this structure (and pointers) */
   gbe_kernel opaque;          /* (Opaque) compiler structure for the OCL kernel */
@@ -73,6 +72,9 @@ struct _cl_kernel {
   void* cmrt_kernel;          /* CmKernel* */
 };
 
+#define CL_OBJECT_KERNEL_MAGIC 0x1234567890abedefLL
+#define CL_OBJECT_IS_KERNEL(obj) (((cl_base_object)obj)->magic == CL_OBJECT_KERNEL_MAGIC)
+
 /* Allocate an empty kernel */
 extern cl_kernel cl_kernel_new(cl_program);
 
diff --git a/src/cl_utils.h b/src/cl_utils.h
index 02ccc72..d779955 100644
--- a/src/cl_utils.h
+++ b/src/cl_utils.h
@@ -251,7 +251,7 @@ do {                                                        \
     err = CL_INVALID_KERNEL;                                \
     goto error;                                             \
   }                                                         \
-  if (UNLIKELY(KERNEL->magic != CL_MAGIC_KERNEL_HEADER)) {  \
+  if (UNLIKELY(!CL_OBJECT_IS_KERNEL(KERNEL))) {            \
     err = CL_INVALID_KERNEL;                                \
     goto error;                                             \
   }                                                         \
-- 
1.7.9.5

____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
Check it out at http://www.inbox.com/earth




More information about the Beignet mailing list