1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/* Copyright (C) 2010-2018 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (nbio_unixmmap.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <file/nbio.h>
#if defined(HAVE_MMAP) && defined(BSD)
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif
#include <fcntl.h>
#include <sys/mman.h>
#ifdef __APPLE__
#ifndef O_CLOEXEC
#define O_CLOEXEC 0x1000000
#endif
#else
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
#endif
struct nbio_mmap_unix_t
{
int fd;
int map_flags;
size_t len;
void* ptr;
};
static void *nbio_mmap_unix_open(const char * filename, unsigned mode)
{
static const int o_flags[] = { O_RDONLY, O_RDWR|O_CREAT|O_TRUNC, O_RDWR, O_RDONLY, O_RDWR|O_CREAT|O_TRUNC };
static const int map_flags[] = { PROT_READ, PROT_WRITE|PROT_READ, PROT_WRITE|PROT_READ, PROT_READ, PROT_WRITE|PROT_READ };
size_t len;
void* ptr = NULL;
struct nbio_mmap_unix_t* handle = NULL;
int fd = open(filename, o_flags[mode]|O_CLOEXEC, 0644);
if (fd < 0)
return NULL;
len = lseek(fd, 0, SEEK_END);
if (len != 0)
ptr = mmap(NULL, len, map_flags[mode], MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED)
{
close(fd);
return NULL;
}
handle = malloc(sizeof(struct nbio_mmap_unix_t));
handle->fd = fd;
handle->map_flags = map_flags[mode];
handle->len = len;
handle->ptr = ptr;
return handle;
}
static void nbio_mmap_unix_begin_read(void *data)
{
/* not needed */
}
static void nbio_mmap_unix_begin_write(void *data)
{
/* not needed */
}
static bool nbio_mmap_unix_iterate(void *data)
{
return true; /* not needed */
}
static void nbio_mmap_unix_resize(void *data, size_t len)
{
struct nbio_mmap_unix_t* handle = (struct nbio_mmap_unix_t*)data;
if (!handle)
return;
if (len < handle->len)
{
/* this works perfectly fine if this check is removed, but it
* won't work on other nbio implementations */
/* therefore, it's blocked so nobody accidentally relies on it */
puts("ERROR - attempted file shrink operation, not implemented");
abort();
}
if (ftruncate(handle->fd, len) != 0)
{
puts("ERROR - couldn't resize file (ftruncate)");
abort(); /* this one returns void and I can't find any other
way for it to report failure */
}
munmap(handle->ptr, handle->len);
handle->ptr = mmap(NULL, len, handle->map_flags, MAP_SHARED, handle->fd, 0);
handle->len = len;
if (handle->ptr == MAP_FAILED)
{
puts("ERROR - couldn't resize file (mmap)");
abort();
}
}
static void *nbio_mmap_unix_get_ptr(void *data, size_t* len)
{
struct nbio_mmap_unix_t* handle = (struct nbio_mmap_unix_t*)data;
if (!handle)
return NULL;
if (len)
*len = handle->len;
return handle->ptr;
}
static void nbio_mmap_unix_cancel(void *data)
{
/* not needed */
}
static void nbio_mmap_unix_free(void *data)
{
struct nbio_mmap_unix_t* handle = (struct nbio_mmap_unix_t*)data;
if (!handle)
return;
close(handle->fd);
munmap(handle->ptr, handle->len);
free(handle);
}
nbio_intf_t nbio_mmap_unix = {
nbio_mmap_unix_open,
nbio_mmap_unix_begin_read,
nbio_mmap_unix_begin_write,
nbio_mmap_unix_iterate,
nbio_mmap_unix_resize,
nbio_mmap_unix_get_ptr,
nbio_mmap_unix_cancel,
nbio_mmap_unix_free,
"nbio_mmap_unix",
};
#else
nbio_intf_t nbio_mmap_unix = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
"nbio_mmap_unix",
};
#endif
|