diff options
author | Jonathan Aillet <jonathan.aillet@iot.bzh> | 2019-06-14 17:59:20 +0200 |
---|---|---|
committer | Jonathan Aillet <jonathan.aillet@iot.bzh> | 2019-06-18 11:34:34 +0200 |
commit | 66f27bce6355cc7df978a9d7a6bc725542057a01 (patch) | |
tree | bed31fbbd60ec80a2f6f57f8182c326ae048d5b7 /lib | |
parent | 870d396127c2e5e247cb85a7921143e78ee81ce4 (diff) |
Refactoring 'probed devices' linked list
Refactoring 'probed devices' linked list code using liburcu.
BUG-AGL: SPEC-2329
Change-Id: I4d0255bc020733b1abbea1f3e6195c73cf8da8f6
Signed-off-by: Jonathan Aillet <jonathan.aillet@iot.bzh>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/4a-hal-utilities/4a-hal-utilities-data.c | 264 | ||||
-rw-r--r-- | lib/4a-hal-utilities/4a-hal-utilities-data.h | 27 |
2 files changed, 163 insertions, 128 deletions
diff --git a/lib/4a-hal-utilities/4a-hal-utilities-data.c b/lib/4a-hal-utilities/4a-hal-utilities-data.c index 6f85565..f3f4229 100644 --- a/lib/4a-hal-utilities/4a-hal-utilities-data.c +++ b/lib/4a-hal-utilities/4a-hal-utilities-data.c @@ -28,6 +28,108 @@ #include "4a-hal-utilities-alsa-data.h" /******************************************************************************* + * Internal Hal - Linked list cleaning function declaration * + ******************************************************************************/ + +void HalUtlFreeSelectedProbedDeviceAllocation(struct InternalHalProbedDevice *probedDeviceToFree); + +/******************************************************************************* + * Internal Hal - Generic function for linked list * + ******************************************************************************/ + +void *HalUtlAddNodeInList(struct cds_list_head *listHead, enum LinkedListType listType) +{ + void *returned = NULL; + struct cds_list_head *nodeToAdd; + + if(! listHead) + return NULL; + + switch(listType) { + case LINKED_LIST_FOR_DEPENDENCIES_DATA: + returned = calloc(1, sizeof(struct InternalHalProbedDevice)); + if(! returned) + return NULL; + nodeToAdd = &((struct InternalHalProbedDevice *) returned)->node; + break; + + + default: + return NULL; + } + + cds_list_add_tail(nodeToAdd, listHead); + + return returned; +} + +int HalUtlRemoveNodeFromList(struct cds_list_head *listHead, void *dataToRemove, enum LinkedListType listType) +{ + if(! listHead || cds_list_empty(listHead) || ! dataToRemove) + return -1; + + switch(listType) { + case LINKED_LIST_FOR_DEPENDENCIES_DATA: + cds_list_del(&((struct InternalHalProbedDevice *) dataToRemove)->node); + + HalUtlFreeSelectedProbedDeviceAllocation((struct InternalHalProbedDevice *) dataToRemove); + break; + + default: + return -2; + } + + return 0; +} + +int HalUtlRemoveAllNodeFromList(struct cds_list_head *listHead, enum LinkedListType listType) +{ + int nodesRemoved = 0; + + struct cds_list_head *nodeToRemove, *savedNode; + + if(! listHead) + return -1; + + if(cds_list_empty(listHead)) + return 0; + + cds_list_for_each_safe(nodeToRemove, savedNode, listHead) { + switch(listType) { + case LINKED_LIST_FOR_DEPENDENCIES_DATA: + if(HalUtlRemoveSelectedProbedDeviceFromList(listHead, + cds_list_entry(nodeToRemove, struct InternalHalProbedDevice, node))) + return -2; + break; + + default: + return -3; + } + + nodesRemoved++; + } + + return nodesRemoved; +} + +int HalUtlGetNumberOfNodesInList(struct cds_list_head *listHead) +{ + int numberOfNodes = 0; + struct cds_list_head *listNode; + + if(! listHead) + return -1; + + if(cds_list_empty(listHead)) + return 0; + + cds_list_for_each(listNode, listHead) + numberOfNodes++; + + return numberOfNodes; +} + +/******************************************************************************* * Internal Hal - Probed devices structure handling functions * ******************************************************************************/ @@ -97,136 +199,65 @@ int HalUtlIsDependencySelected(struct InternalHalProbedDevice *probedDevice, enu } } -struct InternalHalProbedDevice *HalUtlAddProbedDeviceToProbedDeviceList(struct InternalHalProbedDevice **probedDevicesList) +void HalUtlFreeSelectedProbedDeviceAllocation(struct InternalHalProbedDevice *probedDeviceToFree) { - struct InternalHalProbedDevice *currentProbedDevice; + if(! probedDeviceToFree) + return; - if(! probedDevicesList) - return NULL; + free(probedDeviceToFree->uid); - currentProbedDevice = *probedDevicesList; - if(! currentProbedDevice) { - currentProbedDevice = (struct InternalHalProbedDevice *) calloc(1, sizeof(struct InternalHalProbedDevice)); - if(! currentProbedDevice) - return NULL; + if(probedDeviceToFree->deviceData) { + free(probedDeviceToFree->deviceData->extendedCardNb); + free(probedDeviceToFree->deviceData->cardId); + free(probedDeviceToFree->deviceData->cardShortName); + free(probedDeviceToFree->deviceData->cardLongName); + free(probedDeviceToFree->deviceData->cardDriver); + free(probedDeviceToFree->deviceData->cardMixerName); + free(probedDeviceToFree->deviceData->cardComponents); + free(probedDeviceToFree->deviceData->playbackDeviceId); + free(probedDeviceToFree->deviceData->playbackDeviceName); - *probedDevicesList = currentProbedDevice; + free(probedDeviceToFree->deviceData); } - else { - while(currentProbedDevice->next) - currentProbedDevice = currentProbedDevice->next; - currentProbedDevice->next = calloc(1, sizeof(struct InternalHalProbedDevice)); - if(! currentProbedDevice->next) - return NULL; + if(probedDeviceToFree->requestedDeviceJ) + json_object_put(probedDeviceToFree->requestedDeviceJ); - currentProbedDevice = currentProbedDevice->next; - } + free(probedDeviceToFree); +} - return currentProbedDevice; +struct InternalHalProbedDevice *HalUtlAddProbedDeviceToProbedDeviceList(struct cds_list_head *probedDevicesListHead) +{ + return (struct InternalHalProbedDevice *) HalUtlAddNodeInList(probedDevicesListHead, LINKED_LIST_FOR_DEPENDENCIES_DATA); } -int HalUtlRemoveSelectedProbedDeviceFromList(struct InternalHalProbedDevice **probedDevicesList, +int HalUtlRemoveSelectedProbedDeviceFromList(struct cds_list_head *probedDevicesListHead, struct InternalHalProbedDevice *probedDeviceToRemove) { - struct InternalHalProbedDevice *currentProbedDevice, *matchingProbedDevice; - - if(! probedDevicesList || ! *probedDevicesList || ! probedDeviceToRemove) - return -1; - - currentProbedDevice = *probedDevicesList; - if(currentProbedDevice == probedDeviceToRemove) { - *probedDevicesList = currentProbedDevice->next; - matchingProbedDevice = currentProbedDevice; - } - else { - while(currentProbedDevice && currentProbedDevice->next != probedDeviceToRemove) - currentProbedDevice = currentProbedDevice->next; - - if(currentProbedDevice) { - matchingProbedDevice = currentProbedDevice->next; - currentProbedDevice->next = currentProbedDevice->next->next; - } - else { - return -2; - } - } - - free(matchingProbedDevice->uid); - - if(matchingProbedDevice->deviceData) { - free(matchingProbedDevice->deviceData->extendedCardNb); - free(matchingProbedDevice->deviceData->cardId); - free(matchingProbedDevice->deviceData->cardShortName); - free(matchingProbedDevice->deviceData->cardLongName); - free(matchingProbedDevice->deviceData->cardDriver); - free(matchingProbedDevice->deviceData->cardMixerName); - free(matchingProbedDevice->deviceData->cardComponents); - free(matchingProbedDevice->deviceData->playbackDeviceId); - free(matchingProbedDevice->deviceData->playbackDeviceName); - - free(matchingProbedDevice->deviceData); - } - - if(matchingProbedDevice->requestedDeviceJ) - json_object_put(matchingProbedDevice->requestedDeviceJ); - - free(matchingProbedDevice); - - return 0; + return HalUtlRemoveNodeFromList(probedDevicesListHead, (void *) probedDeviceToRemove, LINKED_LIST_FOR_DEPENDENCIES_DATA); } -int HalUtlRemoveAllProbedDevicesFromList(struct InternalHalProbedDevice **probedDevicesList) +int HalUtlRemoveAllProbedDevicesFromList(struct cds_list_head *probedDevicesListHead) { - int probedDevicesRemoved = 0; - - if(! probedDevicesList) - return -1; - - if(! *probedDevicesList) - return 0; - - while(*probedDevicesList) { - if(HalUtlRemoveSelectedProbedDeviceFromList(probedDevicesList, *probedDevicesList)) - return -2; - - probedDevicesRemoved++; - } - - return probedDevicesRemoved; + return HalUtlRemoveAllNodeFromList(probedDevicesListHead, LINKED_LIST_FOR_DEPENDENCIES_DATA); } -int HalUtlGetNumberOfProbedDevicesInList(struct InternalHalProbedDevice **probedDevicesList) +int HalUtlGetNumberOfProbedDevicesInList(struct cds_list_head *probedDevicesListHead) { - int numberOfProbedDevices = 0; - struct InternalHalProbedDevice *currentProbedDevice; - - if(! probedDevicesList) - return -1; - - currentProbedDevice = *probedDevicesList; - while(currentProbedDevice) { - currentProbedDevice = currentProbedDevice->next; - numberOfProbedDevices++; - } - - return numberOfProbedDevices; + return HalUtlGetNumberOfNodesInList(probedDevicesListHead); } -struct InternalHalProbedDevice *HalUtlSearchProbedDeviceDataById(struct InternalHalProbedDevice **probedDevicesList, +struct InternalHalProbedDevice *HalUtlSearchProbedDeviceDataById(struct cds_list_head *probedDevicesListHead, char *uid) { struct InternalHalProbedDevice *currentProbedDevice; - if(! probedDevicesList || ! *probedDevicesList || ! uid) + if(! probedDevicesListHead || cds_list_empty(probedDevicesListHead) || ! uid) return NULL; - currentProbedDevice = *probedDevicesList; - while(currentProbedDevice) { + cds_list_for_each_entry(currentProbedDevice, probedDevicesListHead, node) { if(! strcmp(uid, currentProbedDevice->uid)) return currentProbedDevice; - - currentProbedDevice = currentProbedDevice->next; } return NULL; @@ -428,7 +459,7 @@ json_object *HalUtlGetJsonForSpecificDependencies(afb_api_t apiHandle, } json_object *HalUtlGetJsonForSpecificDependenciesUsingUid(afb_api_t apiHandle, - struct InternalHalProbedDevice **probedDevicesList, + struct cds_list_head *probedDevicesListHead, char *uid, enum DependencyInfoJsonFormat jsonFormat) { @@ -441,7 +472,7 @@ json_object *HalUtlGetJsonForSpecificDependenciesUsingUid(afb_api_t apiHandle, return NULL; } - if(! probedDevicesList || ! *probedDevicesList) { + if(! probedDevicesListHead || cds_list_empty(probedDevicesListHead)) { AFB_API_ERROR(apiHandle, "Probed device list is empty"); return NULL; } @@ -451,7 +482,7 @@ json_object *HalUtlGetJsonForSpecificDependenciesUsingUid(afb_api_t apiHandle, return NULL; } - requestedProbedDevice = HalUtlSearchProbedDeviceDataById(probedDevicesList, uid); + requestedProbedDevice = HalUtlSearchProbedDeviceDataById(probedDevicesListHead, uid); if(! requestedProbedDevice) { AFB_API_ERROR(apiHandle, "Requested dependency uid ('%s) was not found in probed device list", @@ -471,7 +502,7 @@ json_object *HalUtlGetJsonForSpecificDependenciesUsingUid(afb_api_t apiHandle, } json_object *HalUtlGetJsonArrayForSelectedDependencies(afb_api_t apiHandle, - struct InternalHalProbedDevice **probedDevicesList, + struct cds_list_head *probedDevicesListHead, enum DependencyInfoJsonFormat jsonFormat, enum DependencyStatus requestedStatus) { @@ -484,7 +515,7 @@ json_object *HalUtlGetJsonArrayForSelectedDependencies(afb_api_t apiHandle, return NULL; } - if(! probedDevicesList || ! *probedDevicesList) { + if(! probedDevicesListHead || cds_list_empty(probedDevicesListHead)) { AFB_API_ERROR(apiHandle, "Probed device list is empty"); return NULL; } @@ -495,8 +526,7 @@ json_object *HalUtlGetJsonArrayForSelectedDependencies(afb_api_t apiHandle, return NULL; } - currentProbedDevice = *probedDevicesList; - while(currentProbedDevice) { + cds_list_for_each_entry(currentProbedDevice, probedDevicesListHead, node) { requestedProbedDeviceJ = NULL; if(HalUtlIsDependencySelected(currentProbedDevice, requestedStatus)) { @@ -514,15 +544,13 @@ json_object *HalUtlGetJsonArrayForSelectedDependencies(afb_api_t apiHandle, if(requestedProbedDeviceJ) json_object_array_add(requestedDependenciesInfoJ, requestedProbedDeviceJ); - - currentProbedDevice = currentProbedDevice->next; } return requestedDependenciesInfoJ; } json_object *HalUtlGetJsonArrayForAvailableDependencies(afb_api_t apiHandle, - struct InternalHalProbedDevice **probedDevicesList, + struct cds_list_head *probedDevicesListHead, enum DependencyInfoJsonFormat jsonFormat) { json_object *requestedDependenciesInfoJ; @@ -532,13 +560,13 @@ json_object *HalUtlGetJsonArrayForAvailableDependencies(afb_api_t apiHandle, return NULL; } - if(! probedDevicesList || ! *probedDevicesList) { + if(! probedDevicesListHead || cds_list_empty(probedDevicesListHead)) { AFB_API_ERROR(apiHandle, "Probed device list is empty"); return NULL; } requestedDependenciesInfoJ = HalUtlGetJsonArrayForSelectedDependencies(apiHandle, - probedDevicesList, + probedDevicesListHead, jsonFormat, AVAILABLE_DEPENDENCY); if(! requestedDependenciesInfoJ) { @@ -550,7 +578,7 @@ json_object *HalUtlGetJsonArrayForAvailableDependencies(afb_api_t apiHandle, } json_object *HalUtlGetJsonArrayForAllDependencies(afb_api_t apiHandle, - struct InternalHalProbedDevice **probedDevicesList, + struct cds_list_head *probedDevicesListHead, enum DependencyInfoJsonFormat jsonFormat) { json_object *requestedDependenciesInfoJ; @@ -560,13 +588,13 @@ json_object *HalUtlGetJsonArrayForAllDependencies(afb_api_t apiHandle, return NULL; } - if(! probedDevicesList || ! *probedDevicesList) { + if(! probedDevicesListHead || cds_list_empty(probedDevicesListHead)) { AFB_API_ERROR(apiHandle, "Probed device list is empty"); return NULL; } requestedDependenciesInfoJ = HalUtlGetJsonArrayForSelectedDependencies(apiHandle, - probedDevicesList, + probedDevicesListHead, jsonFormat, ALL_DEPENDENCY); if(! requestedDependenciesInfoJ) { @@ -834,7 +862,7 @@ int HalUtlRemoveSelectedHalFromList(struct HalData **halDataList, struct HalData free(matchingHal->internalHalData->mixerApiName); free(matchingHal->internalHalData->prefix); - HalUtlRemoveAllProbedDevicesFromList(&matchingHal->internalHalData->probedDevicesList); + HalUtlRemoveAllProbedDevicesFromList(&matchingHal->internalHalData->probedDevicesListHead); HalUtlRemoveAllMixerData(&matchingHal->internalHalData->streamsData); diff --git a/lib/4a-hal-utilities/4a-hal-utilities-data.h b/lib/4a-hal-utilities/4a-hal-utilities-data.h index c051373..8ac4c25 100644 --- a/lib/4a-hal-utilities/4a-hal-utilities-data.h +++ b/lib/4a-hal-utilities/4a-hal-utilities-data.h @@ -20,6 +20,8 @@ #include <stdio.h> +#include <urcu/list.h> + #include <wrap-json.h> #include <afb/afb-binding.h> @@ -32,6 +34,11 @@ #define HAL_STREAM_UPDATES_EVENT_NAME "stream-updates" +// Enum for linked list type +enum LinkedListType { + LINKED_LIST_FOR_DEPENDENCIES_DATA = 0 +}; + // Enum for hal status enum HalStatus { HAL_STATUS_UNAVAILABLE = 0, @@ -83,7 +90,7 @@ struct InternalHalProbedDevice { struct InternalHalDeviceData *deviceData; - struct InternalHalProbedDevice *next; + struct cds_list_head node; }; // Structure to store stream data @@ -102,7 +109,7 @@ struct InternalHalData { char *prefix; json_object *halMixerJ; - struct InternalHalProbedDevice *probedDevicesList; + struct cds_list_head probedDevicesListHead; struct InternalHalMixerData *streamsData; afb_event_t streamUpdates; @@ -146,26 +153,26 @@ struct HalMgrData { // Internal Hal - Probed devices structure handling functions enum ProbedDeviceClasses HalUtlGetProbedDeviceClassFromString(char *probedDeviceString); char *HalUtlGetProbedDeviceClassString(enum ProbedDeviceClasses deviceClass); -struct InternalHalProbedDevice *HalUtlAddProbedDeviceToProbedDeviceList(struct InternalHalProbedDevice **probedDevicesList); -int HalUtlRemoveSelectedProbedDeviceFromList(struct InternalHalProbedDevice **probedDevicesList, +struct InternalHalProbedDevice *HalUtlAddProbedDeviceToProbedDeviceList(struct cds_list_head *probedDevicesListHead); +int HalUtlRemoveSelectedProbedDeviceFromList(struct cds_list_head *probedDevicesListHead, struct InternalHalProbedDevice *probedDeviceToRemove); -int HalUtlRemoveAllProbedDevicesFromList(struct InternalHalProbedDevice **probedDevicesList); -int HalUtlGetNumberOfProbedDevicesInList(struct InternalHalProbedDevice **probedDevicesList); -struct InternalHalProbedDevice *HalUtlSearchProbedDeviceDataById(struct InternalHalProbedDevice **probedDevicesList, +int HalUtlRemoveAllProbedDevicesFromList(struct cds_list_head *probedDevicesListHead); +int HalUtlGetNumberOfProbedDevicesInList(struct cds_list_head *probedDevicesListHead); +struct InternalHalProbedDevice *HalUtlSearchProbedDeviceDataById(struct cds_list_head *probedDevicesListHead, char *uid); struct InternalHalDeviceData *HalUtlAllocateAndFillProbedDeviceDataUsingInfoGetResponse(json_object *responseJ); json_object *HalUtlGetJsonForSpecificDependencies(afb_api_t apiHandle, struct InternalHalProbedDevice *requestedProbedDevice, enum DependencyInfoJsonFormat jsonFormat); json_object *HalUtlGetJsonForSpecificDependenciesUsingUid(afb_api_t apiHandle, - struct InternalHalProbedDevice **probedDevicesList, + struct cds_list_head *probedDevicesListHead, char *uid, enum DependencyInfoJsonFormat jsonFormat); json_object *HalUtlGetJsonArrayForAvailableDependencies(afb_api_t apiHandle, - struct InternalHalProbedDevice **probedDevicesList, + struct cds_list_head *probedDevicesListHead, enum DependencyInfoJsonFormat jsonFormat); json_object *HalUtlGetJsonArrayForAllDependencies(afb_api_t apiHandle, - struct InternalHalProbedDevice **probedDevicesList, + struct cds_list_head *probedDevicesListHead, enum DependencyInfoJsonFormat jsonFormat); // Internal Hal - Streams data handling functions |