Browse Source

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>
ppa-0.6.1
Mark Beckwith 7 years ago
committed by Rusty Russell
parent
commit
1b50ea2abd
  1. 15
      lightningd/params.c
  2. 10
      lightningd/params.h
  3. 22
      lightningd/test/run-params.c

15
lightningd/params.c

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

10
lightningd/params.h

@ -65,27 +65,27 @@ typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
(cb), (arg), \
const char *, \
const jsmntok_t *), \
(arg), NULL, 0
(arg), 0
/*
* Same as above but for optional parameters.
*/
#define param_opt(ctx, name, cb, arg) \
#define param_opt(name, cb, arg) \
name"", \
typesafe_cb_preargs(bool, void *, \
(cb), *(arg), \
const char *, \
const jsmntok_t *), \
(arg), (ctx), sizeof(**arg)
(arg), sizeof(**arg)
/*
* For when you want an optional raw token.
*
* 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"", \
json_tok_tok, \
(arg) + 0*sizeof(*(arg) == (jsmntok_t *)NULL), \
(ctx), sizeof(const jsmntok_t *)
sizeof(const jsmntok_t *)
#endif /* LIGHTNING_LIGHTNINGD_PARAMS_H */

22
lightningd/test/run-params.c

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

Loading…
Cancel
Save