1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (nbio_unixmmap.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #include <file/nbio.h>
32 #if defined(HAVE_MMAP) && defined(BSD)
45 #define O_CLOEXEC 0x1000000
56 struct nbio_mmap_unix_t
64 static void *nbio_mmap_unix_open(const char * filename, unsigned mode)
66 static const int o_flags[] = { O_RDONLY, O_RDWR|O_CREAT|O_TRUNC, O_RDWR, O_RDONLY, O_RDWR|O_CREAT|O_TRUNC };
67 static const int map_flags[] = { PROT_READ, PROT_WRITE|PROT_READ, PROT_WRITE|PROT_READ, PROT_READ, PROT_WRITE|PROT_READ };
71 struct nbio_mmap_unix_t* handle = NULL;
72 int fd = open(filename, o_flags[mode]|O_CLOEXEC, 0644);
76 len = lseek(fd, 0, SEEK_END);
78 ptr = mmap(NULL, len, map_flags[mode], MAP_SHARED, fd, 0);
80 if (ptr == MAP_FAILED)
86 handle = malloc(sizeof(struct nbio_mmap_unix_t));
88 handle->map_flags = map_flags[mode];
94 static void nbio_mmap_unix_begin_read(void *data)
99 static void nbio_mmap_unix_begin_write(void *data)
104 static bool nbio_mmap_unix_iterate(void *data)
106 return true; /* not needed */
109 static void nbio_mmap_unix_resize(void *data, size_t len)
111 struct nbio_mmap_unix_t* handle = (struct nbio_mmap_unix_t*)data;
114 if (len < handle->len)
116 /* this works perfectly fine if this check is removed, but it
117 * won't work on other nbio implementations */
118 /* therefore, it's blocked so nobody accidentally relies on it */
122 if (ftruncate(handle->fd, len) != 0)
123 abort(); /* this one returns void and I can't find any other
124 way for it to report failure */
126 munmap(handle->ptr, handle->len);
128 handle->ptr = mmap(NULL, len, handle->map_flags, MAP_SHARED, handle->fd, 0);
131 if (handle->ptr == MAP_FAILED)
135 static void *nbio_mmap_unix_get_ptr(void *data, size_t* len)
137 struct nbio_mmap_unix_t* handle = (struct nbio_mmap_unix_t*)data;
145 static void nbio_mmap_unix_cancel(void *data)
150 static void nbio_mmap_unix_free(void *data)
152 struct nbio_mmap_unix_t* handle = (struct nbio_mmap_unix_t*)data;
156 munmap(handle->ptr, handle->len);
160 nbio_intf_t nbio_mmap_unix = {
162 nbio_mmap_unix_begin_read,
163 nbio_mmap_unix_begin_write,
164 nbio_mmap_unix_iterate,
165 nbio_mmap_unix_resize,
166 nbio_mmap_unix_get_ptr,
167 nbio_mmap_unix_cancel,
172 nbio_intf_t nbio_mmap_unix = {