[libcamera-devel,v2,09/10] libcamera: pipeline: raspberrypi: DmaHeaps: Use ScopedFD for a file descriptor
diff mbox series

Message ID 20210610075027.523672-10-hiroh@chromium.org
State Superseded
Headers show
Series
  • Introduce ScopedFD
Related show

Commit Message

Hirokazu Honda June 10, 2021, 7:50 a.m. UTC
Manages a file descriptor owned by DmaHeaps for a dma handle by
ScopedFD. Furthermore, DmaHeaps::alloc() creates a new file
descriptor and the returned file descriptor is owned by a caller.
This also clarifies it by changing the returned value to ScopedFD.

Signed-off-by: Hirokazu Honda <hiroh@chromium.org>
---
 .../pipeline/raspberrypi/dma_heaps.cpp        | 23 ++++++++-----------
 .../pipeline/raspberrypi/dma_heaps.h          | 10 ++++----
 .../pipeline/raspberrypi/raspberrypi.cpp      |  7 ++++--
 3 files changed, 21 insertions(+), 19 deletions(-)

Patch
diff mbox series

diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp
index 4d5dd6cb..d5b000b8 100644
--- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp
+++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp
@@ -35,7 +35,6 @@  LOG_DECLARE_CATEGORY(RPI)
 namespace RPi {
 
 DmaHeap::DmaHeap()
-	: dmaHeapHandle_(-1)
 {
 	for (const char *name : heapNames) {
 		int ret = ::open(name, O_RDWR, 0);
@@ -46,49 +45,47 @@  DmaHeap::DmaHeap()
 			continue;
 		}
 
-		dmaHeapHandle_ = ret;
+		dmaHeapHandle_ = ScopedFD(ret);
 		break;
 	}
 
-	if (dmaHeapHandle_ < 0)
+	if (!dmaHeapHandle_.isValid())
 		LOG(RPI, Error) << "Could not open any dmaHeap device";
 }
 
 DmaHeap::~DmaHeap()
 {
-	if (dmaHeapHandle_ > -1)
-		::close(dmaHeapHandle_);
 }
 
-FileDescriptor DmaHeap::alloc(const char *name, std::size_t size)
+ScopedFD DmaHeap::alloc(const char *name, std::size_t size)
 {
 	int ret;
 
 	if (!name)
-		return FileDescriptor();
+		return ScopedFD();
 
 	struct dma_heap_allocation_data alloc = {};
 
 	alloc.len = size;
 	alloc.fd_flags = O_CLOEXEC | O_RDWR;
 
-	ret = ::ioctl(dmaHeapHandle_, DMA_HEAP_IOCTL_ALLOC, &alloc);
+	ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);
 
 	if (ret < 0) {
 		LOG(RPI, Error) << "dmaHeap allocation failure for "
 				<< name;
-		return FileDescriptor();
+		return ScopedFD();
 	}
 
-	ret = ::ioctl(alloc.fd, DMA_BUF_SET_NAME, name);
+	ScopedFD allocFd(alloc.fd);
+	ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name);
 	if (ret < 0) {
 		LOG(RPI, Error) << "dmaHeap naming failure for "
 				<< name;
-		::close(alloc.fd);
-		return FileDescriptor();
+		return ScopedFD();
 	}
 
-	return FileDescriptor(std::move(alloc.fd));
+	return allocFd;
 }
 
 } /* namespace RPi */
diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.h b/src/libcamera/pipeline/raspberrypi/dma_heaps.h
index 79f39c51..3a0765e6 100644
--- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h
+++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.h
@@ -7,7 +7,9 @@ 
 #ifndef __LIBCAMERA_PIPELINE_RASPBERRYPI_DMA_HEAPS_H__
 #define __LIBCAMERA_PIPELINE_RASPBERRYPI_DMA_HEAPS_H__
 
-#include <libcamera/file_descriptor.h>
+#include <stddef.h>
+
+#include <libcamera/scoped_fd.h>
 
 namespace libcamera {
 
@@ -18,11 +20,11 @@  class DmaHeap
 public:
 	DmaHeap();
 	~DmaHeap();
-	bool isValid() const { return dmaHeapHandle_ > -1; }
-	FileDescriptor alloc(const char *name, std::size_t size);
+	bool isValid() const { return dmaHeapHandle_.isValid(); }
+	ScopedFD alloc(const char *name, std::size_t size);
 
 private:
-	int dmaHeapHandle_;
+	ScopedFD dmaHeapHandle_;
 };
 
 } /* namespace RPi */
diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
index a65b4568..13faa96e 100644
--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
@@ -1263,10 +1263,13 @@  int RPiCameraData::configureIPA(const CameraConfiguration *config)
 
 	/* Allocate the lens shading table via dmaHeap and pass to the IPA. */
 	if (!lsTable_.isValid()) {
-		lsTable_ = dmaHeap_.alloc("ls_grid", ipa::RPi::MaxLsGridSize);
-		if (!lsTable_.isValid())
+		ScopedFD scopedFD =
+			dmaHeap_.alloc("ls_grid", ipa::RPi::MaxLsGridSize);
+		if (!scopedFD.isValid())
 			return -ENOMEM;
 
+		lsTable_ = FileDescriptor(std::move(scopedFD));
+
 		/* Allow the IPA to mmap the LS table via the file descriptor. */
 		/*
 		 * \todo Investigate if mapping the lens shading table buffer