]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Only allocate the pool when it's needed
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Tue, 1 Dec 2020 16:11:29 +0000 (09:11 -0700)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Tue, 1 Dec 2020 16:11:29 +0000 (09:11 -0700)
src/lib/server/tmpl.h
src/lib/server/tmpl_eval.c

index c1e9032260a694abae5666f2995d81726dbc3dc3..8c4520d49119c76d6a25e76c8113c8c998e678a0 100644 (file)
@@ -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);
 
index a4d3269e6581aa753dcc12ba8d6c331f9eeb8a86..c1057f88325e785f37a8236bd2d05fc5f254d86d 100644 (file)
@@ -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