2 * Copyright (C) 2019-2020 Paul Cercueil <paul@crapouillou.net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
16 #include "lightrec-private.h"
17 #include "memmanager.h"
24 #ifdef ENABLE_THREADED_COMPILER
25 #include <stdatomic.h>
27 static atomic_uint lightrec_bytes[MEM_TYPE_END];
29 void lightrec_register(enum mem_type type, unsigned int len)
31 atomic_fetch_add(&lightrec_bytes[type], len);
34 void lightrec_unregister(enum mem_type type, unsigned int len)
36 atomic_fetch_sub(&lightrec_bytes[type], len);
39 unsigned int lightrec_get_mem_usage(enum mem_type type)
41 return atomic_load(&lightrec_bytes[type]);
44 #else /* ENABLE_THREADED_COMPILER */
46 static unsigned int lightrec_bytes[MEM_TYPE_END];
48 void lightrec_register(enum mem_type type, unsigned int len)
50 lightrec_bytes[type] += len;
53 void lightrec_unregister(enum mem_type type, unsigned int len)
55 lightrec_bytes[type] -= len;
58 unsigned int lightrec_get_mem_usage(enum mem_type type)
60 return lightrec_bytes[type];
62 #endif /* ENABLE_THREADED_COMPILER */
64 unsigned int lightrec_get_total_mem_usage(void)
66 unsigned int i, count;
68 for (i = 0, count = 0; i < MEM_TYPE_END; i++)
69 count += lightrec_get_mem_usage((enum mem_type)i);
74 void * lightrec_malloc(struct lightrec_state *state,
75 enum mem_type type, unsigned int len)
80 if (type == MEM_FOR_IR)
81 ptr = tinymm_malloc(state->tinymm, len);
88 lightrec_register(type, len);
93 void * lightrec_calloc(struct lightrec_state *state,
94 enum mem_type type, unsigned int len)
99 if (type == MEM_FOR_IR)
100 ptr = tinymm_zalloc(state->tinymm, len);
103 ptr = calloc(1, len);
107 lightrec_register(type, len);
112 void lightrec_free(struct lightrec_state *state,
113 enum mem_type type, unsigned int len, void *ptr)
115 lightrec_unregister(type, len);
117 if (type == MEM_FOR_IR)
118 tinymm_free(state->tinymm, ptr);
124 float lightrec_get_average_ipi(void)
126 unsigned int code_mem = lightrec_get_mem_usage(MEM_FOR_CODE);
127 unsigned int native_mem = lightrec_get_mem_usage(MEM_FOR_MIPS_CODE);
129 return native_mem ? (float)code_mem / (float)native_mem : 0.0f;