[MEM] add putData function in HWmemImpl
authorYelin Jeong <yelini.jeong@samsung.com>
Wed, 21 Feb 2024 07:21:15 +0000 (16:21 +0900)
committerWook Song <wook16.song@samsung.com>
Tue, 18 Mar 2025 07:28:09 +0000 (16:28 +0900)
This patch adds putData function in HwmemImpl to munmap copied data.

Signed-off-by: Yelin Jeong <yelini.jeong@samsung.com>
src/core/ne-handler.cc
src/core/ne-hwmem.cc
src/core/ne-hwmem.h
tests/unittests/ne_core_hwmem_test.cc

index 5b0274835fbe5207740931ab216b6408cc3f3c95..351815a364d22870b794fd72371ba039f6aed702 100644 (file)
@@ -996,6 +996,7 @@ TrinityVision2::setModel (const generic_buffer *model_buf, Model **model_ptr) {
 
       status =
           comm_.extractGenericBuffer (model_buf, model->getData (), nullptr, 0, NPUBIN_META_SIZE);
+      model->putData();
       if (status != 0) {
         logerr (TAG, "Failed to extract generic buffer: %d\n", status);
         goto delete_exit;
@@ -1035,6 +1036,7 @@ TrinityVision2::setModel (const generic_buffer *model_buf, Model **model_ptr) {
     status = comm_.extractGenericBuffer (model_buf, hwmem_prog->getData (), nullptr,
                                          model->getMetadata ()->getMetaSize (),
                                          model->getMetadata ()->getProgramSize ());
+    hwmem_prog->putData ();
     if (status != 0) {
       logerr (TAG, "Failed to extract generic buffer: %d\n", status);
       goto delete_exit;
@@ -1068,6 +1070,7 @@ TrinityVision2::setModel (const generic_buffer *model_buf, Model **model_ptr) {
 
       status = comm_.extractGenericBuffer (model_buf, hwmem_extended->getData (), nullptr,
                                            NPUBIN_META_SIZE, extended_size);
+      hwmem_extended->putData();
       if (status != 0) {
         logerr (TAG, "Failed to extract generic buffer: %d\n", status);
         goto delete_exit;
@@ -1103,6 +1106,7 @@ TrinityVision2::setModel (const generic_buffer *model_buf, Model **model_ptr) {
         model_buf, hwmem_weight->getData (), nullptr,
         model->getMetadata ()->getMetaSize () + model->getMetadata ()->getProgramSize (),
         model->getMetadata ()->getWeightSize ());
+    hwmem_weight->putData();
     if (status != 0) {
       logerr (TAG, "Failed to extract generic buffer: %d\n", status);
       goto delete_exit;
index 4a65c944c8f265effaeeb49d7f61dad1dc89d75b..1c13b9ef0ae52e83d48fb7cdd123cec12cfddcfa 100644 (file)
@@ -44,6 +44,14 @@ HWmem::getData () {
   return impl_->getData (this);
 }
 
+void
+HWmem::putData () {
+  if (impl_ == nullptr)
+    return;
+
+  return impl_->putData (this);
+}
+
 /**
  * @brief allocate the hwmem data
  * @param[in] size size to allocate
@@ -80,18 +88,8 @@ HWmemDevice::cleanup (HWmem *hwmem) const {
   if (hwmem->getDriverAPI () == nullptr)
     return;
 
-  if (hwmem->getBaseAddr () != nullptr) {
-    hwmem->getDriverAPI ()->munmap (hwmem->getBaseAddr (), ALIGNED_SIZE (hwmem->getSize ()));
-
-    hwmem->setBaseAddr (nullptr);
-  }
-
-  if (hwmem->getDmabuf () >= 0) {
-    hwmem->getDriverAPI ()->dealloc (hwmem->getDmabuf (), hwmem->isContiguous ());
-
-    hwmem->setDmabuf (-1);
-    hwmem->setSize (0);
-  }
+  hwmem->putData();
+  hwmem->dealloc();
 }
 
 /**
@@ -167,6 +165,18 @@ HWmemDevice::getData (HWmem *hwmem) const {
   return static_cast<char *> (hwmem->getBaseAddr ()) + offset;
 }
 
+void
+HWmemDevice::putData (HWmem *hwmem) const {
+  if (hwmem->getDriverAPI () == nullptr)
+    return;
+
+  if (hwmem->getBaseAddr () != nullptr) {
+    hwmem->getDriverAPI ()->munmap (hwmem->getBaseAddr (), ALIGNED_SIZE (hwmem->getSize ()));
+
+    hwmem->setBaseAddr (nullptr);
+  }
+}
+
 /** Impl. of HWmemChunk */
 
 /**
index f552e3e6f4230fe6655029036d63fb1451e6121c..df856b32ea090efa4b9068aee56390179d4c33c0 100644 (file)
@@ -38,6 +38,7 @@ class HWmemImpl {
   virtual int alloc (HWmem *hwmem, size_t size) const { return 0; }
   virtual int dealloc (HWmem *hwmem) const { return 0; }
   virtual char *getData (HWmem *hwmem) const { return nullptr; }
+  virtual void putData(HWmem *hwmem) const {}
 };
 
 /** HWmem allocated by device */
@@ -47,6 +48,7 @@ class HWmemDevice : public HWmemImpl {
   int alloc (HWmem *hwmem, size_t size) const;
   int dealloc (HWmem *hwmem) const;
   char *getData (HWmem *hwmem) const;
+  void putData(HWmem *hwmem) const;
 };
 
 /** HWmem served by another hwmem */
@@ -92,6 +94,7 @@ class HWmem {
 
   /** the below APIs require its HWmem impl. */
   char *getData ();
+  void putData();
   int alloc (size_t size);
   int dealloc ();
 
index c6d487ccd05ca8cf867fc23eeeec6cb2ca78d2b3..e9316229d37061dc266366d7f596b82444dd9b38 100644 (file)
@@ -109,6 +109,7 @@ TEST (ne_core_hwmem_test, hwmem_device) {
   EXPECT_EQ (hwmem->alloc (size), 0);
   EXPECT_EQ (hwmem->getSize (), size);
   EXPECT_NE (hwmem->getData (), nullptr);
+  hwmem->putData();
   EXPECT_EQ (hwmem->dealloc (), 0);
 
   delete hwmem;