update vibration handling
[pcsx_rearmed.git] / deps / lightrec / memmanager.c
CommitLineData
98fa08a5 1// SPDX-License-Identifier: LGPL-2.1-or-later
d16005f8 2/*
98fa08a5 3 * Copyright (C) 2019-2021 Paul Cercueil <paul@crapouillou.net>
d16005f8
PC
4 */
5
98fa08a5 6#include "lightrec-config.h"
d16005f8
PC
7#include "lightrec-private.h"
8#include "memmanager.h"
9
10#include <stdlib.h>
d16005f8 11
cb72ea13 12#if ENABLE_THREADED_COMPILER
d16005f8
PC
13#include <stdatomic.h>
14
15static atomic_uint lightrec_bytes[MEM_TYPE_END];
16
17void lightrec_register(enum mem_type type, unsigned int len)
18{
19 atomic_fetch_add(&lightrec_bytes[type], len);
20}
21
22void lightrec_unregister(enum mem_type type, unsigned int len)
23{
24 atomic_fetch_sub(&lightrec_bytes[type], len);
25}
26
27unsigned 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
34static unsigned int lightrec_bytes[MEM_TYPE_END];
35
36void lightrec_register(enum mem_type type, unsigned int len)
37{
38 lightrec_bytes[type] += len;
39}
40
41void lightrec_unregister(enum mem_type type, unsigned int len)
42{
43 lightrec_bytes[type] -= len;
44}
45
46unsigned int lightrec_get_mem_usage(enum mem_type type)
47{
48 return lightrec_bytes[type];
49}
50#endif /* ENABLE_THREADED_COMPILER */
51
52unsigned 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
62void * lightrec_malloc(struct lightrec_state *state,
63 enum mem_type type, unsigned int len)
64{
65 void *ptr;
66
d8b04acd 67 ptr = malloc(len);
d16005f8
PC
68 if (!ptr)
69 return NULL;
70
71 lightrec_register(type, len);
72
73 return ptr;
74}
75
76void * lightrec_calloc(struct lightrec_state *state,
77 enum mem_type type, unsigned int len)
78{
79 void *ptr;
80
d8b04acd 81 ptr = calloc(1, len);
d16005f8
PC
82 if (!ptr)
83 return NULL;
84
85 lightrec_register(type, len);
86
87 return ptr;
88}
89
90void lightrec_free(struct lightrec_state *state,
91 enum mem_type type, unsigned int len, void *ptr)
92{
93 lightrec_unregister(type, len);
d8b04acd 94 free(ptr);
d16005f8
PC
95}
96
97float 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}