{"id":278,"url":"https://patchwork.libcamera.org/api/1.1/patches/278/?format=json","web_url":"https://patchwork.libcamera.org/patch/278/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20190118232617.14631-5-laurent.pinchart@ideasonboard.com>","date":"2019-01-18T23:26:17","name":"[libcamera-devel,v2,4/4] libcamera: camera: Handle camera objects through shared pointers","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"bad9e045269b6d06d4ec5e83180320044a037b97","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/278/mbox/","series":[{"id":94,"url":"https://patchwork.libcamera.org/api/1.1/series/94/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=94","date":"2019-01-18T23:26:13","name":"Object lifetime management","version":2,"mbox":"https://patchwork.libcamera.org/series/94/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/278/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/278/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 8D0BD60C9B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 19 Jan 2019 00:26:22 +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 3267553E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 19 Jan 2019 00:26:22 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1547853982;\n\tbh=/5MlneqZLpR0AfZBUo1V/Snk2MBQ0uCwkLao+/Lrndo=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=KwbiUCocVgtTtC2BcbblXP18+yHwAcR5DbTHysVba1qagp0ctIXmwC+f2SQizFW8U\n\tIdDjPMgEKI1gdgt/t/1no7zGnLMTp+J4hI1qYEd4gyL2Kh2XOwfUHN1vWqgbUjvRso\n\txorxH5fI/aoOB/E3WnkoFSd5JvtVYJKcS44Zug9U=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Sat, 19 Jan 2019 01:26:17 +0200","Message-Id":"<20190118232617.14631-5-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.19.2","In-Reply-To":"<20190118232617.14631-1-laurent.pinchart@ideasonboard.com>","References":"<20190118232617.14631-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 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":"Fri, 18 Jan 2019 23:26:23 -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\nThis API change prevents pipeline handlers from subclassing the Camera\nclass. This isn't deemed to be an issue. Mark the class final to make\nthis explicit.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\nChanges since v1:\n\n- Make the Camera class final\n- Make the Camera::Camera(const std::string &) constructor explicit\n- Pass the std::shared_ptr<> by value to CameraManager::addCamera()\n---\n include/libcamera/camera.h         | 15 ++++++----\n include/libcamera/camera_manager.h |  8 +++---\n src/libcamera/camera.cpp           | 46 +++++++++++++++++-------------\n src/libcamera/camera_manager.cpp   | 24 ++++++++--------\n src/libcamera/pipeline/vimc.cpp    |  4 +--\n test/list-cameras.cpp              |  2 +-\n 6 files changed, 54 insertions(+), 45 deletions(-)","diff":"diff --git a/include/libcamera/camera.h b/include/libcamera/camera.h\nindex 9a7579d61fa3..2ea1a6883311 100644\n--- a/include/libcamera/camera.h\n+++ b/include/libcamera/camera.h\n@@ -7,22 +7,25 @@\n #ifndef __LIBCAMERA_CAMERA_H__\n #define __LIBCAMERA_CAMERA_H__\n \n+#include <memory>\n #include <string>\n \n namespace libcamera {\n \n-class Camera\n+class Camera final\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+\texplicit Camera(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..9ade29d76692 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 3fa9f23c8e0d..d76eaa7ace86 100644\n--- a/src/libcamera/camera_manager.cpp\n+++ b/src/libcamera/camera_manager.cpp\n@@ -40,9 +40,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@@ -148,15 +150,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@@ -172,9 +172,9 @@ 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-\tfor (Camera *c : cameras_) {\n+\tfor (std::shared_ptr<Camera> c : cameras_) {\n \t\tif (c->name() == camera->name()) {\n \t\t\tLOG(Warning) << \"Registering camera with duplicate name '\"\n \t\t\t\t     << camera->name() << \"'\";\n@@ -182,7 +182,7 @@ void CameraManager::addCamera(Camera *camera)\n \t\t}\n \t}\n \n-\tcameras_.push_back(camera);\n+\tcameras_.push_back(std::move(camera));\n }\n \n /**\ndiff --git a/src/libcamera/pipeline/vimc.cpp b/src/libcamera/pipeline/vimc.cpp\nindex 8742e0bae9a8..82b9237a3d7d 100644\n--- a/src/libcamera/pipeline/vimc.cpp\n+++ b/src/libcamera/pipeline/vimc.cpp\n@@ -64,8 +64,8 @@ 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-\tmanager->addCamera(camera);\n+\tstd::shared_ptr<Camera> camera = Camera::create(\"Dummy VIMC Camera\");\n+\tmanager->addCamera(std::move(camera));\n \n \treturn true;\n }\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","v2","4/4"]}