Merge pull request #654 from pcercuei/lightrec-mmap-zero
[pcsx_rearmed.git] / deps / lightning / lib / jit_memory.c
1 /*
2  * Copyright (C) 2013-2019  Free Software Foundation, Inc.
3  *
4  * This file is part of GNU lightning.
5  *
6  * GNU lightning is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published
8  * by the Free Software Foundation; either version 3, or (at your option)
9  * any later version.
10  *
11  * GNU lightning is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14  * License for more details.
15  *
16  * Authors:
17  *      Paulo Cesar Pereira de Andrade
18  */
19
20 #include <lightning.h>
21 #include <lightning/jit_private.h>
22 #include <sys/mman.h>
23
24 /*
25  * Prototypes
26  */
27 static void *jit_default_alloc_func(size_t);
28 static void *jit_default_realloc_func(void*, size_t);
29 static void jit_default_free_func(void *);
30
31 /*
32  * Initialization
33  */
34 static jit_alloc_func_ptr jit_alloc_ptr = jit_default_alloc_func;
35 static jit_realloc_func_ptr jit_realloc_ptr = jit_default_realloc_func;
36 static jit_free_func_ptr jit_free_ptr = jit_default_free_func;
37
38 /*
39  * Implementation
40  */
41 jit_pointer_t
42 jit_memcpy(jit_pointer_t dst, const void * src, jit_word_t size)
43 {
44     if (size)
45         return (memcpy(dst, src, size));
46     return (dst);
47 }
48
49 jit_pointer_t
50 jit_memmove(jit_pointer_t dst, const void *src , jit_word_t size)
51 {
52     if (size)
53         return (memmove(dst, src, size));
54     return (dst);
55 }
56
57 void
58 jit_set_memory_functions(jit_alloc_func_ptr alloc_ptr,
59                          jit_realloc_func_ptr realloc_ptr,
60                          jit_free_func_ptr free_ptr)
61 {
62     if (alloc_ptr == NULL)
63         alloc_ptr = jit_default_alloc_func;
64     if (realloc_ptr == NULL)
65         realloc_ptr = jit_default_realloc_func;
66     if (free_ptr == NULL)
67         free_ptr = jit_default_free_func;
68     jit_alloc_ptr = alloc_ptr;
69     jit_realloc_ptr = realloc_ptr;
70     jit_free_ptr = free_ptr;
71 }
72
73 void
74 jit_get_memory_functions(jit_alloc_func_ptr *alloc_ptr,
75                          jit_realloc_func_ptr *realloc_ptr,
76                          jit_free_func_ptr *free_ptr)
77 {
78     *alloc_ptr = jit_alloc_ptr;
79     *realloc_ptr = jit_realloc_ptr;
80     *free_ptr = jit_free_ptr;
81 }
82
83 void
84 jit_alloc(jit_pointer_t *ptr, jit_word_t size)
85 {
86     *ptr = (*jit_alloc_ptr)(size);
87     memset(*ptr, 0, size);
88 }
89
90 void
91 jit_realloc(jit_pointer_t *ptr, jit_word_t old_size, jit_word_t new_size)
92 {
93     *ptr = (*jit_realloc_ptr)(*ptr, new_size);
94     if (old_size < new_size)
95         memset((jit_int8_t*)*ptr + old_size, 0, new_size - old_size);
96 }
97
98 void
99 jit_free(jit_pointer_t *ptr)
100 {
101     if (*ptr) {
102         (*jit_free_ptr)(*ptr);
103         *ptr = NULL;
104     }
105 }
106
107 static void *
108 jit_default_alloc_func(size_t size)
109 {
110     return (malloc(size));
111 }
112
113 static void *
114 jit_default_realloc_func(void *ptr, size_t size)
115 {
116     return (realloc(ptr, size));
117 }
118
119 static void
120 jit_default_free_func(void *ptr)
121 {
122     free(ptr);
123 }