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"
15 #ifdef ENABLE_THREADED_COMPILER
16 #include <stdatomic.h>
18 static atomic_uint lightrec_bytes[MEM_TYPE_END];
20 void lightrec_register(enum mem_type type, unsigned int len)
22 atomic_fetch_add(&lightrec_bytes[type], len);
25 void lightrec_unregister(enum mem_type type, unsigned int len)
27 atomic_fetch_sub(&lightrec_bytes[type], len);
30 unsigned int lightrec_get_mem_usage(enum mem_type type)
32 return atomic_load(&lightrec_bytes[type]);
35 #else /* ENABLE_THREADED_COMPILER */
37 static unsigned int lightrec_bytes[MEM_TYPE_END];
39 void lightrec_register(enum mem_type type, unsigned int len)
41 lightrec_bytes[type] += len;
44 void lightrec_unregister(enum mem_type type, unsigned int len)
46 lightrec_bytes[type] -= len;
49 unsigned int lightrec_get_mem_usage(enum mem_type type)
51 return lightrec_bytes[type];
53 #endif /* ENABLE_THREADED_COMPILER */
55 unsigned int lightrec_get_total_mem_usage(void)
57 unsigned int i, count;
59 for (i = 0, count = 0; i < MEM_TYPE_END; i++)
60 count += lightrec_get_mem_usage((enum mem_type)i);
65 void * lightrec_malloc(struct lightrec_state *state,
66 enum mem_type type, unsigned int len)
71 if (type == MEM_FOR_IR)
72 ptr = tinymm_malloc(state->tinymm, len);
79 lightrec_register(type, len);
84 void * lightrec_calloc(struct lightrec_state *state,
85 enum mem_type type, unsigned int len)
90 if (type == MEM_FOR_IR)
91 ptr = tinymm_zalloc(state->tinymm, len);
98 lightrec_register(type, len);
103 void lightrec_free(struct lightrec_state *state,
104 enum mem_type type, unsigned int len, void *ptr)
106 lightrec_unregister(type, len);
108 if (type == MEM_FOR_IR)
109 tinymm_free(state->tinymm, ptr);
115 float lightrec_get_average_ipi(void)
117 unsigned int code_mem = lightrec_get_mem_usage(MEM_FOR_CODE);
118 unsigned int native_mem = lightrec_get_mem_usage(MEM_FOR_MIPS_CODE);
120 return native_mem ? (float)code_mem / (float)native_mem : 0.0f;