Patch Detail
Show a patch.
GET /api/patches/270/?format=api
{ "id": 270, "url": "https://patchwork.libcamera.org/api/patches/270/?format=api", "web_url": "https://patchwork.libcamera.org/patch/270/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20190117235916.1906-5-laurent.pinchart@ideasonboard.com>", "date": "2019-01-17T23:59:16", "name": "[libcamera-devel,4/4] libcamera: camera: Handle camera objects through shared pointers", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "ffed3181c3b1c211ea03e395d3da447e657d00ad", "submitter": { "id": 2, "url": "https://patchwork.libcamera.org/api/people/2/?format=api", "name": "Laurent Pinchart", "email": "laurent.pinchart@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/270/mbox/", "series": [ { "id": 90, "url": "https://patchwork.libcamera.org/api/series/90/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=90", "date": "2019-01-17T23:59:12", "name": "Object lifetime management", "version": 1, "mbox": "https://patchwork.libcamera.org/series/90/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/270/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/270/checks/", "tags": {}, "headers": { "Return-Path": "<laurent.pinchart@ideasonboard.com>", "Received": [ "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 549B460C98\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 18 Jan 2019 00:59:21 +0100 (CET)", "from pendragon.bb.dnainternet.fi\n\t(dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi\n\t[IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id EDE51D4B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 18 Jan 2019 00:59:20 +0100 (CET)" ], "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1547769561;\n\tbh=7vXCoRKGVW4oRqThZ/cG4QU+DohssURNKKDdIE4Ss44=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=ctvBKotT4Cd5c6ioEgjC4S8Qx6xFgnEmZL1TODs5ywEDYnRHksnuD3MaStcsR2seI\n\t/uu5SrXiM1w/uDVO3txd8a+8dicTpsipIdt+Z8x31njfQ3pfhgay5OxZQkN45hpqOe\n\t4JD8FcmVarAkcPUkdIq+HaX6I2J055deiO67+KFk=", "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Fri, 18 Jan 2019 01:59:16 +0200", "Message-Id": "<20190117235916.1906-5-laurent.pinchart@ideasonboard.com>", "X-Mailer": "git-send-email 2.19.2", "In-Reply-To": "<20190117235916.1906-1-laurent.pinchart@ideasonboard.com>", "References": "<20190117235916.1906-1-laurent.pinchart@ideasonboard.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH 4/4] libcamera: camera: Handle camera\n\tobjects through shared pointers", "X-BeenThere": "libcamera-devel@lists.libcamera.org", "X-Mailman-Version": "2.1.23", "Precedence": "list", "List-Id": "<libcamera-devel.lists.libcamera.org>", "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>", "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>", "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>", "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>", "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>", "X-List-Received-Date": "Thu, 17 Jan 2019 23:59:21 -0000" }, "content": "The Camera class is explicitly reference-counted to manage the lifetime\nof camera objects. Replace this open-coded implementation with usage of\nthe std::shared_ptr<> class.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/camera.h | 13 +++++----\n include/libcamera/camera_manager.h | 8 +++---\n src/libcamera/camera.cpp | 46 +++++++++++++++++-------------\n src/libcamera/camera_manager.cpp | 20 ++++++-------\n src/libcamera/pipeline/vimc.cpp | 2 +-\n test/list-cameras.cpp | 2 +-\n 6 files changed, 50 insertions(+), 41 deletions(-)", "diff": "diff --git a/include/libcamera/camera.h b/include/libcamera/camera.h\nindex 9a7579d61fa3..ef0a794e3a82 100644\n--- a/include/libcamera/camera.h\n+++ b/include/libcamera/camera.h\n@@ -7,6 +7,7 @@\n #ifndef __LIBCAMERA_CAMERA_H__\n #define __LIBCAMERA_CAMERA_H__\n \n+#include <memory>\n #include <string>\n \n namespace libcamera {\n@@ -14,15 +15,17 @@ namespace libcamera {\n class Camera\n {\n public:\n-\tCamera(const std::string &name);\n+\tstatic std::shared_ptr<Camera> create(const std::string &name);\n+\n+\tCamera(const Camera &) = delete;\n+\tvoid operator=(const Camera &) = delete;\n \n \tconst std::string &name() const;\n-\tvoid get();\n-\tvoid put();\n \n private:\n-\tvirtual ~Camera() { };\n-\tint ref_;\n+\tCamera(const std::string &name);\n+\t~Camera();\n+\n \tstd::string name_;\n };\n \ndiff --git a/include/libcamera/camera_manager.h b/include/libcamera/camera_manager.h\nindex 4b941fd9183b..738b13f4cfb1 100644\n--- a/include/libcamera/camera_manager.h\n+++ b/include/libcamera/camera_manager.h\n@@ -24,10 +24,10 @@ public:\n \tint start();\n \tvoid stop();\n \n-\tconst std::vector<Camera *> &cameras() const { return cameras_; }\n-\tCamera *get(const std::string &name);\n+\tconst std::vector<std::shared_ptr<Camera>> &cameras() const { return cameras_; }\n+\tstd::shared_ptr<Camera> get(const std::string &name);\n \n-\tvoid addCamera(Camera *camera);\n+\tvoid addCamera(std::shared_ptr<Camera> &camera);\n \n \tstatic CameraManager *instance();\n \n@@ -42,7 +42,7 @@ private:\n \n \tstd::unique_ptr<DeviceEnumerator> enumerator_;\n \tstd::vector<PipelineHandler *> pipes_;\n-\tstd::vector<Camera *> cameras_;\n+\tstd::vector<std::shared_ptr<Camera>> cameras_;\n \n \tstd::unique_ptr<EventDispatcher> dispatcher_;\n };\ndiff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp\nindex 6da2b20137d4..acf912bee95c 100644\n--- a/src/libcamera/camera.cpp\n+++ b/src/libcamera/camera.cpp\n@@ -41,19 +41,36 @@ namespace libcamera {\n * streams from a single image source. It provides the main interface to\n * configuring and controlling the device, and capturing image streams. It is\n * the central object exposed by libcamera.\n+ *\n+ * To support the central nature of Camera objects, libcamera manages the\n+ * lifetime of camera instances with std::shared_ptr<>. Instances shall be\n+ * created with the create() function which returns a shared pointer. The\n+ * Camera constructors and destructor are private, to prevent instances from\n+ * being constructed and destroyed manually.\n */\n \n /**\n- * \\brief Construct a named camera device\n+ * \\brief Create a camera instance\n+ * \\param[in] name The name of the camera device\n *\n- * \\param[in] name The name to set on the camera device\n+ * The caller is responsible for guaranteeing unicity of the camera name.\n *\n- * The caller is responsible for guaranteeing unicity of the camera\n- * device name.\n+ * \\return A shared pointer to the newly created camera object\n */\n-Camera::Camera(const std::string &name)\n-\t: ref_(1), name_(name)\n+std::shared_ptr<Camera> Camera::create(const std::string &name)\n {\n+\tstruct Allocator : std::allocator<Camera> {\n+\t\tvoid construct(void *p, const std::string &name)\n+\t\t{\n+\t\t\t::new(p) Camera(name);\n+\t\t}\n+\t\tvoid destroy(Camera *p)\n+\t\t{\n+\t\t\tp->~Camera();\n+\t\t}\n+\t};\n+\n+\treturn std::allocate_shared<Camera>(Allocator(), name);\n }\n \n /**\n@@ -66,24 +83,13 @@ const std::string &Camera::name() const\n \treturn name_;\n }\n \n-/**\n- * \\brief Acquire a reference to the camera\n- */\n-void Camera::get()\n+Camera::Camera(const std::string &name)\n+\t: name_(name)\n {\n-\tref_++;\n }\n \n-/**\n- * \\brief Release a reference to the camera\n- *\n- * When the last reference is released the camera device is deleted. Callers\n- * shall not access the camera device after calling this function.\n- */\n-void Camera::put()\n+Camera::~Camera()\n {\n-\tif (--ref_ == 0)\n-\t\tdelete this;\n }\n \n } /* namespace libcamera */\ndiff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp\nindex 852e5ed70fe3..d12bd42001ae 100644\n--- a/src/libcamera/camera_manager.cpp\n+++ b/src/libcamera/camera_manager.cpp\n@@ -39,9 +39,11 @@ namespace libcamera {\n * This will enumerate all the cameras present in the system, which can then be\n * listed with list() and retrieved with get().\n *\n- * Cameras are reference-counted, and shall be returned to the camera manager\n- * with Camera::put() after being used. Once all cameras have been returned to\n- * the manager, it can be stopped with stop().\n+ * Cameras are shared through std::shared_ptr<>, ensuring that a camera will\n+ * stay valid until the last reference is released without requiring any special\n+ * action from the application. Once the application has released all the\n+ * references it held to cameras, the camera manager can be stopped with\n+ * stop().\n *\n * \\todo Add ability to add and remove media devices based on hot-(un)plug\n * events coming from the device enumerator.\n@@ -147,15 +149,13 @@ void CameraManager::stop()\n * \\param[in] name Name of camera to get\n *\n * Before calling this function the caller is responsible for ensuring that\n- * the camera manger is running. A camera fetched this way shall be\n- * released by the user with the put() method of the Camera object once\n- * it is done using the camera.\n+ * the camera manger is running.\n *\n- * \\return Pointer to Camera object or nullptr if camera not found\n+ * \\return Shared pointer to Camera object or nullptr if camera not found\n */\n-Camera *CameraManager::get(const std::string &name)\n+std::shared_ptr<Camera> CameraManager::get(const std::string &name)\n {\n-\tfor (Camera *camera : cameras_) {\n+\tfor (std::shared_ptr<Camera> camera : cameras_) {\n \t\tif (camera->name() == name)\n \t\t\treturn camera;\n \t}\n@@ -171,7 +171,7 @@ Camera *CameraManager::get(const std::string &name)\n * handle with the camera manager. Registered cameras are immediately made\n * available to the system.\n */\n-void CameraManager::addCamera(Camera *camera)\n+void CameraManager::addCamera(std::shared_ptr<Camera> &camera)\n {\n \tcameras_.push_back(camera);\n }\ndiff --git a/src/libcamera/pipeline/vimc.cpp b/src/libcamera/pipeline/vimc.cpp\nindex 8742e0bae9a8..306a5b85cd60 100644\n--- a/src/libcamera/pipeline/vimc.cpp\n+++ b/src/libcamera/pipeline/vimc.cpp\n@@ -64,7 +64,7 @@ bool PipeHandlerVimc::match(CameraManager *manager, DeviceEnumerator *enumerator\n \t * will be chosen depends on how the Camera\n \t * object is modeled.\n \t */\n-\tCamera *camera = new Camera(\"Dummy VIMC Camera\");\n+\tstd::shared_ptr<Camera> camera = Camera::create(\"Dummy VIMC Camera\");\n \tmanager->addCamera(camera);\n \n \treturn true;\ndiff --git a/test/list-cameras.cpp b/test/list-cameras.cpp\nindex fdbbda0957b2..070cbf2be977 100644\n--- a/test/list-cameras.cpp\n+++ b/test/list-cameras.cpp\n@@ -30,7 +30,7 @@ protected:\n \t{\n \t\tunsigned int count = 0;\n \n-\t\tfor (Camera *camera : cm->cameras()) {\n+\t\tfor (const std::shared_ptr<Camera> &camera : cm->cameras()) {\n \t\t\tcout << \"- \" << camera->name() << endl;\n \t\t\tcount++;\n \t\t}\n", "prefixes": [ "libcamera-devel", "4/4" ] }