From patchwork Sun Jun 25 04:41:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Sakamoto X-Patchwork-Id: 9807947 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B94606038C for ; Sun, 25 Jun 2017 04:41:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A3C7F2821F for ; Sun, 25 Jun 2017 04:41:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 96A93285E0; Sun, 25 Jun 2017 04:41:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3AEEC2821F for ; Sun, 25 Jun 2017 04:41:47 +0000 (UTC) Received: from alsa0.perex.cz (localhost [127.0.0.1]) by alsa0.perex.cz (Postfix) with ESMTP id 83567266B4B; Sun, 25 Jun 2017 06:41:43 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id 91149266D87; Sun, 25 Jun 2017 06:41:38 +0200 (CEST) Received: from smtp-proxy002.phy.lolipop.jp (smtp-proxy002.phy.lolipop.jp [157.7.104.43]) by alsa0.perex.cz (Postfix) with ESMTP id EC975266B53 for ; Sun, 25 Jun 2017 06:41:34 +0200 (CEST) Received: from smtp-proxy002.phy.lolipop.lan (HELO smtp-proxy002.phy.lolipop.jp) (172.19.44.43) (smtp-auth username m12129643-o-takashi, mechanism plain) by smtp-proxy002.phy.lolipop.jp (qpsmtpd/0.82) with ESMTPA; Sun, 25 Jun 2017 13:41:32 +0900 Received: from 127.0.0.1 (127.0.0.1) by smtp-proxy002.phy.lolipop.jp (LOLIPOP-Fsecure); Sun, 25 Jun 2017 13:41:24 +0900 (JST) X-Virus-Status: clean(LOLIPOP-Fsecure) From: Takashi Sakamoto To: tiwai@suse.de Date: Sun, 25 Jun 2017 13:41:22 +0900 Message-Id: <20170625044124.22234-5-o-takashi@sakamocchi.jp> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170625044124.22234-1-o-takashi@sakamocchi.jp> References: <20170625044124.22234-1-o-takashi@sakamocchi.jp> Cc: alsa-devel@alsa-project.org Subject: [alsa-devel] [PATCH v2 alsa-lib 4/6] pcm: hw: allocate fallback buffer in advance of trials of mapping X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP When allowing failure of map operation for both of status/control data for runtime of PCM substream, applications need to use fallback buffer for an alternative ioctl. However, in current implementation, status mapping is dominant to the allocation. This commit moves code for the allocation outside of the mapping operation for status data. --- src/pcm/pcm_hw.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/pcm/pcm_hw.c b/src/pcm/pcm_hw.c index 1d34956c..a3d1f137 100644 --- a/src/pcm/pcm_hw.c +++ b/src/pcm/pcm_hw.c @@ -867,21 +867,18 @@ static snd_pcm_sframes_t snd_pcm_hw_readn(snd_pcm_t *pcm, void **bufs, snd_pcm_u return xfern.result; } -static int map_status_data(snd_pcm_hw_t *hw) +static int map_status_data(snd_pcm_hw_t *hw, struct snd_pcm_sync_ptr *sync_ptr) { void *ptr; - int err; + ptr = MAP_FAILED; if (hw->sync_ptr_ioctl == 0) ptr = mmap(NULL, page_align(sizeof(struct snd_pcm_mmap_status)), PROT_READ, MAP_FILE|MAP_SHARED, hw->fd, SNDRV_PCM_MMAP_OFFSET_STATUS); if (ptr == MAP_FAILED || ptr == NULL) { - hw->sync_ptr = calloc(1, sizeof(struct snd_pcm_sync_ptr)); - if (hw->sync_ptr == NULL) - return -ENOMEM; - hw->mmap_status = &hw->sync_ptr->s.status; - hw->mmap_control = &hw->sync_ptr->c.control; + hw->mmap_status = &sync_ptr->s.status; + hw->mmap_control = &sync_ptr->c.control; hw->sync_ptr_ioctl = 1; } else { hw->mmap_status = ptr; @@ -894,7 +891,7 @@ static int map_control_data(snd_pcm_hw_t *hw) { void *ptr; int err; - if (hw->sync_ptr == NULL) { + if (hw->sync_ptr_ioctl == 0) { ptr = mmap(NULL, page_align(sizeof(struct snd_pcm_mmap_control)), PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, hw->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL); @@ -912,11 +909,18 @@ static int map_control_data(snd_pcm_hw_t *hw) static int map_status_and_control_data(snd_pcm_t *pcm, bool force_fallback) { snd_pcm_hw_t *hw = pcm->private_data; + struct snd_pcm_sync_ptr *sync_ptr; int err; + /* Preparation for fallback to failure of mmap(2). */ + sync_ptr = malloc(sizeof(*sync_ptr)); + if (sync_ptr == NULL) + return -ENOMEM; + memset(sync_ptr, 0, sizeof(*sync_ptr)); + hw->sync_ptr_ioctl = (int)force_fallback; - err = map_status_data(hw); + err = map_status_data(hw, sync_ptr); if (err < 0) return err; @@ -924,6 +928,14 @@ static int map_status_and_control_data(snd_pcm_t *pcm, bool force_fallback) if (err < 0) return err; + /* Any fallback mode needs to keep the buffer. */ + if (hw->sync_ptr_ioctl == 0) { + hw->sync_ptr = sync_ptr; + } else { + free(sync_ptr); + hw->sync_ptr = NULL; + } + /* Initialize the data. */ hw->mmap_control->appl_ptr = 0; hw->mmap_control->avail_min = 1;