From 60c4cea84734a54599bc92535f191b7622a27d4b Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Mon, 15 Jun 2020 13:02:03 -0500
Subject: [PATCH] Allocators: allow zero size, return None

---
 doc/tools.rst        | 10 ++++++++++
 src/wrap_mempool.cpp |  6 ++++++
 2 files changed, 16 insertions(+)

diff --git a/doc/tools.rst b/doc/tools.rst
index 24353514..c0565d2f 100644
--- a/doc/tools.rst
+++ b/doc/tools.rst
@@ -54,6 +54,11 @@ not complicated::
 
         Allocate a :class:`pyopencl.Buffer` of the given *size*.
 
+        .. versionchanged :: 2020.2
+
+            The allocator will succeed even for allocations of size zero,
+            returning *None*.
+
 .. class:: ImmediateAllocator(queue, mem_flags=pyopencl.mem_flags.READ_WRITE)
 
     *mem_flags* takes its values from :class:`pyopencl.mem_flags` and corresponds
@@ -68,6 +73,11 @@ not complicated::
 
         Allocate a :class:`pyopencl.Buffer` of the given *size*.
 
+        .. versionchanged :: 2020.2
+
+            The allocator will succeed even for allocations of size zero,
+            returning *None*.
+
 .. class:: MemoryPool(allocator[, leading_bits_in_bin_id])
 
     A memory pool for OpenCL device memory. *allocator* must be an instance of
diff --git a/src/wrap_mempool.cpp b/src/wrap_mempool.cpp
index 8778c1bf..dceb33dc 100644
--- a/src/wrap_mempool.cpp
+++ b/src/wrap_mempool.cpp
@@ -104,6 +104,9 @@ namespace
 
       pointer_type allocate(size_type s)
       {
+        if (s == 0)
+          return nullptr;
+
         return pyopencl::create_buffer(m_context->data(), m_flags, s, 0);
       }
   };
@@ -137,6 +140,9 @@ namespace
 
       pointer_type allocate(size_type s)
       {
+        if (s == 0)
+          return nullptr;
+
         pointer_type ptr =  pyopencl::create_buffer(
             m_context->data(), m_flags, s, 0);
 
-- 
GitLab