libretro: adjust psxclock description
[pcsx_rearmed.git] / deps / lightrec / memmanager.c
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * Copyright (C) 2019-2021 Paul Cercueil <paul@crapouillou.net>
4  */
5
6 #include "lightrec-config.h"
7 #include "lightrec-private.h"
8 #include "memmanager.h"
9
10 #include <stdlib.h>
11
12 #if ENABLE_THREADED_COMPILER
13 #include <stdatomic.h>
14
15 static atomic_uint lightrec_bytes[MEM_TYPE_END];
16
17 void lightrec_register(enum mem_type type, unsigned int len)
18 {
19         atomic_fetch_add(&lightrec_bytes[type], len);
20 }
21
22 void lightrec_unregister(enum mem_type type, unsigned int len)
23 {
24         atomic_fetch_sub(&lightrec_bytes[type], len);
25 }
26
27 unsigned int lightrec_get_mem_usage(enum mem_type type)
28 {
29         return atomic_load(&lightrec_bytes[type]);
30 }
31
32 #else /* ENABLE_THREADED_COMPILER */
33
34 static unsigned int lightrec_bytes[MEM_TYPE_END];
35
36 void lightrec_register(enum mem_type type, unsigned int len)
37 {
38         lightrec_bytes[type] += len;
39 }
40
41 void lightrec_unregister(enum mem_type type, unsigned int len)
42 {
43         lightrec_bytes[type] -= len;
44 }
45
46 unsigned int lightrec_get_mem_usage(enum mem_type type)
47 {
48         return lightrec_bytes[type];
49 }
50 #endif /* ENABLE_THREADED_COMPILER */
51
52 unsigned int lightrec_get_total_mem_usage(void)
53 {
54         unsigned int i, count;
55
56         for (i = 0, count = 0; i < MEM_TYPE_END; i++)
57                 count += lightrec_get_mem_usage((enum mem_type)i);
58
59         return count;
60 }
61
62 void * lightrec_malloc(struct lightrec_state *state,
63                        enum mem_type type, unsigned int len)
64 {
65         void *ptr;
66
67         ptr = malloc(len);
68         if (!ptr)
69                 return NULL;
70
71         lightrec_register(type, len);
72
73         return ptr;
74 }
75
76 void * lightrec_calloc(struct lightrec_state *state,
77                        enum mem_type type, unsigned int len)
78 {
79         void *ptr;
80
81         ptr = calloc(1, len);
82         if (!ptr)
83                 return NULL;
84
85         lightrec_register(type, len);
86
87         return ptr;
88 }
89
90 void lightrec_free(struct lightrec_state *state,
91                    enum mem_type type, unsigned int len, void *ptr)
92 {
93         lightrec_unregister(type, len);
94         free(ptr);
95 }
96
97 float lightrec_get_average_ipi(void)
98 {
99         unsigned int code_mem = lightrec_get_mem_usage(MEM_FOR_CODE);
100         unsigned int native_mem = lightrec_get_mem_usage(MEM_FOR_MIPS_CODE);
101
102         return native_mem ? (float)code_mem / (float)native_mem : 0.0f;
103 }