FFmpeg
Loading...
Searching...
No Matches
jit.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2005 Aurelien Jacobs <aurel@gnuage.org>
3 * Copyright (C) 2009, 2026 Ramiro Polla <ramiro.polla@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "config.h"
23
24#include "libavutil/error.h"
25
26#include "jit.h"
27
28#if HAVE_MMAP && HAVE_MPROTECT
29# define _DEFAULT_SOURCE
30# define _SVID_SOURCE // needed for MAP_ANONYMOUS
31# define _DARWIN_C_SOURCE // needed for MAP_ANON
32# include <sys/mman.h>
33# if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
34# define MAP_ANONYMOUS MAP_ANON
35# endif
36#endif
37
38#if HAVE_MMAP && HAVE_MPROTECT && defined(MAP_ANONYMOUS)
39
40void *ff_sws_jit_alloc(size_t size)
41{
42 void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
43 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
44 if (ptr == MAP_FAILED)
45 return NULL;
46 return ptr;
47}
48
49int ff_sws_jit_protect(void *ptr, size_t size)
50{
51 if (mprotect(ptr, size, PROT_READ | PROT_EXEC) == -1)
52 return AVERROR(errno);
53 return 0;
54}
55
56void ff_sws_jit_free(void *ptr, size_t size)
57{
58 if (ptr)
59 munmap(ptr, size);
60}
61
62#elif HAVE_VIRTUALALLOC
63
64#include <windows.h>
65
66void *ff_sws_jit_alloc(size_t size)
67{
68 return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
69}
70
71int ff_sws_jit_protect(void *ptr, size_t size)
72{
73 DWORD old_protect;
74 if (!VirtualProtect(ptr, size, PAGE_EXECUTE_READ, &old_protect))
75 return AVERROR(EINVAL);
76 return 0;
77}
78
79void ff_sws_jit_free(void *ptr, size_t size)
80{
81 if (ptr)
82 VirtualFree(ptr, 0, MEM_RELEASE);
83}
84
85#else
86
87#include "libavutil/mem.h"
88
90{
91 return av_malloc(size);
92}
93
94int ff_sws_jit_protect(void *ptr, size_t size)
95{
96 return 0;
97}
98
99void ff_sws_jit_free(void *ptr, size_t size)
100{
101 av_free(ptr);
102}
103
104#endif
#define NULL
Definition coverity.c:32
error code definitions
#define AVERROR(e)
Definition error.h:45
void * ff_sws_jit_alloc(size_t size)
Definition jit.c:89
void ff_sws_jit_free(void *ptr, size_t size)
Definition jit.c:99
int ff_sws_jit_protect(void *ptr, size_t size)
Definition jit.c:94
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
#define av_free(p)
int size