1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * Copyright (C) 2019-2021 Paul Cercueil <paul@crapouillou.net>
6 #include "lightrec-config.h"
7 #include "lightrec-private.h"
8 #include "memmanager.h"
12 #if ENABLE_THREADED_COMPILER
13 #include <stdatomic.h>
15 static atomic_uint lightrec_bytes[MEM_TYPE_END];
17 void lightrec_register(enum mem_type type, unsigned int len)
19 atomic_fetch_add(&lightrec_bytes[type], len);
22 void lightrec_unregister(enum mem_type type, unsigned int len)
24 atomic_fetch_sub(&lightrec_bytes[type], len);
27 unsigned int lightrec_get_mem_usage(enum mem_type type)
29 return atomic_load(&lightrec_bytes[type]);
32 #else /* ENABLE_THREADED_COMPILER */
34 static unsigned int lightrec_bytes[MEM_TYPE_END];
36 void lightrec_register(enum mem_type type, unsigned int len)
38 lightrec_bytes[type] += len;
41 void lightrec_unregister(enum mem_type type, unsigned int len)
43 lightrec_bytes[type] -= len;
46 unsigned int lightrec_get_mem_usage(enum mem_type type)
48 return lightrec_bytes[type];
50 #endif /* ENABLE_THREADED_COMPILER */
52 unsigned int lightrec_get_total_mem_usage(void)
54 unsigned int i, count;
56 for (i = 0, count = 0; i < MEM_TYPE_END; i++)
57 count += lightrec_get_mem_usage((enum mem_type)i);
62 void * lightrec_malloc(struct lightrec_state *state,
63 enum mem_type type, unsigned int len)
71 lightrec_register(type, len);
76 void * lightrec_calloc(struct lightrec_state *state,
77 enum mem_type type, unsigned int len)
85 lightrec_register(type, len);
90 void lightrec_free(struct lightrec_state *state,
91 enum mem_type type, unsigned int len, void *ptr)
93 lightrec_unregister(type, len);
97 float lightrec_get_average_ipi(void)
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);
102 return native_mem ? (float)code_mem / (float)native_mem : 0.0f;