From: Arran Cudbard-Bell Date: Tue, 1 Dec 2020 16:11:29 +0000 (-0700) Subject: Only allocate the pool when it's needed X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f6915b5f4084a912b8a3fa6556f54d6cdf7f4ead;p=thirdparty%2Ffreeradius-server.git Only allocate the pool when it's needed --- diff --git a/src/lib/server/tmpl.h b/src/lib/server/tmpl.h index c1e9032260a..8c4520d4911 100644 --- a/src/lib/server/tmpl.h +++ b/src/lib/server/tmpl.h @@ -521,7 +521,7 @@ struct tmpl_cursor_nested_s { */ struct tmpl_cursor_ctx_s { TALLOC_CTX *ctx; //!< Temporary allocations go here. - + TALLOC_CTX *pool; //!< Temporary pool. tmpl_t const *vpt; //!< tmpl we're evaluating. request_t *request; //!< Result of following the request references. @@ -846,7 +846,7 @@ void tmpl_debug(tmpl_t const *vpt); fr_pair_t **radius_list(request_t *request, pair_list_t list); -fr_radius_packet_t *radius_packet(request_t *request, pair_list_t list_name); +fr_radius_packet_t *radius_packet(request_t *request, pair_list_t list_name); TALLOC_CTX *radius_list_ctx(request_t *request, pair_list_t list_name); diff --git a/src/lib/server/tmpl_eval.c b/src/lib/server/tmpl_eval.c index a4d3269e658..c1057f88325 100644 --- a/src/lib/server/tmpl_eval.c +++ b/src/lib/server/tmpl_eval.c @@ -832,6 +832,12 @@ static fr_pair_t *_tmpl_cursor_tlv_eval(fr_pair_t **prev, UNUSED fr_pair_t *curr return NULL; } +static inline CC_HINT(always_inline) +void _tmpl_cursor_pool_init(tmpl_cursor_ctx_t *cc) +{ + if (!cc->pool) MEM(cc->pool = talloc_pool(cc->ctx, sizeof(tmpl_cursor_nested_t) * 5)); +} + /** Initialise an evaluation ctx for traversing a TLV attribute * */ @@ -865,7 +871,8 @@ void _tmpl_cursor_tlv_init(TALLOC_CTX *list_ctx, fr_pair_t **list, tmpl_attr_t c break; } else goto no_prev; - MEM(ns = talloc_pooled_object(cc->ctx, tmpl_cursor_nested_t, + _tmpl_cursor_pool_init(cc); + MEM(ns = talloc_pooled_object(cc->pool, tmpl_cursor_nested_t, 1, sizeof(fr_cursor_stack_t) + (sizeof(fr_cursor_t) * span))); *ns = (tmpl_cursor_nested_t){ .ar = ar, @@ -929,7 +936,8 @@ void _tmpl_cursor_group_init(TALLOC_CTX *list_ctx, fr_pair_t **list, tmpl_attr_t { tmpl_cursor_nested_t *ns; - MEM(ns = talloc(cc->ctx, tmpl_cursor_nested_t)); + _tmpl_cursor_pool_init(cc); + MEM(ns = talloc(cc->pool, tmpl_cursor_nested_t)); *ns = (tmpl_cursor_nested_t){ .ar = ar, .func = _tmpl_cursor_group_eval, @@ -1242,13 +1250,6 @@ fr_pair_t *tmpl_cursor_init(int *err, TALLOC_CTX *ctx, tmpl_cursor_ctx_t *cc, }; fr_dlist_init(&cc->nested, tmpl_cursor_nested_t, entry); - /* - * Create a CHILD context, so that we don't have to track - * any child allocations. We can just - * talloc_free(cc->ctx) in tmpl_cursor_clear(). - */ - cc->ctx = talloc(cc->ctx, uint8_t); - /* * Prime the stack! */ @@ -1290,12 +1291,12 @@ fr_pair_t *tmpl_cursor_init(int *err, TALLOC_CTX *ctx, tmpl_cursor_ctx_t *cc, */ void tmpl_cursor_clear(tmpl_cursor_ctx_t *cc) { + TALLOC_FREE(cc->pool); /* just in case we somehow leaked memory */ + if (!fr_dlist_num_elements(&cc->nested)) return;/* Help simplify dealing with unused cursor ctxs */ fr_dlist_remove(&cc->nested, &cc->leaf); /* Noop if leaf isn't inserted */ fr_dlist_talloc_free(&cc->nested); - - talloc_free(cc->ctx); /* free our private talloc ctx */ } /** Copy pairs matching a #tmpl_t in the current #request_t