13 #include "uct/plugins.h"
14 #include "uct/prior.h"
17 /* Plugin interface for UCT. External plugins may hook callbacks on various
18 * events and e.g. bias the tree. */
21 /* Keep the API typedefs in sync with <uct/plugin.h>. */
29 void *(*init
)(char *args
, struct board
*b
, int seed
);
30 void (*prior
)(void *data
, struct tree_node
*node
, struct prior_map
*map
, int eqex
);
31 void (*done
)(void *data
);
34 struct uct_pluginset
{
35 struct plugin
*plugins
;
43 /* We do not support plugins on Windows. Minimal dummy stubs. */
45 struct uct_pluginset
*
46 pluginset_init(struct board
*b
)
51 pluginset_done(struct uct_pluginset
*ps
)
56 plugin_load(struct uct_pluginset
*ps
, char *path
, char *args
)
61 plugin_prior(struct uct_pluginset
*ps
, struct tree_node
*node
, struct prior_map
*map
, int eqex
)
68 struct uct_pluginset
*
69 pluginset_init(struct board
*b
)
71 struct uct_pluginset
*ps
= calloc(1, sizeof(*ps
));
77 pluginset_done(struct uct_pluginset
*ps
)
79 for (int i
= 0; i
< ps
->n_plugins
; i
++) {
80 struct plugin
*p
= &ps
->plugins
[i
];
91 plugin_load(struct uct_pluginset
*ps
, char *path
, char *args
)
93 ps
->plugins
= realloc(ps
->plugins
, ++ps
->n_plugins
* sizeof(ps
->plugins
[0]));
94 struct plugin
*p
= &ps
->plugins
[ps
->n_plugins
- 1];
95 p
->path
= strdup(path
);
96 p
->args
= args
? strdup(args
) : args
;
98 p
->dlh
= dlopen(path
, RTLD_NOW
);
100 fprintf(stderr
, "Cannot load plugin %s: %s\n", path
, dlerror());
103 #define loadsym(s_) do {\
104 p->s_ = dlsym(p->dlh, "pachi_plugin_" #s_); \
106 fprintf(stderr, "Cannot find pachi_plugin_%s in plugin %s: %s\n", #s_, path, dlerror()); \
107 exit(EXIT_FAILURE); \
114 p
->data
= p
->init(p
->args
, ps
->b
, fast_random(65536));
118 plugin_prior(struct uct_pluginset
*ps
, struct tree_node
*node
, struct prior_map
*map
, int eqex
)
120 for (int i
= 0; i
< ps
->n_plugins
; i
++) {
121 struct plugin
*p
= &ps
->plugins
[i
];
122 p
->prior(p
->data
, node
, map
, eqex
);