This is an automated email from the git hooks/post-receive script. Git pushed a commit to branch master in repository ffmpeg.
commit 3fc4f99a79728b9adb7a0c641b4db2dda2445e63 Author: Kacper Michajłow <[email protected]> AuthorDate: Sun Sep 13 02:13:39 2026 +0200 Commit: Kacper Michajłow <[email protected]> CommitDate: Wed Sep 23 16:52:56 2026 +0200 avformat/shared: use av_file_map_shared() and drop the mmap dependency This makes this available on Windows too. The mmap and unistd_h dependencies are gone, the protocol builds everywhere and av_file_map_shared() reports the platforms without shared mappings at runtime. The cache file is no longer shortened when it is longer than the known file size, the mapping simply covers its beginning. Signed-off-by: Kacper Michajłow <[email protected]> --- configure | 1 - libavformat/shared.c | 69 ++++++++++++++++++++++------------------------------ 2 files changed, 29 insertions(+), 41 deletions(-) diff --git a/configure b/configure index 84ab8beb4b..48a3b4e8fb 100755 --- a/configure +++ b/configure @@ -4088,7 +4088,6 @@ schannel_conflict="openssl gnutls libtls mbedtls" sctp_protocol_deps="struct_sctp_event_subscribe struct_msghdr_msg_flags" sctp_protocol_select="network" securetransport_conflict="openssl gnutls libtls mbedtls" -shared_protocol_deps="mmap unistd_h" shared_protocol_deps_any="stdatomic atomics_native" srtp_protocol_select="rtp_protocol srtp" tcp_protocol_select="network" diff --git a/libavformat/shared.c b/libavformat/shared.c index 2205f15377..f5b87c45b0 100644 --- a/libavformat/shared.c +++ b/libavformat/shared.c @@ -26,6 +26,7 @@ #include "libavutil/avstring.h" #include "libavutil/crc.h" #include "libavutil/error.h" +#include "libavutil/file.h" #include "libavutil/hash.h" #include "libavutil/file_open.h" #include "libavutil/mem.h" @@ -33,6 +34,7 @@ #include "libavutil/time.h" #include "internal.h" +#include "os_support.h" #include "url.h" #include <assert.h> @@ -41,10 +43,13 @@ #include <inttypes.h> #include <stdatomic.h> #include <string.h> -#include <sys/file.h> -#include <sys/mman.h> #include <sys/stat.h> +#if HAVE_UNISTD_H #include <unistd.h> +#endif +#ifndef _WIN32 +#include <sys/file.h> +#endif /** * This hash should be resistant against collision attacks, so that an @@ -179,9 +184,9 @@ typedef struct SharedContext { int64_t blocks_max; ///< maximum number of blocks to cache /* cache file */ - uint8_t *cache_data; ///< optional mmap of the cache file + uint8_t *cache_data; ///< optional mapping of the cache file char *cache_path; - off_t cache_size; ///< size of mapped memory region (for munmap) + off_t cache_size; ///< size of mapped memory region (for unmapping) int fd; /* space map */ @@ -200,10 +205,8 @@ static int shared_close(URLContext *h) SharedContext *s = h->priv_data; ffurl_close(s->inner); - if (s->cache_data) - munmap(s->cache_data, s->cache_size); - if (s->spacemap) - munmap(s->spacemap, s->map_size); + av_file_unmap_shared(s->cache_data, s->cache_size); + av_file_unmap_shared(s->spacemap, s->map_size); if (s->fd != -1) close(s->fd); if (s->mapfd != -1) @@ -315,7 +318,7 @@ static int shared_open(URLContext *h, const char *arg, int flags, AVDictionary * av_log(h, AV_LOG_VERBOSE, "Opening cache file '%s' for URI: '%s'\n", s->cache_path, s->inner ? s->inner->filename : arg); - const int mode = O_RDWR | (s->inner ? O_CREAT : 0); + const int mode = O_RDWR | O_BINARY | (s->inner ? O_CREAT : 0); s->fd = avpriv_open(s->cache_path, mode, 0660); s->mapfd = s->fd >= 0 ? avpriv_open(s->map_path, mode, 0660) : -1; if (s->fd < 0 || s->mapfd < 0) { @@ -354,7 +357,7 @@ static int shared_open(URLContext *h, const char *arg, int flags, AVDictionary * if (ret < 0) goto fail; - /* If filesize is known, we can directly mmap() the cache file */ + /* If filesize is known, we can directly map the cache file */ ret = cache_map(h, filesize); if (ret < 0) { av_log(h, AV_LOG_WARNING, "Failed to map cache file: %s. Falling " @@ -387,31 +390,20 @@ static int cache_map(URLContext *h, int64_t filesize) return 0; if (s->cache_data) { - munmap(s->cache_data, s->cache_size); + av_file_unmap_shared(s->cache_data, s->cache_size); s->cache_data = NULL; s->cache_size = 0; } - struct stat st; - int ret = fstat(s->fd, &st); + /* The mapping extends the file to the file size; it can be shorter if + * another process wrote the correct filesize to the header but crashed + * right before actually successfully resizing the file. */ + void *map; + int ret = av_file_map_shared(s->fd, filesize, &map); if (ret < 0) - return AVERROR(errno); - - if (st.st_size != filesize) { - /* Ensure the file size is correct before mapping; this can happen if - * another process wrote the correct filesize to the header but - * crashed right before actually successfully resizing the file. */ - ret = ftruncate(s->fd, filesize); - if (ret < 0) - return AVERROR(errno); - } - - s->cache_data = mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, 0); - if (s->cache_data == MAP_FAILED) { - s->cache_data = NULL; - return AVERROR(errno); - } + return ret; + s->cache_data = map; s->cache_size = filesize; return 0; } @@ -452,25 +444,22 @@ static int spacemap_remap(URLContext *h, size_t map_size) if (st.st_size >= map_size) goto skip_resize; - ret = ftruncate(s->mapfd, map_size); - if (ret < 0) { - ret = AVERROR(errno); - goto fail; - } + /* The new mapping extends the file */ st.st_size = map_size; did_grow = 1; skip_resize: - if (s->spacemap) - munmap(s->spacemap, s->map_size); + av_file_unmap_shared(s->spacemap, s->map_size); + s->spacemap = NULL; s->map_size = st.st_size; - s->spacemap = mmap(NULL, s->map_size, PROT_READ | PROT_WRITE, MAP_SHARED, s->mapfd, 0); - if (s->spacemap == MAP_FAILED) { - s->spacemap = NULL; /* for munmap check */ + + void *map; + ret = av_file_map_shared(s->mapfd, s->map_size, &map); + if (ret < 0) { s->map_size = 0; - ret = AVERROR(errno); goto fail; } + s->spacemap = map; if (locked) { flock(s->mapfd, LOCK_UN); -- To stop receiving notification emails like this one, please contact [email protected]. _______________________________________________ ffmpeg-cvslog mailing list -- [email protected] To unsubscribe send an email to [email protected]
