#include "util/u_cpu_detect.h"
#include "util/u_debug.h"
#include "util/u_memory.h"
+#include "util/u_simple_list.h"
#include "lp_bld_debug.h"
#include "lp_bld_init.h"
{
garbage_collect_callback_func func;
void *cb_data;
+ struct callback *prev, *next;
};
-#define MAX_CALLBACKS 32
-static struct callback Callbacks[MAX_CALLBACKS];
-static unsigned NumCallbacks = 0;
+/** list of all garbage collector callbacks */
+static struct callback callback_list = {NULL, NULL, NULL, NULL};
/**
gallivm_register_garbage_collector_callback(garbage_collect_callback_func func,
void *cb_data)
{
- unsigned i;
+ struct callback *cb;
- for (i = 0; i < NumCallbacks; i++) {
- if (Callbacks[i].func == func && Callbacks[i].cb_data == cb_data) {
- /* already in list: no-op */
+ if (!callback_list.prev) {
+ make_empty_list(&callback_list);
+ }
+
+ /* see if already in list */
+ foreach(cb, &callback_list) {
+ if (cb->func == func && cb->cb_data == cb_data)
return;
- }
}
- assert(NumCallbacks < MAX_CALLBACKS);
- if (NumCallbacks < MAX_CALLBACKS) {
- Callbacks[NumCallbacks].func = func;
- Callbacks[NumCallbacks].cb_data = cb_data;
- NumCallbacks++;
+ /* add to list */
+ cb = CALLOC_STRUCT(callback);
+ if (cb) {
+ cb->func = func;
+ cb->cb_data = cb_data;
+ insert_at_head(&callback_list, cb);
}
}
gallivm_remove_garbage_collector_callback(garbage_collect_callback_func func,
void *cb_data)
{
- unsigned i;
-
- for (i = 0; i < NumCallbacks; i++) {
- if (Callbacks[i].func == func && Callbacks[i].cb_data == cb_data) {
- /* found, now remove it */
- NumCallbacks--;
- for ( ; i < NumCallbacks; i++) {
- Callbacks[i] = Callbacks[i + 1];
- }
+ struct callback *cb;
+
+ /* search list */
+ foreach(cb, &callback_list) {
+ if (cb->func == func && cb->cb_data == cb_data) {
+ /* found, remove it */
+ remove_from_list(cb);
return;
}
}
static void
call_garbage_collector_callbacks(void)
{
- unsigned i;
-
- for (i = 0; i < NumCallbacks; i++) {
- Callbacks[i].func(Callbacks[i].cb_data);
+ struct callback *cb;
+ foreach(cb, &callback_list) {
+ cb->func(cb->cb_data);
}
}