From 93bf6e6901744f4bee2b673361ae81a97d3dd340 Mon Sep 17 00:00:00 2001 From: Thierry Bultel Date: Wed, 13 Feb 2019 11:30:53 +0100 Subject: alsa-transaction: simplify the cleanup This simplifies the invocation of cleanup, by only using AlsaMixerTransactionDelete that performs the 3 actions of cleanup, removal from list, and memory freeing. Fixes a bug at MixerExit, because the first transaction was not removed from the list and led to a double free error. Also added a boolean parameter to AlsaMixerTransactionObjectDelete (was AlsaMixerTransactionObjectForget before), that decides wether or not the found object must be destroyed with its destructor (for most of the cases) or simply freed in memory (which is needed for loop device). Change-Id: I2eacbf80a22e3d556dc432d393a1807fcd7c47fb Signed-off-by: Thierry Bultel --- plugins/alsa/alsa-api-loop.c | 2 +- plugins/alsa/alsa-api-mixer.c | 39 ++++++++++++++++----------------------- plugins/alsa/alsa-transaction.c | 30 ++++++++++++++++++------------ plugins/alsa/alsa-transaction.h | 4 +--- 4 files changed, 36 insertions(+), 39 deletions(-) (limited to 'plugins/alsa') diff --git a/plugins/alsa/alsa-api-loop.c b/plugins/alsa/alsa-api-loop.c index 7e852a8..bc3c62a 100644 --- a/plugins/alsa/alsa-api-loop.c +++ b/plugins/alsa/alsa-api-loop.c @@ -117,7 +117,7 @@ STATIC int CheckOneSubdev(SoftMixerT *mixer, AlsaSndLoopT *loop, AlsaLoopSubdevT pcmCtl->closeAtDeletion = true; // free PCM as we only open loop to assert it's a valid capture device - AlsaMixerTransactionObjectForget(mixer->transaction, pcmCtl); + AlsaMixerTransactionObjectDelete(mixer->transaction, pcmCtl, false); AlsaPcmCtlDelete(mixer, pcmCtl); return 0; diff --git a/plugins/alsa/alsa-api-mixer.c b/plugins/alsa/alsa-api-mixer.c index 328f02e..c000ea8 100644 --- a/plugins/alsa/alsa-api-mixer.c +++ b/plugins/alsa/alsa-api-mixer.c @@ -32,7 +32,7 @@ static json_object *LoopsJ = NULL; static void MixerExit() { SoftMixerT *mixer, *tmp; - printf("%s !\n", __func__); + printf("-------------------------- %s ------------------------!\n", __func__); cds_list_for_each_entry_safe(mixer, tmp, &mixerList, list) { // remove this mixer from the global mixer list @@ -41,13 +41,12 @@ static void MixerExit() { AlsaMixerTransaction * transaction, * tmp_trans; cds_list_for_each_entry_safe(transaction, tmp_trans, &mixer->transactionList, transaction_node) { - cds_list_del(&transaction->transaction_node); - AlsaMixerTransactionDoCleanup(transaction); AlsaMixerTransactionDelete(transaction); } + AFB_API_INFO(mixer->api, "Mixer %s terminated", mixer->uid); MixerDelete(mixer); } - printf("%s DONE ! Bye !\n", __func__); + printf("------------------------- %s DONE ! Bye ! --------------\n", __func__); } CTLP_ONLOAD(plugin, callbacks){ @@ -582,7 +581,7 @@ STATIC void MixerAttachVerb(AFB_ReqT request) { if (playbacksJ) { error = ApiSinkAttach(mixer, request, uid, playbacksJ); - if (error) goto fail_loop; + if (error) goto fail; json_object *resultJ = MixerInfoPcms(mixer, playbacksJ, SND_PCM_STREAM_PLAYBACK, 0); json_object_object_add(responseJ, "playbacks", resultJ); @@ -594,7 +593,7 @@ STATIC void MixerAttachVerb(AFB_ReqT request) { error = ApiSourceAttach(mixer, request, uid, capturesJ); if (error) { AFB_ApiError(mixer->api,"%s: source attach failed", __func__); - goto fail_sink; + goto fail; } json_object *resultJ = MixerInfoPcms(mixer, capturesJ, SND_PCM_STREAM_CAPTURE, 0); @@ -606,7 +605,7 @@ STATIC void MixerAttachVerb(AFB_ReqT request) { if (zonesJ) { error = ApiZoneAttach(mixer, request, uid, zonesJ); if (error) - goto fail_source; + goto fail; json_object *resultJ = MixerInfoZones(mixer, zonesJ, 0); json_object_object_add(responseJ, "zone", resultJ); @@ -627,7 +626,7 @@ STATIC void MixerAttachVerb(AFB_ReqT request) { if (rampsJ) { error = ApiRampAttach(mixer, request, uid, rampsJ); if (error) - goto fail_zone; + goto fail; json_object *resultJ = MixerInfoRamps(mixer, rampsJ, 0); json_object_object_add(responseJ, "ramps", resultJ); @@ -638,7 +637,7 @@ STATIC void MixerAttachVerb(AFB_ReqT request) { if (streamsJ) { error = ApiStreamAttach(mixer, request, uid, prefix, streamsJ); if (error) - goto fail_ramp; + goto fail; json_object *resultJ = MixerInfoStreams(mixer, streamsJ, 0); json_object_object_add(responseJ, "streams", resultJ); @@ -648,7 +647,7 @@ STATIC void MixerAttachVerb(AFB_ReqT request) { if (error) { AFB_ApiError(mixer->api, "%s mixer=%s verb=%s fail to register post attach Verb ", __func__, mixer->uid, uid); - goto fail_stream; + goto fail; } AFB_ApiNotice(mixer->api, "%s responseJ=%s", __func__, json_object_get_string(responseJ)); @@ -657,22 +656,12 @@ STATIC void MixerAttachVerb(AFB_ReqT request) { AFB_ApiInfo(mixer->api,"%s DONE", __func__); return; -fail_stream: - // TODO remove created streams -fail_ramp: - // TODO remove created ramps -fail_zone: - // TODO remove created zone -fail_loop: - // TODO remove created loops -fail_source: - // TODO remove created sources -fail_sink: - // TODO remove created sinks fail: if (mixer->transaction) - free(mixer->transaction); + AlsaMixerTransactionDelete(mixer->transaction); + + mixer->transaction = NULL; AFB_ApiError(mixer->api,"%s FAILED", __func__); return; @@ -771,6 +760,10 @@ CTLP_CAPI(MixerAttach, source, argsJ, responseJ) { return 0; OnErrorExit: + if (mixer->transaction) + AlsaMixerTransactionDelete(mixer->transaction); + + mixer->transaction = NULL; return -1; } diff --git a/plugins/alsa/alsa-transaction.c b/plugins/alsa/alsa-transaction.c index dcfd028..a630bdc 100644 --- a/plugins/alsa/alsa-transaction.c +++ b/plugins/alsa/alsa-transaction.c @@ -6,16 +6,19 @@ #include "wrap-json.h" -void AlsaMixerTransactionDelete(AlsaMixerTransaction * transaction) { +static void AlsaMixerTransactionFree(AlsaMixerTransaction * transaction) { free((char*)transaction->uid); free(transaction); } AlsaMixerTransaction * AlsaMixerTransactionNew(struct SoftMixerT_ * mixer, const char * uid) { + AlsaMixerTransaction * transaction = (AlsaMixerTransaction *) malloc(sizeof(AlsaMixerTransaction)); if (transaction == NULL) goto fail; + AFB_API_INFO(mixer->api, "-------- NEW TRANSACTION %s --------", uid); + CDS_INIT_LIST_HEAD(&transaction->item_list); transaction->uid = strdup(uid); if (transaction->uid == NULL) { @@ -33,14 +36,15 @@ fail: return NULL; } -void AlsaMixerTransactionDataListDestroy(AlsaMixerTransaction* list) { - free(list); -} -void AlsaMixerTransactionObjectForget(AlsaMixerTransaction* list, void * object) { +void AlsaMixerTransactionObjectDelete(AlsaMixerTransaction* transaction, void * object, bool destructor) { AlsaMixerTransactionDataItem *item, *tmp; - cds_list_for_each_entry_safe(item, tmp, &list->item_list, list_entry) + cds_list_for_each_entry_safe(item, tmp, &transaction->item_list, list_entry) if (item->object == object) { + + if (destructor && item->destructor) + item->destructor(transaction->mixer, item->object); + cds_list_del(&item->list_entry); free(item); } @@ -88,7 +92,7 @@ fail: } -void AlsaMixerTransactionDoCleanup(AlsaMixerTransaction* transaction) { +static void AlsaMixerTransactionDoCleanup(AlsaMixerTransaction* transaction) { AlsaMixerTransactionDataItem * item, *sav; AFB_ApiInfo(transaction->mixer->api, "%s for transaction %s", __func__, transaction->uid); @@ -127,11 +131,6 @@ void AlsaMixerTransactionVerbCB(AFB_ReqT request) { } if (strcmp(action, "remove") == 0) { - AlsaMixerTransactionDoCleanup(transaction); - - // remove this transaction for the list of mixer - cds_list_del(&transaction->transaction_node); - error = afb_api_del_verb(transaction->mixer->api, transaction->uid, (void**)NULL); if (error) { AFB_ReqFail(request, "verb deletion" , "verb was not removed"); @@ -154,3 +153,10 @@ fail: return; } + +void AlsaMixerTransactionDelete(AlsaMixerTransaction * transaction) { + AlsaMixerTransactionDoCleanup(transaction); + // remove this transaction for the list of mixer + cds_list_del(&transaction->transaction_node); + AlsaMixerTransactionFree(transaction); +} diff --git a/plugins/alsa/alsa-transaction.h b/plugins/alsa/alsa-transaction.h index ba45ae7..9080518 100644 --- a/plugins/alsa/alsa-transaction.h +++ b/plugins/alsa/alsa-transaction.h @@ -32,9 +32,7 @@ extern void AlsaMixerTransactionDataListDestroy(AlsaMixerTransaction*); extern bool AlsaMixerTransactionObjectAdd(AlsaMixerTransaction*, void * object, AlsaTransactionDestructor destructor); extern bool AlsaMixerTransactionObjectAddTail(AlsaMixerTransaction* list, void* object, AlsaTransactionDestructor destructor); -extern void AlsaMixerTransactionObjectForget(AlsaMixerTransaction* list, void * object); - -extern void AlsaMixerTransactionDoCleanup(AlsaMixerTransaction*); +extern void AlsaMixerTransactionObjectDelete(AlsaMixerTransaction* list, void * object, bool destructor); extern bool AlsaMixerTransactionVerbCreate(AlsaMixerTransaction*); -- cgit 1.2.3-korg