diff --git a/src/wrap_mempool.cpp b/src/wrap_mempool.cpp index dceb33dcab63129ddaac89602324d87b816d7315..16662bc58d2f21db2f25adac31e6de1281c3ec18 100644 --- a/src/wrap_mempool.cpp +++ b/src/wrap_mempool.cpp @@ -197,6 +197,15 @@ namespace alloc.try_release_blocks(); } + if (!mem) + { + if (size == 0) + return nullptr; + else + throw pyopencl::error("Allocator", CL_INVALID_VALUE, + "allocator succeeded but returned NULL cl_mem"); + } + try { return new pyopencl::buffer(mem, false); diff --git a/test/test_wrapper.py b/test/test_wrapper.py index dc5772de57ae5d7c05040cdc98117dbed8d53301..1b4a3b423ba1faf770eb82bfde201fdc4bba0ff9 100644 --- a/test/test_wrapper.py +++ b/test/test_wrapper.py @@ -33,7 +33,8 @@ import pyopencl.array as cl_array import pyopencl.cltypes as cltypes import pyopencl.clrandom from pyopencl.tools import ( # noqa - pytest_generate_tests_for_pyopencl as pytest_generate_tests) + pytest_generate_tests_for_pyopencl as pytest_generate_tests, + ImmediateAllocator, DeferredAllocator) from pyopencl.characterize import get_pocl_version # Are CL implementations crashy? You be the judge. :) @@ -573,6 +574,23 @@ def test_mempool_2(ctx_factory): assert asize < asize*(1+1/8) +@pytest.mark.parametrize("allocator_cls", [ImmediateAllocator, DeferredAllocator]) +def test_allocator(ctx_factory, allocator_cls): + context = ctx_factory() + queue = cl.CommandQueue(context) + + if allocator_cls is DeferredAllocator: + allocator = allocator_cls(context) + else: + allocator = allocator_cls(queue) + + mem = allocator(15) + mem2 = allocator(0) + + assert mem is not None + assert mem2 is None + + def test_vector_args(ctx_factory): context = ctx_factory() queue = cl.CommandQueue(context)