dma_alloc() and dma_zalloc() are implemented with xmemalign() and
can't fail, yet many of their callers check for NULL, while others
rely on them never returning NULL.

As a first step towards making dma_alloc() and dma_zalloc() fallible
like malloc() and calloc(), add dma_xalloc() and dma_xzalloc() as
counterparts to xmalloc() and xzalloc() for the callers that rely on
the allocation to always succeed.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Sascha Hauer <[email protected]>
---
 drivers/dma/map.c | 17 +++++++++++++++++
 include/dma.h     | 12 ++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/drivers/dma/map.c b/drivers/dma/map.c
index 11671f9f66..806439a05d 100644
--- a/drivers/dma/map.c
+++ b/drivers/dma/map.c
@@ -9,6 +9,12 @@ void *dma_alloc(size_t size)
 }
 EXPORT_SYMBOL(dma_alloc);
 
+void *dma_xalloc(size_t size)
+{
+       return xmemalign(DMA_ALIGNMENT, ALIGN(size, DMA_ALIGNMENT));
+}
+EXPORT_SYMBOL(dma_xalloc);
+
 void *dma_zalloc(size_t size)
 {
        void *buf;
@@ -21,6 +27,17 @@ void *dma_zalloc(size_t size)
 }
 EXPORT_SYMBOL(dma_zalloc);
 
+void *dma_xzalloc(size_t size)
+{
+       void *buf;
+
+       buf = dma_xalloc(size);
+       memset(buf, 0x00, size);
+
+       return buf;
+}
+EXPORT_SYMBOL(dma_xzalloc);
+
 void dma_sync_single_for_cpu(struct device *dev, dma_addr_t address,
                             size_t size, enum dma_data_direction dir)
 {
diff --git a/include/dma.h b/include/dma.h
index c285b2095a..600b91c485 100644
--- a/include/dma.h
+++ b/include/dma.h
@@ -28,17 +28,29 @@
 
 #ifdef CONFIG_HAS_DMA
 void *dma_alloc(size_t size);
+void *dma_xalloc(size_t size);
 void *dma_zalloc(size_t size);
+void *dma_xzalloc(size_t size);
 #else
 static inline void *dma_alloc(size_t size)
 {
        return malloc(size);
 }
 
+static inline void *dma_xalloc(size_t size)
+{
+       return xmalloc(size);
+}
+
 static inline void *dma_zalloc(size_t size)
 {
        return calloc(size, 1);
 }
+
+static inline void *dma_xzalloc(size_t size)
+{
+       return xzalloc(size);
+}
 #endif
 
 static inline void dma_free(void *mem)

-- 
2.47.3


Reply via email to