Skip to content

Commit

Permalink
params: removed tal context.
Browse files Browse the repository at this point in the history
@rustyrussell showed we don't need temporary objects for params.

This means params no longer need a tal context.

Signed-off-by: Mark Beckwith <[email protected]>
  • Loading branch information
wythe authored and rustyrussell committed Jul 5, 2018
1 parent 32ccfa5 commit 1b50ea2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
15 changes: 6 additions & 9 deletions lightningd/params.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <lightningd/params.h>

struct param {
const tal_t *ctx;
const char *name;
bool is_set;
param_cb cb;
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -290,17 +288,16 @@ 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;

va_start(ap, tokens);
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);

Expand Down
10 changes: 5 additions & 5 deletions lightningd/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 11 additions & 11 deletions lightningd/test/run-params.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 1b50ea2

Please sign in to comment.