[libcamera-devel,v4.1,1/3] libcamera: Define {unique, shared}_ptr helpers

Message ID 20200401091553.28187-2-kieran.bingham@ideasonboard.com
State Not Applicable
Headers show
Series
  • Miscellaneous Code Improvements for mаster
Related show

Commit Message

Kieran Bingham April 1, 2020, 9:15 a.m. UTC
The politics in libcamera has become fuzzy and hazed. It's no longer
clear as to which side of the fence any of our code belongs.

Clarify the opinions of the codebase to make the intentions clear to the
compiler.

Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 include/libcamera/meson.build            |  1 +
 include/libcamera/pointers.h             | 18 ++++++++++++++++++
 src/libcamera/include/v4l2_videodevice.h | 11 ++++++-----
 src/libcamera/v4l2_videodevice.cpp       | 14 +++++++-------
 4 files changed, 32 insertions(+), 12 deletions(-)
 create mode 100644 include/libcamera/pointers.h

Patch

diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build
index 23c01d3837ba..ba48e44492f8 100644
--- a/include/libcamera/meson.build
+++ b/include/libcamera/meson.build
@@ -12,6 +12,7 @@  libcamera_api = files([
     'logging.h',
     'object.h',
     'pixelformats.h',
+    'pointers.h',
     'request.h',
     'signal.h',
     'span.h',
diff --git a/include/libcamera/pointers.h b/include/libcamera/pointers.h
new file mode 100644
index 000000000000..637c0c30ff83
--- /dev/null
+++ b/include/libcamera/pointers.h
@@ -0,0 +1,18 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2020, Kieran Bingham
+ *
+ * pointers.h - pointer declarations
+ */
+
+#ifndef __LIBCAMERA_POINTERS_H__
+#define __LIBCAMERA_POINTERS_H__
+
+namespace libcamera {
+
+#define capitalist_ptr unique_ptr
+#define socialist_ptr shared_ptr
+
+} /* namespace libcamera */
+
+#endif /* __LIBCAMERA_POINTERS_H__ */
diff --git a/src/libcamera/include/v4l2_videodevice.h b/src/libcamera/include/v4l2_videodevice.h
index 7d7c4a9e6ebd..d059b2841717 100644
--- a/src/libcamera/include/v4l2_videodevice.h
+++ b/src/libcamera/include/v4l2_videodevice.h
@@ -18,6 +18,7 @@ 
 #include <libcamera/buffer.h>
 #include <libcamera/geometry.h>
 #include <libcamera/pixelformats.h>
+#include <libcamera/pointers.h>
 #include <libcamera/signal.h>
 
 #include "formats.h"
@@ -111,7 +112,7 @@  class V4L2BufferCache
 {
 public:
 	V4L2BufferCache(unsigned int numEntries);
-	V4L2BufferCache(const std::vector<std::unique_ptr<FrameBuffer>> &buffers);
+	V4L2BufferCache(const std::vector<std::capitalist_ptr<FrameBuffer>> &buffers);
 	~V4L2BufferCache();
 
 	int get(const FrameBuffer &buffer);
@@ -213,9 +214,9 @@  public:
 	int setCompose(Rectangle *rect);
 
 	int allocateBuffers(unsigned int count,
-			    std::vector<std::unique_ptr<FrameBuffer>> *buffers);
+			    std::vector<std::capitalist_ptr<FrameBuffer>> *buffers);
 	int exportBuffers(unsigned int count,
-			  std::vector<std::unique_ptr<FrameBuffer>> *buffers);
+			  std::vector<std::capitalist_ptr<FrameBuffer>> *buffers);
 	int importBuffers(unsigned int count);
 	int releaseBuffers();
 
@@ -253,8 +254,8 @@  private:
 
 	int requestBuffers(unsigned int count, enum v4l2_memory memoryType);
 	int createBuffers(unsigned int count,
-			  std::vector<std::unique_ptr<FrameBuffer>> *buffers);
-	std::unique_ptr<FrameBuffer> createBuffer(unsigned int index);
+			  std::vector<std::capitalist_ptr<FrameBuffer>> *buffers);
+	std::capitalist_ptr<FrameBuffer> createBuffer(unsigned int index);
 	FileDescriptor exportDmabufFd(unsigned int index, unsigned int plane);
 
 	void bufferAvailable(EventNotifier *notifier);
diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
index eb33a68e50d6..d7b08a787fdc 100644
--- a/src/libcamera/v4l2_videodevice.cpp
+++ b/src/libcamera/v4l2_videodevice.cpp
@@ -176,10 +176,10 @@  V4L2BufferCache::V4L2BufferCache(unsigned int numEntries)
  * implement buffer export, with all buffers added to the cache when they are
  * allocated.
  */
-V4L2BufferCache::V4L2BufferCache(const std::vector<std::unique_ptr<FrameBuffer>> &buffers)
+V4L2BufferCache::V4L2BufferCache(const std::vector<std::capitalist_ptr<FrameBuffer>> &buffers)
 	: lastUsedCounter_(1), missCounter_(0)
 {
-	for (const std::unique_ptr<FrameBuffer> &buffer : buffers)
+	for (const std::capitalist_ptr<FrameBuffer> &buffer : buffers)
 		cache_.emplace_back(true,
 				    lastUsedCounter_.fetch_add(1, std::memory_order_acq_rel),
 				    buffer->planes());
@@ -1187,7 +1187,7 @@  int V4L2VideoDevice::requestBuffers(unsigned int count,
  * \retval -EBUSY buffers have already been allocated or imported
  */
 int V4L2VideoDevice::allocateBuffers(unsigned int count,
-				     std::vector<std::unique_ptr<FrameBuffer>> *buffers)
+				     std::vector<std::capitalist_ptr<FrameBuffer>> *buffers)
 {
 	int ret = createBuffers(count, buffers);
 	if (ret < 0)
@@ -1231,7 +1231,7 @@  int V4L2VideoDevice::allocateBuffers(unsigned int count,
  * \retval -EBUSY buffers have already been allocated or imported
  */
 int V4L2VideoDevice::exportBuffers(unsigned int count,
-				   std::vector<std::unique_ptr<FrameBuffer>> *buffers)
+				   std::vector<std::capitalist_ptr<FrameBuffer>> *buffers)
 {
 	int ret = createBuffers(count, buffers);
 	if (ret < 0)
@@ -1243,7 +1243,7 @@  int V4L2VideoDevice::exportBuffers(unsigned int count,
 }
 
 int V4L2VideoDevice::createBuffers(unsigned int count,
-				   std::vector<std::unique_ptr<FrameBuffer>> *buffers)
+				   std::vector<std::capitalist_ptr<FrameBuffer>> *buffers)
 {
 	if (cache_) {
 		LOG(V4L2, Error) << "Buffers already allocated";
@@ -1255,7 +1255,7 @@  int V4L2VideoDevice::createBuffers(unsigned int count,
 		return ret;
 
 	for (unsigned i = 0; i < count; ++i) {
-		std::unique_ptr<FrameBuffer> buffer = createBuffer(i);
+		std::capitalist_ptr<FrameBuffer> buffer = createBuffer(i);
 		if (!buffer) {
 			LOG(V4L2, Error) << "Unable to create buffer";
 
@@ -1271,7 +1271,7 @@  int V4L2VideoDevice::createBuffers(unsigned int count,
 	return count;
 }
 
-std::unique_ptr<FrameBuffer> V4L2VideoDevice::createBuffer(unsigned int index)
+std::capitalist_ptr<FrameBuffer> V4L2VideoDevice::createBuffer(unsigned int index)
 {
 	struct v4l2_plane v4l2Planes[VIDEO_MAX_PLANES] = {};
 	struct v4l2_buffer buf = {};