Skip to content

Commit

Permalink
libcamera: pipeline_handler: Return unique_ptr from createInstance
Browse files Browse the repository at this point in the history
Avoid naked pointer with memory allocation by returning a unique_ptr
from PipelineHandlerFactory::createInstance(), in order to increase
memory allocation safety.

This allows iterating over factories in the CameraManager and unit tests
using const pointers.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Xavier Roumegue <xavier.roumegue@oss.nxp.com>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
  • Loading branch information
pinchartl committed Oct 7, 2022
1 parent 5a867f3 commit ba3a1ad
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
8 changes: 5 additions & 3 deletions include/libcamera/internal/pipeline_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ class PipelineHandlerFactory
private:
static void registerType(PipelineHandlerFactory *factory);

virtual PipelineHandler *createInstance(CameraManager *manager) const = 0;
virtual std::unique_ptr<PipelineHandler>
createInstance(CameraManager *manager) const = 0;

std::string name_;
};
Expand All @@ -125,9 +126,10 @@ public: \
handler##Factory() : PipelineHandlerFactory(#handler) {} \
\
private: \
PipelineHandler *createInstance(CameraManager *manager) const \
std::unique_ptr<PipelineHandler> \
createInstance(CameraManager *manager) const \
{ \
return new handler(manager); \
return std::make_unique<handler>(manager); \
} \
}; \
static handler##Factory global_##handler##Factory;
Expand Down
4 changes: 2 additions & 2 deletions src/libcamera/camera_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ void CameraManager::Private::createPipelineHandlers()
* file and only fallback on all handlers if there is no
* configuration file.
*/
std::vector<PipelineHandlerFactory *> &factories =
const std::vector<PipelineHandlerFactory *> &factories =
PipelineHandlerFactory::factories();

for (PipelineHandlerFactory *factory : factories) {
for (const PipelineHandlerFactory *factory : factories) {
LOG(Camera, Debug)
<< "Found registered pipeline handler '"
<< factory->name() << "'";
Expand Down
8 changes: 4 additions & 4 deletions src/libcamera/pipeline_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,9 @@ PipelineHandlerFactory::PipelineHandlerFactory(const char *name)
*/
std::shared_ptr<PipelineHandler> PipelineHandlerFactory::create(CameraManager *manager) const
{
PipelineHandler *handler = createInstance(manager);
std::unique_ptr<PipelineHandler> handler = createInstance(manager);
handler->name_ = name_.c_str();
return std::shared_ptr<PipelineHandler>(handler);
return std::shared_ptr<PipelineHandler>(std::move(handler));
}

/**
Expand Down Expand Up @@ -727,8 +727,8 @@ std::vector<PipelineHandlerFactory *> &PipelineHandlerFactory::factories()
* macro. It creates a pipeline handler instance associated with the camera
* \a manager.
*
* \return a pointer to a newly constructed instance of the PipelineHandler
* subclass corresponding to the factory
* \return A unique pointer to a newly constructed instance of the
* PipelineHandler subclass corresponding to the factory
*/

/**
Expand Down
4 changes: 2 additions & 2 deletions test/ipa/ipa_interface_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ class IPAInterfaceTest : public Test, public Object
ipaManager_ = make_unique<IPAManager>();

/* Create a pipeline handler for vimc. */
std::vector<PipelineHandlerFactory *> &factories =
const std::vector<PipelineHandlerFactory *> &factories =
PipelineHandlerFactory::factories();
for (PipelineHandlerFactory *factory : factories) {
for (const PipelineHandlerFactory *factory : factories) {
if (factory->name() == "PipelineHandlerVimc") {
pipe_ = factory->create(nullptr);
break;
Expand Down

0 comments on commit ba3a1ad

Please sign in to comment.