forked from Mirror/frr
lib: Change frrscript to hold many Lua states
Instead of 1 frrscript : 1 lua state, it is changed to 1 : N. The states are hashed with their function names. Signed-off-by: Donald Lee <dlqs@gmx.com>
This commit is contained in:
parent
432bb280fb
commit
105ba9af8f
|
@ -102,6 +102,40 @@ static void codec_free(struct codec *c)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
unsigned int lua_function_hash_key(const void *data)
|
||||||
|
{
|
||||||
|
const struct lua_function_state *lfs = data;
|
||||||
|
|
||||||
|
return string_hash_make(lfs->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool lua_function_hash_cmp(const void *d1, const void *d2)
|
||||||
|
{
|
||||||
|
const struct lua_function_state *lfs1 = d1;
|
||||||
|
const struct lua_function_state *lfs2 = d2;
|
||||||
|
|
||||||
|
return strmatch(lfs1->name, lfs2->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *lua_function_alloc(void *arg)
|
||||||
|
{
|
||||||
|
struct lua_function_state *tmp = arg;
|
||||||
|
|
||||||
|
struct lua_function_state *lfs =
|
||||||
|
XCALLOC(MTYPE_SCRIPT, sizeof(struct lua_function_state));
|
||||||
|
lfs->name = tmp->name;
|
||||||
|
lfs->L = tmp->L;
|
||||||
|
return lfs;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void lua_function_free(struct lua_function_state *lfs)
|
||||||
|
{
|
||||||
|
XFREE(MTYPE_TMP, lfs->name);
|
||||||
|
lua_close(lfs->L);
|
||||||
|
XFREE(MTYPE_TMP, lfs);
|
||||||
|
}
|
||||||
|
|
||||||
/* Generic script APIs */
|
/* Generic script APIs */
|
||||||
|
|
||||||
int _frrscript_call(struct frrscript *fs)
|
int _frrscript_call(struct frrscript *fs)
|
||||||
|
|
|
@ -40,14 +40,31 @@ struct frrscript_codec {
|
||||||
decoder_func decoder;
|
decoder_func decoder;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct lua_function_state {
|
||||||
|
const char *name;
|
||||||
|
lua_State *L;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
struct frrscript {
|
struct frrscript {
|
||||||
/* Script name */
|
/* Script name */
|
||||||
char *name;
|
char *name;
|
||||||
|
|
||||||
/* Lua state */
|
/* Hash of Lua function name to Lua function state */
|
||||||
struct lua_State *L;
|
struct hash *lua_function_hash;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Hash related functions for lua_function_hash
|
||||||
|
*/
|
||||||
|
|
||||||
|
void *lua_function_alloc(void *arg);
|
||||||
|
|
||||||
|
unsigned int lua_function_hash_key(const void *data);
|
||||||
|
|
||||||
|
bool lua_function_hash_cmp(const void *d1, const void *d2);
|
||||||
|
|
||||||
struct frrscript_env {
|
struct frrscript_env {
|
||||||
/* Value type */
|
/* Value type */
|
||||||
const char *typename;
|
const char *typename;
|
||||||
|
|
Loading…
Reference in a new issue