summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorBernard Xiong <[email protected]>2018-09-05 11:18:51 +0800
committerGitHub <[email protected]>2018-09-05 11:18:51 +0800
commit6abe7e4ebd4c4cbd19aa68d348376ea668770d6b (patch)
tree95069ed2bd1c65577a7569cd005d5b71f12a7940 /components
parent95c36f32e20b9799907890cbb6d366ad783501b1 (diff)
parent1571ef5acfa6a092e6c32f5cda4526dab35a7cbc (diff)
Merge pull request #1771 from Lawlieta/chenyong
[net][sal] Fix close/shutdown memory leak issue.
Diffstat (limited to 'components')
-rw-r--r--components/net/sal_socket/src/sal_socket.c40
1 files changed, 28 insertions, 12 deletions
diff --git a/components/net/sal_socket/src/sal_socket.c b/components/net/sal_socket/src/sal_socket.c
index 1346911ae7..c4b4afe900 100644
--- a/components/net/sal_socket/src/sal_socket.c
+++ b/components/net/sal_socket/src/sal_socket.c
@@ -30,11 +30,13 @@
#include <sal.h>
#define DBG_ENABLE
-#define DBG_SECTION_NAME "SAL_SOC"
-#define DBG_LEVEL DBG_INFO
+#define DBG_SECTION_NAME "SAL_SOC"
+#define DBG_LEVEL DBG_INFO
#define DBG_COLOR
#include <rtdbg.h>
+#define SOCKET_TABLE_STEP_LEN 4
+
/* the socket table used to dynamic allocate sockets */
struct sal_socket_table
{
@@ -52,19 +54,29 @@ static rt_bool_t init_ok = RT_FALSE;
/**
* SAL (Socket Abstraction Layer) initialize.
*
- * @return result
- * >= 0: initialize success
+ * @return result 0: initialize success
+ * -1: initialize failed
*/
int sal_init(void)
{
+ int cn;
+
if(init_ok)
{
LOG_D("Socket Abstraction Layer is already initialized.");
return 0;
}
- /* clean sal socket table */
- rt_memset(&socket_table, 0, sizeof(socket_table));
+ /* init sal socket table */
+ cn = SOCKET_TABLE_STEP_LEN < SAL_SOCKETS_NUM ? SOCKET_TABLE_STEP_LEN : SAL_SOCKETS_NUM;
+ socket_table.max_socket = cn;
+ socket_table.sockets = rt_calloc(1, cn * sizeof(struct sal_socket *));
+ if (socket_table.sockets == RT_NULL)
+ {
+ LOG_E("No memory for socket table.\n");
+ return -1;
+ }
+
/* create sal socket lock */
rt_mutex_init(&sal_core_lock, "sal_lock", RT_IPC_FLAG_FIFO);
@@ -80,7 +92,7 @@ INIT_COMPONENT_EXPORT(sal_init);
*
* @param pf protocol family object
*
- * @return >=0 : protocol family object index
+ * @return 0 : protocol family object register success
* -1 : the global array of available protocol families is full
*/
int sal_proto_family_register(const struct proto_family *pf)
@@ -331,8 +343,8 @@ static int socket_alloc(struct sal_socket_table *st, int f_socket)
int cnt, index;
struct sal_socket **sockets;
- /* increase the number of FD with 4 step length */
- cnt = st->max_socket + 4;
+ /* increase the number of socket with 4 step length */
+ cnt = st->max_socket + SOCKET_TABLE_STEP_LEN;
cnt = cnt > SAL_SOCKETS_NUM ? SAL_SOCKETS_NUM : cnt;
sockets = rt_realloc(st->sockets, cnt * sizeof(struct sal_socket *));
@@ -352,7 +364,7 @@ static int socket_alloc(struct sal_socket_table *st, int f_socket)
/* allocate 'struct sal_socket' */
if (idx < (int) st->max_socket && st->sockets[idx] == RT_NULL)
{
- st->sockets[idx] = rt_malloc(sizeof(struct sal_socket));
+ st->sockets[idx] = rt_calloc(1, sizeof(struct sal_socket));
if (st->sockets[idx] == RT_NULL)
{
idx = st->max_socket;
@@ -476,7 +488,9 @@ int sal_shutdown(int socket, int how)
if (sock->ops->shutdown((int) sock->user_data, how) == 0)
{
- rt_memset(sock, 0x00, sizeof(struct sal_socket));
+ rt_free(sock);
+ sock = RT_NULL;
+
return 0;
}
@@ -683,7 +697,9 @@ int sal_closesocket(int socket)
if (sock->ops->closesocket((int) sock->user_data) == 0)
{
- rt_memset(sock, 0x00, sizeof(struct sal_socket));
+ rt_free(sock);
+ sock = RT_NULL;
+
return 0;
}