Add the support for clSetMemObjectDestructorCallback API
authorJunyan He <junyan.he@linux.intel.com>
Fri, 12 Jul 2013 08:02:32 +0000 (16:02 +0800)
committerZhigang Gong <zhigang.gong@linux.intel.com>
Tue, 16 Jul 2013 06:00:04 +0000 (14:00 +0800)
Reviewed-by: "Xing, Homer" <homer.xing@intel.com>
src/cl_api.c
src/cl_mem.c
src/cl_mem.h

index 20cbc1e..03cad52 100644 (file)
@@ -585,8 +585,24 @@ clSetMemObjectDestructorCallback(cl_mem  memobj,
                                  void (CL_CALLBACK *pfn_notify) (cl_mem, void*),
                                  void * user_data)
 {
-  NOT_IMPLEMENTED;
-  return 0;
+  cl_int err = CL_SUCCESS;
+  CHECK_MEM(memobj);
+  INVALID_VALUE_IF (pfn_notify == 0);
+
+  cl_mem_dstr_cb *cb = (cl_mem_dstr_cb*)malloc(sizeof(cl_mem_dstr_cb));
+  if (!cb) {
+    err = CL_OUT_OF_HOST_MEMORY;
+    goto error;
+  }
+
+  memset(cb, 0, sizeof(cl_mem_dstr_cb));
+  cb->pfn_notify = pfn_notify;
+  cb->user_data = user_data;
+  cb->next = memobj->dstr_cb;
+  memobj->dstr_cb = cb;
+
+error:
+  return err;
 }
 
 cl_sampler
index 9691ba3..b554f8f 100644 (file)
@@ -498,6 +498,16 @@ cl_mem_delete(cl_mem mem)
   if (mem->mapped_ptr)
     free(mem->mapped_ptr);
 
+  if (mem->dstr_cb) {
+    cl_mem_dstr_cb *cb = mem->dstr_cb;
+    while (mem->dstr_cb) {
+      cb = mem->dstr_cb;
+      cb->pfn_notify(mem, cb->user_data);
+      mem->dstr_cb = cb->next;
+      free(cb);
+    }
+  }
+
   cl_free(mem);
 }
 
index 66518a6..1b1709a 100644 (file)
@@ -55,6 +55,12 @@ typedef struct _cl_mapped_ptr {
   size_t size;
 }cl_mapped_ptr;
 
+typedef struct _cl_mem_dstr_cb {
+  struct _cl_mem_dstr_cb * next;
+  void (CL_CALLBACK *pfn_notify)(cl_mem memobj, void *user_data);
+  void *user_data;
+}cl_mem_dstr_cb;
+
 /* Used for buffers and images */
 struct _cl_mem {
   DEFINE_ICD(dispatch)
@@ -78,6 +84,7 @@ struct _cl_mem {
   cl_mapped_ptr* mapped_ptr;/* Store the mapped addresses and size by caller. */
   int mapped_ptr_sz;        /* The array size of mapped_ptr. */
   int map_ref;              /* The mapped count. */
+  cl_mem_dstr_cb *dstr_cb;  /* The destroy callback. */
 };
 
 /* Query information about a memory object */