Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports-gnome
Path: blob/main/audio/alsa-plugins/files/patch-usb__stream_pcm__usb__stream.c
16166 views
1
based on https://github.com/dankamongmen/libdank/blob/master/libdank/compat-FreeBSD.c
2
3
--- usb_stream/pcm_usb_stream.c.orig 2016-03-31 13:11:29 UTC
4
+++ usb_stream/pcm_usb_stream.c
5
@@ -79,6 +79,69 @@ typedef struct {
6
static struct user_usb_stream *uus;
7
static pthread_mutex_t uus_mutex = PTHREAD_MUTEX_INITIALIZER;
8
9
+#ifndef __linux__
10
+/*
11
+ * Copyright (c) 2000-2011, Nick Black et al
12
+ * All rights reserved.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without
15
+ * modification, are permitted provided that the following conditions are met:
16
+ * * Redistributions of source code must retain the above copyright
17
+ * notice, this list of conditions and the following disclaimer.
18
+ * * Redistributions in binary form must reproduce the above copyright
19
+ * notice, this list of conditions and the following disclaimer in the
20
+ * documentation and/or other materials provided with the distribution.
21
+ * * Neither the name of Nick Black nor the names of other contributors may
22
+ * be used to endorse or promote products derived from this software
23
+ * without specific prior written permission.
24
+ *
25
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
27
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <copyright
28
+ * holder> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
+ * POSSIBILITY OF SUCH DAMAGE.
35
+ */
36
+
37
+// This is suitable really only for use with libdank's mremap_and_ftruncate(),
38
+// due to assumptions it makes about the flags to pass to mmap(2). The only
39
+// mremap(2) use case addressed is that of MREMAP_MAYMOVE. oldaddr must be a
40
+// valid previous return from mmap(); NULL is not acceptable (ala Linux's
41
+// mremap(2)), resulting in undefined behavior, despite realloc(3) semantics.
42
+// Similarly, oldlen and newlen must be non-zero (and page-aligned).
43
+void *mremap_compat(int fd,void *oldaddr,size_t oldlen,
44
+ size_t newlen,int prot,int flags){
45
+ void *ret;
46
+
47
+ // From mmap(2) on freebsd 6.3: A successful FIXED mmap deletes any
48
+ // previous mapping in the allocated address range. This means:
49
+ // remapping over a current map will blow it away (unless FIXED isn't
50
+ // provided, in which case it can't overlap an old mapping. See bug
51
+ // 733 for extensive discussion of this issue for Linux and FreeBSD).
52
+ if((ret = mmap((char *)oldaddr + oldlen,newlen - oldlen,prot,flags,fd,oldlen)) == MAP_FAILED){
53
+ // We couldn't get the memory whatsoever (or we were a fresh
54
+ // allocation that succeeded). Return the immediate result...
55
+ return ret;
56
+ } // ret != MAP_FAILED. Did we squash?
57
+ if(ret != (char *)oldaddr + oldlen){
58
+ // We got the memory, but not where we wanted it. Copy over the
59
+ // old map, and then free it up...
60
+ munmap(ret,newlen - oldlen);
61
+ if((ret = mmap(NULL,newlen,prot,flags,fd,0)) == MAP_FAILED){
62
+ return ret;
63
+ }
64
+ memcpy(ret,oldaddr,oldlen);
65
+ munmap(oldaddr,oldlen); // Free the old mapping
66
+ return ret;
67
+ } // We successfully squashed. Return a pointer to the first buf.
68
+ return oldaddr;
69
+}
70
+#endif
71
+
72
static struct user_usb_stream *get_uus(const char *card)
73
{
74
pthread_mutex_lock(&uus_mutex);
75
@@ -219,7 +282,11 @@ static int snd_pcm_us_prepare(snd_pcm_io
76
}
77
78
79
+#ifdef __linux__
80
uus->s = mremap(uus->s, sizeof(struct usb_stream), uus->s->read_size, MREMAP_MAYMOVE);
81
+#else
82
+ uus->s = mremap_compat(us->pfd.fd, uus->s, sizeof(struct usb_stream), uus->s->read_size, PROT_READ, MAP_SHARED);
83
+#endif
84
if (MAP_FAILED == uus->s) {
85
perror("ALSA/USX2Y: mmap");
86
return -EPERM;
87
88