From 2a9e5ed7f825c1a8e6adac6fe22e2caf0faba706 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Wed, 22 Jan 2025 07:19:48 +0800 Subject: [PATCH] camerad: add destructor to SpectraBuffer for proper resource cleanup (#34419) Add destructor to SpectraBuffer for proper resource cleanup Co-authored-by: Adeeb Shihadeh --- system/camerad/cameras/spectra.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/system/camerad/cameras/spectra.h b/system/camerad/cameras/spectra.h index 689fcb9cc3..92562c2d7b 100644 --- a/system/camerad/cameras/spectra.h +++ b/system/camerad/cameras/spectra.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -72,10 +73,21 @@ public: class SpectraBuf { public: + SpectraBuf() = default; + + ~SpectraBuf() { + if (video_fd >= 0 && ptr) { + munmap(ptr, mmap_size); + release(video_fd, handle); + } + } + void init(SpectraMaster *m, int s, int a, int flags, int mmu_hdl = 0, int mmu_hdl2 = 0, int count=1) { + video_fd = m->video0_fd; size = s; alignment = a; - void *p = alloc_w_mmu_hdl(m->video0_fd, ALIGNED_SIZE(size, alignment)*count, (uint32_t*)&handle, alignment, flags, mmu_hdl, mmu_hdl2); + mmap_size = aligned_size() * count; + void *p = alloc_w_mmu_hdl(video_fd, mmap_size, (uint32_t*)&handle, alignment, flags, mmu_hdl, mmu_hdl2); ptr = (unsigned char*)p; assert(ptr != NULL); }; @@ -84,8 +96,9 @@ public: return ALIGNED_SIZE(size, alignment); }; - unsigned char *ptr; - int size, alignment, handle; + int video_fd = -1; + unsigned char *ptr = nullptr; + int size = 0, alignment = 0, handle = 0, mmap_size = 0; }; class SpectraCamera {