Files
FFmpeg/libswscale/jit.c
T
Martin Storsjö 35b7df64a0 swscale/jit: Fix getting MAP_ANONYMOUS defined on older glibc
This file attempts to define _DEFAULT_SOURCE and similar macros
to affect the system headers regarding what APIs they expose.

However, as this file includes libavutil/error.h before defining
the _*_SOURCE macros, we may end up including system headers that
end up initializing which features are exposed, before we set our
defines (before including <sys/mman.h>).

This is not an issue with glibc 2.37 and newer, since those versions
stopped looking for defines like _DEFAULT_SOURCE for exposing
MAP_ANONYMOUS (see [1]). This wasn't an issue before these functions
were split out to a separate file in
4d9be5f57c either, as these defines
used to be set at the top of libswscale/utils.c, before including other
headers than config.h.

[1] https://sourceware.org/git/?p=glibc.git;a=commit;h=954b8f38958de72c4712088735eb175118f17b51
2026-09-08 12:03:06 +00:00

105 lines
2.4 KiB
C

/*
* Copyright (C) 2005 Aurelien Jacobs <aurel@gnuage.org>
* Copyright (C) 2009, 2026 Ramiro Polla <ramiro.polla@gmail.com>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#if HAVE_MMAP && HAVE_MPROTECT
# define _DEFAULT_SOURCE
# define _SVID_SOURCE // needed for MAP_ANONYMOUS
# define _DARWIN_C_SOURCE // needed for MAP_ANON
# include <sys/mman.h>
# if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
# define MAP_ANONYMOUS MAP_ANON
# endif
#endif
#include "libavutil/error.h"
#include "jit.h"
#if HAVE_MMAP && HAVE_MPROTECT && defined(MAP_ANONYMOUS)
void *ff_sws_jit_alloc(size_t size)
{
void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ptr == MAP_FAILED)
return NULL;
return ptr;
}
int ff_sws_jit_protect(void *ptr, size_t size)
{
if (mprotect(ptr, size, PROT_READ | PROT_EXEC) == -1)
return AVERROR(errno);
return 0;
}
void ff_sws_jit_free(void *ptr, size_t size)
{
if (ptr)
munmap(ptr, size);
}
#elif HAVE_VIRTUALALLOC
#include <windows.h>
void *ff_sws_jit_alloc(size_t size)
{
return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
}
int ff_sws_jit_protect(void *ptr, size_t size)
{
DWORD old_protect;
if (!VirtualProtect(ptr, size, PAGE_EXECUTE_READ, &old_protect))
return AVERROR(EINVAL);
return 0;
}
void ff_sws_jit_free(void *ptr, size_t size)
{
if (ptr)
VirtualFree(ptr, 0, MEM_RELEASE);
}
#else
#include "libavutil/mem.h"
void *ff_sws_jit_alloc(size_t size)
{
return av_malloc(size);
}
int ff_sws_jit_protect(void *ptr, size_t size)
{
return 0;
}
void ff_sws_jit_free(void *ptr, size_t size)
{
av_free(ptr);
}
#endif