Merge pull request #657 from pcercuei/update-lightrec-20220529
[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
23 /*
24  * Prototypes
25  */
26 static void *jit_default_alloc_func(size_t);
27 static void *jit_default_realloc_func(void*, size_t);
28 static void jit_default_free_func(void *);
29
30 /*
31  * Initialization
32  */
33 static jit_alloc_func_ptr jit_alloc_ptr = jit_default_alloc_func;
34 static jit_realloc_func_ptr jit_realloc_ptr = jit_default_realloc_func;
35 static jit_free_func_ptr jit_free_ptr = jit_default_free_func;
36
37 /*
38  * Implementation
39  */
40 jit_pointer_t
41 jit_memcpy(jit_pointer_t dst, const void * src, jit_word_t size)
42 {
43     if (size)
44         return (memcpy(dst, src, size));
45     return (dst);
46 }
47
48 jit_pointer_t
49 jit_memmove(jit_pointer_t dst, const void *src , jit_word_t size)
50 {
51     if (size)
52         return (memmove(dst, src, size));
53     return (dst);
54 }
55
56 void
57 jit_set_memory_functions(jit_alloc_func_ptr alloc_ptr,
58                          jit_realloc_func_ptr realloc_ptr,
59                          jit_free_func_ptr free_ptr)
60 {
61     if (alloc_ptr == NULL)
62         alloc_ptr = jit_default_alloc_func;
63     if (realloc_ptr == NULL)
64         realloc_ptr = jit_default_realloc_func;
65     if (free_ptr == NULL)
66         free_ptr = jit_default_free_func;
67     jit_alloc_ptr = alloc_ptr;
68     jit_realloc_ptr = realloc_ptr;
69     jit_free_ptr = free_ptr;
70 }
71
72 void
73 jit_get_memory_functions(jit_alloc_func_ptr *alloc_ptr,
74                          jit_realloc_func_ptr *realloc_ptr,
75                          jit_free_func_ptr *free_ptr)
76 {
77     *alloc_ptr = jit_alloc_ptr;
78     *realloc_ptr = jit_realloc_ptr;
79     *free_ptr = jit_free_ptr;
80 }
81
82 void
83 jit_alloc(jit_pointer_t *ptr, jit_word_t size)
84 {
85     *ptr = (*jit_alloc_ptr)(size);
86     memset(*ptr, 0, size);
87 }
88
89 void
90 jit_realloc(jit_pointer_t *ptr, jit_word_t old_size, jit_word_t new_size)
91 {
92     *ptr = (*jit_realloc_ptr)(*ptr, new_size);
93     if (old_size < new_size)
94         memset((jit_int8_t*)*ptr + old_size, 0, new_size - old_size);
95 }
96
97 void
98 jit_free(jit_pointer_t *ptr)
99 {
100     if (*ptr) {
101         (*jit_free_ptr)(*ptr);
102         *ptr = NULL;
103     }
104 }
105
106 static void *
107 jit_default_alloc_func(size_t size)
108 {
109     return (malloc(size));
110 }
111
112 static void *
113 jit_default_realloc_func(void *ptr, size_t size)
114 {
115     return (realloc(ptr, size));
116 }
117
118 static void
119 jit_default_free_func(void *ptr)
120 {
121     free(ptr);
122 }