params: removed tal context.

@rustyrussell showed we don't need temporary objects for params.

This means params no longer need a tal context.

Signed-off-by: Mark Beckwith <wythe@intrig.com>
This commit is contained in:
Mark Beckwith
2018-07-04 11:03:12 -05:00
committed by Rusty Russell
parent 32ccfa5b29
commit 1b50ea2abd
3 changed files with 22 additions and 25 deletions

View File

@@ -8,7 +8,6 @@
#include <lightningd/params.h> #include <lightningd/params.h>
struct param { struct param {
const tal_t *ctx;
const char *name; const char *name;
bool is_set; bool is_set;
param_cb cb; param_cb cb;
@@ -18,7 +17,7 @@ struct param {
static void param_add(struct param **params, static void param_add(struct param **params,
const char *name, param_cb cb, void *arg, const char *name, param_cb cb, void *arg,
const tal_t *ctx, size_t argsize) size_t argsize)
{ {
#if DEVELOPER #if DEVELOPER
assert(name); assert(name);
@@ -30,14 +29,13 @@ static void param_add(struct param **params,
tal_resize(params, tal_count(*params) + 1); tal_resize(params, tal_count(*params) + 1);
last = &(*params)[tal_count(*params) - 1]; last = &(*params)[tal_count(*params) - 1];
last->ctx = ctx;
last->is_set = false; last->is_set = false;
last->name = name; last->name = name;
last->cb = cb; last->cb = cb;
last->arg = arg; last->arg = arg;
last->argsize = argsize; last->argsize = argsize;
/* Non-NULL means we are supposed to allocate iff found */ /* Non-0 means we are supposed to allocate iff found */
if (last->ctx) if (last->argsize != 0)
*(void **)last->arg = NULL; *(void **)last->arg = NULL;
} }
@@ -77,7 +75,7 @@ static bool make_callback(struct command *cmd,
if (def->argsize && def->cb != (param_cb)json_tok_tok) { if (def->argsize && def->cb != (param_cb)json_tok_tok) {
*(void **)def->arg *(void **)def->arg
= arg = arg
= tal_alloc_(def->ctx, def->argsize, false, false, = tal_alloc_(cmd, def->argsize, false, false,
"param"); "param");
} else } else
arg = def->arg; arg = def->arg;
@@ -290,7 +288,7 @@ static bool param_parse_arr(struct command *cmd,
bool param_parse(struct command *cmd, const char *buffer, bool param_parse(struct command *cmd, const char *buffer,
const jsmntok_t tokens[], ...) const jsmntok_t tokens[], ...)
{ {
struct param *params = tal_arr(tmpctx, struct param, 0); struct param *params = tal_arr(cmd, struct param, 0);
const char *name; const char *name;
va_list ap; va_list ap;
@@ -298,9 +296,8 @@ bool param_parse(struct command *cmd, const char *buffer,
while ((name = va_arg(ap, const char *)) != NULL) { while ((name = va_arg(ap, const char *)) != NULL) {
param_cb cb = va_arg(ap, param_cb); param_cb cb = va_arg(ap, param_cb);
void *arg = va_arg(ap, void *); void *arg = va_arg(ap, void *);
const tal_t *ctx = va_arg(ap, const tal_t *);
size_t argsize = va_arg(ap, size_t); size_t argsize = va_arg(ap, size_t);
param_add(&params, name, cb, arg, ctx, argsize); param_add(&params, name, cb, arg, argsize);
} }
va_end(ap); va_end(ap);

View File

@@ -65,27 +65,27 @@ typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
(cb), (arg), \ (cb), (arg), \
const char *, \ const char *, \
const jsmntok_t *), \ const jsmntok_t *), \
(arg), NULL, 0 (arg), 0
/* /*
* Same as above but for optional parameters. * Same as above but for optional parameters.
*/ */
#define param_opt(ctx, name, cb, arg) \ #define param_opt(name, cb, arg) \
name"", \ name"", \
typesafe_cb_preargs(bool, void *, \ typesafe_cb_preargs(bool, void *, \
(cb), *(arg), \ (cb), *(arg), \
const char *, \ const char *, \
const jsmntok_t *), \ const jsmntok_t *), \
(arg), (ctx), sizeof(**arg) (arg), sizeof(**arg)
/* /*
* For when you want an optional raw token. * For when you want an optional raw token.
* *
* Note: weird sizeof() does type check that arg really is a (const) jsmntok_t **. * Note: weird sizeof() does type check that arg really is a (const) jsmntok_t **.
*/ */
#define param_opt_tok(ctx, name, arg) \ #define param_opt_tok(name, arg) \
name"", \ name"", \
json_tok_tok, \ json_tok_tok, \
(arg) + 0*sizeof(*(arg) == (jsmntok_t *)NULL), \ (arg) + 0*sizeof(*(arg) == (jsmntok_t *)NULL), \
(ctx), sizeof(const jsmntok_t *) sizeof(const jsmntok_t *)
#endif /* LIGHTNING_LIGHTNINGD_PARAMS_H */ #endif /* LIGHTNING_LIGHTNINGD_PARAMS_H */

View File

@@ -175,7 +175,7 @@ static void tok_tok(void)
struct json *j = json_parse(cmd, "{}"); struct json *j = json_parse(cmd, "{}");
assert(param_parse(cmd, j->buffer, j->toks, assert(param_parse(cmd, j->buffer, j->toks,
param_opt_tok(cmd, "satoshi", &tok), NULL)); param_opt_tok("satoshi", &tok), NULL));
/* make sure it *is* NULL */ /* make sure it *is* NULL */
assert(tok == NULL); assert(tok == NULL);
@@ -211,9 +211,9 @@ static void null_params(void)
param_req("1", json_tok_u64, &ints[1]), param_req("1", json_tok_u64, &ints[1]),
param_req("2", json_tok_u64, &ints[2]), param_req("2", json_tok_u64, &ints[2]),
param_req("3", json_tok_u64, &ints[3]), param_req("3", json_tok_u64, &ints[3]),
param_opt(tmpctx, "4", json_tok_u64, &intptrs[0]), param_opt("4", json_tok_u64, &intptrs[0]),
param_opt(tmpctx, "5", json_tok_u64, &intptrs[1]), param_opt("5", json_tok_u64, &intptrs[1]),
param_opt(tmpctx, "6", json_tok_u64, &intptrs[2]), param_opt("6", json_tok_u64, &intptrs[2]),
NULL)); NULL));
for (int i = 0; i < tal_count(ints); ++i) for (int i = 0; i < tal_count(ints); ++i)
assert(ints[i] == i + 10); assert(ints[i] == i + 10);
@@ -232,9 +232,9 @@ static void null_params(void)
param_req("1", json_tok_u64, &ints[1]), param_req("1", json_tok_u64, &ints[1]),
param_req("2", json_tok_u64, &ints[2]), param_req("2", json_tok_u64, &ints[2]),
param_req("3", json_tok_u64, &ints[3]), param_req("3", json_tok_u64, &ints[3]),
param_opt(tmpctx, "4", json_tok_u64, &intptrs[0]), param_opt("4", json_tok_u64, &intptrs[0]),
param_opt(tmpctx, "5", json_tok_u64, &intptrs[1]), param_opt("5", json_tok_u64, &intptrs[1]),
param_opt(tmpctx, "6", json_tok_u64, &intptrs[2]), param_opt("6", json_tok_u64, &intptrs[2]),
NULL)); NULL));
assert(*intptrs[0] == 14); assert(*intptrs[0] == 14);
assert(intptrs[1] == NULL); assert(intptrs[1] == NULL);
@@ -342,7 +342,7 @@ static void bad_programmer(void)
param_parse(cmd, j->buffer, j->toks, param_parse(cmd, j->buffer, j->toks,
param_req("u64", json_tok_u64, &ival), param_req("u64", json_tok_u64, &ival),
param_req("double", json_tok_double, &dval), param_req("double", json_tok_double, &dval),
param_opt(tmpctx, "msatoshi", param_opt("msatoshi",
json_tok_number, &msatoshi), json_tok_number, &msatoshi),
param_req("riskfactor", json_tok_double, param_req("riskfactor", json_tok_double,
&riskfactor), NULL); &riskfactor), NULL);
@@ -367,7 +367,7 @@ static void add_members(struct param **params,
&ints[i], &ints[i],
const char *, const char *,
const jsmntok_t *), const jsmntok_t *),
&ints[i], NULL, 0); &ints[i], 0);
} }
} }
@@ -417,8 +417,8 @@ static void sendpay(void)
if (!param_parse(cmd, j->buffer, j->toks, if (!param_parse(cmd, j->buffer, j->toks,
param_req("route", json_tok_tok, &routetok), param_req("route", json_tok_tok, &routetok),
param_req("cltv", json_tok_number, &cltv), param_req("cltv", json_tok_number, &cltv),
param_opt_tok(tmpctx, "note", &note), param_opt_tok("note", &note),
param_opt(tmpctx, "msatoshi", json_tok_u64, &msatoshi), param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL)) NULL))
assert(false); assert(false);