This is an automated email from the ASF dual-hosted git repository. pengzheng pushed a commit to branch feature/more-auto-cleanup in repository https://gitbox.apache.org/repos/asf/celix.git
commit 6dc674861a22902f3ab98e95e45eff640e2b99b0 Author: PengZheng <[email protected]> AuthorDate: Sun Aug 6 18:34:05 2023 +0800 Add autocleanup for celix_filter. --- bundles/pubsub/pubsub_utils/src/pubsub_utils.c | 5 +---- .../discovery_common/src/discovery.c | 8 ++------ .../remote_services/topology_manager/src/scope.c | 10 +++------- .../topology_manager/src/topology_manager.c | 12 +++--------- libs/framework/src/bundle_context.c | 8 ++++---- libs/framework/src/framework.c | 21 +++++++++------------ libs/framework/src/service_registry.c | 3 +-- libs/utils/gtest/src/FilterTestSuite.cc | 4 ++++ libs/utils/include/celix_filter.h | 3 +++ 9 files changed, 30 insertions(+), 44 deletions(-) diff --git a/bundles/pubsub/pubsub_utils/src/pubsub_utils.c b/bundles/pubsub/pubsub_utils/src/pubsub_utils.c index 2ec9bfb7..e003a218 100644 --- a/bundles/pubsub/pubsub_utils/src/pubsub_utils.c +++ b/bundles/pubsub/pubsub_utils/src/pubsub_utils.c @@ -52,7 +52,7 @@ celix_status_t pubsub_getPubSubInfoFromFilter(const char* filterstr, char **scop const char *scope = NULL; const char *topic = NULL; const char *objectClass = NULL; - celix_filter_t *filter = celix_filter_create(filterstr); + celix_autoptr(celix_filter_t) filter = celix_filter_create(filterstr); scope = celix_filter_findAttribute(filter, PUBSUB_PUBLISHER_SCOPE); topic = celix_filter_findAttribute(filter, PUBSUB_PUBLISHER_TOPIC); objectClass = celix_filter_findAttribute(filter, OSGI_FRAMEWORK_OBJECTCLASS); @@ -75,9 +75,6 @@ celix_status_t pubsub_getPubSubInfoFromFilter(const char* filterstr, char **scop *scopeOut = NULL; } - if (filter != NULL) { - filter_destroy(filter); - } return status; } diff --git a/bundles/remote_services/discovery_common/src/discovery.c b/bundles/remote_services/discovery_common/src/discovery.c index 3e582765..2387cb76 100644 --- a/bundles/remote_services/discovery_common/src/discovery.c +++ b/bundles/remote_services/discovery_common/src/discovery.c @@ -91,7 +91,7 @@ celix_status_t discovery_endpointListenerAdded(void* handle, service_reference_p const char *scope = NULL; serviceReference_getProperty(reference, OSGI_ENDPOINT_LISTENER_SCOPE, &scope); - celix_filter_t *filter = celix_filter_create(scope); + celix_autoptr(celix_filter_t) filter = celix_filter_create(scope); if (discoveryListener != NULL && strcmp(discoveryListener, "true") == 0) { celix_logHelper_info(discovery->loghelper, "EndpointListener Ignored - Discovery listener"); @@ -119,8 +119,6 @@ celix_status_t discovery_endpointListenerAdded(void* handle, service_reference_p celixThreadMutex_unlock(&discovery->mutex); } - celix_filter_destroy(filter); - return status; } @@ -170,7 +168,7 @@ celix_status_t discovery_informEndpointListeners(discovery_t *discovery, endpoin const char* scope = NULL; serviceReference_getProperty(reference, OSGI_ENDPOINT_LISTENER_SCOPE, &scope); - celix_filter_t *filter = celix_filter_create(scope); + celix_autoptr(celix_filter_t) filter = celix_filter_create(scope); bool matchResult = celix_filter_match(filter, endpoint->properties); if (matchResult) { bundleContext_getService(discovery->context, reference, (void **) &listener); @@ -185,8 +183,6 @@ celix_status_t discovery_informEndpointListeners(discovery_t *discovery, endpoin } bundleContext_ungetService(discovery->context, reference, NULL); } - - celix_filter_destroy(filter); } hashMapIterator_destroy(iter); } diff --git a/bundles/remote_services/topology_manager/src/scope.c b/bundles/remote_services/topology_manager/src/scope.c index 76e2bcc4..c4ac6b7a 100644 --- a/bundles/remote_services/topology_manager/src/scope.c +++ b/bundles/remote_services/topology_manager/src/scope.c @@ -113,11 +113,9 @@ celix_status_t tm_addImportScope(void *handle, char *filter) { celix_status_t status = CELIX_SUCCESS; scope_pt scope = (scope_pt) handle; - filter_pt new; - if (handle == NULL) return CELIX_ILLEGAL_ARGUMENT; - new = filter_create(filter); + celix_autoptr(celix_filter_t) new = celix_filter_create(filter); if (new == NULL) { return CELIX_ILLEGAL_ARGUMENT; // filter not parsable } @@ -125,9 +123,8 @@ celix_status_t tm_addImportScope(void *handle, char *filter) { int index = arrayList_indexOf(scope->importScopes, new); filter_pt present = (filter_pt) arrayList_get(scope->importScopes, index); if (present == NULL) { - arrayList_add(scope->importScopes, new); + arrayList_add(scope->importScopes, celix_steal_ptr(new)); } else { - filter_destroy(new); status = CELIX_ILLEGAL_ARGUMENT; } @@ -300,7 +297,7 @@ celix_status_t scope_getExportProperties(scope_pt scope, service_reference_pt re while ((!found) && hashMapIterator_hasNext(scopedPropIter)) { hash_map_entry_pt scopedEntry = hashMapIterator_nextEntry(scopedPropIter); char *filterStr = (char *) hashMapEntry_getKey(scopedEntry); - filter_pt filter = filter_create(filterStr); + celix_autoptr(celix_filter_t) filter = celix_filter_create(filterStr); if (filter != NULL) { // test if the scope filter matches the exported service properties status = filter_match(filter, serviceProperties, &found); @@ -309,7 +306,6 @@ celix_status_t scope_getExportProperties(scope_pt scope, service_reference_pt re *props = item->props; } } - filter_destroy(filter); } hashMapIterator_destroy(scopedPropIter); celix_properties_destroy(serviceProperties); diff --git a/bundles/remote_services/topology_manager/src/topology_manager.c b/bundles/remote_services/topology_manager/src/topology_manager.c index 2cb24fc1..ad1d3c23 100644 --- a/bundles/remote_services/topology_manager/src/topology_manager.c +++ b/bundles/remote_services/topology_manager/src/topology_manager.c @@ -317,7 +317,7 @@ celix_status_t topologyManager_exportScopeChanged(void *handle, char *filterStr) const char* serviceId = NULL; bool found; celix_properties_t *props; - filter_pt filter = filter_create(filterStr); + celix_autoptr(celix_filter_t) filter = celix_filter_create(filterStr); if (filter == NULL) { printf("filter creating failed\n"); @@ -382,9 +382,6 @@ celix_status_t topologyManager_exportScopeChanged(void *handle, char *filterStr) // should unlock until here ?, avoid srvRefs[i] is released during topologyManager_removeExportedService celixThreadMutex_unlock(&manager->lock); - - filter_destroy(filter); - return status; } @@ -683,7 +680,7 @@ celix_status_t topologyManager_endpointListenerAdded(void* handle, service_refer serviceReference_getProperty(reference, OSGI_ENDPOINT_LISTENER_SCOPE, &scope); - filter_pt filter = filter_create(scope); + celix_autoptr(celix_filter_t) filter = celix_filter_create(scope); hash_map_iterator_pt refIter = hashMapIterator_create(manager->exportedServices); @@ -720,8 +717,6 @@ celix_status_t topologyManager_endpointListenerAdded(void* handle, service_refer celixThreadMutex_unlock(&manager->lock); - filter_destroy(filter); - return status; } @@ -765,7 +760,7 @@ static celix_status_t topologyManager_notifyListenersEndpointAdded(topology_mana status = bundleContext_getService(manager->context, reference, (void **) &epl); if (status == CELIX_SUCCESS) { - filter_pt filter = filter_create(scope); + celix_autoptr(celix_filter_t) filter = celix_filter_create(scope); int regSize = celix_arrayList_size(registrations); for (int regIt = 0; regIt < regSize; regIt++) { @@ -782,7 +777,6 @@ static celix_status_t topologyManager_notifyListenersEndpointAdded(topology_mana status = substatus; } } - filter_destroy(filter); bundleContext_ungetService(manager->context, reference, NULL); } } diff --git a/libs/framework/src/bundle_context.c b/libs/framework/src/bundle_context.c index 05541e16..eb970f02 100644 --- a/libs/framework/src/bundle_context.c +++ b/libs/framework/src/bundle_context.c @@ -1391,12 +1391,13 @@ static celix_status_t bundleContext_callServicedTrackerTrackerCallback(void *han celix_bundle_t *bnd = NULL; bundleContext_getBundle(info->context, &bnd); + celix_autoptr(celix_filter_t) filter = celix_filter_create(info->filter); celix_service_tracker_info_t trkInfo; memset(&trkInfo, 0, sizeof(trkInfo)); trkInfo.bundleId = celix_bundle_getId(bnd); - trkInfo.filter = celix_filter_create(info->filter); - trkInfo.serviceName = celix_filter_findAttribute(trkInfo.filter, OSGI_FRAMEWORK_OBJECTCLASS); - const char *filterSvcName = celix_filter_findAttribute(trkInfo.filter, OSGI_FRAMEWORK_OBJECTCLASS); + trkInfo.filter = filter; + trkInfo.serviceName = celix_filter_findAttribute(filter, OSGI_FRAMEWORK_OBJECTCLASS); + const char *filterSvcName = celix_filter_findAttribute(filter, OSGI_FRAMEWORK_OBJECTCLASS); bool match = entry->serviceName == NULL || (filterSvcName != NULL && strncmp(filterSvcName, entry->serviceName, 1024*1024) == 0); @@ -1405,7 +1406,6 @@ static celix_status_t bundleContext_callServicedTrackerTrackerCallback(void *han } else if (!add && entry->remove != NULL && match) { entry->remove(entry->callbackHandle, &trkInfo); } - celix_filter_destroy(trkInfo.filter); } } return CELIX_SUCCESS; diff --git a/libs/framework/src/framework.c b/libs/framework/src/framework.c index c5fed072..fc61a53b 100644 --- a/libs/framework/src/framework.c +++ b/libs/framework/src/framework.c @@ -808,20 +808,17 @@ celix_status_t fw_registerServiceFactory(framework_pt framework, service_registr celix_status_t fw_getServiceReferences(framework_pt framework, array_list_pt *references, bundle_pt bundle, const char * serviceName, const char * sfilter) { celix_status_t status = CELIX_SUCCESS; - filter_pt filter = NULL; - unsigned int refIdx = 0; + celix_autoptr(celix_filter_t) filter = NULL; + unsigned int refIdx = 0; if (sfilter != NULL) { - filter = filter_create(sfilter); - } + filter = celix_filter_create(sfilter); + } - status = CELIX_DO_IF(status, serviceRegistry_getServiceReferences(framework->registry, bundle, serviceName, filter, references)); + status = CELIX_DO_IF(status, serviceRegistry_getServiceReferences(framework->registry, bundle, serviceName, filter, references)); - if (filter != NULL) { - filter_destroy(filter); - } - if (status == CELIX_SUCCESS) { + if (status == CELIX_SUCCESS) { for (refIdx = 0; (*references != NULL) && refIdx < arrayList_size(*references); refIdx++) { service_reference_pt ref = (service_reference_pt) arrayList_get(*references, refIdx); service_registration_pt reg = NULL; @@ -838,11 +835,11 @@ celix_status_t fw_getServiceReferences(framework_pt framework, array_list_pt *re } } } - } + } - framework_logIfError(framework->logger, status, NULL, "Failed to get service references"); + framework_logIfError(framework->logger, status, NULL, "Failed to get service references"); - return status; + return status; } celix_status_t fw_getBundleRegisteredServices(framework_pt framework, bundle_pt bundle, array_list_pt *services) { diff --git a/libs/framework/src/service_registry.c b/libs/framework/src/service_registry.c index 4de6ee02..c1e5ac3b 100644 --- a/libs/framework/src/service_registry.c +++ b/libs/framework/src/service_registry.c @@ -878,7 +878,7 @@ celix_array_list_t* celix_serviceRegisrty_findServices( celix_service_registry_t* registry, const char* filterStr) { - celix_filter_t* filter = celix_filter_create(filterStr); + celix_autoptr(celix_filter_t) filter = celix_filter_create(filterStr); if (filter == NULL) { celix_framework_log(registry->framework->logger, CELIX_LOG_LEVEL_ERROR, __FUNCTION__, __BASE_FILE__, __LINE__, "Error incorrect filter."); @@ -913,7 +913,6 @@ celix_array_list_t* celix_serviceRegisrty_findServices( } celixThreadRwlock_unlock(®istry->lock); - celix_filter_destroy(filter); celix_arrayList_destroy(matchedRegistrations); return result; } diff --git a/libs/utils/gtest/src/FilterTestSuite.cc b/libs/utils/gtest/src/FilterTestSuite.cc index fc746c07..3a1b41c0 100644 --- a/libs/utils/gtest/src/FilterTestSuite.cc +++ b/libs/utils/gtest/src/FilterTestSuite.cc @@ -414,6 +414,10 @@ TEST_F(FilterTestSuite, filterMatch) { celix_filter_destroy(f3); } +TEST_F(FilterTestSuite, AutoCleanupTest) { + celix_autoptr(celix_filter_t) filter = celix_filter_create("(test_attr1=attr1)"); +} + #include "filter.h" TEST_F(FilterTestSuite, deprecatedApi) { auto* f1 = filter_create("(test_attr1=attr1)"); diff --git a/libs/utils/include/celix_filter.h b/libs/utils/include/celix_filter.h index 32fbc565..24287a8f 100644 --- a/libs/utils/include/celix_filter.h +++ b/libs/utils/include/celix_filter.h @@ -22,6 +22,7 @@ #include "celix_properties.h" #include "celix_array_list.h" +#include "celix_cleanup.h" #include "celix_utils_export.h" #ifdef __cplusplus @@ -65,6 +66,8 @@ CELIX_UTILS_EXPORT celix_filter_t* celix_filter_create(const char *filterStr); CELIX_UTILS_EXPORT void celix_filter_destroy(celix_filter_t *filter); +CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(celix_filter_t, celix_filter_destroy) + CELIX_UTILS_EXPORT bool celix_filter_match(const celix_filter_t *filter, const celix_properties_t* props); CELIX_UTILS_EXPORT bool celix_filter_matchFilter(const celix_filter_t *filter1, const celix_filter_t *filter2);
