summaryrefslogtreecommitdiff
path: root/src/cl_mem.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cl_mem.c')
-rw-r--r--src/cl_mem.c37
1 files changed, 27 insertions, 10 deletions
diff --git a/src/cl_mem.c b/src/cl_mem.c
index 916e909a..3055bea5 100644
--- a/src/cl_mem.c
+++ b/src/cl_mem.c
@@ -266,16 +266,26 @@ cl_mem_allocate(enum cl_mem_type type,
#ifdef HAS_USERPTR
if (ctx->device->host_unified_memory) {
+ int page_size = getpagesize();
/* currently only cl buf is supported, will add cl image support later */
- if ((flags & CL_MEM_USE_HOST_PTR) && host_ptr != NULL) {
- /* userptr not support tiling */
- if (!is_tiled) {
- int page_size = getpagesize();
- if ((((unsigned long)host_ptr | sz) & (page_size - 1)) == 0) {
- mem->is_userptr = 1;
- mem->bo = cl_buffer_alloc_userptr(bufmgr, "CL userptr memory object", host_ptr, sz, 0);
+ if (type == CL_MEM_BUFFER_TYPE) {
+ if (flags & CL_MEM_USE_HOST_PTR) {
+ assert(host_ptr != NULL);
+ /* userptr not support tiling */
+ if (!is_tiled) {
+ if ((((unsigned long)host_ptr | sz) & (page_size - 1)) == 0) {
+ mem->is_userptr = 1;
+ mem->bo = cl_buffer_alloc_userptr(bufmgr, "CL userptr memory object", host_ptr, sz, 0);
+ }
}
}
+ else if (flags & CL_MEM_ALLOC_HOST_PTR) {
+ const size_t alignedSZ = ALIGN(sz, page_size);
+ void* internal_host_ptr = cl_aligned_malloc(alignedSZ, page_size);
+ mem->host_ptr = internal_host_ptr;
+ mem->is_userptr = 1;
+ mem->bo = cl_buffer_alloc_userptr(bufmgr, "CL userptr memory object", internal_host_ptr, alignedSZ, 0);
+ }
}
}
@@ -400,13 +410,17 @@ cl_mem_new_buffer(cl_context ctx,
goto error;
/* Copy the data if required */
- if (flags & CL_MEM_COPY_HOST_PTR)
- cl_buffer_subdata(mem->bo, 0, sz, data);
+ if (flags & CL_MEM_COPY_HOST_PTR) {
+ if (mem->is_userptr)
+ memcpy(mem->host_ptr, data, sz);
+ else
+ cl_buffer_subdata(mem->bo, 0, sz, data);
+ }
if ((flags & CL_MEM_USE_HOST_PTR) && !mem->is_userptr)
cl_buffer_subdata(mem->bo, 0, sz, data);
- if (flags & CL_MEM_USE_HOST_PTR || flags & CL_MEM_COPY_HOST_PTR)
+ if (flags & CL_MEM_USE_HOST_PTR)
mem->host_ptr = data;
exit:
@@ -1069,6 +1083,9 @@ cl_mem_delete(cl_mem mem)
cl_buffer_unreference(mem->bo);
}
+ if (mem->is_userptr && (mem->flags & CL_MEM_ALLOC_HOST_PTR))
+ cl_free(mem->host_ptr);
+
cl_free(mem);
}