11 #include "uct/plugins.h"
12 #include "uct/prior.h"
15 /* Plugin interface for UCT. External plugins may hook callbacks on various
16 * events and e.g. bias the tree. */
19 /* Keep the API typedefs in sync with <uct/plugin.h>. */
27 void *(*init
)(char *args
, struct board
*b
, int seed
);
28 void (*prior
)(void *data
, struct tree_node
*node
, struct prior_map
*map
, int eqex
);
29 void (*done
)(void *data
);
32 struct uct_pluginset
{
33 struct plugin
*plugins
;
39 struct uct_pluginset
*
40 pluginset_init(struct board
*b
)
42 struct uct_pluginset
*ps
= calloc(1, sizeof(*ps
));
48 pluginset_done(struct uct_pluginset
*ps
)
50 for (int i
= 0; i
< ps
->n_plugins
; i
++) {
51 struct plugin
*p
= &ps
->plugins
[i
];
62 plugin_load(struct uct_pluginset
*ps
, char *path
, char *args
)
64 ps
->plugins
= realloc(ps
->plugins
, ++ps
->n_plugins
* sizeof(ps
->plugins
[0]));
65 struct plugin
*p
= &ps
->plugins
[ps
->n_plugins
- 1];
66 p
->path
= strdup(path
);
67 p
->args
= args
? strdup(args
) : args
;
69 p
->dlh
= dlopen(path
, RTLD_NOW
);
71 fprintf(stderr
, "Cannot load plugin %s: %s\n", path
, dlerror());
74 #define loadsym(s_) do {\
75 p->s_ = dlsym(p->dlh, "pachi_plugin_" #s_); \
77 fprintf(stderr, "Cannot find pachi_plugin_%s in plugin %s: %s\n", #s_, path, dlerror()); \
85 p
->data
= p
->init(p
->args
, ps
->b
, fast_random(65536));
89 plugin_prior(struct uct_pluginset
*ps
, struct tree_node
*node
, struct prior_map
*map
, int eqex
)
91 for (int i
= 0; i
< ps
->n_plugins
; i
++) {
92 struct plugin
*p
= &ps
->plugins
[i
];
93 p
->prior(p
->data
, node
, map
, eqex
);