libcamera: software_isp: Replace malloc() with new[]()
diff mbox series

Message ID 20240803211007.18330-1-laurent.pinchart@ideasonboard.com
State Superseded
Headers show
Series
  • libcamera: software_isp: Replace malloc() with new[]()
Related show

Commit Message

Laurent Pinchart Aug. 3, 2024, 9:10 p.m. UTC
libcamera is implemented in C++, use new[]() instead of malloc() (and
delete[]() instead of free()).

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/libcamera/software_isp/debayer_cpu.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)


base-commit: 19bbca3c0b376ba0183f5db53472c8c46cd402b5

Comments

Barnabás Pőcze Aug. 3, 2024, 10:59 p.m. UTC | #1
2024. augusztus 3., szombat 23:10 keltezéssel, Laurent Pinchart <laurent.pinchart@ideasonboard.com> írta:

> libcamera is implemented in C++, use new[]() instead of malloc() (and
> delete[]() instead of free()).
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  src/libcamera/software_isp/debayer_cpu.cpp | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp
> index f8d2677d657a..249294efca01 100644
> --- a/src/libcamera/software_isp/debayer_cpu.cpp
> +++ b/src/libcamera/software_isp/debayer_cpu.cpp
> @@ -57,7 +57,7 @@ DebayerCpu::DebayerCpu(std::unique_ptr<SwStatsCpu> stats)
>  DebayerCpu::~DebayerCpu()
>  {
>  	for (unsigned int i = 0; i < kMaxLineBuffers; i++)
> -		free(lineBuffers_[i]);
> +		delete[] lineBuffers_[i];
>  }
> 
>  #define DECLARE_SRC_POINTERS(pixel_t)                            \
> @@ -529,8 +529,8 @@ int DebayerCpu::configure(const StreamConfiguration &inputCfg,
>  	for (unsigned int i = 0;
>  	     i < (inputConfig_.patternSize.height + 1) && enableInputMemcpy_;
>  	     i++) {
> -		free(lineBuffers_[i]);
> -		lineBuffers_[i] = (uint8_t *)malloc(lineBufferLength_);
> +		delete[] lineBuffers_[i];
> +		lineBuffers_[i] = new uint8_t[lineBufferLength_];
>  		if (!lineBuffers_[i])

The condition can now never be true.

Also, why not use `std::unique_ptr<uint8_t[]> lineBuffers_[kMaxLineBuffers];` instead?


Regards,
Barnabás Pőcze


>  			return -ENOMEM;
>  	}
> 
> base-commit: 19bbca3c0b376ba0183f5db53472c8c46cd402b5
> --
> Regards,
> 
> Laurent Pinchart
> 
>
Laurent Pinchart Aug. 4, 2024, 11:44 a.m. UTC | #2
Hi Barnabás,

On Sat, Aug 03, 2024 at 10:59:22PM +0000, Barnabás Pőcze wrote:
> 2024. augusztus 3., szombat 23:10 keltezéssel, Laurent Pinchart <laurent.pinchart@ideasonboard.com> írta:
> 
> > libcamera is implemented in C++, use new[]() instead of malloc() (and
> > delete[]() instead of free()).
> > 
> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > ---
> >  src/libcamera/software_isp/debayer_cpu.cpp | 6 +++---
> >  1 file changed, 3 insertions(+), 3 deletions(-)
> > 
> > diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp
> > index f8d2677d657a..249294efca01 100644
> > --- a/src/libcamera/software_isp/debayer_cpu.cpp
> > +++ b/src/libcamera/software_isp/debayer_cpu.cpp
> > @@ -57,7 +57,7 @@ DebayerCpu::DebayerCpu(std::unique_ptr<SwStatsCpu> stats)
> >  DebayerCpu::~DebayerCpu()
> >  {
> >  	for (unsigned int i = 0; i < kMaxLineBuffers; i++)
> > -		free(lineBuffers_[i]);
> > +		delete[] lineBuffers_[i];
> >  }
> > 
> >  #define DECLARE_SRC_POINTERS(pixel_t)                            \
> > @@ -529,8 +529,8 @@ int DebayerCpu::configure(const StreamConfiguration &inputCfg,
> >  	for (unsigned int i = 0;
> >  	     i < (inputConfig_.patternSize.height + 1) && enableInputMemcpy_;
> >  	     i++) {
> > -		free(lineBuffers_[i]);
> > -		lineBuffers_[i] = (uint8_t *)malloc(lineBufferLength_);
> > +		delete[] lineBuffers_[i];
> > +		lineBuffers_[i] = new uint8_t[lineBufferLength_];
> >  		if (!lineBuffers_[i])
> 
> The condition can now never be true.

Oops indeed. I'll fix that in v2.

> Also, why not use `std::unique_ptr<uint8_t[]> lineBuffers_[kMaxLineBuffers];` instead?

That, or `std::vector<uint8_t> lineBuffers_[kMaxLineBuffers]`. I'll do
that in v2.

> >  			return -ENOMEM;
> >  	}
> > 
> > base-commit: 19bbca3c0b376ba0183f5db53472c8c46cd402b5

Patch
diff mbox series

diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp
index f8d2677d657a..249294efca01 100644
--- a/src/libcamera/software_isp/debayer_cpu.cpp
+++ b/src/libcamera/software_isp/debayer_cpu.cpp
@@ -57,7 +57,7 @@  DebayerCpu::DebayerCpu(std::unique_ptr<SwStatsCpu> stats)
 DebayerCpu::~DebayerCpu()
 {
 	for (unsigned int i = 0; i < kMaxLineBuffers; i++)
-		free(lineBuffers_[i]);
+		delete[] lineBuffers_[i];
 }
 
 #define DECLARE_SRC_POINTERS(pixel_t)                            \
@@ -529,8 +529,8 @@  int DebayerCpu::configure(const StreamConfiguration &inputCfg,
 	for (unsigned int i = 0;
 	     i < (inputConfig_.patternSize.height + 1) && enableInputMemcpy_;
 	     i++) {
-		free(lineBuffers_[i]);
-		lineBuffers_[i] = (uint8_t *)malloc(lineBufferLength_);
+		delete[] lineBuffers_[i];
+		lineBuffers_[i] = new uint8_t[lineBufferLength_];
 		if (!lineBuffers_[i])
 			return -ENOMEM;
 	}