4343#include <soc.h>
4444#include "hal/debug.h"
4545
46- /* LLCP Memory Pool Descriptor */
47- struct mem_pool {
48- void * free ;
49- uint8_t * pool ;
50- };
51-
5246#define LLCTRL_PDU_SIZE (offsetof(struct pdu_data, llctrl) + sizeof(struct pdu_data_llctrl))
5347#define PROC_CTX_BUF_SIZE WB_UP(sizeof(struct proc_ctx))
5448#define TX_CTRL_BUF_SIZE WB_UP(offsetof(struct node_tx, pdu) + LLCTRL_PDU_SIZE)
@@ -62,26 +56,36 @@ static uint8_t common_tx_buffer_alloc;
6256
6357/* TODO: Determine 'correct' number of tx nodes */
6458static uint8_t buffer_mem_tx [TX_CTRL_BUF_SIZE * LLCP_TX_CTRL_BUF_COUNT ];
65- static struct mem_pool mem_tx = { .pool = buffer_mem_tx };
59+ static struct llcp_mem_pool mem_tx = { .pool = buffer_mem_tx };
6660
6761/* TODO: Determine 'correct' number of ctx */
6862static uint8_t buffer_mem_ctx [PROC_CTX_BUF_SIZE * CONFIG_BT_CTLR_LLCP_PROC_CTX_BUF_NUM ];
69- static struct mem_pool mem_ctx = { .pool = buffer_mem_ctx };
63+ static struct llcp_mem_pool mem_ctx = { .pool = buffer_mem_ctx };
7064
7165/*
7266 * LLCP Resource Management
7367 */
74- static struct proc_ctx * proc_ctx_acquire (void )
68+ static struct proc_ctx * proc_ctx_acquire (struct llcp_mem_pool * owner )
7569{
7670 struct proc_ctx * ctx ;
7771
78- ctx = (struct proc_ctx * )mem_acquire (& mem_ctx .free );
72+ ctx = (struct proc_ctx * )mem_acquire (& owner -> free );
73+
74+ if (ctx ) {
75+ /* Set the owner */
76+ ctx -> owner = owner ;
77+ }
78+
7979 return ctx ;
8080}
8181
8282void llcp_proc_ctx_release (struct proc_ctx * ctx )
8383{
84- mem_release (ctx , & mem_ctx .free );
84+ /* We need to have an owner otherwise the memory allocated would leak */
85+ LL_ASSERT (ctx -> owner );
86+
87+ /* Release the memory back to the owner */
88+ mem_release (ctx , & ctx -> owner -> free );
8589}
8690
8791#if defined(LLCP_TX_CTRL_BUF_QUEUE_ENABLE )
@@ -249,11 +253,11 @@ void llcp_tx_flush(struct ll_conn *conn)
249253 * LLCP Procedure Creation
250254 */
251255
252- static struct proc_ctx * create_procedure (enum llcp_proc proc )
256+ static struct proc_ctx * create_procedure (enum llcp_proc proc , struct llcp_mem_pool * ctx_pool )
253257{
254258 struct proc_ctx * ctx ;
255259
256- ctx = proc_ctx_acquire ();
260+ ctx = proc_ctx_acquire (ctx_pool );
257261 if (!ctx ) {
258262 return NULL ;
259263 }
@@ -278,7 +282,7 @@ struct proc_ctx *llcp_create_local_procedure(enum llcp_proc proc)
278282{
279283 struct proc_ctx * ctx ;
280284
281- ctx = create_procedure (proc );
285+ ctx = create_procedure (proc , & mem_ctx );
282286 if (!ctx ) {
283287 return NULL ;
284288 }
@@ -346,7 +350,7 @@ struct proc_ctx *llcp_create_remote_procedure(enum llcp_proc proc)
346350{
347351 struct proc_ctx * ctx ;
348352
349- ctx = create_procedure (proc );
353+ ctx = create_procedure (proc , & mem_ctx );
350354 if (!ctx ) {
351355 return NULL ;
352356 }
@@ -1042,7 +1046,7 @@ void test_int_mem_proc_ctx(void)
10421046 zassert_equal (nr_of_free_ctx , CONFIG_BT_CTLR_LLCP_PROC_CTX_BUF_NUM , NULL );
10431047
10441048 for (int i = 0U ; i < CONFIG_BT_CTLR_LLCP_PROC_CTX_BUF_NUM ; i ++ ) {
1045- ctx1 = proc_ctx_acquire ();
1049+ ctx1 = proc_ctx_acquire (& mem_ctx );
10461050
10471051 /* The previous acquire should be valid */
10481052 zassert_not_null (ctx1 , NULL );
@@ -1051,7 +1055,7 @@ void test_int_mem_proc_ctx(void)
10511055 nr_of_free_ctx = ctx_buffers_free ();
10521056 zassert_equal (nr_of_free_ctx , 0 , NULL );
10531057
1054- ctx2 = proc_ctx_acquire ();
1058+ ctx2 = proc_ctx_acquire (& mem_ctx );
10551059
10561060 /* The last acquire should fail */
10571061 zassert_is_null (ctx2 , NULL );
@@ -1060,7 +1064,7 @@ void test_int_mem_proc_ctx(void)
10601064 nr_of_free_ctx = ctx_buffers_free ();
10611065 zassert_equal (nr_of_free_ctx , 1 , NULL );
10621066
1063- ctx1 = proc_ctx_acquire ();
1067+ ctx1 = proc_ctx_acquire (& mem_ctx );
10641068
10651069 /* Releasing returns the context to the avilable pool */
10661070 zassert_not_null (ctx1 , NULL );
@@ -1137,7 +1141,7 @@ void test_int_create_proc(void)
11371141
11381142 ull_cp_init ();
11391143
1140- ctx = create_procedure (PROC_VERSION_EXCHANGE );
1144+ ctx = create_procedure (PROC_VERSION_EXCHANGE , & mem_ctx );
11411145 zassert_not_null (ctx , NULL );
11421146
11431147 zassert_equal (ctx -> proc , PROC_VERSION_EXCHANGE , NULL );
@@ -1146,7 +1150,7 @@ void test_int_create_proc(void)
11461150
11471151 for (int i = 0U ; i < CONFIG_BT_CTLR_LLCP_PROC_CTX_BUF_NUM ; i ++ ) {
11481152 zassert_not_null (ctx , NULL );
1149- ctx = create_procedure (PROC_VERSION_EXCHANGE );
1153+ ctx = create_procedure (PROC_VERSION_EXCHANGE , & mem_ctx );
11501154 }
11511155
11521156 zassert_is_null (ctx , NULL );
0 commit comments