This is an automated email from the ASF dual-hosted git repository.

pengzheng pushed a commit to branch feature/574-scope-based-resource-management
in repository https://gitbox.apache.org/repos/asf/celix.git

commit 37ea7c06c5b53782879b139218c3af3be986ef2e
Author: PengZheng <[email protected]>
AuthorDate: Tue Jul 18 20:16:09 2023 +0800

    Add initial implementation of scope-based resource management for Celix.
---
 libs/utils/CMakeLists.txt          |  1 +
 libs/utils/include/celix_cleanup.h | 75 ++++++++++++++++++++++++++++++++++++++
 libs/utils/src/celix_cleanup.c     | 20 ++++++++++
 3 files changed, 96 insertions(+)

diff --git a/libs/utils/CMakeLists.txt b/libs/utils/CMakeLists.txt
index e9362dc0..e1caefc4 100644
--- a/libs/utils/CMakeLists.txt
+++ b/libs/utils/CMakeLists.txt
@@ -47,6 +47,7 @@ set(UTILS_SRC
         src/celix_convert_utils.c
         src/celix_errno.c
         src/celix_err.c
+        src/celix_cleanup.c
         ${MEMSTREAM_SOURCES}
         )
 set(UTILS_PRIVATE_DEPS libzip::libzip)
diff --git a/libs/utils/include/celix_cleanup.h 
b/libs/utils/include/celix_cleanup.h
new file mode 100644
index 00000000..568ad733
--- /dev/null
+++ b/libs/utils/include/celix_cleanup.h
@@ -0,0 +1,75 @@
+//  Licensed to the Apache Software Foundation (ASF) under one
+//  or more contributor license agreements.  See the NOTICE file
+//  distributed with this work for additional information
+//  regarding copyright ownership.  The ASF licenses this file
+//  to you under the Apache License, Version 2.0 (the
+//  "License"); you may not use this file except in compliance
+//  with the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing,
+//  software distributed under the License is distributed on an
+//  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+//  KIND, either express or implied.  See the License for the
+//  specific language governing permissions and limitations
+//  under the License.
+
+#ifndef CELIX_CELIX_CLEANUP_H
+#define CELIX_CELIX_CLEANUP_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdlib.h>
+
+/* private */
+#define _CELIX_AUTOPTR_FUNC_NAME(TypeName) celix_autoptr_cleanup_##TypeName
+#define _CELIX_AUTOPTR_CLEAR_FUNC_NAME(TypeName) celix_autoptr_clear_##TypeName
+#define _CELIX_AUTOPTR_TYPENAME(TypeName) TypeName##_autoptr
+#define _CELIX_AUTO_FUNC_NAME(TypeName) celix_auto_cleanup_##TypeName
+#define _CELIX_CLEANUP(func) __attribute__((cleanup(func)))
+#define _CELIX_DEFINE_AUTOPTR_CLEANUP_FUNCS(TypeName, cleanup)                 
                                        \
+    typedef TypeName* _CELIX_AUTOPTR_TYPENAME(TypeName);                       
                                        \
+    static __attribute__((__unused__)) inline void 
_CELIX_AUTOPTR_CLEAR_FUNC_NAME(TypeName)(TypeName * _ptr) {         \
+        if (_ptr)                                                              
                                        \
+            (cleanup)(_ptr);                                                   
                             \
+    }                                                                          
                                        \
+    static __attribute__((__unused__)) inline void 
_CELIX_AUTOPTR_FUNC_NAME(TypeName)(TypeName** _ptr) {               \
+        _CELIX_AUTOPTR_CLEAR_FUNC_NAME(TypeName)(*_ptr);                       
                                        \
+    }
+
+
+/* API */
+#define CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) 
_CELIX_DEFINE_AUTOPTR_CLEANUP_FUNCS(TypeName, func)
+#define CELIX_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func)                   
                                        \
+    static __attribute__((__unused__)) inline void 
_CELIX_AUTO_FUNC_NAME(TypeName)(TypeName* _ptr) { (func)(_ptr); }
+#define CELIX_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none)              
                                        \
+    static __attribute__((__unused__)) inline void 
_CELIX_AUTO_FUNC_NAME(TypeName)(TypeName* _ptr)  {                  \
+        if (*_ptr != none)                                                     
                                        \
+            (func)(*_ptr);                                                     
                                        \
+    }
+
+#define celix_autoptr(TypeName) 
_CELIX_CLEANUP(_CELIX_AUTOPTR_FUNC_NAME(TypeName)) 
_CELIX_AUTOPTR_TYPENAME(TypeName)
+#define celix_auto(TypeName) _CELIX_CLEANUP(_CELIX_AUTO_FUNC_NAME(TypeName)) 
TypeName
+
+static inline void celix_autoptr_cleanup_generic_free(void* p) {
+    void** pp = (void**)p;
+    free(*pp);
+}
+
+#define celix_autofree _CELIX_CLEANUP(celix_autoptr_cleanup_generic_free)
+
+#ifdef __cplusplus
+#define celix_steal_ptr(p) \
+    ({ auto __ptr = (p); (p) = NULL; __ptr; })
+#else
+#define celix_steal_ptr(p) \
+    ({ __auto_type __ptr = (p); (p) = NULL; __ptr; })
+#endif
+
+
+#ifdef __cplusplus
+}
+#endif
+#endif // CELIX_CELIX_CLEANUP_H
diff --git a/libs/utils/src/celix_cleanup.c b/libs/utils/src/celix_cleanup.c
new file mode 100644
index 00000000..c2c8207b
--- /dev/null
+++ b/libs/utils/src/celix_cleanup.c
@@ -0,0 +1,20 @@
+//  Licensed to the Apache Software Foundation (ASF) under one
+//  or more contributor license agreements.  See the NOTICE file
+//  distributed with this work for additional information
+//  regarding copyright ownership.  The ASF licenses this file
+//  to you under the Apache License, Version 2.0 (the
+//  "License"); you may not use this file except in compliance
+//  with the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing,
+//  software distributed under the License is distributed on an
+//  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+//  KIND, either express or implied.  See the License for the
+//  specific language governing permissions and limitations
+//  under the License.
+
+#include "celix_cleanup.h"
+
+// empty source to make sure that the corresponding header is self-contained
\ No newline at end of file

Reply via email to