From a2906fe28bf83723acb7223091fff9cf3582c7da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20K=2E=20Guti=C3=A9rrez?= Date: Thu, 14 Mar 2024 13:19:30 -0600 Subject: [PATCH] Add qvi_delete(). (#87) Signed-off-by: Samuel K. Gutierrez --- src/qvi-hwpool.cc | 7 +------ src/qvi-utils.h | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/qvi-hwpool.cc b/src/qvi-hwpool.cc index 36fe1819..5b09b969 100644 --- a/src/qvi-hwpool.cc +++ b/src/qvi-hwpool.cc @@ -239,12 +239,7 @@ void qvi_hwpool_free( qvi_hwpool_t **rpool ) { - if (!rpool) return; - qvi_hwpool_t *irpool = *rpool; - if (!irpool) goto out; - delete irpool; -out: - *rpool = nullptr; + qvi_delete(rpool); } int diff --git a/src/qvi-utils.h b/src/qvi-utils.h index fb618313..2d240f04 100644 --- a/src/qvi-utils.h +++ b/src/qvi-utils.h @@ -21,6 +21,22 @@ #ifdef __cplusplus +/** + * Simple wrapper around delete that also nullifies the input pointer. + */ +template +void +qvi_delete( + T **t +) { + if (!t) return; + T *it = *t; + if (!it) goto out; + delete it; +out: + *t = nullptr; +} + /** * Returns the code captured by a constructor. Since we are trying to avoid the * use of exceptions, we instead use this method for checking the state of an