Path: blob/main/audio/alsa-plugins/files/patch-usb__stream_pcm__usb__stream.c
16462 views
based on https://github.com/dankamongmen/libdank/blob/master/libdank/compat-FreeBSD.c1--- usb_stream/pcm_usb_stream.c.orig 2024-06-10 09:18:39 UTC2+++ usb_stream/pcm_usb_stream.c3@@ -77,6 +77,69 @@ static pthread_mutex_t uus_mutex = PTHREAD_MUTEX_INITI4static struct user_usb_stream *uus;5static pthread_mutex_t uus_mutex = PTHREAD_MUTEX_INITIALIZER;67+#ifndef __linux__8+/*9+ * Copyright (c) 2000-2011, Nick Black et al10+ * All rights reserved.11+ *12+ * Redistribution and use in source and binary forms, with or without13+ * modification, are permitted provided that the following conditions are met:14+ * * Redistributions of source code must retain the above copyright15+ * notice, this list of conditions and the following disclaimer.16+ * * Redistributions in binary form must reproduce the above copyright17+ * notice, this list of conditions and the following disclaimer in the18+ * documentation and/or other materials provided with the distribution.19+ * * Neither the name of Nick Black nor the names of other contributors may20+ * be used to endorse or promote products derived from this software21+ * without specific prior written permission.22+ *23+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,24+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND25+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <copyright26+ * holder> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,27+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF28+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS29+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN30+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)31+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE32+ * POSSIBILITY OF SUCH DAMAGE.33+ */34+35+// This is suitable really only for use with libdank's mremap_and_ftruncate(),36+// due to assumptions it makes about the flags to pass to mmap(2). The only37+// mremap(2) use case addressed is that of MREMAP_MAYMOVE. oldaddr must be a38+// valid previous return from mmap(); NULL is not acceptable (ala Linux's39+// mremap(2)), resulting in undefined behavior, despite realloc(3) semantics.40+// Similarly, oldlen and newlen must be non-zero (and page-aligned).41+void *mremap_compat(int fd,void *oldaddr,size_t oldlen,42+ size_t newlen,int prot,int flags){43+ void *ret;44+45+ // From mmap(2) on freebsd 6.3: A successful FIXED mmap deletes any46+ // previous mapping in the allocated address range. This means:47+ // remapping over a current map will blow it away (unless FIXED isn't48+ // provided, in which case it can't overlap an old mapping. See bug49+ // 733 for extensive discussion of this issue for Linux and FreeBSD).50+ if((ret = mmap((char *)oldaddr + oldlen,newlen - oldlen,prot,flags,fd,oldlen)) == MAP_FAILED){51+ // We couldn't get the memory whatsoever (or we were a fresh52+ // allocation that succeeded). Return the immediate result...53+ return ret;54+ } // ret != MAP_FAILED. Did we squash?55+ if(ret != (char *)oldaddr + oldlen){56+ // We got the memory, but not where we wanted it. Copy over the57+ // old map, and then free it up...58+ munmap(ret,newlen - oldlen);59+ if((ret = mmap(NULL,newlen,prot,flags,fd,0)) == MAP_FAILED){60+ return ret;61+ }62+ memcpy(ret,oldaddr,oldlen);63+ munmap(oldaddr,oldlen); // Free the old mapping64+ return ret;65+ } // We successfully squashed. Return a pointer to the first buf.66+ return oldaddr;67+}68+#endif69+70static struct user_usb_stream *get_uus(int card)71{72pthread_mutex_lock(&uus_mutex);73@@ -217,7 +280,11 @@ static int snd_pcm_us_prepare(snd_pcm_ioplug_t *io)74}757677+#ifdef __linux__78uus->s = mremap(uus->s, sizeof(struct usb_stream), uus->s->read_size, MREMAP_MAYMOVE);79+#else80+ uus->s = mremap_compat(us->pfd.fd, uus->s, sizeof(struct usb_stream), uus->s->read_size, PROT_READ, MAP_SHARED);81+#endif82if (MAP_FAILED == uus->s) {83perror("ALSA/USX2Y: mmap");84return -EPERM;858687