From 2a2dda732feec8eb9ad1d757146b32bef19c17de Mon Sep 17 00:00:00 2001 From: Yang Rong Date: Tue, 7 Jan 2014 11:30:54 +0800 Subject: [PATCH] Move the memory allocate size check to the callee. Because image's alignment, the alloc size may exceed the CL_DEVICE_MAX_MEM_ALLOC_SIZE if the image's size is calculate from it. So move the size check from cl_mem_allocate to the callee, and slightly enlarge the limit size when check in allocate image. Signed-off-by: Yang Rong Reviewed-by: Zhigang Gong --- src/cl_gt_device.h | 2 +- src/cl_mem.c | 29 +++++++++++++++-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/cl_gt_device.h b/src/cl_gt_device.h index 30692f8..110988a 100644 --- a/src/cl_gt_device.h +++ b/src/cl_gt_device.h @@ -51,7 +51,7 @@ .min_data_type_align_size = sizeof(cl_long) * 16, .single_fp_config = 0, /* XXX */ .global_mem_cache_type = CL_READ_WRITE_CACHE, -.global_mem_size = 256 * 1024 * 1024, +.global_mem_size = 128 * 1024 * 1024, .max_constant_buffer_size = 512 << 10, .max_constant_args = 8, .error_correction_support = CL_FALSE, diff --git a/src/cl_mem.c b/src/cl_mem.c index 5c4b197..40e0a99 100644 --- a/src/cl_mem.c +++ b/src/cl_mem.c @@ -200,23 +200,9 @@ cl_mem_allocate(enum cl_mem_type type, cl_mem mem = NULL; cl_int err = CL_SUCCESS; size_t alignment = 64; - cl_ulong max_mem_size; assert(ctx); - /* Due to alignment, the image size may exceed alloc max size, check global mem instead */ - if ((err = cl_get_device_info(ctx->device, - CL_DEVICE_GLOBAL_MEM_SIZE, - sizeof(max_mem_size), - &max_mem_size, - NULL)) != CL_SUCCESS) { - goto error; - } - if (UNLIKELY(sz > max_mem_size)) { - err = CL_INVALID_BUFFER_SIZE; - goto error; - } - /* Allocate and inialize the structure itself */ if (type == CL_MEM_IMAGE_TYPE) { struct _cl_mem_image *image = NULL; @@ -289,6 +275,7 @@ cl_mem_new_buffer(cl_context ctx, cl_int err = CL_SUCCESS; cl_mem mem = NULL; + cl_ulong max_mem_size; if (UNLIKELY(sz == 0)) { err = CL_INVALID_BUFFER_SIZE; @@ -334,6 +321,19 @@ cl_mem_new_buffer(cl_context ctx, goto error; } + if ((err = cl_get_device_info(ctx->device, + CL_DEVICE_MAX_MEM_ALLOC_SIZE, + sizeof(max_mem_size), + &max_mem_size, + NULL)) != CL_SUCCESS) { + goto error; + } + + if (UNLIKELY(sz > max_mem_size)) { + err = CL_INVALID_BUFFER_SIZE; + goto error; + } + /* Create the buffer in video memory */ mem = cl_mem_allocate(CL_MEM_BUFFER_TYPE, ctx, flags, sz, CL_FALSE, &err); if (mem == NULL || err != CL_SUCCESS) @@ -596,6 +596,7 @@ _cl_mem_new_image(cl_context ctx, } sz = aligned_pitch * aligned_h * depth; + mem = cl_mem_allocate(CL_MEM_IMAGE_TYPE, ctx, flags, sz, tiling != CL_NO_TILE, &err); if (mem == NULL || err != CL_SUCCESS) goto error; -- 2.7.4