-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
net: Add buffer pool to replace connection allocation #15285
Open
wengzhe
wants to merge
2
commits into
apache:master
Choose a base branch
from
wengzhe:upstream-net
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+550
−875
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,18 +46,22 @@ | |
#ifdef CONFIG_NET_CAN | ||
|
||
/**************************************************************************** | ||
* Private Data | ||
* Pre-processor Definitions | ||
****************************************************************************/ | ||
|
||
/* The array containing all NetLink connections. */ | ||
|
||
#if CONFIG_CAN_PREALLOC_CONNS > 0 | ||
static struct can_conn_s g_can_connections[CONFIG_CAN_PREALLOC_CONNS]; | ||
#ifndef CONFIG_CAN_MAX_CONNS | ||
# define CONFIG_CAN_MAX_CONNS 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
#endif | ||
|
||
/* A list of all free NetLink connections */ | ||
/**************************************************************************** | ||
* Private Data | ||
****************************************************************************/ | ||
|
||
/* The array containing all NetLink connections. */ | ||
|
||
static dq_queue_t g_free_can_connections; | ||
NET_BUFPOOL_DECLARE(g_can_connections, sizeof(struct can_conn_s), | ||
CONFIG_CAN_PREALLOC_CONNS, CONFIG_CAN_ALLOC_CONNS, | ||
CONFIG_CAN_MAX_CONNS); | ||
static mutex_t g_free_lock = NXMUTEX_INITIALIZER; | ||
|
||
/* A list of all allocated NetLink connections */ | ||
|
@@ -79,16 +83,7 @@ static dq_queue_t g_active_can_connections; | |
|
||
void can_initialize(void) | ||
{ | ||
#if CONFIG_CAN_PREALLOC_CONNS > 0 | ||
int i; | ||
|
||
for (i = 0; i < CONFIG_CAN_PREALLOC_CONNS; i++) | ||
{ | ||
/* Mark the connection closed and move it to the free list */ | ||
|
||
dq_addlast(&g_can_connections[i].sconn.node, &g_free_can_connections); | ||
} | ||
#endif | ||
NET_BUFPOOL_INIT(g_can_connections); | ||
} | ||
|
||
/**************************************************************************** | ||
|
@@ -103,37 +98,12 @@ void can_initialize(void) | |
FAR struct can_conn_s *can_alloc(void) | ||
{ | ||
FAR struct can_conn_s *conn; | ||
#if CONFIG_CAN_ALLOC_CONNS > 0 | ||
int i; | ||
#endif | ||
|
||
/* The free list is protected by a a mutex. */ | ||
|
||
nxmutex_lock(&g_free_lock); | ||
#if CONFIG_CAN_ALLOC_CONNS > 0 | ||
if (dq_peek(&g_free_can_connections) == NULL) | ||
{ | ||
#if CONFIG_CAN_MAX_CONNS > 0 | ||
if (dq_count(&g_active_can_connections) + | ||
CONFIG_CAN_ALLOC_CONNS > CONFIG_CAN_MAX_CONNS) | ||
{ | ||
nxmutex_unlock(&g_free_lock); | ||
return NULL; | ||
} | ||
#endif | ||
|
||
conn = kmm_zalloc(sizeof(*conn) * CONFIG_CAN_ALLOC_CONNS); | ||
if (conn != NULL) | ||
{ | ||
for (i = 0; i < CONFIG_CAN_ALLOC_CONNS; i++) | ||
{ | ||
dq_addlast(&conn[i].sconn.node, &g_free_can_connections); | ||
} | ||
} | ||
} | ||
#endif | ||
|
||
conn = (FAR struct can_conn_s *)dq_remfirst(&g_free_can_connections); | ||
conn = NET_BUFPOOL_TRYALLOC(g_can_connections); | ||
if (conn != NULL) | ||
{ | ||
/* FIXME SocketCAN default behavior enables loopback */ | ||
|
@@ -184,22 +154,9 @@ void can_free(FAR struct can_conn_s *conn) | |
|
||
dq_rem(&conn->sconn.node, &g_active_can_connections); | ||
|
||
/* If this is a preallocated or a batch allocated connection store it in | ||
* the free connections list. Else free it. | ||
*/ | ||
/* Free the connection. */ | ||
|
||
#if CONFIG_CAN_ALLOC_CONNS == 1 | ||
if (conn < g_can_connections || conn >= (g_can_connections + | ||
CONFIG_CAN_PREALLOC_CONNS)) | ||
{ | ||
kmm_free(conn); | ||
} | ||
else | ||
#endif | ||
{ | ||
memset(conn, 0, sizeof(*conn)); | ||
dq_addlast(&conn->sconn.node, &g_free_can_connections); | ||
} | ||
NET_BUFPOOL_FREE(g_can_connections, conn); | ||
|
||
nxmutex_unlock(&g_free_lock); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wengzhe why to use 0 as max connections? Please for logic reasoning use 1 and later in your logic operation decrease 1