Skip to content

Commit cecb12e

Browse files
thoh-otcarlescufi
authored andcommitted
Bluetooth: controller: Improve mem mngt of proc ctx
Move the mem_pool structure to the internal header for wider access. Introduce a mem_pool owner in the proc_ctx, sch that multiple mem_pool could be used for memory management of proc_ctx, also static 'foreign' allocated proc_ctx would be supported by this. Signed-off-by: Thomas Ebert Hansen <[email protected]>
1 parent f7c4fe6 commit cecb12e

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

subsys/bluetooth/controller/ll_sw/ull_llcp.c

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@
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 */
6458
static 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 */
6862
static 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

8282
void 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);

subsys/bluetooth/controller/ll_sw/ull_llcp_internal.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
/* LLCP Memory Pool Descriptor */
8+
struct llcp_mem_pool {
9+
void *free;
10+
uint8_t *pool;
11+
};
12+
713
/* LLCP Procedure */
814
enum llcp_proc {
915
PROC_UNKNOWN,
@@ -114,6 +120,9 @@ struct proc_ctx {
114120
/* Must be the first for sys_slist to work */
115121
sys_snode_t node;
116122

123+
/* llcp_mem_pool owner of this context */
124+
struct llcp_mem_pool *owner;
125+
117126
/* PROC_ */
118127
enum llcp_proc proc;
119128

@@ -335,6 +344,7 @@ struct proc_ctx *llcp_create_remote_procedure(enum llcp_proc proc);
335344
bool llcp_tx_alloc_peek(struct ll_conn *conn, struct proc_ctx *ctx);
336345
void llcp_tx_alloc_unpeek(struct proc_ctx *ctx);
337346
struct node_tx *llcp_tx_alloc(struct ll_conn *conn, struct proc_ctx *ctx);
347+
void llcp_proc_ctx_release(struct proc_ctx *ctx);
338348

339349
/*
340350
* ULL -> LLL Interface
@@ -571,7 +581,6 @@ void llcp_pdu_encode_conn_param_rsp(struct proc_ctx *ctx, struct pdu_data *pdu);
571581
void llcp_pdu_decode_conn_param_rsp(struct proc_ctx *ctx, struct pdu_data *pdu);
572582
void llcp_pdu_encode_conn_update_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
573583
void llcp_pdu_decode_conn_update_ind(struct proc_ctx *ctx, struct pdu_data *pdu);
574-
void llcp_proc_ctx_release(struct proc_ctx *ctx);
575584

576585
/*
577586
* Remote Channel Map Update Procedure Helper

0 commit comments

Comments
 (0)