[06/11] Runtime: Apply base object to cl_mem.

Submitted by junyan.he@inbox.com on July 19, 2016, 11:25 a.m.

Details

Message ID 1468927557-5051-6-git-send-email-junyan.he@inbox.com
State New
Headers show
Series "Series without cover letter" ( rev: 1 ) in Beignet

Not browsing as part of any series.

Commit Message

junyan.he@inbox.com July 19, 2016, 11:25 a.m.
From: Junyan He <junyan.he@intel.com>

Signed-off-by: Junyan He <junyan.he@intel.com>
---
 src/cl_mem.c   |   19 +++++++++----------
 src/cl_mem.h   |    9 +++++----
 src/cl_utils.h |    2 +-
 3 files changed, 15 insertions(+), 15 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/cl_mem.c b/src/cl_mem.c
index e6eda66..b42573e 100644
--- a/src/cl_mem.c
+++ b/src/cl_mem.c
@@ -111,7 +111,7 @@  cl_get_mem_object_info(cl_mem mem,
     *((cl_uint *)param_value) = mem->map_ref;
     break;
   case CL_MEM_REFERENCE_COUNT:
-    *((cl_uint *)param_value) = mem->ref_n;
+    *((cl_uint *)param_value) = CL_OBJECT_GET_REF(mem);
     break;
   case CL_MEM_CONTEXT:
     *((cl_context *)param_value) = mem->ctx;
@@ -262,10 +262,9 @@  cl_mem_allocate(enum cl_mem_type type,
     TRY_ALLOC (buffer, CALLOC(struct _cl_mem_buffer));
     mem = &buffer->base;
   }
+
+  CL_OBJECT_INIT_BASE(mem, CL_OBJECT_MEM_MAGIC);
   mem->type = type;
-  SET_ICD(mem->dispatch)
-  mem->ref_n = 1;
-  mem->magic = CL_MAGIC_MEM_HEADER;
   mem->flags = flags;
   mem->is_userptr = 0;
   mem->offset = 0;
@@ -389,7 +388,7 @@  is_valid_mem(cl_mem mem, cl_mem buffers)
   cl_mem tmp = buffers;
   while(tmp){
     if(mem == tmp){
-      if (UNLIKELY(mem->magic != CL_MAGIC_MEM_HEADER))
+      if (UNLIKELY(!CL_OBJECT_IS_MEM(mem)))
         return CL_INVALID_MEM_OBJECT;
       return CL_SUCCESS;
     }
@@ -557,10 +556,9 @@  cl_mem_new_sub_buffer(cl_mem buffer,
   /* Now create the sub buffer and link it to the buffer. */
   TRY_ALLOC (sub_buf, CALLOC(struct _cl_mem_buffer));
   mem = &sub_buf->base;
+
+  CL_OBJECT_INIT_BASE(mem, CL_OBJECT_MEM_MAGIC);
   mem->type = CL_MEM_SUBBUFFER_TYPE;
-  SET_ICD(mem->dispatch)
-  mem->ref_n = 1;
-  mem->magic = CL_MAGIC_MEM_HEADER;
   mem->flags = flags;
   mem->offset = buffer->offset;
   mem->is_userptr = buffer->is_userptr;
@@ -1176,7 +1174,7 @@  cl_mem_delete(cl_mem mem)
   cl_int i;
   if (UNLIKELY(mem == NULL))
     return;
-  if (atomic_dec(&mem->ref_n) > 1)
+  if (CL_OBJECT_DEC_REF(mem) > 1)
     return;
 #ifdef HAS_EGL
   if (UNLIKELY(IS_GL_IMAGE(mem))) {
@@ -1266,6 +1264,7 @@  cl_mem_delete(cl_mem mem)
       (mem->type != CL_MEM_SUBBUFFER_TYPE))
     cl_free(mem->host_ptr);
 
+  CL_OBJECT_DESTROY_BASE(mem);
   cl_free(mem);
 }
 
@@ -1273,7 +1272,7 @@  LOCAL void
 cl_mem_add_ref(cl_mem mem)
 {
   assert(mem);
-  atomic_inc(&mem->ref_n);
+  CL_OBJECT_INC_REF(mem);
 }
 
 #define LOCAL_SZ_0   16
diff --git a/src/cl_mem.h b/src/cl_mem.h
index c8f256d..39dbab1 100644
--- a/src/cl_mem.h
+++ b/src/cl_mem.h
@@ -23,7 +23,7 @@ 
 #include "cl_internals.h"
 #include "cl_driver_type.h"
 #include "CL/cl.h"
-#include "cl_khr_icd.h"
+#include "cl_base_object.h"
 #include <assert.h>
 #include <pthread.h>
 
@@ -78,11 +78,9 @@  enum cl_mem_type {
 #define IS_GL_IMAGE(mem) (mem->type == CL_MEM_GL_IMAGE_TYPE)
 
 typedef  struct _cl_mem {
-  DEFINE_ICD(dispatch)
-  uint64_t magic;           /* To identify it as a memory object */
+  _cl_base_object base;
   cl_mem prev, next;        /* We chain the memory buffers together */
   enum cl_mem_type type;
-  volatile int ref_n;       /* This object is reference counted */
   cl_buffer bo;             /* Data in GPU memory */
   size_t size;              /* original request size, not alignment size, used in constant buffer */
   cl_context ctx;           /* Context it belongs to */
@@ -100,6 +98,9 @@  typedef  struct _cl_mem {
   void* cmrt_mem;
 } _cl_mem;
 
+#define CL_OBJECT_MEM_MAGIC 0x381a27b9ee6504dfLL
+#define CL_OBJECT_IS_MEM(obj) (((cl_base_object)obj)->magic == CL_OBJECT_MEM_MAGIC)
+
 struct _cl_mem_image {
   _cl_mem base;
   cl_image_format fmt;            /* only for images */
diff --git a/src/cl_utils.h b/src/cl_utils.h
index e118ef1..c7742cb 100644
--- a/src/cl_utils.h
+++ b/src/cl_utils.h
@@ -148,7 +148,7 @@  do {                                                        \
     err = CL_INVALID_MEM_OBJECT;                            \
     goto error;                                             \
   }                                                         \
-  if (UNLIKELY(MEM->magic != CL_MAGIC_MEM_HEADER)) {        \
+  if (UNLIKELY(!CL_OBJECT_IS_MEM(MEM))) {                  \
     err = CL_INVALID_MEM_OBJECT;                            \
     goto error;                                             \
   }                                                         \